Skip to content

Commit

Permalink
Fix up the CLI main unit tests (#680)
Browse files Browse the repository at this point in the history
* Add unit test of the recursive flag
* Be sure to call sys.exit in main, even when 0
* replace the need to chdir by passing full paths in tests

Signed-off-by: Eric Brown <eric.brown@securesauce.dev>
  • Loading branch information
ericwb authored Nov 1, 2024
1 parent f305ca3 commit b94d3ce
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
1 change: 1 addition & 0 deletions precli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
# SPDX-License-Identifier: BUSL-1.1
from precli.cli import main


main.main()
2 changes: 2 additions & 0 deletions precli/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,8 @@ def main():
if args.gist is True:
create_gist(file, renderer)

sys.exit(0)


if __name__ == "__main__":
main()
40 changes: 22 additions & 18 deletions tests/unit/cli/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@


class TestMain:
@classmethod
def setup_class(cls):
cls.current_dir = os.getcwd()

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

def test_main_target_not_found(self, monkeypatch):
monkeypatch.setattr("sys.argv", ["precli", "missing_file.py"])
with pytest.raises(SystemExit) as excinfo:
Expand All @@ -44,19 +36,19 @@ def test_main_config_not_found(self, monkeypatch):
main.main()
assert excinfo.value.code == 2

def test_main_invalid_config(self, monkeypatch):
monkeypatch.setattr("sys.argv", ["precli", "-c", "not_toml.json", "."])
def test_main_invalid_config(self, monkeypatch, capsys):
temp_dir = tempfile.mkdtemp()
os.chdir(temp_dir)
config = {
"enable": ["PY001"]
}
with open("not_toml.json", "w") as fd:
config_path = os.path.join(temp_dir, "not_toml.toml")
config = { "enable": ["PY001"] }
with open(config_path, "w") as fd:
json.dump(config, fd)

monkeypatch.setattr("sys.argv", ["precli", "-c", config_path, "."])
with pytest.raises(SystemExit) as excinfo:
main.main()
assert excinfo.value.code == 2
captured = capsys.readouterr()
assert "argument -c/--config: can't load" in captured.err

def test_main_invalid_output(self, monkeypatch):
monkeypatch.setattr("sys.argv", ["precli", "-o", "../does/not/exists"])
Expand All @@ -66,12 +58,12 @@ def test_main_invalid_output(self, monkeypatch):

@mock.patch("builtins.input", lambda _: "no")
def test_main_output_already_exists(self, monkeypatch, capsys):
monkeypatch.setattr("sys.argv", ["precli", "-o", "output.txt"])
temp_dir = tempfile.mkdtemp()
os.chdir(temp_dir)
with open("output.txt", "w") as fd:
output_path = os.path.join(temp_dir, "output.txt")
with open(output_path, "w") as fd:
fd.write("This file already exists. Do not overwrite.")

monkeypatch.setattr("sys.argv", ["precli", ".", "-o", output_path])
with pytest.raises(SystemExit) as excinfo:
main.main()
assert excinfo.value.code == 2
Expand All @@ -94,6 +86,18 @@ def test_main_gist_no_github_token(self, monkeypatch, capsys):
captured = capsys.readouterr()
assert "environment variable GITHUB_TOKEN undefined" in captured.err

def test_recursive_flag(self, monkeypatch):
temp_dir = tempfile.mkdtemp()
nested_dir = os.path.join(temp_dir, "nested")
os.makedirs(nested_dir)
with open(os.path.join(nested_dir, "test_file.py"), "w") as f:
f.write("print('test')")

monkeypatch.setattr("sys.argv", ["precli", "--recursive", temp_dir])
with pytest.raises(SystemExit) as excinfo:
main.main()
assert excinfo.value.code == 0

def test_main_version(self, monkeypatch, capsys):
monkeypatch.setattr("sys.argv", ["precli", "--version"])
with pytest.raises(SystemExit) as excinfo:
Expand Down

0 comments on commit b94d3ce

Please sign in to comment.