def test_bump_when_bumpping_is_not_support(mocker, tmp_commitizen_project): create_file_and_commit( "feat: new user interface\n\nBREAKING CHANGE: age is no longer supported" ) testargs = ["cz", "-n", "cz_jira", "bump", "--yes"] mocker.patch.object(sys, "argv", testargs) with pytest.raises(NoPatternMapError) as excinfo: cli.main() assert "'cz_jira' rule does not support bump" in str(excinfo.value)
def test_bump_with_changelog_arg(mocker, changelog_path): create_file_and_commit("feat(user): new file") testargs = ["cz", "bump", "--yes", "--changelog"] mocker.patch.object(sys, "argv", testargs) cli.main() tag_exists = git.tag_exist("0.2.0") assert tag_exists is True with open(changelog_path, "r") as f: out = f.read() assert out.startswith("#") assert "0.2.0" in out
def test_bump_dry_run(mocker, capsys, tmp_commitizen_project): create_file_and_commit("feat: new file") testargs = ["cz", "bump", "--yes", "--dry-run"] mocker.patch.object(sys, "argv", testargs) with pytest.raises(DryRunExit): cli.main() out, _ = capsys.readouterr() assert "0.2.0" in out tag_exists = git.tag_exist("0.2.0") assert tag_exists is False
def test_bump_minor_increment_annotated_config_file(commit_msg, mocker, tmp_commitizen_project): tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml") tmp_commitizen_cfg_file.write(f"{tmp_commitizen_cfg_file.read()}\n" f"annotated_tag = 1") create_file_and_commit(commit_msg) testargs = ["cz", "bump", "--yes"] mocker.patch.object(sys, "argv", testargs) cli.main() tag_exists = git.tag_exist("0.2.0") cmd_res = cmd.run( 'git for-each-ref refs/tags --format "%(objecttype):%(refname)"') assert tag_exists is True and "tag:refs/tags/0.2.0\n" in cmd_res.out
def test_breaking_change_content_v1(mocker, capsys): commit_message = ("feat(users): email pattern corrected\n\n" "body content\n\n" "BREAKING CHANGE: migrate by renaming user to users") create_file_and_commit(commit_message) testargs = ["cz", "changelog", "--dry-run"] mocker.patch.object(sys, "argv", testargs) with pytest.raises(DryRunExit): cli.main() out, _ = capsys.readouterr() assert out == ( "## Unreleased\n\n### Feat\n\n- **users**: email pattern corrected\n\n" "### BREAKING CHANGE\n\n- migrate by renaming user to users\n\n")
def test_changelog_multiple_incremental_do_not_add_new_lines( mocker, capsys, changelog_path ): """Test for bug https://github.com/commitizen-tools/commitizen/issues/192""" create_file_and_commit("feat: add new output") testargs = ["cz", "changelog", "--incremental"] mocker.patch.object(sys, "argv", testargs) cli.main() create_file_and_commit("fix: output glitch") testargs = ["cz", "changelog", "--incremental"] mocker.patch.object(sys, "argv", testargs) cli.main() create_file_and_commit("fix: mama gotta work") testargs = ["cz", "changelog", "--incremental"] mocker.patch.object(sys, "argv", testargs) cli.main() create_file_and_commit("feat: add more stuff") testargs = ["cz", "changelog", "--incremental"] mocker.patch.object(sys, "argv", testargs) cli.main() with open(changelog_path, "r") as f: out = f.read() assert out.startswith("#")
def test_changelog_with_different_cz(mocker, capsys): create_file_and_commit("JRA-34 #comment corrected indent issue") create_file_and_commit("JRA-35 #time 1w 2d 4h 30m Total work logged") testargs = ["cz", "-n", "cz_jira", "changelog", "--dry-run"] mocker.patch.object(sys, "argv", testargs) with pytest.raises(DryRunExit): cli.main() out, _ = capsys.readouterr() assert ( out == "## Unreleased\n\n\n- JRA-35 #time 1w 2d 4h 30m Total work logged\n- JRA-34 #comment corrected indent issue\n\n" )
def test_bump_on_git_with_hooks_no_verify_enabled(mocker): cmd.run("mkdir .git/hooks") with open(".git/hooks/pre-commit", "w") as f: f.write("#!/usr/bin/env bash\n" 'echo "0.1.0"') cmd.run("chmod +x .git/hooks/pre-commit") # MINOR create_file_and_commit("feat: new file") testargs = ["cz", "bump", "--yes", "--no-verify"] mocker.patch.object(sys, "argv", testargs) cli.main() tag_exists = git.tag_exist("0.2.0") assert tag_exists is True
def test_bump_with_changelog_to_stdout_arg(mocker, capsys, changelog_path): create_file_and_commit("feat(user): this should appear in stdout") testargs = ["cz", "bump", "--yes", "--changelog-to-stdout"] mocker.patch.object(sys, "argv", testargs) cli.main() out, _ = capsys.readouterr() assert "this should appear in stdout" in out tag_exists = git.tag_exist("0.2.0") assert tag_exists is True with open(changelog_path, "r") as f: out = f.read() assert out.startswith("#") assert "0.2.0" in out
def test_bump_tag_exists_raises_exception(mocker): cmd.run("mkdir .git/hooks") with open(".git/hooks/post-commit", "w") as f: f.write("#!/usr/bin/env bash\n" "exit 9") cmd.run("chmod +x .git/hooks/post-commit") # MINOR create_file_and_commit("feat: new file") git.tag("0.2.0") testargs = ["cz", "bump", "--yes"] mocker.patch.object(sys, "argv", testargs) with pytest.raises(BumpTagFailedError) as excinfo: cli.main() assert "0.2.0" in str(excinfo.value) # This should be a fatal error
def test_none_increment_should_not_call_git_tag(mocker, tmp_commitizen_project): create_file_and_commit("test(test_get_all_droplets): fix bad comparison test") testargs = ["cz", "bump", "--yes"] mocker.patch.object(sys, "argv", testargs) # stash git.tag for later restore stashed_git_tag = git.tag dummy_value = git.tag("0.0.2") git.tag = MagicMock(return_value=dummy_value) with pytest.raises(NoneIncrementExit): cli.main() git.tag.assert_not_called() # restore pop stashed git.tag = stashed_git_tag
def test_bump_on_git_with_hooks_no_verify_disabled(mocker): cmd.run("mkdir .git/hooks") with open(".git/hooks/pre-commit", "w") as f: f.write("#!/usr/bin/env bash\n" 'echo "0.1.0"') cmd.run("chmod +x .git/hooks/pre-commit") # MINOR create_file_and_commit("feat: new file") testargs = ["cz", "bump", "--yes"] mocker.patch.object(sys, "argv", testargs) with pytest.raises(BumpCommitFailedError) as excinfo: cli.main() assert 'git.commit error: "0.1.0"' in str(excinfo.value)
def test_changelog_with_different_tag_name_and_changelog_content( mocker, tmp_commitizen_project): changelog_file = tmp_commitizen_project.join("CHANGELOG.md") changelog_file.write(""" # Unreleased ## v1.0.0 """) create_file_and_commit("feat: new file") git.tag("2.0.0") # create_file_and_commit("feat: new file") testargs = ["cz", "changelog", "--incremental"] mocker.patch.object(sys, "argv", testargs) with pytest.raises(NoRevisionError): cli.main()
def test_bump_on_git_with_hooks_no_verify_disabled(mocker, capsys): cmd.run("mkdir .git/hooks") with open(".git/hooks/pre-commit", "w") as f: f.write("#!/usr/bin/env bash\n" 'echo "0.1.0"') cmd.run("chmod +x .git/hooks/pre-commit") # MINOR create_file_and_commit("feat: new file") testargs = ["cz", "bump", "--yes"] mocker.patch.object(sys, "argv", testargs) with pytest.raises(SystemExit): cli.main() _, err = capsys.readouterr() assert 'git.commit errror: "0.1.0"' in err
def test_changelog_config_flag_increment(mocker, changelog_path, config_path): with open(config_path, "a") as f: f.write("changelog_incremental = true\n") with open(changelog_path, "a") as f: f.write("\nnote: this should be persisted using increment\n") create_file_and_commit("feat: add new output") testargs = ["cz", "changelog"] mocker.patch.object(sys, "argv", testargs) cli.main() with open(changelog_path, "r") as f: out = f.read() assert "this should be persisted using increment" in out
def test_bump_command_prelease(mocker): # PRERELEASE create_file_and_commit("feat: location") testargs = ["cz", "bump", "--prerelease", "alpha", "--yes"] mocker.patch.object(sys, "argv", testargs) cli.main() tag_exists = git.tag_exist("0.2.0a0") assert tag_exists is True # PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE testargs = ["cz", "bump"] mocker.patch.object(sys, "argv", testargs) cli.main() tag_exists = git.tag_exist("0.2.0") assert tag_exists is True
def test_bump_when_bumpping_is_not_support(mocker, capsys, tmpdir): with tmpdir.as_cwd(): with open("./pyproject.toml", "w") as f: f.write("[tool.commitizen]\n" 'version="0.1.0"') cmd.run("git init") create_file_and_commit( "feat: new user interface\n\nBREAKING CHANGE: age is no longer supported" ) testargs = ["cz", "-n", "cz_jira", "bump", "--yes"] mocker.patch.object(sys, "argv", testargs) with pytest.raises(SystemExit): cli.main() _, err = capsys.readouterr() assert "'cz_jira' rule does not support bump" in err
def test_bump_local_version(mocker, tmp_commitizen_project): tmp_version_file = tmp_commitizen_project.join("__version__.py") tmp_version_file.write("4.5.1+0.1.0") tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml") tmp_commitizen_cfg_file.write( f"[tool.commitizen]\n" 'version="4.5.1+0.1.0"\n' f'version_files = ["{str(tmp_version_file)}"]') create_file_and_commit("feat: new user interface") testargs = ["cz", "bump", "--yes", "--local-version"] mocker.patch.object(sys, "argv", testargs) cli.main() tag_exists = git.tag_exist("4.5.1+0.2.0") assert tag_exists is True with open(tmp_version_file, "r") as f: assert "4.5.1+0.2.0" in f.read()
def test_bump_when_version_inconsistent_in_version_files( tmp_commitizen_project, mocker): tmp_version_file = tmp_commitizen_project.join("__version__.py") tmp_version_file.write("100.999.10000") tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml") tmp_commitizen_cfg_file.write( f"{tmp_commitizen_cfg_file.read()}\n" f'version_files = ["{str(tmp_version_file)}"]') create_file_and_commit("feat: new file") testargs = ["cz", "bump", "--yes", "--check-consistency"] mocker.patch.object(sys, "argv", testargs) with pytest.raises(CurrentVersionNotFoundError) as excinfo: cli.main() partial_expected_error_message = "Current version 0.1.0 is not found in" assert partial_expected_error_message in str(excinfo.value)
def test_changelog_incremental_newline_separates_new_content_from_old( mocker, changelog_path): """Test for https://github.com/commitizen-tools/commitizen/issues/509""" with open(changelog_path, "w") as f: f.write("Pre-existing content that should be kept\n") create_file_and_commit("feat: add more cat videos") testargs = ["cz", "changelog", "--incremental"] mocker.patch.object(sys, "argv", testargs) cli.main() with open(changelog_path, "r") as f: out = f.read() assert ( out == "Pre-existing content that should be kept\n\n## Unreleased\n\n### Feat\n\n- add more cat videos\n" )
def test_bump_when_version_inconsistent_in_version_files( tmp_commitizen_project, mocker, capsys): tmp_version_file = tmp_commitizen_project.join("__version__.py") tmp_version_file.write("100.999.10000") tmp_commitizen_cfg_file = tmp_commitizen_project.join("pyproject.toml") tmp_commitizen_cfg_file.write( f"{tmp_commitizen_cfg_file.read()}\n" f'version_files = ["{str(tmp_version_file)}"]') create_file_and_commit("feat: new file") testargs = ["cz", "bump", "--yes", "--check-consistency"] mocker.patch.object(sys, "argv", testargs) with pytest.raises(SystemExit) as excinfo: cli.main() partial_expected_error_message = "Current version 0.1.0 is not found in" _, err = capsys.readouterr() assert excinfo.value.code == CURRENT_VERSION_NOT_FOUND assert partial_expected_error_message in err
def test_changelog_from_rev_single_version_not_found(mocker, config_path, changelog_path): with open(config_path, "a") as f: f.write('tag_format = "$version"\n') # create commit and tag create_file_and_commit("feat: new file") testargs = ["cz", "bump", "--yes"] mocker.patch.object(sys, "argv", testargs) cli.main() wait_for_tag() create_file_and_commit("feat: after 0.2.0") create_file_and_commit("feat: another feature") testargs = ["cz", "bump", "--yes"] mocker.patch.object(sys, "argv", testargs) cli.main() wait_for_tag() testargs = ["cz", "changelog", "0.8.0"] # it shouldn't exist mocker.patch.object(sys, "argv", testargs) with pytest.raises(NoCommitsFoundError) as excinfo: cli.main() assert "No commits found" in str(excinfo)
def test_changelog_from_rev_version_range_including_first_tag( mocker, config_path, changelog_path, file_regression): mocker.patch("commitizen.git.GitTag.date", "2022-02-13") with open(config_path, "a") as f: f.write('tag_format = "$version"\n') # create commit and tag create_file_and_commit("feat: new file") testargs = ["cz", "bump", "--yes"] mocker.patch.object(sys, "argv", testargs) cli.main() create_file_and_commit("feat: after 0.2.0") create_file_and_commit("feat: another feature") testargs = ["cz", "bump", "--yes"] mocker.patch.object(sys, "argv", testargs) cli.main() testargs = ["cz", "changelog", "0.2.0..0.3.0"] mocker.patch.object(sys, "argv", testargs) cli.main() with open(changelog_path, "r") as f: out = f.read() file_regression.check(out, extension=".md")
def test_changelog_from_rev_latest_version_dry_run(mocker, capsys, config_path, changelog_path, file_regression): mocker.patch("commitizen.git.GitTag.date", "2022-02-13") with open(config_path, "a") as f: f.write('tag_format = "$version"\n') # create commit and tag create_file_and_commit("feat: new file") testargs = ["cz", "bump", "--yes"] mocker.patch.object(sys, "argv", testargs) cli.main() wait_for_tag() create_file_and_commit("feat: after 0.2.0") create_file_and_commit("feat: another feature") testargs = ["cz", "bump", "--yes"] mocker.patch.object(sys, "argv", testargs) cli.main() capsys.readouterr() wait_for_tag() testargs = ["cz", "changelog", "0.3.0", "--dry-run"] mocker.patch.object(sys, "argv", testargs) with pytest.raises(DryRunExit): cli.main() out, _ = capsys.readouterr() file_regression.check(out, extension=".md")
def test_bump_command(mocker): # MINOR create_file_and_commit("feat: new file") testargs = ["cz", "bump", "--yes"] mocker.patch.object(sys, "argv", testargs) cli.main() tag_exists = git.tag_exist("0.2.0") assert tag_exists is True # PATCH create_file_and_commit("fix: username exception") testargs = ["cz", "bump"] mocker.patch.object(sys, "argv", testargs) cli.main() tag_exists = git.tag_exist("0.2.1") assert tag_exists is True # PRERELEASE create_file_and_commit("feat: location") testargs = ["cz", "bump", "--prerelease", "alpha"] mocker.patch.object(sys, "argv", testargs) cli.main() tag_exists = git.tag_exist("0.3.0a0") assert tag_exists is True # PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE testargs = ["cz", "bump"] mocker.patch.object(sys, "argv", testargs) cli.main() tag_exists = git.tag_exist("0.3.0") assert tag_exists is True # MAJOR create_file_and_commit( "feat: new user interface\n\nBREAKING CHANGE: age is no longer supported" ) testargs = ["cz", "bump"] mocker.patch.object(sys, "argv", testargs) cli.main() tag_exists = git.tag_exist("1.0.0") assert tag_exists is True
def test_prevent_prerelease_when_no_increment_detected(mocker, capsys, tmp_commitizen_project): create_file_and_commit("feat: new file") testargs = ["cz", "bump", "--yes"] mocker.patch.object(sys, "argv", testargs) cli.main() out, _ = capsys.readouterr() assert "0.2.0" in out create_file_and_commit("test: new file") testargs = ["cz", "bump", "-pr", "beta"] mocker.patch.object(sys, "argv", testargs) with pytest.raises(NoCommitsFoundError) as excinfo: cli.main() expected_error_message = ("[NO_COMMITS_FOUND]\n" "No commits found to generate a pre-release.") assert expected_error_message in str(excinfo.value)
def test_changelog_from_version_zero_point_two(mocker, capsys): create_file_and_commit("feat: new file") create_file_and_commit("refactor: not in changelog") # create tag testargs = ["cz", "bump", "--yes"] mocker.patch.object(sys, "argv", testargs) cli.main() capsys.readouterr() create_file_and_commit("feat: after 0.2.0") create_file_and_commit("feat: after 0.2") testargs = ["cz", "changelog", "--start-rev", "0.2.0", "--dry-run"] mocker.patch.object(sys, "argv", testargs) with pytest.raises(DryRunExit): cli.main() out, _ = capsys.readouterr() assert out == "## Unreleased\n\n### Feat\n\n- after 0.2\n- after 0.2.0\n\n"
def test_changlog_hook(mocker, config): 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") changelog = Changelog( config, {"unreleased_version": None, "incremental": True, "dry_run": False}, ) mocker.patch.object(changelog.cz, "changelog_hook", changelog_hook_mock) changelog() full_changelog = "\n## Unreleased\n\n### Refactor\n\n- is in changelog\n\n### Feat\n\n- new file\n" changelog_hook_mock.assert_called_with(full_changelog, full_changelog)
def test_changelog_from_start(mocker, capsys, changelog_path): create_file_and_commit("feat: new file") create_file_and_commit("refactor: is in changelog") create_file_and_commit("Merge into master") testargs = ["cz", "changelog"] mocker.patch.object(sys, "argv", testargs) cli.main() with open(changelog_path, "r") as f: out = f.read() assert ( out == "## Unreleased\n\n### Refactor\n\n- is in changelog\n\n### Feat\n\n- new file\n" )
def test_changelog_config_start_rev_option(mocker, capsys, config_path): # create commit and tag create_file_and_commit("feat: new file") testargs = ["cz", "bump", "--yes"] mocker.patch.object(sys, "argv", testargs) cli.main() capsys.readouterr() create_file_and_commit("feat: after 0.2.0") create_file_and_commit("feat: after 0.2") with open(config_path, "a") as f: f.write('changelog_start_rev = "0.2.0"\n') testargs = ["cz", "changelog", "--dry-run"] mocker.patch.object(sys, "argv", testargs) with pytest.raises(DryRunExit): cli.main() out, _ = capsys.readouterr() assert out == "## Unreleased\n\n### Feat\n\n- after 0.2\n- after 0.2.0\n\n"