From 0bf5c8a96dc4273d0b9899d6428c02514f548dfa Mon Sep 17 00:00:00 2001 From: Scrocky <--global> Date: Sun, 24 Sep 2023 19:54:38 +0800 Subject: [PATCH] refactor(extra_args): Fixed broken code due to rebase and finalized tests --- commitizen/commands/commit.py | 8 +++++--- commitizen/git.py | 7 +++++-- docs/bump.md | 2 +- docs/commit.md | 2 +- tests/commands/test_commit_command.py | 26 +++++++++++++++++++++++--- tests/test_cli.py | 10 +++++++--- tests/test_command_commit.py | 22 ---------------------- 7 files changed, 42 insertions(+), 35 deletions(-) delete mode 100644 tests/test_command_commit.py diff --git a/commitizen/commands/commit.py b/commitizen/commands/commit.py index 3eec0a1798..5fde3ea6b8 100644 --- a/commitizen/commands/commit.py +++ b/commitizen/commands/commit.py @@ -98,10 +98,12 @@ def __call__(self): ) if signoff: - out.warn("signoff mechanic is deprecated, please use `cz commit -- -s` instead.") - extra_args = "-s " + self.arguments.get("extra_cli_args") + out.warn( + "signoff mechanic is deprecated, please use `cz commit -- -s` instead." + ) + extra_args = self.arguments.get("extra_cli_args", "--") + " -s" else: - extra_args = self.arguments.get("extra_cli_args") + extra_args = self.arguments.get("extra_cli_args", "") c = git.commit(m, extra_args=extra_args) diff --git a/commitizen/git.py b/commitizen/git.py index 2565ad8296..33f4e455b0 100644 --- a/commitizen/git.py +++ b/commitizen/git.py @@ -98,13 +98,16 @@ def add(args: str = "") -> cmd.Command: def commit( - message: str, args: str = "", extra_args: str = "", committer_date: str | None = None + message: str, + args: str = "", + extra_args: str = "", + committer_date: str | None = None, ) -> cmd.Command: f = NamedTemporaryFile("wb", delete=False) f.write(message.encode("utf-8")) f.close() - command = cmd.run(f"git commit {args} {extra_args} -F {f.name}") + command = f"git commit {args} {extra_args} -F {f.name}" if committer_date and os.name == "nt": # pragma: no cover # Using `cmd /v /c "{command}"` sets environment variables only for that command diff --git a/docs/bump.md b/docs/bump.md index 287d4e1e35..e58a11e18d 100644 --- a/docs/bump.md +++ b/docs/bump.md @@ -323,7 +323,7 @@ These are used in: * `cz bump`: Find previous release tag (exact match) and generate new tag. * Find previous release tags in `cz changelog`. - * If `--incremental`: Using latest version found in the changelog, scan existing Git tags with 89\% similarity match. + * If `--incremental`: Using latest version found in the changelog, scan existing Git tags with 89\% similarity match. * `--rev-range` is converted to Git tag names with `tag_format` before searching Git history. * If the `scm` `version_provider` is used, it uses different regexes to find the previous version tags: * If `tag_format` is set to `$version` (default): `VersionProtocol.parser` (allows `v` prefix) diff --git a/docs/commit.md b/docs/commit.md index 50eeea8c2c..2215e0d805 100644 --- a/docs/commit.md +++ b/docs/commit.md @@ -28,4 +28,4 @@ For example, using the `-S` option on `git commit` to sign a commit is now commi !!! note Deprecation warning: A commit can be signed off using `cz commit --signoff` or the shortcut `cz commit -s`. - This syntax is now deprecated in favor of the new `cz commit -- -s` syntax. \ No newline at end of file + This syntax is now deprecated in favor of the new `cz commit -- -s` syntax. diff --git a/tests/commands/test_commit_command.py b/tests/commands/test_commit_command.py index b48ac9d0ed..16489d9858 100644 --- a/tests/commands/test_commit_command.py +++ b/tests/commands/test_commit_command.py @@ -88,7 +88,7 @@ def test_commit_retry_works(config, mocker: MockFixture): commands.Commit(config, {"retry": True})() - commit_mock.assert_called_with("feat: user created\n\ncloses #21") + commit_mock.assert_called_with("feat: user created\n\ncloses #21", extra_args="") prompt_mock.assert_called_once() success_mock.assert_called_once() assert not os.path.isfile(temp_file) @@ -174,7 +174,7 @@ def test_commit_command_with_signoff_option(config, mocker: MockFixture): commands.Commit(config, {"signoff": True})() - commit_mock.assert_called_once_with(ANY, "-s") + commit_mock.assert_called_once_with(ANY, extra_args="-- -s") success_mock.assert_called_once() @@ -197,7 +197,7 @@ def test_commit_command_with_always_signoff_enabled(config, mocker: MockFixture) config.settings["always_signoff"] = True commands.Commit(config, {})() - commit_mock.assert_called_once_with(ANY, "-s") + commit_mock.assert_called_once_with(ANY, extra_args="-- -s") success_mock.assert_called_once() @@ -276,3 +276,23 @@ def test_commit_command_with_all_option(config, mocker: MockFixture): commands.Commit(config, {"all": True})() add_mock.assert_called() success_mock.assert_called_once() + + +@pytest.mark.usefixtures("staging_is_clean") +def test_commit_command_with_extra_args(config, mocker: MockFixture): + prompt_mock = mocker.patch("questionary.prompt") + prompt_mock.return_value = { + "prefix": "feat", + "subject": "user created", + "scope": "", + "is_breaking_change": False, + "body": "", + "footer": "", + } + + commit_mock = mocker.patch("commitizen.git.commit") + commit_mock.return_value = cmd.Command("success", "", b"", b"", 0) + success_mock = mocker.patch("commitizen.out.success") + commands.Commit(config, {"extra_cli_args": "-- -extra-args1 -extra-arg2"})() + commit_mock.assert_called_once_with(ANY, extra_args="-- -extra-args1 -extra-arg2") + success_mock.assert_called_once() diff --git a/tests/test_cli.py b/tests/test_cli.py index ed083bc10d..93f6c16ddd 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -8,7 +8,10 @@ from commitizen import cli from commitizen.exceptions import ( - ExpectedExit, NoCommandFoundError, NotAGitProjectError, InvalidCommandArgumentError, NothingToCommitError + ExpectedExit, + NoCommandFoundError, + NotAGitProjectError, + InvalidCommandArgumentError, ) @@ -166,5 +169,6 @@ def test_unknown_args_before_double_dash_raises(mocker: MockFixture): mocker.patch.object(sys, "argv", testargs) with pytest.raises(InvalidCommandArgumentError) as excinfo: cli.main() - assert "Invalid commitizen arguments were found before -- separator" in str(excinfo.value) - + assert "Invalid commitizen arguments were found before -- separator" in str( + excinfo.value + ) diff --git a/tests/test_command_commit.py b/tests/test_command_commit.py deleted file mode 100644 index 59670f65d7..0000000000 --- a/tests/test_command_commit.py +++ /dev/null @@ -1,22 +0,0 @@ -import sys - -from unittest.mock import patch -from pytest_mock import MockFixture - -from commitizen import cli -from commitizen.commands.commit import Commit - - -def test_extra_args_no_raise(mocker: MockFixture): - testargs = ["cz", "c", "--dry-run", "--", "-extra-args1", "-extra-arg2"] - extra_cli_args = "-extra-args1 -extra-args2" - mocker.patch.object(sys, "argv", testargs) - commit_call = mocker.patch.object(Commit, "__call__") - - def assert_extra_args(self): - assert self.arguments["extra_cli_args"] == extra_cli_args - - with patch.object(Commit, "test_extra_args", assert_extra_args, autospec=True) as mock: - commit_call.side_effect = Commit.test_extra_args - cli.main() - Commit.__call__.assert_called_once()