|
1 | 1 | """ Regenerate golden-record """
|
2 | 2 | import shutil
|
| 3 | +import tempfile |
| 4 | +import os |
| 5 | +import filecmp |
3 | 6 | from pathlib import Path
|
4 | 7 |
|
5 | 8 | from typer.testing import CliRunner
|
6 | 9 |
|
7 | 10 | from openapi_python_client.cli import app
|
8 | 11 |
|
9 |
| -if __name__ == "__main__": |
| 12 | +def regen_golden_record(): |
10 | 13 | runner = CliRunner()
|
11 | 14 | openapi_path = Path(__file__).parent / "openapi.json"
|
12 | 15 |
|
|
24 | 27 | if result.exception:
|
25 | 28 | raise result.exception
|
26 | 29 | output_path.rename(gr_path)
|
| 30 | + |
| 31 | + |
| 32 | +def regen_custom_template_golden_record(): |
| 33 | + runner = CliRunner() |
| 34 | + openapi_path = Path(__file__).parent / "openapi.json" |
| 35 | + tpl_dir = Path(__file__).parent / "test_custom_templates" |
| 36 | + |
| 37 | + gr_path = Path(__file__).parent / "golden-record" |
| 38 | + tpl_gr_path = Path(__file__).parent / "custom-templates-golden-record" |
| 39 | + |
| 40 | + output_path = Path(tempfile.mkdtemp()) |
| 41 | + config_path = Path(__file__).parent / "config.yml" |
| 42 | + |
| 43 | + shutil.rmtree(tpl_gr_path, ignore_errors=True) |
| 44 | + |
| 45 | + os.chdir(str(output_path.absolute())) |
| 46 | + result = runner.invoke(app, ["generate", f"--config={config_path}", f"--path={openapi_path}", f"--custom-template-path={tpl_dir}"]) |
| 47 | + |
| 48 | + if result.stdout: |
| 49 | + generated_output_path = output_path / 'my-test-api-client' |
| 50 | + for f in generated_output_path.glob("**/*"): # nb: works for Windows and Unix |
| 51 | + relative_to_generated = f.relative_to(generated_output_path) |
| 52 | + gr_file = gr_path / relative_to_generated |
| 53 | + if not gr_file.exists(): |
| 54 | + print(f"{gr_file} does not exist, ignoring") |
| 55 | + continue |
| 56 | + |
| 57 | + if not gr_file.is_file(): |
| 58 | + continue |
| 59 | + |
| 60 | + if not filecmp.cmp(gr_file, f, shallow=False): |
| 61 | + target_file = tpl_gr_path / relative_to_generated |
| 62 | + target_dir = target_file.parent |
| 63 | + |
| 64 | + target_dir.mkdir(parents=True, exist_ok=True) |
| 65 | + shutil.copy(f"{f}", f"{target_file}") |
| 66 | + |
| 67 | + shutil.rmtree(output_path, ignore_errors=True) |
| 68 | + |
| 69 | + if result.exception: |
| 70 | + shutil.rmtree(output_path, ignore_errors=True) |
| 71 | + raise result.exception |
| 72 | + |
| 73 | + |
| 74 | +if __name__ == "__main__": |
| 75 | + regen_golden_record() |
| 76 | + regen_custom_template_golden_record() |
0 commit comments