@matheusbsbs / matheus-bagliano / commits / 2caf20c52b
Fork 0

test-lib: user-friendly alternatives to test [-d|-f|-e]

The helper functions are implemented, documented, and used in a few places to validate them, but not everywhere to avoid useless code churn. Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matthieu Moy committed Aug 10, 2010 at 17:17 UTC 2caf20c52b7f646d0a7481c25415c48d687773b2
4 files changed +51 -12
t/README
+7
@@ -467,6 +467,13 @@ library for your script to use.
467 <expected> file. This behaves like "cmp" but produces more
468 helpful output when the test is run with "-v" option.
469
470 + - test_path_is_file <file> [<diagnosis>]
471 + test_path_is_dir <dir> [<diagnosis>]
472 + test_path_is_missing <path> [<diagnosis>]
473 +
474 + Check whether a file/directory exists or doesn't. <diagnosis> will
475 + be displayed if the test fails.
476 +
477 - test_when_finished <script>
478
479 Prepend <script> to a list of commands to run to clean up
t/t3404-rebase-interactive.sh
+9 -9
@@ -79,18 +79,18 @@ test_expect_success 'rebase -i with the exec command' '
79 export FAKE_LINES &&
80 test_must_fail git rebase -i A
81 ) &&
82 - test -f touch-one &&
83 - test -f touch-two &&
84 - ! test -f touch-three &&
82 + test_path_is_file touch-one &&
83 + test_path_is_file touch-two &&
84 + test_path_is_missing touch-three " (should have stopped before)" &&
85 test $(git rev-parse C) = $(git rev-parse HEAD) || {
86 echo "Stopped at wrong revision:"
87 echo "($(git describe --tags HEAD) instead of C)"
88 false
89 } &&
90 git rebase --continue &&
91 - test -f touch-three &&
92 - test -f "touch-file name with spaces" &&
93 - test -f touch-after-semicolon &&
91 + test_path_is_file touch-three &&
92 + test_path_is_file "touch-file name with spaces" &&
93 + test_path_is_file touch-after-semicolon &&
94 test $(git rev-parse master) = $(git rev-parse HEAD) || {
95 echo "Stopped at wrong revision:"
96 echo "($(git describe --tags HEAD) instead of master)"
@@ -105,7 +105,7 @@ test_expect_success 'rebase -i with the exec command runs from tree root' '
105 FAKE_LINES="1 exec_>touch-subdir" \
106 git rebase -i HEAD^ &&
107 cd .. &&
108 - test -f touch-subdir &&
108 + test_path_is_file touch-subdir &&
109 rm -fr subdir
110 '
111
@@ -204,7 +204,7 @@ test_expect_success 'abort' '
204 git rebase --abort &&
205 test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
206 test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
207 - ! test -d .git/rebase-merge
207 + test_path_is_missing .git/rebase-merge
208 '
209
210 test_expect_success 'abort with error when new base cannot be checked out' '
@@ -213,7 +213,7 @@ test_expect_success 'abort with error when new base cannot be checked out' '
213 test_must_fail git rebase -i master > output 2>&1 &&
214 grep "Untracked working tree file .file1. would be overwritten" \
215 output &&
216 - ! test -d .git/rebase-merge &&
216 + test_path_is_missing .git/rebase-merge &&
217 git reset --hard HEAD^
218 '
219
t/t3407-rebase-abort.sh
+3 -3
@@ -38,7 +38,7 @@ testrebase() {
38 # Clean up the state from the previous one
39 git reset --hard pre-rebase &&
40 test_must_fail git rebase$type master &&
41 - test -d "$dotest" &&
41 + test_path_is_dir "$dotest" &&
42 git rebase --abort &&
43 test $(git rev-parse to-rebase) = $(git rev-parse pre-rebase) &&
44 test ! -d "$dotest"
@@ -49,7 +49,7 @@ testrebase() {
49 # Clean up the state from the previous one
50 git reset --hard pre-rebase &&
51 test_must_fail git rebase$type master &&
52 - test -d "$dotest" &&
52 + test_path_is_dir "$dotest" &&
53 test_must_fail git rebase --skip &&
54 test $(git rev-parse HEAD) = $(git rev-parse master) &&
55 git rebase --abort &&
@@ -62,7 +62,7 @@ testrebase() {
62 # Clean up the state from the previous one
63 git reset --hard pre-rebase &&
64 test_must_fail git rebase$type master &&
65 - test -d "$dotest" &&
65 + test_path_is_dir "$dotest" &&
66 echo c > a &&
67 echo d >> a &&
68 git add a &&
t/test-lib.sh
+32
@@ -542,6 +542,38 @@ test_external_without_stderr () {
542 fi
543 }
544
545 +# debugging-friendly alternatives to "test [-f|-d|-e]"
546 +# The commands test the existence or non-existence of $1. $2 can be
547 +# given to provide a more precise diagnosis.
548 +test_path_is_file () {
549 + if ! [ -f "$1" ]
550 + then
551 + echo "File $1 doesn't exist. $*"
552 + false
553 + fi
554 +}
555 +
556 +test_path_is_dir () {
557 + if ! [ -d "$1" ]
558 + then
559 + echo "Directory $1 doesn't exist. $*"
560 + false
561 + fi
562 +}
563 +
564 +test_path_is_missing () {
565 + if [ -e "$1" ]
566 + then
567 + echo "Path exists:"
568 + ls -ld "$1"
569 + if [ $# -ge 1 ]; then
570 + echo "$*"
571 + fi
572 + false
573 + fi
574 +}
575 +
576 +
577 # This is not among top-level (test_expect_success | test_expect_failure)
578 # but is a prefix that can be used in the test script, like:
579 #