git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] test-lib: make sure TEST_DIRECTORY has no trailing slash

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 4, 2023, 17:15 UTC
Message-ID
<xmqqttr6pavh.fsf@gitster.g>
In-Reply-To
<20231004190140+0200.740775-stepnem@smrk.net>
Štěpán Němec <stepnem@smrk.net> writes:
Show 17 quoted lines
> Yes, this is even simpler and more obvious, although for some reason,
> the subshell version works just as well, i.e., with just
> TEST_DIRECTORY=$(cd . && pwd) (no cd in the parent) in test-lib.sh and
>
> cat <<EOF >./tslash.sh
> #!/bin/sh
> test_description='yada yada'
>
> . ./test-lib.sh
>
> test_expect_success 'check TEST_DIRECTORY for trailing slash' '
>         echo "$TEST_DIRECTORY" &&
>         test "$TEST_DIRECTORY" = "${TEST_DIRECTORY%/}"
> '
>
> test_done
> EOF

If the only thing you checked was if TEST_DIRECTORY has or does not have a trailing slash, then it is totally understandable how and why the chdir inside subshell works. The value $(pwd) in the subshell returns, that is assigned to TEST_DIRECTORY, is canonicalized. But the outer shell still thinks $(pwd) is with trailing slash, and the above does not test it.

    test_expect_success 'check $PWD for trailing slash' '
            echo "$PWD" &&
            test "$PWD" = "${PWD%/}"
    '

The primary reason why I said I was silly doing it in a subshell was because I wanted to make sure that any test that refers to $PWD or $(pwd) later in the code would not get upset the same way with trailing slash after $PWD or $(pwd). As long as it is not overdone, it is a good practice to consider possibilities of similar problems triggered by the same root cause, which we may not have got bitten by yet, and preventing it from happening with the same fix.

Thanks.
Previous: Štěpán NěmecNext: Štěpán Němec
Message 7 of 9 in “test-lib: make sure TEST_DIRECTORY has no trailing slash”
  1. test-lib: make sure TEST_DIRECTORY has no trailing slashŠtěpán Němec, Oct 3, 2023
  2. Junio C HamanoOct 3, 2023
  3. Junio C HamanoOct 3, 2023
  4. Štěpán NěmecOct 4, 2023
  5. Junio C HamanoOct 4, 2023
  6. Štěpán NěmecOct 4, 2023
  7. Junio C HamanoOct 4, 2023
  8. Štěpán NěmecOct 4, 2023
  9. Junio C HamanoOct 4, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.