From acae8b66181558bc85511bef6b3c9207bbd36b8f Mon Sep 17 00:00:00 2001 From: TTW Date: Sat, 30 Mar 2024 15:50:22 +0800 Subject: [PATCH 1/2] fix(commitizen/git.py,-tests/test_git.py): Resolve tempfile path spaces issue in git commit function This test verifies parameter passing using markers to ensure that function arguments are correctly handled and propagated. #572 --- commitizen/git.py | 2 +- tests/test_git.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/commitizen/git.py b/commitizen/git.py index 900ca9298e..53e7335a3f 100644 --- a/commitizen/git.py +++ b/commitizen/git.py @@ -112,7 +112,7 @@ def commit( f.write(message.encode("utf-8")) f.close() - command = f"git commit {args} -F {f.name}" + command = f'git commit {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/tests/test_git.py b/tests/test_git.py index 3b7a08f94a..60001d2e9b 100644 --- a/tests/test_git.py +++ b/tests/test_git.py @@ -293,3 +293,32 @@ def test_create_tag_with_message(tmp_commitizen_project): assert git.get_tag_message(tag_name) == ( tag_message if platform.system() != "Windows" else f"'{tag_message}'" ) + + +@pytest.mark.parametrize( + "file_path,expected_cmd", + [ + ( + "/tmp/temp file", + 'git commit --signoff -F "/tmp/temp file"', + ), # File contains spaces + ( + "/tmp dir/temp file", + 'git commit --signoff -F "/tmp dir/temp file"', + ), # Path contains spaces + ( + "/tmp/tempfile", + 'git commit --signoff -F "/tmp/tempfile"', + ), # Path does not contain spaces + ], +) +def test_commit_with_spaces_in_path(mocker, file_path, expected_cmd): + mock_run = mocker.patch("commitizen.cmd.run", return_value=FakeCommand()) + mock_unlink = mocker.patch("os.unlink") + mock_temp_file = mocker.patch("commitizen.git.NamedTemporaryFile") + mock_temp_file.return_value.name = file_path + + git.commit("feat: new feature", "--signoff") + + mock_run.assert_called_once_with(expected_cmd) + mock_unlink.assert_called_once_with(file_path) From ca06a734a4670e17132226b07b4d92ce52065493 Mon Sep 17 00:00:00 2001 From: TTW Date: Sat, 30 Mar 2024 16:22:08 +0800 Subject: [PATCH 2/2] test(tests/test_git.py): use the IDs tool to clearly describe each test item --- tests/test_git.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/test_git.py b/tests/test_git.py index 60001d2e9b..8af332d214 100644 --- a/tests/test_git.py +++ b/tests/test_git.py @@ -301,15 +301,20 @@ def test_create_tag_with_message(tmp_commitizen_project): ( "/tmp/temp file", 'git commit --signoff -F "/tmp/temp file"', - ), # File contains spaces + ), ( "/tmp dir/temp file", 'git commit --signoff -F "/tmp dir/temp file"', - ), # Path contains spaces + ), ( "/tmp/tempfile", 'git commit --signoff -F "/tmp/tempfile"', - ), # Path does not contain spaces + ), + ], + ids=[ + "File contains spaces", + "Path contains spaces", + "Path does not contain spaces", ], ) def test_commit_with_spaces_in_path(mocker, file_path, expected_cmd):