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

test: refactor tests for CLI #63

Merged
merged 1 commit into from
Aug 12, 2024
Merged
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
80 changes: 37 additions & 43 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,80 +36,74 @@ def __getattr__(self, name):
class TestCLIGetArgs:
# get_args

@patch(
"argparse.ArgumentParser.parse_args",
return_value=ArgsMock(
commit_message="commit message",
file=None,
hash=None,
from_hash=None,
quiet=None,
),
)
@patch("sys.argv", ["prog", "commit message"])
def test__get_args__with_commit_message(self, *_):
args = get_args()
assert args.commit_message == "commit message"
assert args.file is None
assert args.hash is None
assert args.from_hash is None
assert args.quiet is None

@patch(
"argparse.ArgumentParser.parse_args",
return_value=ArgsMock(file="path/to/file.txt"),
)
@patch("sys.argv", ["prog"])
def test__get_args__without_commit_message(self, *_):
with pytest.raises(SystemExit) as ex:
get_args()
assert ex.value.code == 2

@patch("sys.argv", ["prog", "--file", "path/to/file.txt"])
def test__get_args__with_file(self, *_):
args = get_args()
assert args.file == "path/to/file.txt"

@patch(
"argparse.ArgumentParser.parse_args",
return_value=ArgsMock(hash="commit_hash", file=None),
)
@patch("sys.argv", ["prog", "--hash", "commit_hash"])
def test__get_args__with_hash(self, *_):
args = get_args()
assert args.hash == "commit_hash"
assert args.file is None

@patch(
"argparse.ArgumentParser.parse_args",
return_value=ArgsMock(from_hash="from_commit_hash", file=None, hash=None),
)
@patch("sys.argv", ["prog", "--from-hash", "from_commit_hash"])
def test__get_args__with_from_hash(self, *_):
args = get_args()
assert args.from_hash == "from_commit_hash"
assert args.file is None
assert args.hash is None
assert args.to_hash == "HEAD"

@patch("sys.argv", ["prog", "--to-hash", "to_commit_hash"])
def test__get_args__with_to_hash_without_from_hash(self, *_):
with pytest.raises(SystemExit) as ex:
get_args()
assert ex.value.code == 2

@patch(
"argparse.ArgumentParser.parse_args",
return_value=ArgsMock(
from_hash="from_commit_hash", to_hash="to_commit_hash", file=None, hash=None
),
"sys.argv",
["prog", "--from-hash", "from_commit_hash", "--to-hash", "to_commit_hash"],
)
def test__get_args__with_to_hash(self, *_):
args = get_args()
assert args.from_hash == "from_commit_hash"
assert args.to_hash == "to_commit_hash"
assert args.file is None
assert args.hash is None

@patch(
"argparse.ArgumentParser.parse_args",
return_value=ArgsMock(skip_detail=True),
)
@patch("sys.argv", ["prog", "--skip-detail", "commit_msg"])
def test__get_args__with_skip_detail(self, *_):
args = get_args()
assert args.skip_detail is True

@patch(
"argparse.ArgumentParser.parse_args",
return_value=ArgsMock(hide_input=True),
)
@patch("sys.argv", ["prog", "--hide-input", "commit_msg"])
def test__get_args__with_hide_input(self, *_):
args = get_args()
assert args.hide_input is True

@patch("sys.argv", ["prog", "--verbose", "commit_msg"])
def test__get_args__with_verbose(self, *_):
args = get_args()
assert args.verbose is True

@patch("sys.argv", ["prog", "--quiet", "commit_msg"])
def test__get_args__with_quiet(self, *_):
args = get_args()
assert args.quiet is True

@patch("sys.argv", ["prog", "--quiet", "--verbose", "commit_msg"])
def test__get_args___fails_with_quiet_and_verbose(self, *_):
with pytest.raises(SystemExit) as ex:
get_args()
assert ex.value.code == 2


@patch("commitlint.console.success")
@patch("commitlint.console.error")
Expand Down
Loading