Skip to content

Commit

Permalink
fix(commitizen/git.py,-tests/test_git.py): Resolve tempfile path spac…
Browse files Browse the repository at this point in the history
…es issue in git commit function

This test verifies parameter passing using markers to ensure that function arguments are correctly handled and propagated.

#572
  • Loading branch information
ttw225 authored and Lee-W committed Mar 30, 2024
1 parent 223532b commit 3519ae7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion commitizen/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 3519ae7

Please sign in to comment.