Skip to content

Commit

Permalink
Update CLI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emdoyle committed Feb 8, 2024
1 parent 28641d3 commit 989e746
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,20 @@ def mock_isdir(path: str) -> bool:
mocker.patch("modguard.cli.os.path.isdir", mock_isdir)


@pytest.fixture
def parser():
return cli.build_parser()


def test_execute_with_valid_dir(capfd, parser, mock_isdir, mock_check):
def test_execute_with_valid_dir(capfd, mock_isdir, mock_check):
# Test with a valid path as mocked
args = parser.parse_args(["valid_dir"])
args = cli.parse_base_arguments(["valid_dir"])
with pytest.raises(SystemExit) as sys_exit:
cli.main(args)
cli.modguard(args)
captured = capfd.readouterr()
assert sys_exit.value.code == 0
assert "✅" in captured.out
assert "All modules safely guarded!" in captured.out


def test_execute_with_error(capfd, parser, mock_isdir, mock_check):
def test_execute_with_error(capfd, mock_isdir, mock_check):
# Test with a valid path as mocked
args = parser.parse_args(["valid_dir"])
args = cli.parse_base_arguments(["valid_dir"])
# Mock an error returned from check
location = "valid_dir/file.py"
message = "Import valid_dir in valid_dir/file.py is blocked by boundary"
Expand All @@ -53,40 +48,40 @@ def test_execute_with_error(capfd, parser, mock_isdir, mock_check):
)
]
with pytest.raises(SystemExit) as sys_exit:
cli.main(args)
cli.modguard(args)
captured = capfd.readouterr()
assert sys_exit.value.code == 1
assert location in captured.err
assert message in captured.err


def test_execute_with_invalid_dir(capfd, mock_isdir, parser):
def test_execute_with_invalid_dir(capfd, mock_isdir):
# Test with an invalid path as mocked
args = parser.parse_args(["invalid_dir"])
args = cli.parse_base_arguments(["invalid_dir"])
with pytest.raises(SystemExit) as sys_exit:
cli.main(args)
cli.modguard(args)
captured = capfd.readouterr()
assert sys_exit.value.code == 1
assert "invalid_dir is not a valid directory" in captured.err


def test_execute_with_valid_exclude(capfd, parser, mock_isdir, mock_check):
def test_execute_with_valid_exclude(capfd, mock_isdir, mock_check):
# Test with a valid path as mocked
args = parser.parse_args(["valid_dir", "--exclude", "valid_dir"])
args = cli.parse_base_arguments(["valid_dir", "--exclude", "valid_dir"])
with pytest.raises(SystemExit) as sys_exit:
cli.main(args)
cli.modguard(args)
captured = capfd.readouterr()
assert sys_exit.value.code == 0
assert "✅" in captured.out
assert "All modules safely guarded!" in captured.out


def test_execute_with_invalid_exclude(capfd, parser, mock_isdir):
def test_execute_with_invalid_exclude(capfd, mock_isdir):
# Test with a valid path as mocked
args = parser.parse_args(["valid_dir", "--exclude", "invalid_dir"])
args = cli.parse_base_arguments(["valid_dir", "--exclude", "invalid_dir"])
# Mock a valid return from check
with pytest.raises(SystemExit) as sys_exit:
cli.main(args)
cli.modguard(args)
captured = capfd.readouterr()
assert sys_exit.value.code == 1
assert "invalid_dir is not a valid dir or file" in captured.err

0 comments on commit 989e746

Please sign in to comment.