-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
2 changed files
with
67 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |