Skip to content

Commit f469acf

Browse files
committed
t: avoid incorrect negated test expressions
In commit a5d20de of PR git-lfs#5282 we revised a number of our test scripts so as to avoid the use of the "!" shell word in cases where we want to confirm that a command fails and returns a non-zero exit code. We made this change because we had a variety of instances where we had checks of this form which would in practice always pass, regardless of the exit code of their commands. (This behaviour is a consequence of our use of "set -e" in all our test scripts. The Git project's shell test scripts do not use this option, but ours do; it means that the shell will exit immediately if a normal command returns a non-zero exit code. However, according to the POSIX shell specification, when the "set -e" option is enabled and a command's exit code is inverted with "!", the shell will not exit immediately even if the inverted value is non-zero.) Commit a5d20de addressed all of the instances in our test scripts where we relied on an incorrect usage of the "!" shell word. However, we have another similar form of incorrect usage in a number of our test scripts where we rely on the "!" test operator to confirm that a command's exit code is non-zero (i.e., in conditions where we expect the command to fail). In particular, we often use tests of the following form, with the expectation that "foo" should not appear in the log file, and if it does the test should fail: [ ! $(grep "foo" bar.log) ] These checks always succeed, but for the wrong reason. When the grep(1) command does not find the given string in its input stream or the specified file, it prints nothing to its standard output and returns a non-zero exit code. Because we are evaluating a command substitution inside the test expression, and the output of that substitution is the standard output of the command, which is empty, the test devolves to an expression of the form: [ ! ] The Bash shell treats the absence of an expression as a false value, which is then inverted by the "!" operator, so the test passes, but not because has confirmed that the command's exit code was non-zero. Some of these improper test expressions could still catch regressions in the Git LFS client's behaviour, though. For instance, in the example above, if "foo" was found in the log file, the test expression that would be evaluated would have the following form, where all the log lines which matched the string "foo" would appear after the "!" operator: [ ! ... foo ... ] This might form an invalid shell expression, or one with too many arguments (thus causing a "[: too many arguments" error), in which case the test would fail. We can replace all the instances where we rely on these improper test expressions using the same approach we used in commit a5d20de. We simply perform the commands from what was enclosed in a command substitution as the first command pipeline in an AND list, where "exit 1" is the second and final command in the list. If the first command pipeline returns a non-zero exit code, as we expect it to, the "exit 1" is not executed. However, if the pipeline should catch a regression and unexpectedly succeed, "exit 1" will be executed, causing our test to fail. Note that we do not rely on the "set -e" option here, because it ignores the return values of command lists entirely. In cases where our revised check is the last line of one of our tests, we add a "true" statement, because otherwise the test will fail since the last evaluated command would be the first one in the list, i.e., the grep(1) command that we expect will find no matches in its input. In our t/t-migrate-import.sh test script we also have some improper test expresssions of the following form, where we expect that the output of the "git cat-file" command should be empty because the ".gitattributes" file at the given Git ref is blank: [ ! $(git cat-file -p "ref:.gitattributes") ] As with the improper tests of the output of grep(1) commands, these tests succeed but for the wrong reason, because they also devolve to expressions of the form: [ ! ] To repair these checks, we simply replace the "!" test operator with the "-z" one, as we just want to confirm that the output of the command substitution is a zero-length string.
1 parent f66f9db commit f469acf

File tree

7 files changed

+42
-39
lines changed

7 files changed

+42
-39
lines changed

t/t-askpass.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ begin_test "askpass: defaults to provided credentials"
112112

113113
GIT_ASKPASS="lfs-askpass" GIT_TRACE=1 GIT_CURL_VERBOSE=1 git push origin main 2>&1 | tee push.log
114114

115-
[ ! $(grep "filling with GIT_ASKPASS" push.log) ]
115+
grep "filling with GIT_ASKPASS" push.log && exit 1
116116
grep "main -> main" push.log
117117
)
118118
end_test

t/t-clone.sh

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ begin_test "clone"
5757
grep "Cloning into" lfsclone.log
5858
grep "Downloading LFS objects:" lfsclone.log
5959
# should be no filter errors
60-
[ ! $(grep "filter" lfsclone.log) ]
61-
[ ! $(grep "error" lfsclone.log) ]
60+
grep "filter" lfsclone.log && exit 1
61+
grep "error" lfsclone.log && exit 1
6262
# should be cloned into location as per arg
6363
[ -d "$newclonedir" ]
6464

@@ -78,8 +78,8 @@ begin_test "clone"
7878
grep "Cloning into" lfsclone.log
7979
grep "Downloading LFS objects:" lfsclone.log
8080
# should be no filter errors
81-
[ ! $(grep "filter" lfsclone.log) ]
82-
[ ! $(grep "error" lfsclone.log) ]
81+
grep "filter" lfsclone.log && exit 1
82+
grep "error" lfsclone.log && exit 1
8383
# clone location should be implied
8484
[ -d "$reponame" ]
8585

@@ -142,8 +142,8 @@ begin_test "cloneSSL"
142142
grep "Cloning into" lfsclone.log
143143
grep "Downloading LFS objects:" lfsclone.log
144144
# should be no filter errors
145-
[ ! $(grep "filter" lfsclone.log) ]
146-
[ ! $(grep "error" lfsclone.log) ]
145+
grep "filter" lfsclone.log && exit 1
146+
grep "error" lfsclone.log && exit 1
147147
# should be cloned into location as per arg
148148
[ -d "$newclonedir" ]
149149

@@ -216,8 +216,8 @@ begin_test "clone ClientCert"
216216
grep "Cloning into" lfsclone.log
217217
grep "Downloading LFS objects:" lfsclone.log
218218
# should be no filter errors
219-
[ ! $(grep "filter" lfsclone.log) ]
220-
[ ! $(grep "error" lfsclone.log) ]
219+
grep "filter" lfsclone.log && exit 1
220+
grep "error" lfsclone.log && exit 1
221221
# should be cloned into location as per arg
222222
[ -d "$newclonedir" ]
223223

@@ -804,8 +804,8 @@ begin_test "clone (HTTP server/proxy require cookies)"
804804
grep "Cloning into" lfsclone.log
805805
grep "Downloading LFS objects:" lfsclone.log
806806
# should be no filter errors
807-
[ ! $(grep "filter" lfsclone.log) ]
808-
[ ! $(grep "error" lfsclone.log) ]
807+
grep "filter" lfsclone.log && exit 1
808+
grep "error" lfsclone.log && exit 1
809809
# should be cloned into location as per arg
810810
[ -d "$newclonedir" ]
811811

@@ -825,8 +825,8 @@ begin_test "clone (HTTP server/proxy require cookies)"
825825
grep "Cloning into" lfsclone.log
826826
grep "Downloading LFS objects:" lfsclone.log
827827
# should be no filter errors
828-
[ ! $(grep "filter" lfsclone.log) ]
829-
[ ! $(grep "error" lfsclone.log) ]
828+
grep "filter" lfsclone.log && exit 1
829+
grep "error" lfsclone.log && exit 1
830830
# clone location should be implied
831831
[ -d "$reponame" ]
832832

t/t-credentials.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,7 @@ begin_test "credentials from lfs.url"
637637
grep "HTTP: 401" push.log
638638
# Ensure we didn't make a second batch request, which means the request
639639
# was successfully retried internally
640-
[ ! "$(grep "tq: retrying object" push.log)" ]
640+
grep "tq: retrying object" push.log && exit 1
641641
grep "Uploading LFS objects: 0% (0/1), 0 B" push.log
642642

643643
echo "bad fetch"
@@ -654,7 +654,7 @@ begin_test "credentials from lfs.url"
654654
GIT_TRACE=1 git lfs fetch --all 2>&1 | tee fetch.log
655655
# No 401 should occur as we've already set an access mode for the
656656
# storage endpoint during the push
657-
[ ! "$(grep "HTTP: 401" fetch.log)" ]
657+
grep "HTTP: 401" fetch.log && exit 1
658658
git lfs fsck
659659

660660
echo "good fetch, setting access mode"
@@ -668,7 +668,7 @@ begin_test "credentials from lfs.url"
668668
grep "HTTP: 401" fetch.log
669669
# Ensure we didn't make a second batch request, which means the request
670670
# was successfully retried internally
671-
[ ! "$(grep "tq: retrying object" fetch.log)" ]
671+
grep "tq: retrying object" fetch.log && exit 1
672672

673673
git lfs fsck
674674
)
@@ -702,7 +702,7 @@ begin_test "credentials from remote.origin.url"
702702
grep "HTTP: 401" push.log
703703
# Ensure we didn't make a second batch request, which means the request
704704
# was successfully retried internally
705-
[ ! "$(grep "tq: retrying object" push.log)" ]
705+
grep "tq: retrying object" push.log && exit 1
706706
grep "Uploading LFS objects: 100% (1/1), 7 B" push.log
707707

708708
echo "bad fetch"
@@ -720,7 +720,7 @@ begin_test "credentials from remote.origin.url"
720720
GIT_TRACE=1 git lfs fetch --all 2>&1 | tee fetch.log
721721
# No 401 should occur as we've already set an access mode for the
722722
# storage endpoint during the push
723-
[ ! "$(grep "HTTP: 401" fetch.log)" ]
723+
grep "HTTP: 401" fetch.log && exit 1
724724
git lfs fsck
725725

726726
echo "good fetch, setting access mode"
@@ -734,7 +734,7 @@ begin_test "credentials from remote.origin.url"
734734
grep "HTTP: 401" fetch.log
735735
# Ensure we didn't make a second batch request, which means the request
736736
# was successfully retried internally
737-
[ ! "$(grep "tq: retrying object" fetch.log)" ]
737+
grep "tq: retrying object" fetch.log && exit 1
738738

739739
git lfs fsck
740740
)

t/t-migrate-export.sh

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ begin_test "migrate export (default branch)"
5252
echo "$main_attrs" | grep -q "*.md !text !filter !merge !diff"
5353
echo "$main_attrs" | grep -q "*.txt !text !filter !merge !diff"
5454

55-
[ ! $(echo "$feature_attrs" | grep -q "*.md !text !filter !merge !diff") ]
56-
[ ! $(echo "$feature_attrs" | grep -q "*.txt !text !filter !merge !diff") ]
55+
echo "$feature_attrs" | grep -q "*.md !text !filter !merge !diff" && exit 1
56+
echo "$feature_attrs" | grep -q "*.txt !text !filter !merge !diff" && exit 1
57+
true
5758
)
5859
end_test
5960

@@ -253,8 +254,9 @@ begin_test "migrate export (exclude remote refs)"
253254
echo "$main_attrs" | grep -q "*.md !text !filter !merge !diff"
254255
echo "$main_attrs" | grep -q "*.txt !text !filter !merge !diff"
255256

256-
[ ! $(echo "$remote_attrs" | grep -q "*.md !text !filter !merge !diff") ]
257-
[ ! $(echo "$remote_attrs" | grep -q "*.txt !text !filter !merge !diff") ]
257+
echo "$remote_attrs" | grep -q "*.md !text !filter !merge !diff" && exit 1
258+
echo "$remote_attrs" | grep -q "*.txt !text !filter !merge !diff" && exit 1
259+
true
258260
)
259261
end_test
260262

@@ -361,8 +363,8 @@ begin_test "migrate export (include/exclude ref)"
361363
remote_attrs="$(git cat-file -p "$remote:.gitattributes")"
362364
feature_attrs="$(git cat-file -p "$feature:.gitattributes")"
363365

364-
[ ! $(echo "$main_attrs" | grep -q "*.txt !text !filter !merge !diff") ]
365-
[ ! $(echo "$remote_attrs" | grep -q "*.txt !text !filter !merge !diff") ]
366+
echo "$main_attrs" | grep -q "*.txt !text !filter !merge !diff" && exit 1
367+
echo "$remote_attrs" | grep -q "*.txt !text !filter !merge !diff" && exit 1
366368
echo "$feature_attrs" | grep -q "*.txt !text !filter !merge !diff"
367369
)
368370
end_test

t/t-migrate-import-no-rewrite.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ begin_test "migrate import --no-rewrite (nested .gitattributes)"
132132

133133
# Ensure a .md filter does not exist in the top-level .gitattributes
134134
main_attrs="$(git cat-file -p "$main:.gitattributes")"
135-
[ !"$(echo "$main_attrs" | grep -q ".md")" ]
135+
echo "$main_attrs" | grep -q ".md" && exit 1
136136

137137
# Ensure a .md filter exists in the nested .gitattributes
138138
nested_attrs="$(git cat-file -p "$main:b/.gitattributes")"

t/t-migrate-import.sh

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ begin_test "migrate import (default branch)"
2626
feature="$(git rev-parse refs/heads/my-feature)"
2727

2828
main_attrs="$(git cat-file -p "$main:.gitattributes")"
29-
[ ! $(git cat-file -p "$feature:.gitattributes") ]
29+
[ -z "$(git cat-file -p "$feature:.gitattributes")" ]
3030

3131
echo "$main_attrs" | grep -q "*.md filter=lfs diff=lfs merge=lfs"
3232
echo "$main_attrs" | grep -q "*.txt filter=lfs diff=lfs merge=lfs"
@@ -103,7 +103,7 @@ begin_test "migrate import (default branch with filter)"
103103
feature="$(git rev-parse refs/heads/my-feature)"
104104

105105
main_attrs="$(git cat-file -p "$main:.gitattributes")"
106-
[ ! $(git cat-file -p "$feature:.gitattributes") ]
106+
[ -z "$(git cat-file -p "$feature:.gitattributes")" ]
107107

108108
echo "$main_attrs" | grep -q "*.md filter=lfs diff=lfs merge=lfs"
109109
echo "$main_attrs" | grep -vq "*.txt filter=lfs diff=lfs merge=lfs"
@@ -167,7 +167,7 @@ begin_test "migrate import (default branch, exclude remote refs)"
167167
remote="$(git rev-parse refs/remotes/origin/main)"
168168

169169
main_attrs="$(git cat-file -p "$main:.gitattributes")"
170-
[ ! $(git cat-file -p "$remote:.gitattributes") ]
170+
[ -z "$(git cat-file -p "$remote:.gitattributes")" ]
171171

172172
echo "$main_attrs" | grep -q "*.md filter=lfs diff=lfs merge=lfs"
173173
echo "$main_attrs" | grep -vq "*.txt filter=lfs diff=lfs merge=lfs"
@@ -206,7 +206,7 @@ begin_test "migrate import (given branch, exclude remote refs)"
206206
remote="$(git rev-parse refs/remotes/origin/main)"
207207

208208
main_attrs="$(git cat-file -p "$main:.gitattributes")"
209-
[ ! $(git cat-file -p "$remote:.gitattributes") ]
209+
[ -z "$(git cat-file -p "$remote:.gitattributes")" ]
210210
feature_attrs="$(git cat-file -p "$feature:.gitattributes")"
211211

212212
echo "$main_attrs" | grep -q "*.md filter=lfs diff=lfs merge=lfs"
@@ -322,8 +322,8 @@ begin_test "migrate import (include/exclude ref)"
322322
feature="$(git rev-parse refs/heads/my-feature)"
323323
remote="$(git rev-parse refs/remotes/origin/main)"
324324

325-
[ ! $(git cat-file -p "$main:.gitattributes") ]
326-
[ ! $(git cat-file -p "$remote:.gitattributes") ]
325+
[ -z "$(git cat-file -p "$main:.gitattributes")" ]
326+
[ -z "$(git cat-file -p "$remote:.gitattributes")" ]
327327
feature_attrs="$(git cat-file -p "$feature:.gitattributes")"
328328

329329
echo "$feature_attrs" | grep -q "*.md filter=lfs diff=lfs merge=lfs"
@@ -360,8 +360,8 @@ begin_test "migrate import (include/exclude ref args)"
360360
feature="$(git rev-parse refs/heads/my-feature)"
361361
remote="$(git rev-parse refs/remotes/origin/main)"
362362

363-
[ ! $(git cat-file -p "$main:.gitattributes") ]
364-
[ ! $(git cat-file -p "$remote:.gitattributes") ]
363+
[ -z "$(git cat-file -p "$main:.gitattributes")" ]
364+
[ -z "$(git cat-file -p "$remote:.gitattributes")" ]
365365
feature_attrs="$(git cat-file -p "$feature:.gitattributes")"
366366

367367
echo "$feature_attrs" | grep -q "*.md filter=lfs diff=lfs merge=lfs"
@@ -400,8 +400,8 @@ begin_test "migrate import (include/exclude ref with filter)"
400400
feature="$(git rev-parse refs/heads/my-feature)"
401401
remote="$(git rev-parse refs/remotes/origin/main)"
402402

403-
[ ! $(git cat-file -p "$main:.gitattributes") ]
404-
[ ! $(git cat-file -p "$remote:.gitattributes") ]
403+
[ -z "$(git cat-file -p "$main:.gitattributes")" ]
404+
[ -z "$(git cat-file -p "$remote:.gitattributes")" ]
405405
feature_attrs="$(git cat-file -p "$feature:.gitattributes")"
406406

407407
echo "$feature_attrs" | grep -vq "*.md filter=lfs diff=lfs merge=lfs"

t/t-track-wildcards.sh

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ begin_test "track files using wildcard pattern with leading slash"
3030
git lfs ls-files | tee files.log
3131

3232
grep "a.dat" files.log
33-
[ ! $(grep "dir/b.dat" files.log) ] # Subdirectories ignored
33+
grep "dir/b.dat" files.log && exit 1 # Subdirectories ignored
3434

3535
# Add files after being tracked by LFS
3636
printf "contents" > c.dat
@@ -43,9 +43,10 @@ begin_test "track files using wildcard pattern with leading slash"
4343
git lfs ls-files | tee new_files.log
4444

4545
grep "a.dat" new_files.log
46-
[ ! $(grep "dir/b.dat" new_files.log) ]
46+
grep "dir/b.dat" new_files.log && exit 1
4747
grep "c.dat" new_files.log
48-
[ ! $(grep "dir/d.dat" new_files.log) ]
48+
grep "dir/d.dat" new_files.log && exit 1
49+
true
4950
)
5051
end_test
5152

0 commit comments

Comments
 (0)