cat-file tests: test for missing/bogus object with -t, -s and -p

When we look up a missing object with cat_one_file() what error we print out currently depends on whether we'll error out early in get_oid_with_context(), or if we'll get an error later from oid_object_info_extended(). The --allow-unknown-type flag then changes whether we pass the "OBJECT_INFO_ALLOW_UNKNOWN_TYPE" flag to get_oid_with_context() or not. The "-p" flag is yet another special-case in printing the same output on the deadbeef OID as we'd emit on the deadbeef_short OID for the "-s" and "-t" options, it also doesn't support the "--allow-unknown-type" flag at all. Let's test the combination of the two sets of [-t, -s, -p] and [--{no-}allow-unknown-type] (the --no-allow-unknown-type is implicit in not supplying it), as well as a [missing,bogus] object pair. This extends tests added in 3e370f9faf0 (t1006: add tests for git cat-file --allow-unknown-type, 2015-05-03). Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Oct 1, 2021 at 11:16 UTC 59b8283d557a00d0c09684e621f5e000e6996b5f
2 files changed +77
t/oid-info/oid
+2
@@ -27,3 +27,5 @@ numeric sha1:0123456789012345678901234567890123456789
27 numeric sha256:0123456789012345678901234567890123456789012345678901234567890123
28 deadbeef sha1:deadbeefdeadbeefdeadbeefdeadbeefdeadbeef
29 deadbeef sha256:deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef
30 +deadbeef_short sha1:deadbeefdeadbeefdeadbeefdeadbeefdeadbee
31 +deadbeef_short sha256:deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbee
t/t1006-cat-file.sh
+75
@@ -327,6 +327,81 @@ test_expect_success 'setup bogus data' '
327 bogus_long_sha1=$(echo_without_newline "$bogus_long_content" | git hash-object -t $bogus_long_type --literally -w --stdin)
328 '
329
330 +for arg1 in '' --allow-unknown-type
331 +do
332 + for arg2 in -s -t -p
333 + do
334 + if test "$arg1" = "--allow-unknown-type" && test "$arg2" = "-p"
335 + then
336 + continue
337 + fi
338 +
339 +
340 + test_expect_success "cat-file $arg1 $arg2 error on bogus short OID" '
341 + cat >expect <<-\EOF &&
342 + fatal: invalid object type
343 + EOF
344 +
345 + if test "$arg1" = "--allow-unknown-type"
346 + then
347 + git cat-file $arg1 $arg2 $bogus_short_sha1
348 + else
349 + test_must_fail git cat-file $arg1 $arg2 $bogus_short_sha1 >out 2>actual &&
350 + test_must_be_empty out &&
351 + test_cmp expect actual
352 + fi
353 + '
354 +
355 + test_expect_success "cat-file $arg1 $arg2 error on bogus full OID" '
356 + if test "$arg2" = "-p"
357 + then
358 + cat >expect <<-EOF
359 + error: unable to unpack $bogus_long_sha1 header
360 + fatal: Not a valid object name $bogus_long_sha1
361 + EOF
362 + else
363 + cat >expect <<-EOF
364 + error: unable to unpack $bogus_long_sha1 header
365 + fatal: git cat-file: could not get object info
366 + EOF
367 + fi &&
368 +
369 + if test "$arg1" = "--allow-unknown-type"
370 + then
371 + git cat-file $arg1 $arg2 $bogus_short_sha1
372 + else
373 + test_must_fail git cat-file $arg1 $arg2 $bogus_long_sha1 >out 2>actual &&
374 + test_must_be_empty out &&
375 + test_cmp expect actual
376 + fi
377 + '
378 +
379 + test_expect_success "cat-file $arg1 $arg2 error on missing short OID" '
380 + cat >expect.err <<-EOF &&
381 + fatal: Not a valid object name $(test_oid deadbeef_short)
382 + EOF
383 + test_must_fail git cat-file $arg1 $arg2 $(test_oid deadbeef_short) >out 2>err.actual &&
384 + test_must_be_empty out
385 + '
386 +
387 + test_expect_success "cat-file $arg1 $arg2 error on missing full OID" '
388 + if test "$arg2" = "-p"
389 + then
390 + cat >expect.err <<-EOF
391 + fatal: Not a valid object name $(test_oid deadbeef)
392 + EOF
393 + else
394 + cat >expect.err <<-\EOF
395 + fatal: git cat-file: could not get object info
396 + EOF
397 + fi &&
398 + test_must_fail git cat-file $arg1 $arg2 $(test_oid deadbeef) >out 2>err.actual &&
399 + test_must_be_empty out &&
400 + test_cmp expect.err err.actual
401 + '
402 + done
403 +done
404 +
405 test_expect_success "Type of broken object is correct" '
406 echo $bogus_short_type >expect &&
407 git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&