12
12
def _compare_directories (
13
13
record : Path ,
14
14
test_subject : Path ,
15
- expected_differences : Optional [Dict [str , str ]] = None ,
15
+ expected_differences : Optional [
16
+ Dict [str , str ]
17
+ ] = None , # key: path relative to generated directory, value: expected generated content
18
+ depth = 0 ,
16
19
):
17
20
first_printable = record .relative_to (Path .cwd ())
18
21
second_printable = test_subject .relative_to (Path .cwd ())
@@ -22,27 +25,41 @@ def _compare_directories(
22
25
pytest .fail (f"{ first_printable } or { second_printable } was missing: { missing_files } " , pytrace = False )
23
26
24
27
expected_differences = expected_differences or {}
25
- _ , mismatch , errors = cmpfiles (record , test_subject , dc .common_files , shallow = False )
26
- mismatch = set (mismatch )
27
-
28
- for file_name in mismatch | set (expected_differences .keys ()):
29
- if file_name not in expected_differences :
30
- continue
31
- if file_name not in mismatch :
32
- pytest .fail (f"Expected { file_name } to be different but it was not" , pytrace = False )
33
- generated = (test_subject / file_name ).read_text ()
34
- assert generated == expected_differences [file_name ], f"Unexpected output in { file_name } "
35
- del expected_differences [file_name ]
36
- mismatch .remove (file_name )
37
-
38
- if mismatch :
28
+ _ , mismatches , errors = cmpfiles (record , test_subject , dc .common_files , shallow = False )
29
+ mismatches = set (mismatches )
30
+
31
+ expected_path_mismatches = []
32
+ for file_name in mismatches :
33
+
34
+ mismatch_file_path = test_subject .joinpath (file_name )
35
+ for expected_differences_path in expected_differences .keys ():
36
+
37
+ if mismatch_file_path .match (str (expected_differences_path )):
38
+
39
+ generated_content = (test_subject / file_name ).read_text ()
40
+ expected_content = expected_differences [expected_differences_path ]
41
+ assert generated_content == expected_content , f"Unexpected output in { mismatch_file_path } "
42
+ expected_path_mismatches .append (expected_differences_path )
43
+
44
+ for path_mismatch in expected_path_mismatches :
45
+ matched_file_name = path_mismatch .name
46
+ mismatches .remove (matched_file_name )
47
+ del expected_differences [path_mismatch ]
48
+
49
+ if mismatches :
39
50
pytest .fail (
40
- f"{ first_printable } and { second_printable } had differing files: { mismatch } , and errors { errors } " ,
51
+ f"{ first_printable } and { second_printable } had differing files: { mismatches } , and errors { errors } " ,
41
52
pytrace = False ,
42
53
)
43
54
44
55
for sub_path in dc .common_dirs :
45
- _compare_directories (record / sub_path , test_subject / sub_path , expected_differences = expected_differences )
56
+ _compare_directories (
57
+ record / sub_path , test_subject / sub_path , expected_differences = expected_differences , depth = depth + 1
58
+ )
59
+
60
+ if depth == 0 and len (expected_differences .keys ()) > 0 :
61
+ failure = "\n " .join ([f"Expected { path } to be different but it was not" for path in expected_differences .keys ()])
62
+ pytest .fail (failure , pytrace = False )
46
63
47
64
48
65
def run_e2e_test (extra_args = None , expected_differences = None ):
@@ -60,6 +77,7 @@ def run_e2e_test(extra_args=None, expected_differences=None):
60
77
61
78
if result .exit_code != 0 :
62
79
raise result .exception
80
+
63
81
_compare_directories (gr_path , output_path , expected_differences = expected_differences )
64
82
65
83
import mypy .api
@@ -75,7 +93,20 @@ def test_end_to_end():
75
93
76
94
77
95
def test_custom_templates ():
96
+ expected_differences = {} # key: path relative to generated directory, value: expected generated content
97
+ expected_difference_paths = [
98
+ Path ("README.md" ),
99
+ Path ("my_test_api_client" ).joinpath ("api" , "__init__.py" ),
100
+ Path ("my_test_api_client" ).joinpath ("api" , "tests" , "__init__.py" ),
101
+ Path ("my_test_api_client" ).joinpath ("api" , "default" , "__init__.py" ),
102
+ ]
103
+
104
+ golden_tpls_root_dir = Path (__file__ ).parent .joinpath ("custom-templates-golden-record" )
105
+ for expected_difference_path in expected_difference_paths :
106
+ path = Path ("my-test-api-client" ).joinpath (expected_difference_path )
107
+ expected_differences [path ] = (golden_tpls_root_dir / expected_difference_path ).read_text ()
108
+
78
109
run_e2e_test (
79
- extra_args = ["--custom-template-path=end_to_end_tests/test_custom_templates" ],
80
- expected_differences = { "README.md" : "my-test-api-client" } ,
110
+ extra_args = ["--custom-template-path=end_to_end_tests/test_custom_templates/ " ],
111
+ expected_differences = expected_differences ,
81
112
)
0 commit comments