t0002: simplify error checking

This ancient test script does a lot of manual checking of test conditions with "if" blocks. We can simplify this by relying on helpers like test_must_fail. Note that a failing "grep" call here won't produce any verbose output, but that's OK. These days we rely on "-x" to tell us about such commands. And in addition, these greps are soon to be converted to test_i18ngrep (which is itself soon learning to be more verbose). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 10, 2018 at 06:31 UTC dedfdb9c60035eb1b8fd5da1afd6002e841daf19
1 file changed +10 -43
t/t0002-gitfile.sh
+10 -43
@@ -10,15 +10,6 @@ objpath() {
10 echo "$1" | sed -e 's|\(..\)|\1/|'
11 }
12
13 -objck() {
14 - p=$(objpath "$1")
15 - if test ! -f "$REAL/objects/$p"
16 - then
17 - echo "Object not found: $REAL/objects/$p"
18 - false
19 - fi
20 -}
21 -
13 test_expect_success 'initial setup' '
14 REAL="$(pwd)/.real" &&
15 mv .git "$REAL"
@@ -26,30 +17,14 @@ test_expect_success 'initial setup' '
17
18 test_expect_success 'bad setup: invalid .git file format' '
19 echo "gitdir $REAL" >.git &&
29 - if git rev-parse 2>.err
30 - then
31 - echo "git rev-parse accepted an invalid .git file"
32 - false
33 - fi &&
34 - if ! grep "Invalid gitfile format" .err
35 - then
36 - echo "git rev-parse returned wrong error"
37 - false
38 - fi
20 + test_must_fail git rev-parse 2>.err &&
21 + grep "Invalid gitfile format" .err
22 '
23
24 test_expect_success 'bad setup: invalid .git file path' '
25 echo "gitdir: $REAL.not" >.git &&
43 - if git rev-parse 2>.err
44 - then
45 - echo "git rev-parse accepted an invalid .git file path"
46 - false
47 - fi &&
48 - if ! grep "Not a git repository" .err
49 - then
50 - echo "git rev-parse returned wrong error"
51 - false
52 - fi
26 + test_must_fail git rev-parse 2>.err &&
27 + grep "Not a git repository" .err
28 '
29
30 test_expect_success 'final setup + check rev-parse --git-dir' '
@@ -60,7 +35,7 @@ test_expect_success 'final setup + check rev-parse --git-dir' '
35 test_expect_success 'check hash-object' '
36 echo "foo" >bar &&
37 SHA=$(cat bar | git hash-object -w --stdin) &&
63 - objck $SHA
38 + test_path_is_file "$REAL/objects/$(objpath $SHA)"
39 '
40
41 test_expect_success 'check cat-file' '
@@ -69,29 +44,21 @@ test_expect_success 'check cat-file' '
44 '
45
46 test_expect_success 'check update-index' '
72 - if test -f "$REAL/index"
73 - then
74 - echo "Hmm, $REAL/index exists?"
75 - false
76 - fi &&
47 + test_path_is_missing "$REAL/index" &&
48 rm -f "$REAL/objects/$(objpath $SHA)" &&
49 git update-index --add bar &&
79 - if ! test -f "$REAL/index"
80 - then
81 - echo "$REAL/index not found"
82 - false
83 - fi &&
84 - objck $SHA
50 + test_path_is_file "$REAL/index" &&
51 + test_path_is_file "$REAL/objects/$(objpath $SHA)"
52 '
53
54 test_expect_success 'check write-tree' '
55 SHA=$(git write-tree) &&
89 - objck $SHA
56 + test_path_is_file "$REAL/objects/$(objpath $SHA)"
57 '
58
59 test_expect_success 'check commit-tree' '
60 SHA=$(echo "commit bar" | git commit-tree $SHA) &&
94 - objck $SHA
61 + test_path_is_file "$REAL/objects/$(objpath $SHA)"
62 '
63
64 test_expect_success 'check rev-list' '