Skip to content

Commit e7f9a06

Browse files
committed
Add some testing of CLI loading of config
The unit testing was lacking any testing at all. This change adds coverage of CLI loading of configuration files. Signed-off-by: Eric Brown <eric.brown@securesauce.dev>
1 parent 726acc0 commit e7f9a06

2 files changed

Lines changed: 67 additions & 8 deletions

File tree

precli/cli/main.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,20 +163,32 @@ def setup_arg_parser():
163163
parser.print_usage()
164164
sys.exit(2)
165165

166-
return args
166+
return parser, args
167167

168168

169-
def load_config(config: dict, targets: list[str]) -> dict:
169+
def load_config(
170+
parser: argparse.ArgumentParser, config: dict, targets: list[str]
171+
) -> dict:
170172
if config:
171-
return tomllib.load(config)
173+
try:
174+
return tomllib.load(config)
175+
except tomllib.TOMLDecodeError as err:
176+
parser.error(
177+
f"argument -c/--config: can't load '{config.name}': {err}"
178+
)
172179
else:
173180
default_confs = (".precli.toml", "precli.toml", "pyproject.toml")
174181
for target in filter(os.path.isdir, targets):
175182
for conf in default_confs:
176183
path = pathlib.Path(target) / conf
177-
if path.exists():
178-
with open(path, "rb") as f:
179-
return tomllib.load(f)
184+
try:
185+
if path.exists():
186+
with open(path, "rb") as f:
187+
return tomllib.load(f)
188+
except tomllib.TOMLDecodeError as err:
189+
# TODO: Log but don't exit
190+
pass
191+
180192
return {}
181193

182194

@@ -364,10 +376,10 @@ def main():
364376
logging.getLogger("urllib3").setLevel(debug)
365377

366378
# Setup the command line arguments
367-
args = setup_arg_parser()
379+
parser, args = setup_arg_parser()
368380

369381
# Load optional configuration file
370-
config = load_config(args.config, args.targets)
382+
config = load_config(parser, args.config, args.targets)
371383

372384
# CLI enabled/disabled override any config in files
373385
config["enabled"] = (

tests/unit/cli/test_main.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2024 Secure Sauce LLC
2+
# SPDX-License-Identifier: BUSL-1.1
3+
import json
4+
import os
5+
import tempfile
6+
from unittest import mock
7+
8+
import pytest
9+
10+
from precli.cli import main
11+
12+
13+
class TestMain:
14+
@classmethod
15+
def setup_class(cls):
16+
cls.base_path = os.path.join(
17+
"tests",
18+
"unit",
19+
"cli",
20+
"examples",
21+
)
22+
cls.current_dir = os.getcwd()
23+
24+
@classmethod
25+
def teardown_class(cls):
26+
os.chdir(cls.current_dir)
27+
28+
29+
@mock.patch("sys.argv", ["precli", "-c", "missing_file.toml"])
30+
def test_main_config_not_found(self):
31+
with pytest.raises(SystemExit) as excinfo:
32+
main.main()
33+
assert str(excinfo.value) == "2"
34+
35+
@mock.patch("sys.argv", ["precli", "-c", "not_toml.json", "."])
36+
def test_main_invalid_config(self):
37+
temp_dir = tempfile.mkdtemp()
38+
os.chdir(temp_dir)
39+
config = {
40+
"enable": ["PY001"]
41+
}
42+
with open("not_toml.json", "w") as fd:
43+
json.dump(config, fd)
44+
45+
with pytest.raises(SystemExit) as excinfo:
46+
main.main()
47+
assert str(excinfo.value) == "2"

0 commit comments

Comments
 (0)