t/lib-commit-graph.sh: allow `graph_read_expect()` in sub-directories

The `graph_read_expect()` function is used to ensure that the output of the "read-graph" test helper matches certain parameters (e.g., how many commits are in the graph, which chunks were written, etc.). It expects the Git repository being tested to be at the current working directory. However, a handful of t5318 tests use different repositories stored in sub-directories. To work around this, several tests in t5318 change into the relevant repository outside of a sub-shell, altering the context for the rest of the suite. Prepare to remove these globally-scoped directory changes by teaching `graph_read_expect()` to take an optional "-C dir" to specify where the repository containing the commit-graph being tested is. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Jul 24, 2023 at 12:39 UTC c355b641769db757bc4187aa03f18510a289c86d
1 file changed +13 -3
t/lib-commit-graph.sh
+13 -3
@@ -32,6 +32,13 @@ graph_git_behavior() {
32 graph_read_expect() {
33 OPTIONAL=""
34 NUM_CHUNKS=3
35 + DIR="."
36 + if test "$1" = -C
37 + then
38 + shift
39 + DIR="$1"
40 + shift
41 + fi
42 if test -n "$2"
43 then
44 OPTIONAL=" $2"
@@ -47,12 +54,15 @@ graph_read_expect() {
54 then
55 OPTIONS=" read_generation_data"
56 fi
50 - cat >expect <<- EOF
57 + cat >"$DIR/expect" <<-EOF
58 header: 43475048 1 $(test_oid oid_version) $NUM_CHUNKS 0
59 num_commits: $1
60 chunks: oid_fanout oid_lookup commit_metadata$OPTIONAL
61 options:$OPTIONS
62 EOF
56 - test-tool read-graph >output &&
57 - test_cmp expect output
63 + (
64 + cd "$DIR" &&
65 + test-tool read-graph >output &&
66 + test_cmp expect output
67 + )
68 }