Skip to content

Commit ffc5a52

Browse files
committed
Added more logging into editor and handling of invalid TOML files
1 parent a8c8ded commit ffc5a52

File tree

5 files changed

+29
-10
lines changed

5 files changed

+29
-10
lines changed

devcli/commands/edit.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ def run_editor(file: Path):
1414
"""Open editor on file"""
1515
c_editor = ctx.obj["devcli.commands.edit.editor"]
1616
e_editor = os.getenv("EDITOR", "vim")
17+
logger.info(f"env EDITOR={e_editor} config={c_editor}")
1718

1819
# config first, env and last default
1920
editor = c_editor if c_editor else e_editor
21+
logger.info(f"selected editor: {editor}")
2022

2123
os.execvp(editor, [editor, file])
2224

@@ -27,18 +29,23 @@ def open_editor(file: Path):
2729

2830

2931
@cli.command()
30-
def config(ctx: Context):
32+
def config(ctx: Context, dry: bool = False):
33+
""" Opens the most specific configuration """
3134
editor = prep_editor(ctx)
3235
configs = ctx.obj.files()
36+
logger.info(f"Found these configurations: {configs}")
3337
if not configs:
3438
cmd.stop("No configuration files were found.")
35-
36-
# last file is the most specific one
37-
editor(configs[-1])
39+
elif dry:
40+
cmd.info(f"Will edit: {configs[-1]}")
41+
else:
42+
# last file is the most specific one
43+
editor(configs[-1])
3844

3945

4046
@cli.command("command")
4147
def edit_command(ctx: Context, name: str):
48+
""" Opens the command [name] for edit """
4249
editor = prep_editor(ctx)
4350
configs = ctx.obj.files()
4451
p = Path(configs[-1]).parent

devcli/config/config.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ def add_config(self, config_file: str | Path):
3535
if Path(config_file).is_file():
3636
with open(config_file) as file:
3737
self.logger.debug(f"loading {config_file} data")
38-
config_contents = toml.load(file)
39-
self._audit[config_file] = copy.deepcopy(config_contents)
40-
self.logger.debug(f"configuration contents: {config_contents}")
41-
self.merge_update(self._config, config_contents)
38+
try:
39+
config_contents = toml.load(file)
40+
self._audit[config_file] = copy.deepcopy(config_contents)
41+
self.logger.debug(f"configuration contents: {config_contents}")
42+
self.merge_update(self._config, config_contents)
43+
except:
44+
self.logger.error(f"failed to load {config_file}, check syntax")
4245
else:
4346
self.logger.warning(f"{config_file} is not a file or does not exist")
4447

tests/commands/test_edit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ def setup(devcli_cmd):
1616

1717

1818
def test_edit_config_calls_editor(mock_exec):
19-
result = edit("config")
19+
edit("config")
2020
mock_exec.assert_called_once_with("nvim", ["nvim", ANY])
2121

2222

2323
def test_edit_command_calls_editor(mock_exec):
24-
result = edit("command", "placeholder")
24+
edit("command", "placeholder")
2525
mock_exec.assert_called_once_with("nvim", ["nvim", ANY])

tests/fixtures/invalid.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[devcli]
2+
3+
# value without quotes breaks TOML
4+
key = value

tests/test_conf.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,8 @@ def test_files_returns_files_loaded():
7777
conf.add_config(spec_config)
7878
# and now it is listed in files()
7979
assert spec_config in conf.files()
80+
81+
82+
def test_handles_error_parsing_config():
83+
invalid_config = project_root("tests/fixtures/invalid.toml")
84+
conf.add_config(invalid_config)

0 commit comments

Comments
 (0)