Skip to content

Commit 2caf20c

Browse files
moygitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cd035b1 commit 2caf20c

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

t/README

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,13 @@ library for your script to use.
467467
<expected> file. This behaves like "cmp" but produces more
468468
helpful output when the test is run with "-v" option.
469469

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+
470477
- test_when_finished <script>
471478

472479
Prepend <script> to a list of commands to run to clean up

t/t3404-rebase-interactive.sh

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,18 @@ test_expect_success 'rebase -i with the exec command' '
7979
export FAKE_LINES &&
8080
test_must_fail git rebase -i A
8181
) &&
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)" &&
8585
test $(git rev-parse C) = $(git rev-parse HEAD) || {
8686
echo "Stopped at wrong revision:"
8787
echo "($(git describe --tags HEAD) instead of C)"
8888
false
8989
} &&
9090
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 &&
9494
test $(git rev-parse master) = $(git rev-parse HEAD) || {
9595
echo "Stopped at wrong revision:"
9696
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' '
105105
FAKE_LINES="1 exec_>touch-subdir" \
106106
git rebase -i HEAD^ &&
107107
cd .. &&
108-
test -f touch-subdir &&
108+
test_path_is_file touch-subdir &&
109109
rm -fr subdir
110110
'
111111

@@ -204,7 +204,7 @@ test_expect_success 'abort' '
204204
git rebase --abort &&
205205
test $(git rev-parse new-branch1) = $(git rev-parse HEAD) &&
206206
test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
207-
! test -d .git/rebase-merge
207+
test_path_is_missing .git/rebase-merge
208208
'
209209

210210
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' '
213213
test_must_fail git rebase -i master > output 2>&1 &&
214214
grep "Untracked working tree file .file1. would be overwritten" \
215215
output &&
216-
! test -d .git/rebase-merge &&
216+
test_path_is_missing .git/rebase-merge &&
217217
git reset --hard HEAD^
218218
'
219219

t/t3407-rebase-abort.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ testrebase() {
3838
# Clean up the state from the previous one
3939
git reset --hard pre-rebase &&
4040
test_must_fail git rebase$type master &&
41-
test -d "$dotest" &&
41+
test_path_is_dir "$dotest" &&
4242
git rebase --abort &&
4343
test $(git rev-parse to-rebase) = $(git rev-parse pre-rebase) &&
4444
test ! -d "$dotest"
@@ -49,7 +49,7 @@ testrebase() {
4949
# Clean up the state from the previous one
5050
git reset --hard pre-rebase &&
5151
test_must_fail git rebase$type master &&
52-
test -d "$dotest" &&
52+
test_path_is_dir "$dotest" &&
5353
test_must_fail git rebase --skip &&
5454
test $(git rev-parse HEAD) = $(git rev-parse master) &&
5555
git rebase --abort &&
@@ -62,7 +62,7 @@ testrebase() {
6262
# Clean up the state from the previous one
6363
git reset --hard pre-rebase &&
6464
test_must_fail git rebase$type master &&
65-
test -d "$dotest" &&
65+
test_path_is_dir "$dotest" &&
6666
echo c > a &&
6767
echo d >> a &&
6868
git add a &&

t/test-lib.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,38 @@ test_external_without_stderr () {
542542
fi
543543
}
544544

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+
545577
# This is not among top-level (test_expect_success | test_expect_failure)
546578
# but is a prefix that can be used in the test script, like:
547579
#

0 commit comments

Comments
 (0)