Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix up the CLI main unit tests #680

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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