t1006: update 'run_tests' to test generic object specifiers

Update the 'run_tests' test wrapper so that the first argument may refer to any specifier that uniquely identifies an object (e.g. a ref name, '<OID>:<path>', '<OID>^{<type>}', etc.), rather than only a full object ID. Also add tests that use non-OID identifiers, ensuring appropriate parsing in 'cat-file'. The identifiers used in some of the added tests include a space, which is incompatible with the '%(rest)' atom. To accommodate that without removing the test case, use 'test_expect_failure' when 'object_name' includes a space. Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Victoria Dye <vdye@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Victoria Dye committed Jun 2, 2025 at 18:55 UTC 9fd38038b9cf7d221bfa2d2ee95c8fad6d8b3d64
1 file changed +36 -20
t/t1006-cat-file.sh
+36 -20
@@ -113,53 +113,54 @@ strlen () {
113
114 run_tests () {
115 type=$1
116 - oid=$2
116 + object_name="$2"
117 size=$3
118 content=$4
119 pretty_content=$5
120 + oid=${6:-"$object_name"}
121
122 batch_output="$oid $type $size
123 $content"
124
125 test_expect_success "$type exists" '
125 - git cat-file -e $oid
126 + git cat-file -e "$object_name"
127 '
128
129 test_expect_success "Type of $type is correct" '
130 echo $type >expect &&
130 - git cat-file -t $oid >actual &&
131 + git cat-file -t "$object_name" >actual &&
132 test_cmp expect actual
133 '
134
135 test_expect_success "Size of $type is correct" '
136 echo $size >expect &&
136 - git cat-file -s $oid >actual &&
137 + git cat-file -s "$object_name" >actual &&
138 test_cmp expect actual
139 '
140
141 test -z "$content" ||
142 test_expect_success "Content of $type is correct" '
143 echo_without_newline "$content" >expect &&
143 - git cat-file $type $oid >actual &&
144 + git cat-file $type "$object_name" >actual &&
145 test_cmp expect actual
146 '
147
148 test_expect_success "Pretty content of $type is correct" '
149 echo_without_newline "$pretty_content" >expect &&
149 - git cat-file -p $oid >actual &&
150 + git cat-file -p "$object_name" >actual &&
151 test_cmp expect actual
152 '
153
154 test -z "$content" ||
155 test_expect_success "--batch output of $type is correct" '
156 echo "$batch_output" >expect &&
156 - echo $oid | git cat-file --batch >actual &&
157 + echo "$object_name" | git cat-file --batch >actual &&
158 test_cmp expect actual
159 '
160
161 test_expect_success "--batch-check output of $type is correct" '
162 echo "$oid $type $size" >expect &&
162 - echo_without_newline $oid | git cat-file --batch-check >actual &&
163 + echo_without_newline "$object_name" | git cat-file --batch-check >actual &&
164 test_cmp expect actual
165 '
166
@@ -168,13 +169,13 @@ $content"
169 test -z "$content" ||
170 test_expect_success "--batch-command $opt output of $type content is correct" '
171 echo "$batch_output" >expect &&
171 - test_write_lines "contents $oid" | git cat-file --batch-command $opt >actual &&
172 + test_write_lines "contents $object_name" | git cat-file --batch-command $opt >actual &&
173 test_cmp expect actual
174 '
175
176 test_expect_success "--batch-command $opt output of $type info is correct" '
177 echo "$oid $type $size" >expect &&
177 - test_write_lines "info $oid" |
178 + test_write_lines "info $object_name" |
179 git cat-file --batch-command $opt >actual &&
180 test_cmp expect actual
181 '
@@ -182,19 +183,28 @@ $content"
183
184 test_expect_success "custom --batch-check format" '
185 echo "$type $oid" >expect &&
185 - echo $oid | git cat-file --batch-check="%(objecttype) %(objectname)" >actual &&
186 + echo "$object_name" | git cat-file --batch-check="%(objecttype) %(objectname)" >actual &&
187 test_cmp expect actual
188 '
189
190 test_expect_success "custom --batch-command format" '
191 echo "$type $oid" >expect &&
191 - echo "info $oid" | git cat-file --batch-command="%(objecttype) %(objectname)" >actual &&
192 + echo "info $object_name" | git cat-file --batch-command="%(objecttype) %(objectname)" >actual &&
193 test_cmp expect actual
194 '
195
195 - test_expect_success '--batch-check with %(rest)' '
196 + # FIXME: %(rest) is incompatible with object names that include whitespace,
197 + # e.g. HEAD:path/to/a/file with spaces. Use the resolved OID as input to
198 + # test this instead of the raw object name.
199 + if echo "$object_name" | grep " "; then
200 + test_rest=test_expect_failure
201 + else
202 + test_rest=test_expect_success
203 + fi
204 +
205 + $test_rest '--batch-check with %(rest)' '
206 echo "$type this is some extra content" >expect &&
197 - echo "$oid this is some extra content" |
207 + echo "$object_name this is some extra content" |
208 git cat-file --batch-check="%(objecttype) %(rest)" >actual &&
209 test_cmp expect actual
210 '
@@ -205,7 +215,7 @@ $content"
215 echo "$size" &&
216 echo "$content"
217 } >expect &&
208 - echo $oid | git cat-file --batch="%(objectsize)" >actual &&
218 + echo "$object_name" | git cat-file --batch="%(objectsize)" >actual &&
219 test_cmp expect actual
220 '
221
@@ -215,7 +225,7 @@ $content"
225 echo "$type" &&
226 echo "$content"
227 } >expect &&
218 - echo $oid | git cat-file --batch="%(objecttype)" >actual &&
228 + echo "$object_name" | git cat-file --batch="%(objecttype)" >actual &&
229 test_cmp expect actual
230 '
231 }
@@ -230,6 +240,8 @@ test_expect_success "setup" '
240 git config extensions.compatobjectformat $test_compat_hash_algo &&
241 echo_without_newline "$hello_content" > hello &&
242 git update-index --add hello &&
243 + echo_without_newline "$hello_content" > "path with spaces" &&
244 + git update-index --add --chmod=+x "path with spaces" &&
245 git commit -m "add hello file"
246 '
247
@@ -269,13 +281,17 @@ test_expect_success '--batch-check without %(rest) considers whole line' '
281
282 tree_oid=$(git write-tree)
283 tree_compat_oid=$(git rev-parse --output-object-format=$test_compat_hash_algo $tree_oid)
272 -tree_size=$(($(test_oid rawsz) + 13))
273 -tree_compat_size=$(($(test_oid --hash=compat rawsz) + 13))
274 -tree_pretty_content="100644 blob $hello_oid hello${LF}"
275 -tree_compat_pretty_content="100644 blob $hello_compat_oid hello${LF}"
284 +tree_size=$((2 * $(test_oid rawsz) + 13 + 24))
285 +tree_compat_size=$((2 * $(test_oid --hash=compat rawsz) + 13 + 24))
286 +tree_pretty_content="100644 blob $hello_oid hello${LF}100755 blob $hello_oid path with spaces${LF}"
287 +tree_compat_pretty_content="100644 blob $hello_compat_oid hello${LF}100755 blob $hello_compat_oid path with spaces${LF}"
288
289 run_tests 'tree' $tree_oid $tree_size "" "$tree_pretty_content"
290 run_tests 'tree' $tree_compat_oid $tree_compat_size "" "$tree_compat_pretty_content"
291 +run_tests 'blob' "$tree_oid:hello" $hello_size "" "$hello_content" $hello_oid
292 +run_tests 'blob' "$tree_compat_oid:hello" $hello_size "" "$hello_content" $hello_compat_oid
293 +run_tests 'blob' "$tree_oid:path with spaces" $hello_size "" "$hello_content" $hello_oid
294 +run_tests 'blob' "$tree_compat_oid:path with spaces" $hello_size "" "$hello_content" $hello_compat_oid
295
296 commit_message="Initial commit"
297 commit_oid=$(echo_without_newline "$commit_message" | git commit-tree $tree_oid)