Skip to content

Commit ee27f31

Browse files
authored
Don't fully delete output directories (#1183)
Closes #1105 Co-authored-by: Dylan Anthony <[email protected]>
1 parent 3616419 commit ee27f31

File tree

7 files changed

+25
-11
lines changed

7 files changed

+25
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
default: major
3+
---
4+
5+
# Delete fewer files with `--overwrite`
6+
7+
`--overwrite` will no longer delete the entire output directory before regenerating. Instead, it will only delete
8+
specific, known directories within that directory. Right now, that is only the generated `models` and `api` directories.
9+
10+
Other generated files, like `README.md`, will be overwritten. Extra files and directories outside of those listed above
11+
will be left untouched, so you can any extra modules or files around while still updating `pyproject.toml` automatically.
12+
13+
Closes #1105.

end_to_end_tests/test_end_to_end.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
def _compare_directories(
1414
record: Path,
1515
test_subject: Path,
16-
expected_differences: dict[Path, str],
16+
expected_differences: Optional[dict[Path, str]] = None,
1717
expected_missing: Optional[set[str]] = None,
1818
ignore: list[str] = None,
1919
depth=0,
@@ -298,11 +298,11 @@ def test_update_integration_tests():
298298
config_path = source_path / "config.yaml"
299299
_run_command(
300300
"generate",
301-
extra_args=["--meta=none", "--overwrite", f"--output-path={source_path / 'integration_tests'}"],
301+
extra_args=["--overwrite", "--meta=pdm", f"--output-path={temp_dir}"],
302302
url=url,
303303
config_path=config_path
304304
)
305-
_compare_directories(temp_dir, source_path, expected_differences={})
305+
_compare_directories(source_path, temp_dir, ignore=["pyproject.toml"])
306306
import mypy.api
307307

308308
out, err, status = mypy.api.run([str(temp_dir), "--strict"])

integration-tests/.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ dmypy.json
2020
.idea/
2121

2222
/coverage.xml
23-
/.coverage
23+
/.coverage
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Marker file for PEP 561

integration-tests/pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ version = "0.0.1"
44
description = "A client library for accessing OpenAPI Test Server"
55
authors = []
66
readme = "README.md"
7+
requires-python = ">=3.9,<4.0"
78
dependencies = [
89
"httpx>=0.20.0,<0.29.0",
910
"attrs>=21.3.0",
1011
"python-dateutil>=2.8.0",
1112
]
12-
requires-python = ">=3.8,<4.0"
1313

1414
[tool.pdm]
1515
distribution = true

openapi_python_client/__init__.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,11 @@ def build(self) -> Sequence[GeneratorError]:
108108
"""Create the project from templates"""
109109

110110
print(f"Generating {self.project_dir}")
111-
if self.config.overwrite:
112-
shutil.rmtree(self.project_dir, ignore_errors=True)
113-
114111
try:
115112
self.project_dir.mkdir()
116113
except FileExistsError:
117-
return [GeneratorError(detail="Directory already exists. Delete it or use the --overwrite option.")]
114+
if not self.config.overwrite:
115+
return [GeneratorError(detail="Directory already exists. Delete it or use the --overwrite option.")]
118116
self._create_package()
119117
self._build_metadata()
120118
self._build_models()
@@ -158,7 +156,7 @@ def _get_errors(self) -> list[GeneratorError]:
158156

159157
def _create_package(self) -> None:
160158
if self.package_dir != self.project_dir:
161-
self.package_dir.mkdir()
159+
self.package_dir.mkdir(exist_ok=True)
162160
# Package __init__.py
163161
package_init = self.package_dir / "__init__.py"
164162

@@ -214,6 +212,7 @@ def _build_setup_py(self) -> None:
214212
def _build_models(self) -> None:
215213
# Generate models
216214
models_dir = self.package_dir / "models"
215+
shutil.rmtree(models_dir, ignore_errors=True)
217216
models_dir.mkdir()
218217
models_init = models_dir / "__init__.py"
219218
imports = []
@@ -259,6 +258,7 @@ def _build_api(self) -> None:
259258

260259
# Generate endpoints
261260
api_dir = self.package_dir / "api"
261+
shutil.rmtree(api_dir, ignore_errors=True)
262262
api_dir.mkdir()
263263
api_init_path = api_dir / "__init__.py"
264264
api_init_template = self.env.get_template("api_init.py.jinja")

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ composite = ["test --cov openapi_python_client tests --cov-report=term-missing"]
130130

131131
[tool.pdm.scripts.regen_integration]
132132
shell = """
133-
openapi-python-client generate --overwrite --url https://raw.githubusercontent.com/openapi-generators/openapi-test-server/main/openapi.json --config integration-tests/config.yaml --meta none --output-path integration-tests/integration_tests \
133+
openapi-python-client generate --overwrite --url https://raw.githubusercontent.com/openapi-generators/openapi-test-server/main/openapi.json --config integration-tests/config.yaml --meta pdm --output-path integration-tests \
134134
"""
135135

136136
[build-system]

0 commit comments

Comments
 (0)