Skip to content
Open
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
2 changes: 1 addition & 1 deletion tests/commands/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


@pytest.fixture()
def config():
def mock_config():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not mocking a config here. We're providing a sample config instead. mock_config is inaccurate. I probably would try sample_config or something similiar

_config = BaseConfig()
_config.settings.update({"name": defaults.DEFAULT_SETTINGS["name"]})
return _config
Expand Down
2 changes: 1 addition & 1 deletion tests/commands/test_bump_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ def test_bump_dry_run(mocker: MockFixture, capsys):
assert tag_exists is False


def test_bump_in_non_git_project(tmpdir, config, mocker: MockFixture):
def test_bump_in_non_git_project(tmpdir, mock_config, mocker: MockFixture):
testargs = ["cz", "bump", "--yes"]
mocker.patch.object(sys, "argv", testargs)

Expand Down
23 changes: 12 additions & 11 deletions tests/commands/test_changelog_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,17 +279,18 @@ def test_changelog_incremental_keep_a_changelog_sample(

@pytest.mark.usefixtures("tmp_commitizen_project")
@pytest.mark.parametrize("dry_run", [True, False])
def test_changelog_hook(mocker: MockFixture, config: BaseConfig, dry_run: bool):
def test_changelog_hook(mocker: MockFixture, mock_config: BaseConfig, dry_run: bool):
changelog_hook_mock = mocker.Mock()
changelog_hook_mock.return_value = "cool changelog hook"

create_file_and_commit("feat: new file")
create_file_and_commit("refactor: is in changelog")
create_file_and_commit("Merge into master")

config.settings["change_type_order"] = ["Refactor", "Feat"] # type: ignore[typeddict-unknown-key]
mock_config.settings["change_type_order"] = ["Refactor", "Feat"] # type: ignore[typeddict-unknown-key]
changelog = Changelog(
config, {"unreleased_version": None, "incremental": True, "dry_run": dry_run}
mock_config,
{"unreleased_version": None, "incremental": True, "dry_run": dry_run},
)
mocker.patch.object(changelog.cz, "changelog_hook", changelog_hook_mock)
try:
Expand Down Expand Up @@ -330,7 +331,7 @@ def test_changelog_hook_customize(mocker: MockFixture, config_customize):


@pytest.mark.usefixtures("tmp_commitizen_project")
def test_changelog_release_hook(mocker: MockFixture, config):
def test_changelog_release_hook(mocker: MockFixture, mock_config):
def changelog_release_hook(release: dict, tag: git.GitTag) -> dict:
return release

Expand All @@ -340,9 +341,9 @@ def changelog_release_hook(release: dict, tag: git.GitTag) -> dict:
create_file_and_commit("Merge into master")
git.tag(f"0.{i + 1}.0")

# changelog = Changelog(config, {})
# changelog = Changelog(mock_config, {})
changelog = Changelog(
config, {"unreleased_version": None, "incremental": True, "dry_run": False}
mock_config, {"unreleased_version": None, "incremental": True, "dry_run": False}
)
mocker.patch.object(changelog.cz, "changelog_release_hook", changelog_release_hook)
spy = mocker.spy(changelog.cz, "changelog_release_hook")
Expand Down Expand Up @@ -529,7 +530,7 @@ def test_changelog_with_different_tag_name_and_changelog_content(
cli.main()


def test_changelog_in_non_git_project(tmpdir, config, mocker: MockFixture):
def test_changelog_in_non_git_project(tmpdir, mock_config, mocker: MockFixture):
testargs = ["cz", "changelog", "--incremental"]
mocker.patch.object(sys, "argv", testargs)

Expand Down Expand Up @@ -1382,7 +1383,7 @@ def test_changelog_prerelease_rev_with_use_scheme_semver(


@pytest.mark.usefixtures("tmp_commitizen_project")
def test_changelog_uses_version_tags_for_header(mocker: MockFixture, config):
def test_changelog_uses_version_tags_for_header(mocker: MockFixture, mock_config):
"""Tests that changelog headers always use version tags even if there are non-version tags

This tests a scenario fixed in this commit:
Expand All @@ -1396,7 +1397,7 @@ def test_changelog_uses_version_tags_for_header(mocker: MockFixture, config):
write_patch = mocker.patch("commitizen.commands.changelog.out.write")

changelog = Changelog(
config, {"dry_run": True, "incremental": True, "unreleased_version": None}
mock_config, {"dry_run": True, "incremental": True, "unreleased_version": None}
)

with pytest.raises(DryRunExit):
Expand All @@ -1411,7 +1412,7 @@ def test_changelog_uses_version_tags_for_header(mocker: MockFixture, config):

@pytest.mark.usefixtures("tmp_commitizen_project")
def test_changelog_from_current_version_tag_with_nonversion_tag(
mocker: MockFixture, config
mocker: MockFixture, mock_config
):
"""Tests that changelog generation for a single version works even if
there is a non-version tag in the list of tags
Expand Down Expand Up @@ -1450,7 +1451,7 @@ def test_changelog_from_current_version_tag_with_nonversion_tag(
write_patch = mocker.patch("commitizen.commands.changelog.out.write")

changelog = Changelog(
config,
mock_config,
{
"dry_run": True,
"incremental": False,
Expand Down
104 changes: 60 additions & 44 deletions tests/commands/test_check_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,17 @@ def test_check_conventional_commit_succeeds(mocker: MockFixture, capsys):
),
),
)
def test_check_no_conventional_commit(commit_msg, config, mocker: MockFixture, tmpdir):
def test_check_no_conventional_commit(
commit_msg, mock_config, mocker: MockFixture, tmpdir
):
with pytest.raises(InvalidCommitMessageError):
error_mock = mocker.patch("commitizen.out.error")

tempfile = tmpdir.join("temp_commit_file")
tempfile.write(commit_msg)

check_cmd = commands.Check(
config=config, arguments={"commit_msg_file": tempfile}
config=mock_config, arguments={"commit_msg_file": tempfile}
)
check_cmd()
error_mock.assert_called_once()
Expand All @@ -164,56 +166,62 @@ def test_check_no_conventional_commit(commit_msg, config, mocker: MockFixture, t
"bump: 0.0.1 -> 1.0.0",
),
)
def test_check_conventional_commit(commit_msg, config, mocker: MockFixture, tmpdir):
def test_check_conventional_commit(
commit_msg, mock_config, mocker: MockFixture, tmpdir
):
success_mock = mocker.patch("commitizen.out.success")

tempfile = tmpdir.join("temp_commit_file")
tempfile.write(commit_msg)

check_cmd = commands.Check(config=config, arguments={"commit_msg_file": tempfile})
check_cmd = commands.Check(
config=mock_config, arguments={"commit_msg_file": tempfile}
)

check_cmd()
success_mock.assert_called_once()


def test_check_command_when_commit_file_not_found(config):
def test_check_command_when_commit_file_not_found(mock_config):
with pytest.raises(FileNotFoundError):
commands.Check(config=config, arguments={"commit_msg_file": "no_such_file"})()
commands.Check(
config=mock_config, arguments={"commit_msg_file": "no_such_file"}
)()


def test_check_a_range_of_git_commits(config, mocker: MockFixture):
def test_check_a_range_of_git_commits(mock_config, mocker: MockFixture):
success_mock = mocker.patch("commitizen.out.success")
mocker.patch(
"commitizen.git.get_commits", return_value=_build_fake_git_commits(COMMIT_LOG)
)

check_cmd = commands.Check(
config=config, arguments={"rev_range": "HEAD~10..master"}
config=mock_config, arguments={"rev_range": "HEAD~10..master"}
)

check_cmd()
success_mock.assert_called_once()


def test_check_a_range_of_git_commits_and_failed(config, mocker: MockFixture):
def test_check_a_range_of_git_commits_and_failed(mock_config, mocker: MockFixture):
error_mock = mocker.patch("commitizen.out.error")
mocker.patch(
"commitizen.git.get_commits",
return_value=_build_fake_git_commits(["This commit does not follow rule"]),
)
check_cmd = commands.Check(
config=config, arguments={"rev_range": "HEAD~10..master"}
config=mock_config, arguments={"rev_range": "HEAD~10..master"}
)

with pytest.raises(InvalidCommitMessageError):
check_cmd()
error_mock.assert_called_once()


def test_check_command_with_invalid_argument(config):
def test_check_command_with_invalid_argument(mock_config):
with pytest.raises(InvalidCommandArgumentError) as excinfo:
commands.Check(
config=config,
config=mock_config,
arguments={"commit_msg_file": "some_file", "rev_range": "HEAD~10..master"},
)
assert (
Expand All @@ -223,18 +231,20 @@ def test_check_command_with_invalid_argument(config):


@pytest.mark.usefixtures("tmp_commitizen_project")
def test_check_command_with_empty_range(config, mocker: MockFixture):
def test_check_command_with_empty_range(mock_config, mocker: MockFixture):
# must initialize git with a commit
create_file_and_commit("feat: initial")

check_cmd = commands.Check(config=config, arguments={"rev_range": "master..master"})
check_cmd = commands.Check(
config=mock_config, arguments={"rev_range": "master..master"}
)
with pytest.raises(NoCommitsFoundError) as excinfo:
check_cmd()

assert "No commit found with range: 'master..master'" in str(excinfo)


def test_check_a_range_of_failed_git_commits(config, mocker: MockFixture):
def test_check_a_range_of_failed_git_commits(mock_config, mocker: MockFixture):
ill_formated_commits_msgs = [
"First commit does not follow rule",
"Second commit does not follow rule",
Expand All @@ -245,98 +255,102 @@ def test_check_a_range_of_failed_git_commits(config, mocker: MockFixture):
return_value=_build_fake_git_commits(ill_formated_commits_msgs),
)
check_cmd = commands.Check(
config=config, arguments={"rev_range": "HEAD~10..master"}
config=mock_config, arguments={"rev_range": "HEAD~10..master"}
)

with pytest.raises(InvalidCommitMessageError) as excinfo:
check_cmd()
assert all([msg in str(excinfo.value) for msg in ill_formated_commits_msgs])


def test_check_command_with_valid_message(config, mocker: MockFixture):
def test_check_command_with_valid_message(mock_config, mocker: MockFixture):
success_mock = mocker.patch("commitizen.out.success")
check_cmd = commands.Check(
config=config, arguments={"message": "fix(scope): some commit message"}
config=mock_config, arguments={"message": "fix(scope): some commit message"}
)

check_cmd()
success_mock.assert_called_once()


def test_check_command_with_invalid_message(config, mocker: MockFixture):
def test_check_command_with_invalid_message(mock_config, mocker: MockFixture):
error_mock = mocker.patch("commitizen.out.error")
check_cmd = commands.Check(config=config, arguments={"message": "bad commit"})
check_cmd = commands.Check(config=mock_config, arguments={"message": "bad commit"})

with pytest.raises(InvalidCommitMessageError):
check_cmd()
error_mock.assert_called_once()


def test_check_command_with_empty_message(config, mocker: MockFixture):
def test_check_command_with_empty_message(mock_config, mocker: MockFixture):
error_mock = mocker.patch("commitizen.out.error")
check_cmd = commands.Check(config=config, arguments={"message": ""})
check_cmd = commands.Check(config=mock_config, arguments={"message": ""})

with pytest.raises(InvalidCommitMessageError):
check_cmd()
error_mock.assert_called_once()


def test_check_command_with_allow_abort_arg(config, mocker: MockFixture):
def test_check_command_with_allow_abort_arg(mock_config, mocker: MockFixture):
success_mock = mocker.patch("commitizen.out.success")
check_cmd = commands.Check(
config=config, arguments={"message": "", "allow_abort": True}
config=mock_config, arguments={"message": "", "allow_abort": True}
)

check_cmd()
success_mock.assert_called_once()


def test_check_command_with_allow_abort_config(config, mocker: MockFixture):
def test_check_command_with_allow_abort_config(mock_config, mocker: MockFixture):
success_mock = mocker.patch("commitizen.out.success")
config.settings["allow_abort"] = True
check_cmd = commands.Check(config=config, arguments={"message": ""})
mock_config.settings["allow_abort"] = True
check_cmd = commands.Check(config=mock_config, arguments={"message": ""})

check_cmd()
success_mock.assert_called_once()


def test_check_command_override_allow_abort_config(config, mocker: MockFixture):
def test_check_command_override_allow_abort_config(mock_config, mocker: MockFixture):
error_mock = mocker.patch("commitizen.out.error")
config.settings["allow_abort"] = True
mock_config.settings["allow_abort"] = True
check_cmd = commands.Check(
config=config, arguments={"message": "", "allow_abort": False}
config=mock_config, arguments={"message": "", "allow_abort": False}
)

with pytest.raises(InvalidCommitMessageError):
check_cmd()
error_mock.assert_called_once()


def test_check_command_with_allowed_prefixes_arg(config, mocker: MockFixture):
def test_check_command_with_allowed_prefixes_arg(mock_config, mocker: MockFixture):
success_mock = mocker.patch("commitizen.out.success")
check_cmd = commands.Check(
config=config,
config=mock_config,
arguments={"message": "custom! test", "allowed_prefixes": ["custom!"]},
)

check_cmd()
success_mock.assert_called_once()


def test_check_command_with_allowed_prefixes_config(config, mocker: MockFixture):
def test_check_command_with_allowed_prefixes_config(mock_config, mocker: MockFixture):
success_mock = mocker.patch("commitizen.out.success")
config.settings["allowed_prefixes"] = ["custom!"]
check_cmd = commands.Check(config=config, arguments={"message": "custom! test"})
mock_config.settings["allowed_prefixes"] = ["custom!"]
check_cmd = commands.Check(
config=mock_config, arguments={"message": "custom! test"}
)

check_cmd()
success_mock.assert_called_once()


def test_check_command_override_allowed_prefixes_config(config, mocker: MockFixture):
def test_check_command_override_allowed_prefixes_config(
mock_config, mocker: MockFixture
):
error_mock = mocker.patch("commitizen.out.error")
config.settings["allow_abort"] = ["fixup!"]
mock_config.settings["allow_abort"] = ["fixup!"]
check_cmd = commands.Check(
config=config,
config=mock_config,
arguments={"message": "fixup! test", "allowed_prefixes": ["custom!"]},
)

Expand Down Expand Up @@ -429,23 +443,25 @@ def test_check_command_shows_description_when_use_help_option(
file_regression.check(out, extension=".txt")


def test_check_command_with_message_length_limit(config, mocker: MockFixture):
def test_check_command_with_message_length_limit(mock_config, mocker: MockFixture):
success_mock = mocker.patch("commitizen.out.success")
message = "fix(scope): some commit message"
check_cmd = commands.Check(
config=config,
config=mock_config,
arguments={"message": message, "message_length_limit": len(message) + 1},
)

check_cmd()
success_mock.assert_called_once()


def test_check_command_with_message_length_limit_exceeded(config, mocker: MockFixture):
def test_check_command_with_message_length_limit_exceeded(
mock_config, mocker: MockFixture
):
error_mock = mocker.patch("commitizen.out.error")
message = "fix(scope): some commit message"
check_cmd = commands.Check(
config=config,
config=mock_config,
arguments={"message": message, "message_length_limit": len(message) - 1},
)

Expand All @@ -454,9 +470,9 @@ def test_check_command_with_message_length_limit_exceeded(config, mocker: MockFi
error_mock.assert_called_once()


def test_check_command_with_amend_prefix_default(config, mocker: MockFixture):
def test_check_command_with_amend_prefix_default(mock_config, mocker: MockFixture):
success_mock = mocker.patch("commitizen.out.success")
check_cmd = commands.Check(config=config, arguments={"message": "amend! test"})
check_cmd = commands.Check(config=mock_config, arguments={"message": "amend! test"})

check_cmd()
success_mock.assert_called_once()
Loading
Loading