Skip to content

Commit

Permalink
Fail if a generated file exists already
Browse files Browse the repository at this point in the history
Closes #993
  • Loading branch information
senier committed Sep 22, 2022
1 parent 8f8d689 commit 4009bb0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ Specification:

- Private types (#1156)

### Changed

Generator:

- Detect when a generated file would overwrite an existing file (#993)

## [0.6.0] - 2022-08-31

### Added
Expand Down
6 changes: 2 additions & 4 deletions rflx/generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1465,10 +1465,8 @@ def _refinement_conditions(
def create_file(filename: Path, content: str) -> None:
log.info("Creating %s", filename)

try:
filename.unlink()
except FileNotFoundError:
pass
if filename.exists():
fail(f"file {filename} already exists", subsystem=Subsystem.GENERATOR)
filename.write_text(content)


Expand Down
6 changes: 4 additions & 2 deletions tests/unit/cli_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,16 @@ def test_main_generate_no_library_files(tmp_path: Path) -> None:


def test_main_generate_prefix(tmp_path: Path) -> None:
for prefix in ["", " ", "A", "A.B", "A.B.C"]:
for index, prefix in enumerate(["", " ", "A", "A.B", "A.B.C"]):
path = tmp_path / str(index)
path.mkdir ()
assert (
cli.main(
[
"rflx",
"generate",
"-d",
str(tmp_path),
str(path),
"-p",
prefix,
MESSAGE_SPEC_FILE,
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/generator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ def test_generate_missing_template_files(monkeypatch: MonkeyPatch, tmp_path: Pat
Generator().generate(Model(), Integration(), tmp_path)


def test_generate_existing_file(tmp_path: Path) -> None:
Generator().generate(models.TLV_MODEL, Integration(), tmp_path)
with pytest.raises(RecordFluxError, match="^generator: error: file [^ ]+ already exists$"):
Generator().generate(models.TLV_MODEL, Integration(), tmp_path)


@pytest.mark.parametrize("model", MODELS)
def test_equality(model: Model, tmp_path: Path) -> None:
assert_equal_code(model, Integration(), GENERATED_DIR, tmp_path, accept_extra_files=True)
Expand Down

0 comments on commit 4009bb0

Please sign in to comment.