From c801f7c31160e94c88ab66b346ad65b6ff319680 Mon Sep 17 00:00:00 2001 From: Guilherme Lima Date: Fri, 21 Jun 2024 21:37:59 +0200 Subject: [PATCH] fix: let 'set' command fail on key not found Whenever a key is not found within a 'set' command, the command should error out instead of trying to continue execution and assign the value to a partially parsed key. This way no unintended modifications to the TOML file are performed. Example: `toml set --toml-path pyproject.toml tool.ruffs.target-version py39` The table `tool.ruffs` does not exist but the current implementation captures this exception and continues along, actually executing `tool.target-version = "py39"`. --- tests/toml_cli/test_init.py | 3 +++ toml_cli/__init__.py | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/toml_cli/test_init.py b/tests/toml_cli/test_init.py index fb5feeb..ef27309 100644 --- a/tests/toml_cli/test_init.py +++ b/tests/toml_cli/test_init.py @@ -68,6 +68,9 @@ def test_set_value(tmp_path: pathlib.Path): """ ) + result = runner.invoke(app, ["set", "--toml-path", str(test_toml_path), "person.KEY_THAT_DOES_NOT_EXIST.name", "15"]) + assert result.exit_code == 1 + result = runner.invoke(app, ["set", "--toml-path", str(test_toml_path), "person.age", "15"]) assert result.exit_code == 0 assert 'age = "15"' in test_toml_path.read_text() diff --git a/toml_cli/__init__.py b/toml_cli/__init__.py index aa51b14..ee286a3 100644 --- a/toml_cli/__init__.py +++ b/toml_cli/__init__.py @@ -51,7 +51,8 @@ def set_( try: toml_part = toml_part[key_part] except tomlkit.exceptions.NonExistentKey: - typer.echo(f"Key {key} can not set", err=True) + typer.echo(f"error: non-existent key '{key}' can not be set to value '{value}'", err=True) + exit(1) if to_int: value = int(value)