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

[PATCH v2] t: run tests from a normalized working directory

From
Mark Mentovai <mark@chromium.org>
Date
May 28, 2025, 20:17 UTC
Message-ID
<20250528201737.55268-1-mark@chromium.org>
In-Reply-To
<20250523193722.68344-1-mark@chromium.org>

Some tests make git perform actions that produce observable pathnames, and have expectations on those paths. Tests run with $HOME set to a $TRASH_DIRECTORY, and with their working directory the same $TRASH_DIRECTORY, although these paths are logically identical, they do not observe the same pathname canonicalization rules and thus might not be represented by strings that compare equal. In particular, no pathname normalization is applied to $TRASH_DIRECTORY or $HOME, while tests change their working directory with `cd -P`, which normalizes the working directory's path by fully resolving symbolic links.

t7900's macOS maintenance tests (which are not limited to running on macOS) have an expectation on a path that `git maintenance` forms by using abspath.c strbuf_realpath() to resolve a canonical absolute path based on $HOME. When t7900 runs from a working directory that contains symbolic links in its pathname, $HOME will also contain symbolic links, which `git maintenance` resolves but the test's expectation does not, causing a test failure.

Align $TRASH_DIRECTORY and $HOME with the normalized path as used for the working directory by resetting them to match the working directory after it's established by `cd -P`. With all paths in agreement and symbolic links resolved, pathname expectations can be set and met based on string comparison without regard to external environmental factors such as the presence of symbolic links in a path.

Suggested-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Mark Mentovai <mark@chromium.org>
---
 t/test-lib.sh | 2 ++
 1 file changed, 2 insertions(+)
diff --git a/t/test-lib.sh b/t/test-lib.sh
index af722d383d9b..92d0db13d742 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -1577,6 +1577,8 @@ fi
 # Use -P to resolve symlinks in our working directory so that the cwd
 # in subprocesses like git equals our $PWD (for pathname comparisons).
 cd -P "$TRASH_DIRECTORY" || BAIL_OUT "cannot cd -P to \"$TRASH_DIRECTORY\""
+TRASH_DIRECTORY=$(pwd)
+HOME="$TRASH_DIRECTORY"
 
 start_test_output "$0"
 

Range-diff against v1:
1:  95491c294c1e ! 1:  496e490a6462 t7900: use pwd -P in macOS maintenance test
    @@ Metadata
     Author: Mark Mentovai <mark@chromium.org>
     
      ## Commit message ##
    -    t7900: use pwd -P in macOS maintenance test
    +    t: run tests from a normalized working directory
     
    -    $pfx is the basis for the expectation that launchd plist paths formed by
    -    `git maintenance start` will be compared against. These paths are formed
    -    in `git maintenance` by builtin/gc.c launchctl_service_filename(), which
    -    calls path.c interpolate_path() with real_home = 1, causing abspath.c
    -    strbuf_realpath() to resolve a canonical absolute path. Since $pfx is
    -    not determined according to the same realpath semantics, when t7900 is
    -    run from a working directory that contains a symbolic link in its path,
    -    the realpath operation will produce a different path than $pfx contains,
    -    although both paths logically reference the same directory. The test
    -    fails in this case.
    +    Some tests make git perform actions that produce observable pathnames,
    +    and have expectations on those paths. Tests run with $HOME set to a
    +    $TRASH_DIRECTORY, and with their working directory the same
    +    $TRASH_DIRECTORY, although these paths are logically identical, they do
    +    not observe the same pathname canonicalization rules and thus might not
    +    be represented by strings that compare equal. In particular, no pathname
    +    normalization is applied to $TRASH_DIRECTORY or $HOME, while tests
    +    change their working directory with `cd -P`, which normalizes the
    +    working directory's path by fully resolving symbolic links.
     
    -    Base $pfx on the physical working directory (pwd -P), with all symbolic
    -    links fully resolved, so that the path that the test expects matches
    -    what `git maintenance` generates, even when running from a working
    -    directory whose path contains a symbolic link.
    +    t7900's macOS maintenance tests (which are not limited to running on
    +    macOS) have an expectation on a path that `git maintenance` forms by
    +    using abspath.c strbuf_realpath() to resolve a canonical absolute path
    +    based on $HOME. When t7900 runs from a working directory that contains
    +    symbolic links in its pathname, $HOME will also contain symbolic links,
    +    which `git maintenance` resolves but the test's expectation does not,
    +    causing a test failure.
     
    +    Align $TRASH_DIRECTORY and $HOME with the normalized path as used for
    +    the working directory by resetting them to match the working directory
    +    after it's established by `cd -P`. With all paths in agreement and
    +    symbolic links resolved, pathname expectations can be set and met based
    +    on string comparison without regard to external environmental factors
    +    such as the presence of symbolic links in a path.
    +
    +    Suggested-by: Junio C Hamano <gitster@pobox.com>
         Signed-off-by: Mark Mentovai <mark@chromium.org>
     
    - ## t/t7900-maintenance.sh ##
    -@@ t/t7900-maintenance.sh: test_expect_success 'stop preserves surrounding schedule' '
    + ## t/test-lib.sh ##
    +@@ t/test-lib.sh: fi
    + # Use -P to resolve symlinks in our working directory so that the cwd
    + # in subprocesses like git equals our $PWD (for pathname comparisons).
    + cd -P "$TRASH_DIRECTORY" || BAIL_OUT "cannot cd -P to \"$TRASH_DIRECTORY\""
    ++TRASH_DIRECTORY=$(pwd)
    ++HOME="$TRASH_DIRECTORY"
      
    - test_expect_success 'start and stop macOS maintenance' '
    - 	# ensure $HOME can be compared against hook arguments on all platforms
    --	pfx=$(cd "$HOME" && pwd) &&
    -+	pfx=$(cd "$HOME" && pwd -P) &&
    + start_test_output "$0"
      
    - 	write_script print-args <<-\EOF &&
    - 	echo $* | sed "s:gui/[0-9][0-9]*:gui/[UID]:" >>args
-- 
2.49.0
Previous: Eric SunshineNext: Torsten Bögershausen
Message 10 of 19 in “t7900: use pwd -P in macOS maintenance test”
  1. t7900: use pwd -P in macOS maintenance testMark Mentovai, May 23, 2025
  2. Eric SunshineMay 23, 2025
  3. Junio C HamanoMay 23, 2025
  4. Eric SunshineMay 23, 2025
  5. Junio C HamanoMay 23, 2025
  6. Mark MentovaiMay 24, 2025
  7. Junio C HamanoMay 27, 2025
  8. Mark MentovaiMay 23, 2025
  9. Eric SunshineMay 23, 2025
  10. t: run tests from a normalized working directoryMark Mentovai, May 28, 2025
  11. Torsten BögershausenMay 28, 2025
  12. Junio C HamanoMay 30, 2025
  13. Torsten BögershausenMay 31, 2025
  14. Junio C HamanoJun 1, 2025
  15. Mark MentovaiJun 2, 2025
  16. Junio C HamanoJun 2, 2025
  17. Torsten BögershausenJun 3, 2025
  18. Mark MentovaiJun 3, 2025
  19. Junio C HamanoJun 3, 2025

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.