1
+ from pathlib import Path
1
2
import shutil
3
+ import subprocess
2
4
3
5
import pytest
4
6
import yaml
5
7
from httpx import ConnectError
6
8
from orjson import orjson
7
9
from pydantic import ValidationError
8
10
9
- from openapi_python_generator .common import HTTPLibrary
11
+ from openapi_python_generator .common import FormatOptions , Formatter , HTTPLibrary
10
12
from openapi_python_generator .common import library_config_dict
11
13
from openapi_python_generator .generate_data import generate_data
12
14
from openapi_python_generator .generate_data import get_open_api
@@ -66,7 +68,7 @@ def test_generate_data(model_data_with_cleanup):
66
68
67
69
def test_write_data (model_data_with_cleanup ):
68
70
result = generator (model_data_with_cleanup , library_config_dict [HTTPLibrary .httpx ])
69
- write_data (result , test_result_path )
71
+ write_data (result , test_result_path , Formatter . BLACK )
70
72
71
73
assert test_result_path .exists ()
72
74
assert test_result_path .is_dir ()
@@ -90,7 +92,7 @@ def test_write_data(model_data_with_cleanup):
90
92
model_data_copy .paths = None
91
93
92
94
result = generator (model_data_copy , library_config_dict [HTTPLibrary .httpx ])
93
- write_data (result , test_result_path )
95
+ write_data (result , test_result_path , Formatter . BLACK )
94
96
95
97
assert test_result_path .exists ()
96
98
assert test_result_path .is_dir ()
@@ -105,3 +107,78 @@ def test_write_data(model_data_with_cleanup):
105
107
assert (test_result_path / "models" / "__init__.py" ).is_file ()
106
108
assert (test_result_path / "__init__.py" ).exists ()
107
109
assert (test_result_path / "__init__.py" ).is_file ()
110
+
111
+ def test_write_formatted_data (model_data_with_cleanup ):
112
+ result = generator (model_data_with_cleanup , library_config_dict [HTTPLibrary .httpx ])
113
+
114
+ # First write code without formatter
115
+ write_data (result , test_result_path , Formatter .NONE )
116
+
117
+ assert test_result_path .exists ()
118
+ assert test_result_path .is_dir ()
119
+ assert (test_result_path / "api_config.py" ).exists ()
120
+ assert (test_result_path / "models" ).exists ()
121
+ assert (test_result_path / "models" ).is_dir ()
122
+ assert (test_result_path / "services" ).exists ()
123
+ assert (test_result_path / "services" ).is_dir ()
124
+ assert (test_result_path / "models" / "__init__.py" ).exists ()
125
+ assert (test_result_path / "services" / "__init__.py" ).exists ()
126
+ assert (test_result_path / "services" / "__init__.py" ).is_file ()
127
+ assert (test_result_path / "models" / "__init__.py" ).is_file ()
128
+ assert (test_result_path / "__init__.py" ).exists ()
129
+ assert (test_result_path / "__init__.py" ).is_file ()
130
+
131
+ assert not files_are_black_formatted (test_result_path )
132
+
133
+ # delete test_result_path folder
134
+ shutil .rmtree (test_result_path )
135
+
136
+ model_data_copy = model_data_with_cleanup .copy ()
137
+ model_data_copy .components = None
138
+ model_data_copy .paths = None
139
+
140
+ result = generator (model_data_copy , library_config_dict [HTTPLibrary .httpx ])
141
+ write_data (result , test_result_path , Formatter .BLACK )
142
+
143
+ assert test_result_path .exists ()
144
+ assert test_result_path .is_dir ()
145
+ assert (test_result_path / "api_config.py" ).exists ()
146
+ assert (test_result_path / "models" ).exists ()
147
+ assert (test_result_path / "models" ).is_dir ()
148
+ assert (test_result_path / "services" ).exists ()
149
+ assert (test_result_path / "services" ).is_dir ()
150
+ assert (test_result_path / "models" / "__init__.py" ).exists ()
151
+ assert (test_result_path / "services" / "__init__.py" ).exists ()
152
+ assert (test_result_path / "services" / "__init__.py" ).is_file ()
153
+ assert (test_result_path / "models" / "__init__.py" ).is_file ()
154
+ assert (test_result_path / "__init__.py" ).exists ()
155
+ assert (test_result_path / "__init__.py" ).is_file ()
156
+
157
+ assert files_are_black_formatted (test_result_path )
158
+
159
+ def files_are_black_formatted (test_result_path : Path ) -> bool :
160
+ # Run the `black --check` command on all files. This does not write any file.
161
+ result = subprocess .run ([
162
+ "black" ,
163
+ "--check" ,
164
+ # Overwrite any exclusion due to a .gitignore.
165
+ "--exclude" , "''" ,
166
+ # Settings also used when formatting the code when writing it
167
+ "--fast" if FormatOptions .skip_validation else "--safe" ,
168
+ "--line-length" , str (FormatOptions .line_length ),
169
+ # The source directory
170
+ str (test_result_path .absolute ())
171
+ ],
172
+ capture_output = True ,
173
+ text = True
174
+ )
175
+
176
+ # With `--check` the return status has the following meaning:
177
+ # - Return code 0 means nothing would change.
178
+ # - Return code 1 means some files would be reformatted.
179
+ # - Return code 123 means there was an internal error.
180
+
181
+ if result .returncode == 123 :
182
+ result .check_returncode # raise the error
183
+
184
+ return result .returncode == 0
0 commit comments