37
37
38
38
39
39
TESTS_DIR = os .path .join (os .path .dirname (__file__ ), "tests" )
40
+ REGENERATE_GOLDEN_IMAGES = False
40
41
41
42
42
43
@dataclass
@@ -63,19 +64,33 @@ def __init__(self, test_case: GeneratorTestCase, checker: unittest.TestCase):
63
64
self .checker = checker
64
65
self .checked_files = set ()
65
66
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
+
66
75
def get_existing_data (self , relative_path : str ):
67
76
self .checked_files .add (relative_path )
68
77
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 ()
73
82
74
83
# This will attempt a new write, causing a unit test failure
75
84
self .checker .fail ("Expected output %s not found" % relative_path )
76
85
return None
77
86
78
87
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
+
79
94
# This is a unit test failure: we do NOT expect
80
95
# to write any new data
81
96
@@ -148,4 +163,8 @@ def test_generators(self):
148
163
149
164
150
165
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
151
170
unittest .main ()
0 commit comments