]> git.ipfire.org Git - thirdparty/git.git/commitdiff
t7500: fix tests with absolute path following ":(optional)" on Windows
authorJohannes Sixt <j6t@kdbg.org>
Mon, 20 Oct 2025 09:40:08 +0000 (11:40 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 20 Oct 2025 16:16:49 +0000 (09:16 -0700)
On Windows, the MSYS layer translates absolute path names generated by
a shell script from the POSIX style /c/dir/file to the Windows style
C:/dir/file form that is understood by git.exe. This happens only when
the absolute path stands on its own as a program argument or a value of
an environment variable.

The earlier commits 749d6d166d (config: values of pathname type can be
prefixed with :(optional), 2025-09-28) and ccfcaf399f (parseopt: values
of pathname type can be prefixed with :(optional), 2025-09-28) added
test cases where ":(optional)" is inserted before an absolute path.
$PWD is used to construct the absolute paths, which gives the POSIX
form, and the result is ":(optional)/c/dir/template". Such command line
arguments are no longer recognized as absolute paths and do not undergo
translation.

Existing test cases that expect that the specified file does not exist
are not incorrect (after all, git.exe will not find /c/dir/template).
Yet, they are conceptually incorrect. That the use of $PWD is erroneous
is revealed by a test case that expects that the optional file exists.
Since no such test case is present, add one. Use "$(pwd)" to generate
the absolute paths, so that the command line arguments become
":(optional)C:/dir/template".

Signed-off-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
t/t7500-commit-template-squash-signoff.sh

index 1935171d684bee06e6b65de6247faa6ae0eba53f..66aff8e0976e799819126b863e7dfdb4c8993fc3 100755 (executable)
@@ -33,7 +33,7 @@ test_expect_success 'nonexistent template file should return error' '
        (
                GIT_EDITOR="echo hello >" &&
                export GIT_EDITOR &&
-               test_must_fail git commit --template "$PWD"/notexist
+               test_must_fail git commit --template "$(pwd)"/notexist
        )
 '
 
@@ -43,12 +43,12 @@ test_expect_success 'nonexistent optional template file on command line' '
        (
                GIT_EDITOR="echo hello >\"\$1\"" &&
                export GIT_EDITOR &&
-               git commit --template ":(optional)$PWD/notexist"
+               git commit --template ":(optional)$(pwd)/notexist"
        )
 '
 
 test_expect_success 'nonexistent template file in config should return error' '
-       test_config commit.template "$PWD"/notexist &&
+       test_config commit.template "$(pwd)"/notexist &&
        (
                GIT_EDITOR="echo hello >" &&
                export GIT_EDITOR &&
@@ -57,7 +57,7 @@ test_expect_success 'nonexistent template file in config should return error' '
 '
 
 test_expect_success 'nonexistent optional template file in config' '
-       test_config commit.template ":(optional)$PWD"/notexist &&
+       test_config commit.template ":(optional)$(pwd)"/notexist &&
        GIT_EDITOR="echo hello >" git commit --allow-empty &&
        git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
        echo hello >expect &&
@@ -65,7 +65,7 @@ test_expect_success 'nonexistent optional template file in config' '
 '
 
 # From now on we'll use a template file that exists.
-TEMPLATE="$PWD"/template
+TEMPLATE="$(pwd)"/template
 
 test_expect_success 'unedited template should not commit' '
        echo "template line" >"$TEMPLATE" &&
@@ -99,6 +99,15 @@ test_expect_success 'adding real content to a template should commit' '
        commit_msg_is "template linecommit message"
 '
 
+test_expect_success 'existent template marked optional should commit' '
+       echo "existent template" >"$TEMPLATE" &&
+       (
+               test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
+               git commit --allow-empty --template ":(optional)$TEMPLATE"
+       ) &&
+       commit_msg_is "existent templatecommit message"
+'
+
 test_expect_success '-t option should be short for --template' '
        echo "short template" > "$TEMPLATE" &&
        echo "new content" >> foo &&