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