def test_cz_with_arg_but_without_command(mocker): testargs = ["cz", "--name", "cz_jira"] mocker.patch.object(sys, "argv", testargs) with pytest.raises(NoCommandFoundError) as excinfo: cli.main() assert "Command is required" in str(excinfo.value)
def test_changelog_in_non_git_project(tmpdir, config, mocker): testargs = ["cz", "changelog", "--incremental"] mocker.patch.object(sys, "argv", testargs) with tmpdir.as_cwd(): with pytest.raises(NotAGitProjectError): cli.main()
def test_name(mocker, capsys): testargs = ["cz", "-n", "cz_jira", "example"] mocker.patch.object(sys, "argv", testargs) cli.main() out, _ = capsys.readouterr() assert out.startswith("JRA")
def test_version(mocker, capsys): testargs = ["cz", "--version"] mocker.patch.object(sys, "argv", testargs) cli.main() out, _ = capsys.readouterr() assert out.strip() == __version__
def test_changelog_incremental_angular_sample(mocker, capsys, changelog_path): with open(changelog_path, "w") as f: f.write( "# [10.0.0-next.3](https://github.com/angular/angular/compare/10.0.0-next.2...10.0.0-next.3) (2020-04-22)\n" "\n" "### Bug Fixes" "\n" "* **common:** format day-periods that cross midnight ([#36611](https://github.com/angular/angular/issues/36611)) ([c6e5fc4](https://github.com/angular/angular/commit/c6e5fc4)), closes [#36566](https://github.com/angular/angular/issues/36566)\n" ) create_file_and_commit("irrelevant commit") git.tag("10.0.0-next.3") create_file_and_commit("feat: add new output") create_file_and_commit("fix: output glitch") create_file_and_commit("fix: mama gotta work") create_file_and_commit("feat: add more stuff") create_file_and_commit("Merge into master") testargs = ["cz", "changelog", "--incremental"] mocker.patch.object(sys, "argv", testargs) cli.main() with open(changelog_path, "r") as f: out = f.read() assert ( out == "## Unreleased\n\n### Feat\n\n- add more stuff\n- add new output\n\n### Fix\n\n- mama gotta work\n- output glitch\n\n# [10.0.0-next.3](https://github.com/angular/angular/compare/10.0.0-next.2...10.0.0-next.3) (2020-04-22)\n\n### Bug Fixes\n* **common:** format day-periods that cross midnight ([#36611](https://github.com/angular/angular/issues/36611)) ([c6e5fc4](https://github.com/angular/angular/commit/c6e5fc4)), closes [#36566](https://github.com/angular/angular/issues/36566)\n" )
def test_bump_files_only(mocker, tmp_commitizen_project): tmp_version_file = tmp_commitizen_project.join("__version__.py") tmp_version_file.write("0.1.0") 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 user interface") 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 create_file_and_commit("feat: another new feature") testargs = ["cz", "bump", "--yes", "--files-only"] mocker.patch.object(sys, "argv", testargs) with pytest.raises(ExpectedExit): cli.main() tag_exists = git.tag_exist("0.3.0") assert tag_exists is False with open(tmp_version_file, "r") as f: assert "0.3.0" in f.read() with open(tmp_commitizen_cfg_file, "r") as f: assert "0.3.0" in f.read()
def test_changelog_incremental_keep_a_changelog_sample(mocker, capsys, changelog_path): with open(changelog_path, "w") as f: f.write(KEEP_A_CHANGELOG) create_file_and_commit("irrelevant commit") git.tag("1.0.0") create_file_and_commit("feat: add new output") create_file_and_commit("fix: output glitch") create_file_and_commit("fix: mama gotta work") create_file_and_commit("feat: add more stuff") create_file_and_commit("Merge into master") testargs = ["cz", "changelog", "--incremental"] mocker.patch.object(sys, "argv", testargs) cli.main() with open(changelog_path, "r") as f: out = f.read() assert ( out == """# Changelog\nAll notable changes to this project will be documented in this file.\n\nThe format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),\nand this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).\n\n## Unreleased\n\n### Feat\n\n- add more stuff\n- add new output\n\n### Fix\n\n- mama gotta work\n- output glitch\n\n## [1.0.0] - 2017-06-20\n### Added\n- New visual identity by [@tylerfortune8](https://github.com/tylerfortune8).\n- Version navigation.\n\n### Changed\n- Start using "changelog" over "change log" since it\'s the common usage.\n\n### Removed\n- Section about "changelog" vs "CHANGELOG".\n\n## [0.3.0] - 2015-12-03\n### Added\n- RU translation from [@aishek](https://github.com/aishek).\n""" )
def test_bump_when_version_is_not_specify(mocker): mocker.patch.object(sys, "argv", ["cz", "bump"]) with pytest.raises(NoVersionSpecifiedError) as excinfo: cli.main() assert NoVersionSpecifiedError.message in str(excinfo.value)
def test_changelog_incremental_with_release_candidate_version( mocker, changelog_path, file_regression, test_input): """Fix #357""" with open(changelog_path, "w") as f: f.write(KEEP_A_CHANGELOG) create_file_and_commit("irrelevant commit") git.tag("1.0.0", annotated=True) create_file_and_commit("feat: add new output") create_file_and_commit("fix: output glitch") testargs = [ "cz", "bump", "--changelog", "--prerelease", test_input, "--yes" ] mocker.patch.object(sys, "argv", testargs) cli.main() create_file_and_commit("fix: mama gotta work") create_file_and_commit("feat: add more stuff") create_file_and_commit("Merge into master") testargs = ["cz", "changelog", "--incremental"] 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_bump_patch_increment(commit_msg, mocker): create_file_and_commit(commit_msg) testargs = ["cz", "bump", "--yes"] mocker.patch.object(sys, "argv", testargs) cli.main() tag_exists = git.tag_exist("0.1.1") assert tag_exists is True
def test_name_default_value(mocker, capsys): testargs = ["cz", "example"] mocker.patch.object(sys, "argv", testargs) cli.main() out, _ = capsys.readouterr() assert out.startswith("fix: correct minor typos in code")
def test_bump_in_non_git_project(tmpdir, config, mocker): testargs = ["cz", "bump", "--yes"] mocker.patch.object(sys, "argv", testargs) with tmpdir.as_cwd(): with pytest.raises(NotAGitProjectError): with pytest.raises(ExpectedExit): cli.main()
def test_changelog_on_empty_project(mocker): testargs = ["cz", "changelog", "--dry-run"] mocker.patch.object(sys, "argv", testargs) with pytest.raises(NoCommitsFoundError) as excinfo: cli.main() assert "No commits found" in str(excinfo)
def test_version(mocker): testargs = ["cz", "--version"] mocker.patch.object(sys, "argv", testargs) error_mock = mocker.patch("commitizen.out.error") cli.main() error_mock.assert_called_once()
def test_cz_with_arg_but_without_command(mocker, capsys): testargs = ["cz", "--name", "cz_jira"] mocker.patch.object(sys, "argv", testargs) with pytest.raises(SystemExit): cli.main() _, err = capsys.readouterr() assert "Command is required" in err
def test_check_command_with_pipe_message(mocker, capsys): testargs = ["cz", "check"] mocker.patch.object(sys, "argv", testargs) mocker.patch("sys.stdin", StringIO("fix(scope): some commit message")) cli.main() out, _ = capsys.readouterr() assert "Commit validation: successful!" in out
def test_check_command_with_pipe_message_and_failed(mocker): testargs = ["cz", "check"] mocker.patch.object(sys, "argv", testargs) mocker.patch("sys.stdin", StringIO("bad commit message")) with pytest.raises(InvalidCommitMessageError) as excinfo: cli.main() assert "commit validation: failed!" in str(excinfo.value)
def test_sysexit_no_argv(mocker, capsys): testargs = ["cz"] mocker.patch.object(sys, "argv", testargs) with pytest.raises(ExpectedExit): cli.main() out, _ = capsys.readouterr() assert out.startswith("usage")
def test_ls(mocker, capsys): testargs = ["cz", "-n", "cz_jira", "ls"] mocker.patch.object(sys, "argv", testargs) cli.main() out, err = capsys.readouterr() assert "cz_conventional_commits" in out assert isinstance(out, str)
def test_bump_minor_increment_annotated(commit_msg, mocker): create_file_and_commit(commit_msg) testargs = ["cz", "bump", "--yes", "--annotated-tag"] 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_bump_when_no_new_commit(mocker): testargs = ["cz", "bump", "--yes"] mocker.patch.object(sys, "argv", testargs) with pytest.raises(NoCommitsFoundError) as excinfo: cli.main() expected_error_message = "[NO_COMMITS_FOUND]\n" "No new commits found." assert expected_error_message in str(excinfo.value)
def test_check_jira_text_between_issue_and_command(mocker, capsys): testargs = ["cz", "-n", "cz_jira", "check", "--commit-msg-file", "some_file"] mocker.patch.object(sys, "argv", testargs) mocker.patch( "commitizen.commands.check.open", mocker.mock_open(read_data="JR-234 some text #command some arguments etc"), ) cli.main() out, _ = capsys.readouterr() assert "Commit validation: successful!" in out
def test_check_jira_fails(mocker): testargs = ["cz", "-n", "cz_jira", "check", "--commit-msg-file", "some_file"] mocker.patch.object(sys, "argv", testargs) mocker.patch( "commitizen.commands.check.open", mocker.mock_open(read_data="random message for J-2 #fake_command blah"), ) with pytest.raises(InvalidCommitMessageError) as excinfo: cli.main() assert "commit validation: failed!" in str(excinfo.value)
def test_check_conventional_commit_succeeds(mocker, capsys): testargs = ["cz", "check", "--commit-msg-file", "some_file"] mocker.patch.object(sys, "argv", testargs) mocker.patch( "commitizen.commands.check.open", mocker.mock_open(read_data="fix(scope): some commit message"), ) cli.main() out, _ = capsys.readouterr() assert "Commit validation: successful!" in out
def test_check_jira_multiple_commands(mocker, capsys): testargs = ["cz", "-n", "cz_jira", "check", "--commit-msg-file", "some_file"] mocker.patch.object(sys, "argv", testargs) mocker.patch( "commitizen.commands.check.open", mocker.mock_open(read_data="JRA-23 some text #command1 args #command2 args"), ) cli.main() out, _ = capsys.readouterr() assert "Commit validation: successful!" in out
def test_bump_command_increment_option(commit_msg, increment, expected_tag, mocker): create_file_and_commit(commit_msg) testargs = ["cz", "bump", "--increment", increment, "--yes"] mocker.patch.object(sys, "argv", testargs) cli.main() tag_exists = git.tag_exist(expected_tag) assert tag_exists is True
def test_bump_when_not_new_commit(mocker, capsys): testargs = ["cz", "bump", "--yes"] mocker.patch.object(sys, "argv", testargs) with pytest.raises(SystemExit): cli.main() expected_error_message = "[NO_COMMITS_FOUND]\n" "No new commits found." _, err = capsys.readouterr() assert expected_error_message in err
def test_bump_command(mocker, create_project): with open("./pyproject.toml", "w") as f: f.write("[tool.commitizen]\n" 'version="0.1.0"') cmd.run("git init") # 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_arg_debug(mocker): testargs = ["cz", "--debug", "info"] mocker.patch.object(sys, "argv", testargs) with pytest.warns(DeprecationWarning) as record: cli.main() assert record[0].message.args[0] == ( "Debug will be deprecated in next major version. " "Please remove it from your scripts" )
def test_check_jira_fails(mocker, capsys): testargs = ["cz", "-n", "cz_jira", "check", "--commit-msg-file", "some_file"] mocker.patch.object(sys, "argv", testargs) mocker.patch( "commitizen.commands.check.open", mocker.mock_open(read_data="random message for J-2 #fake_command blah"), ) with pytest.raises(SystemExit): cli.main() _, err = capsys.readouterr() assert "commit validation: failed!" in err