Skip to content

Commit

Permalink
[Tests][Import][Added] Various errors checks
Browse files Browse the repository at this point in the history
- Fixed one of the messages
- Added potential fix for a typo
  • Loading branch information
set-soft committed Jan 19, 2024
1 parent 5033a29 commit 8cdd6aa
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 3 deletions.
12 changes: 9 additions & 3 deletions kibot/config_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
AUTO_DOWN_URL = GITHUB_RAW+'auto_download-22x22.png'
AUTO_DOWN = '![Auto-download]('+AUTO_DOWN_URL+')'
VALID_SECTIONS = {'kiplot', 'kibot', 'import', 'global', 'filters', 'variants', 'preflight', 'outputs', 'groups'}
VALID_IMPORT = {'file', 'is_external', 'outputs', 'preflights', 'filters', 'variants', 'global', 'globals', 'groups',
'definitions'}
VALID_KIBOT_SEC = {'version', 'imported_global_has_less_priority'}
RST_WARNING = ".. Automatically generated by KiBot, please don't edit this file\n"
rst_mode = False
Expand Down Expand Up @@ -317,9 +319,9 @@ def _parse_import_items(kind, fname, value):
if isinstance(v, str):
values.append(v)
else:
CfgYamlReader._config_error_import(fname, '`{}` items must be strings ({})'.format(kind, str(v)))
CfgYamlReader._config_error_import(fname, f'`{kind}` items must be strings ({v})')
return values
CfgYamlReader._config_error_import(fname, '`{}` must be a string or a list ({})'.format(kind, str(v)))
CfgYamlReader._config_error_import(fname, f'`{kind}` must be a string or a list ({value})')

def _parse_import_outputs(self, outs, explicit_outs, fn_rel, data, imported):
sel_outs = []
Expand Down Expand Up @@ -556,7 +558,11 @@ def _parse_import(self, imp, name, collected_definitions, apply=True, depth=0):
CfgYamlReader._config_error_import(fn, 'definitions must be a dict')
local_defs = v
else:
self._config_error_import(fn, "Unknown import entry `{}`".format(str(v)))
msg = f"Unknown import entry `{k}`"
best_matches = difflib.get_close_matches(k, VALID_IMPORT)
if best_matches:
msg += " (did you mean {}?)".format(' or '.join(best_matches))
self._config_error_import(fn, msg)
if fn is None:
raise KiPlotConfigurationError("`import` entry without `file` ({})".format(str(entry)))
else:
Expand Down
54 changes: 54 additions & 0 deletions tests/test_plot/test_yaml_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,3 +808,57 @@ def test_pre_list_instead_of_dict(test_dir):
ctx.run(EXIT_BAD_CONFIG)
assert ctx.search_err(r"Found .*list.* instead of dict")
ctx.clean_up(keep_project=True)


@pytest.mark.indep
def test_import_not_list(test_dir):
""" Import preflights, but give a number """
ctx = context.TestContext(test_dir, PRJ, 'error_import_not_list')
ctx.run(EXIT_BAD_CONFIG)
assert ctx.search_err(r"`preflights` must be a string or a list")
ctx.clean_up(keep_project=True)


@pytest.mark.indep
def test_import_item_not_str(test_dir):
""" Import preflights, but give a number in the list """
ctx = context.TestContext(test_dir, PRJ, 'error_import_item_not_str')
ctx.run(EXIT_BAD_CONFIG)
assert ctx.search_err(r"`preflights` items must be strings")
ctx.clean_up(keep_project=True)


@pytest.mark.indep
def test_import_defs_not_dict(test_dir):
""" Import definitions, but not a dict """
ctx = context.TestContext(test_dir, PRJ, 'error_import_defs_not_dict')
ctx.run(EXIT_BAD_CONFIG)
assert ctx.search_err(r"definitions must be a dict")
ctx.clean_up(keep_project=True)


@pytest.mark.indep
def test_import_unk_entry(test_dir):
""" Import unknown entry (pre-flight) """
ctx = context.TestContext(test_dir, PRJ, 'error_import_unk_entry')
ctx.run(EXIT_BAD_CONFIG)
assert ctx.search_err(r"Unknown import entry `pre-flights` .* in .unnamed. import")
ctx.clean_up(keep_project=True)


@pytest.mark.indep
def test_import_no_file(test_dir):
""" Import no file name """
ctx = context.TestContext(test_dir, PRJ, 'error_import_no_file')
ctx.run(EXIT_BAD_CONFIG)
assert ctx.search_err(r"`import` entry without `file`")
ctx.clean_up(keep_project=True)


@pytest.mark.indep
def test_import_no_str_or_dict(test_dir):
""" Import no file name """
ctx = context.TestContext(test_dir, PRJ, 'error_import_no_str_or_dict')
ctx.run(EXIT_BAD_CONFIG)
assert ctx.search_err(r"`import` items must be strings or dicts")
ctx.clean_up(keep_project=True)
6 changes: 6 additions & 0 deletions tests/yaml_samples/error_import_defs_not_dict.kibot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kibot:
version: 1

import:
- file: drc.kibot.yaml
definitions: 10
6 changes: 6 additions & 0 deletions tests/yaml_samples/error_import_item_not_str.kibot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kibot:
version: 1

import:
- file: drc.kibot.yaml
preflights: ['a', 10]
5 changes: 5 additions & 0 deletions tests/yaml_samples/error_import_no_file.kibot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kibot:
version: 1

import:
- preflights: run_drc
5 changes: 5 additions & 0 deletions tests/yaml_samples/error_import_no_str_or_dict.kibot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kibot:
version: 1

import:
- 10
6 changes: 6 additions & 0 deletions tests/yaml_samples/error_import_not_list.kibot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kibot:
version: 1

import:
- file: drc.kibot.yaml
preflights: 10
5 changes: 5 additions & 0 deletions tests/yaml_samples/error_import_unk_entry.kibot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kibot:
version: 1

import:
- pre-flights: 10

0 comments on commit 8cdd6aa

Please sign in to comment.