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