Skip to content

Commit 49b025c

Browse files
andy31415adbridge
authored andcommitted
Setup keep_trailing_newline=True in python jinja templates (project-chip#23235)
* Keep trailing newlins in codegen * Add option to auto-regenerate golden data * Re-generate test outputs after skip whitespace changes * Restyle
1 parent 3a2a704 commit 49b025c

30 files changed

+53
-26
lines changed

scripts/idl/generators/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ def __init__(self, storage: GeneratorStorage, idl: Idl):
113113
self.storage = storage
114114
self.idl = idl
115115
self.jinja_env = jinja2.Environment(
116-
loader=jinja2.FileSystemLoader(searchpath=os.path.dirname(__file__)))
116+
loader=jinja2.FileSystemLoader(searchpath=os.path.dirname(__file__)),
117+
keep_trailing_newline=True)
117118
self.dry_run = False
118119

119120
RegisterCommonFilters(self.jinja_env.filters)

scripts/idl/test_generators.py

+23-4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838

3939
TESTS_DIR = os.path.join(os.path.dirname(__file__), "tests")
40+
REGENERATE_GOLDEN_IMAGES = False
4041

4142

4243
@dataclass
@@ -63,19 +64,33 @@ def __init__(self, test_case: GeneratorTestCase, checker: unittest.TestCase):
6364
self.checker = checker
6465
self.checked_files = set()
6566

67+
def get_existing_data_path(self, relative_path: str):
68+
for expected in self.test_case.outputs:
69+
if expected.file_name == relative_path:
70+
return os.path.join(TESTS_DIR, expected.golden_path)
71+
72+
self.checker.fail("Expected output %s not found" % relative_path)
73+
return None
74+
6675
def get_existing_data(self, relative_path: str):
6776
self.checked_files.add(relative_path)
6877

69-
for expected in self.test_case.outputs:
70-
if expected.file_name == relative_path:
71-
with open(os.path.join(TESTS_DIR, expected.golden_path), 'rt') as golden:
72-
return golden.read()
78+
path = self.get_existing_data_path(relative_path)
79+
if path:
80+
with open(path, 'rt') as golden:
81+
return golden.read()
7382

7483
# This will attempt a new write, causing a unit test failure
7584
self.checker.fail("Expected output %s not found" % relative_path)
7685
return None
7786

7887
def write_new_data(self, relative_path: str, content: str):
88+
if REGENERATE_GOLDEN_IMAGES:
89+
# Expect writing only on regeneration
90+
with open(self.get_existing_data_path(relative_path), 'wt') as golden:
91+
golden.write(content)
92+
return
93+
7994
# This is a unit test failure: we do NOT expect
8095
# to write any new data
8196

@@ -148,4 +163,8 @@ def test_generators(self):
148163

149164

150165
if __name__ == '__main__':
166+
if 'IDL_GOLDEN_REGENERATE' in os.environ:
167+
# run with `IDL_GOLDEN_REGENERATE=1` to cause a regeneration of test
168+
# data. Then one can use `git diff` to see if the deltas make sense
169+
REGENERATE_GOLDEN_IMAGES = True
151170
unittest.main()

scripts/idl/tests/outputs/cluster_struct_attribute/bridge/BridgeClustersImpl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ struct ClusterInfo
2323
},
2424
};
2525

26-
}
26+
}

scripts/idl/tests/outputs/cluster_struct_attribute/bridge/BridgeGlobalStructs.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ namespace clusters {
1111

1212

1313

14-
}
14+
}

scripts/idl/tests/outputs/cluster_struct_attribute/bridge/DemoCluster.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ struct DemoClusterCluster : public GeneratedCluster
2929
ListAttribute<std::vector<ArmFailSafeRequest>> mArmFailsafes;
3030
};
3131

32-
}
32+
}

scripts/idl/tests/outputs/cluster_struct_attribute/jni/DemoClusterClient-InvokeSubscribeImpl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ JNI_METHOD(void, DemoClusterCluster, subscribeArmFailsafesAttribute)(JNIEnv * en
5353

5454
onSuccess.release();
5555
onFailure.release();
56-
}
56+
}

scripts/idl/tests/outputs/cluster_struct_attribute/jni/DemoClusterClient-ReadImpl.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ JNI_METHOD(void, DemoClusterCluster, readArmFailsafesAttribute)(JNIEnv * env, jo
3535
onFailure.release();
3636
}
3737

38+

scripts/idl/tests/outputs/global_struct_attribute/bridge/BridgeClustersImpl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ struct ClusterInfo
2323
},
2424
};
2525

26-
}
26+
}

scripts/idl/tests/outputs/global_struct_attribute/bridge/BridgeGlobalStructs.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ struct LabelStruct
3434
std::string value;
3535
};
3636

37-
}
37+
}

scripts/idl/tests/outputs/global_struct_attribute/bridge/DemoCluster.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,4 @@ struct DemoClusterCluster : public GeneratedCluster
2929
ListAttribute<std::vector<LabelStruct>> mSomeLabels;
3030
};
3131

32-
}
32+
}

scripts/idl/tests/outputs/global_struct_attribute/jni/DemoClusterClient-InvokeSubscribeImpl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ JNI_METHOD(void, DemoClusterCluster, subscribeSomeLabelsAttribute)(JNIEnv * env,
5353

5454
onSuccess.release();
5555
onFailure.release();
56-
}
56+
}

scripts/idl/tests/outputs/global_struct_attribute/jni/DemoClusterClient-ReadImpl.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ JNI_METHOD(void, DemoClusterCluster, readSomeLabelsAttribute)(JNIEnv * env, jobj
3535
onFailure.release();
3636
}
3737

38+

scripts/idl/tests/outputs/optional_argument/jni/MyClusterClient-InvokeSubscribeImpl.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,4 @@ JNI_METHOD(void, MyClusterCluster,
7676
onSuccess.release();
7777
onFailure.release();
7878
}
79+

scripts/idl/tests/outputs/optional_argument/jni/MyClusterClient-ReadImpl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
#include <platform/PlatformManager.h>
1212

1313
#define JNI_METHOD(RETURN, CLASS_NAME, METHOD_NAME) \
14-
extern "C" JNIEXPORT RETURN JNICALL Java_chip_devicecontroller_ChipClusters_00024##CLASS_NAME##_##METHOD_NAME
14+
extern "C" JNIEXPORT RETURN JNICALL Java_chip_devicecontroller_ChipClusters_00024##CLASS_NAME##_##METHOD_NAME

scripts/idl/tests/outputs/several_clusters/bridge/BridgeClustersImpl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ struct ClusterInfo
4141
},
4242
};
4343

44-
}
44+
}

scripts/idl/tests/outputs/several_clusters/bridge/BridgeGlobalStructs.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ namespace clusters {
1111

1212

1313

14-
}
14+
}

scripts/idl/tests/outputs/several_clusters/bridge/First.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ struct FirstCluster : public GeneratedCluster
2626
Attribute<uint16_t> mSomeInteger;
2727
};
2828

29-
}
29+
}

scripts/idl/tests/outputs/several_clusters/bridge/Second.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ struct SecondCluster : public GeneratedCluster
2626
Attribute<std::string> mSomeBytes;
2727
};
2828

29-
}
29+
}

scripts/idl/tests/outputs/several_clusters/bridge/Third.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ struct ThirdCluster : public GeneratedCluster
2626
Attribute<uint8_t> mSomeEnum;
2727
};
2828

29-
}
29+
}

scripts/idl/tests/outputs/several_clusters/jni/FirstClient-InvokeSubscribeImpl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ JNI_METHOD(void, FirstCluster, subscribeSomeIntegerAttribute)(JNIEnv * env, jobj
5353

5454
onSuccess.release();
5555
onFailure.release();
56-
}
56+
}

scripts/idl/tests/outputs/several_clusters/jni/FirstClient-ReadImpl.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ JNI_METHOD(void, FirstCluster, readSomeIntegerAttribute)(JNIEnv * env, jobject s
3535
onFailure.release();
3636
}
3737

38+

scripts/idl/tests/outputs/several_clusters/jni/SecondClient-InvokeSubscribeImpl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ JNI_METHOD(void, SecondCluster, subscribeSomeBytesAttribute)(JNIEnv * env, jobje
5353

5454
onSuccess.release();
5555
onFailure.release();
56-
}
56+
}

scripts/idl/tests/outputs/several_clusters/jni/SecondClient-ReadImpl.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ JNI_METHOD(void, SecondCluster, readSomeBytesAttribute)(JNIEnv * env, jobject se
3535
onFailure.release();
3636
}
3737

38+

scripts/idl/tests/outputs/several_clusters/jni/ThirdClient-InvokeSubscribeImpl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ JNI_METHOD(void, ThirdCluster, subscribeSomeEnumAttribute)(JNIEnv * env, jobject
5353

5454
onSuccess.release();
5555
onFailure.release();
56-
}
56+
}

scripts/idl/tests/outputs/several_clusters/jni/ThirdClient-ReadImpl.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ JNI_METHOD(void, ThirdCluster, readSomeEnumAttribute)(JNIEnv * env, jobject self
3535
onFailure.release();
3636
}
3737

38+

scripts/idl/tests/outputs/simple_attribute/bridge/BridgeClustersImpl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ struct ClusterInfo
2323
},
2424
};
2525

26-
}
26+
}

scripts/idl/tests/outputs/simple_attribute/bridge/BridgeGlobalStructs.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ namespace clusters {
1111

1212

1313

14-
}
14+
}

scripts/idl/tests/outputs/simple_attribute/bridge/MyCluster.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ struct MyClusterCluster : public GeneratedCluster
2626
Attribute<uint16_t> mClusterAttr;
2727
};
2828

29-
}
29+
}

scripts/idl/tests/outputs/simple_attribute/jni/MyClusterClient-InvokeSubscribeImpl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,4 @@ JNI_METHOD(void, MyClusterCluster, subscribeClusterAttrAttribute)(JNIEnv * env,
5353

5454
onSuccess.release();
5555
onFailure.release();
56-
}
56+
}

scripts/idl/tests/outputs/simple_attribute/jni/MyClusterClient-ReadImpl.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ JNI_METHOD(void, MyClusterCluster, readClusterAttrAttribute)(JNIEnv * env, jobje
3535
onFailure.release();
3636
}
3737

38+

0 commit comments

Comments
 (0)