Skip to content

Commit

Permalink
Add some testing of CLI loading of config
Browse files Browse the repository at this point in the history
The unit testing was lacking any testing at all. This change adds
coverage of CLI loading of configuration files.

Signed-off-by: Eric Brown <[email protected]>
  • Loading branch information
ericwb committed Oct 29, 2024
1 parent 726acc0 commit e7f9a06
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 8 deletions.
28 changes: 20 additions & 8 deletions precli/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,32 @@ def setup_arg_parser():
parser.print_usage()
sys.exit(2)

return args
return parser, args


def load_config(config: dict, targets: list[str]) -> dict:
def load_config(
parser: argparse.ArgumentParser, config: dict, targets: list[str]
) -> dict:
if config:
return tomllib.load(config)
try:
return tomllib.load(config)
except tomllib.TOMLDecodeError as err:
parser.error(
f"argument -c/--config: can't load '{config.name}': {err}"
)
else:
default_confs = (".precli.toml", "precli.toml", "pyproject.toml")
for target in filter(os.path.isdir, targets):
for conf in default_confs:
path = pathlib.Path(target) / conf
if path.exists():
with open(path, "rb") as f:
return tomllib.load(f)
try:
if path.exists():
with open(path, "rb") as f:
return tomllib.load(f)
except tomllib.TOMLDecodeError as err:
# TODO: Log but don't exit
pass

return {}


Expand Down Expand Up @@ -364,10 +376,10 @@ def main():
logging.getLogger("urllib3").setLevel(debug)

# Setup the command line arguments
args = setup_arg_parser()
parser, args = setup_arg_parser()

# Load optional configuration file
config = load_config(args.config, args.targets)
config = load_config(parser, args.config, args.targets)

# CLI enabled/disabled override any config in files
config["enabled"] = (
Expand Down
47 changes: 47 additions & 0 deletions tests/unit/cli/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2024 Secure Sauce LLC
# SPDX-License-Identifier: BUSL-1.1
import json
import os
import tempfile
from unittest import mock

import pytest

from precli.cli import main


class TestMain:
@classmethod
def setup_class(cls):
cls.base_path = os.path.join(
"tests",
"unit",
"cli",
"examples",
)
cls.current_dir = os.getcwd()

@classmethod
def teardown_class(cls):
os.chdir(cls.current_dir)


@mock.patch("sys.argv", ["precli", "-c", "missing_file.toml"])
def test_main_config_not_found(self):
with pytest.raises(SystemExit) as excinfo:
main.main()
assert str(excinfo.value) == "2"

@mock.patch("sys.argv", ["precli", "-c", "not_toml.json", "."])
def test_main_invalid_config(self):
temp_dir = tempfile.mkdtemp()
os.chdir(temp_dir)
config = {
"enable": ["PY001"]
}
with open("not_toml.json", "w") as fd:
json.dump(config, fd)

with pytest.raises(SystemExit) as excinfo:
main.main()
assert str(excinfo.value) == "2"

0 comments on commit e7f9a06

Please sign in to comment.