1
1
import shutil
2
2
from filecmp import cmpfiles , dircmp
3
3
from pathlib import Path
4
- from typing import Dict , Optional
4
+ from typing import Dict , List , Optional
5
5
6
6
import pytest
7
7
from typer .testing import CliRunner
12
12
def _compare_directories (
13
13
record : Path ,
14
14
test_subject : Path ,
15
- expected_differences : Optional [
16
- Dict [Path , str ]
17
- ] = None , # key: path relative to generated directory, value: expected generated content
15
+ expected_differences : Dict [Path , str ],
18
16
depth = 0 ,
19
17
):
18
+ """
19
+ Compare two directories and assert that only expected_differences are different
20
+
21
+ Args:
22
+ record: Path to the expected output
23
+ test_subject: Path to the generated code being checked
24
+ expected_differences: key: path relative to generated directory, value: expected generated content
25
+ depth: Used to track recursion
26
+ """
20
27
first_printable = record .relative_to (Path .cwd ())
21
28
second_printable = test_subject .relative_to (Path .cwd ())
22
29
dc = dircmp (record , test_subject )
@@ -30,16 +37,14 @@ def _compare_directories(
30
37
31
38
expected_path_mismatches = []
32
39
for file_name in mismatches :
33
-
34
40
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 )):
41
+ expected_content = expected_differences .get ( mismatch_file_path )
42
+ if expected_content is None :
43
+ continue
38
44
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 )
45
+ generated_content = (test_subject / file_name ).read_text ()
46
+ assert generated_content == expected_content , f"Unexpected output in { mismatch_file_path } "
47
+ expected_path_mismatches .append (mismatch_file_path )
43
48
44
49
for path_mismatch in expected_path_mismatches :
45
50
matched_file_name = path_mismatch .name
@@ -62,7 +67,7 @@ def _compare_directories(
62
67
pytest .fail (failure , pytrace = False )
63
68
64
69
65
- def run_e2e_test (extra_args = None , expected_differences = None ):
70
+ def run_e2e_test (extra_args : List [ str ] , expected_differences : Dict [ Path , str ] ):
66
71
runner = CliRunner ()
67
72
openapi_path = Path (__file__ ).parent / "openapi.json"
68
73
config_path = Path (__file__ ).parent / "config.yml"
@@ -78,6 +83,8 @@ def run_e2e_test(extra_args=None, expected_differences=None):
78
83
if result .exit_code != 0 :
79
84
raise result .exception
80
85
86
+ # Use absolute paths for expected differences for easier comparisons
87
+ expected_differences = {output_path .joinpath (key ): value for key , value in expected_differences .items ()}
81
88
_compare_directories (gr_path , output_path , expected_differences = expected_differences )
82
89
83
90
import mypy .api
@@ -89,7 +96,7 @@ def run_e2e_test(extra_args=None, expected_differences=None):
89
96
90
97
91
98
def test_end_to_end ():
92
- run_e2e_test ()
99
+ run_e2e_test ([], {} )
93
100
94
101
95
102
def test_custom_templates ():
@@ -104,8 +111,7 @@ def test_custom_templates():
104
111
105
112
golden_tpls_root_dir = Path (__file__ ).parent .joinpath ("custom-templates-golden-record" )
106
113
for expected_difference_path in expected_difference_paths :
107
- path = Path ("my-test-api-client" ).joinpath (expected_difference_path )
108
- expected_differences [path ] = (golden_tpls_root_dir / expected_difference_path ).read_text ()
114
+ expected_differences [expected_difference_path ] = (golden_tpls_root_dir / expected_difference_path ).read_text ()
109
115
110
116
run_e2e_test (
111
117
extra_args = ["--custom-template-path=end_to_end_tests/test_custom_templates/" ],
0 commit comments