Merge branch 'jc/test-must-be-empty'
authorJunio C Hamano <gitster@pobox.com>
Thu, 8 Mar 2018 20:36:27 +0000 (12:36 -0800)
committerJunio C Hamano <gitster@pobox.com>
Thu, 8 Mar 2018 20:36:28 +0000 (12:36 -0800)
Test framework tweak to catch developer thinko.

* jc/test-must-be-empty:
test_must_be_empty: make sure the file exists, not just empty

1  2 
t/test-lib-functions.sh
diff --combined t/test-lib-functions.sh
index 42a6781358f85cd43100078e7457e18962f6f07e,d2eaf5ab67269730fd293e2032100cdb34283225..6e342804f96fb62ff0b3cfaf667e7efce79dc6af
@@@ -610,14 -610,6 +610,14 @@@ list_contains () 
  #
  # Writing this as "! git checkout ../outerspace" is wrong, because
  # the failure could be due to a segv.  We want a controlled failure.
 +#
 +# Accepts the following options:
 +#
 +#   ok=<signal-name>[,<...>]:
 +#     Don't treat an exit caused by the given signal as error.
 +#     Multiple signals can be specified as a comma separated list.
 +#     Currently recognized signal names are: sigpipe, success.
 +#     (Don't use 'success', use 'test_might_fail' instead.)
  
  test_must_fail () {
        case "$1" in
        exit_code=$?
        if test $exit_code -eq 0 && ! list_contains "$_test_ok" success
        then
 -              echo >&2 "test_must_fail: command succeeded: $*"
 +              echo >&4 "test_must_fail: command succeeded: $*"
                return 1
        elif test_match_signal 13 $exit_code && list_contains "$_test_ok" sigpipe
        then
                return 0
        elif test $exit_code -gt 129 && test $exit_code -le 192
        then
 -              echo >&2 "test_must_fail: died by signal $(($exit_code - 128)): $*"
 +              echo >&4 "test_must_fail: died by signal $(($exit_code - 128)): $*"
                return 1
        elif test $exit_code -eq 127
        then
 -              echo >&2 "test_must_fail: command not found: $*"
 +              echo >&4 "test_must_fail: command not found: $*"
                return 1
        elif test $exit_code -eq 126
        then
 -              echo >&2 "test_must_fail: valgrind error: $*"
 +              echo >&4 "test_must_fail: valgrind error: $*"
                return 1
        fi
        return 0
  #
  # Writing "git config --unset all.configuration || :" would be wrong,
  # because we want to notice if it fails due to segv.
 +#
 +# Accepts the same options as test_must_fail.
  
  test_might_fail () {
        test_must_fail ok=success "$@"
@@@ -688,7 -678,7 +688,7 @@@ test_expect_code () 
                return 0
        fi
  
 -      echo >&2 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
 +      echo >&4 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
        return 1
  }
  
@@@ -715,66 -705,12 +715,66 @@@ test_cmp_bin() 
        cmp "$@"
  }
  
 +# Use this instead of test_cmp to compare files that contain expected and
 +# actual output from git commands that can be translated.  When running
 +# under GETTEXT_POISON this pretends that the command produced expected
 +# results.
 +test_i18ncmp () {
 +      test -n "$GETTEXT_POISON" || test_cmp "$@"
 +}
 +
 +# Use this instead of "grep expected-string actual" to see if the
 +# output from a git command that can be translated either contains an
 +# expected string, or does not contain an unwanted one.  When running
 +# under GETTEXT_POISON this pretends that the command produced expected
 +# results.
 +test_i18ngrep () {
 +      eval "last_arg=\${$#}"
 +
 +      test -f "$last_arg" ||
 +      error "bug in the test script: test_i18ngrep requires a file" \
 +            "to read as the last parameter"
 +
 +      if test $# -lt 2 ||
 +         { test "x!" = "x$1" && test $# -lt 3 ; }
 +      then
 +              error "bug in the test script: too few parameters to test_i18ngrep"
 +      fi
 +
 +      if test -n "$GETTEXT_POISON"
 +      then
 +              # pretend success
 +              return 0
 +      fi
 +
 +      if test "x!" = "x$1"
 +      then
 +              shift
 +              ! grep "$@" && return 0
 +
 +              echo >&4 "error: '! grep $@' did find a match in:"
 +      else
 +              grep "$@" && return 0
 +
 +              echo >&4 "error: 'grep $@' didn't find a match in:"
 +      fi
 +
 +      if test -s "$last_arg"
 +      then
 +              cat >&4 "$last_arg"
 +      else
 +              echo >&4 "<File '$last_arg' is empty>"
 +      fi
 +
 +      return 1
 +}
 +
  # Call any command "$@" but be more verbose about its
  # failure. This is handy for commands like "test" which do
  # not output anything when they fail.
  verbose () {
        "$@" && return 0
 -      echo >&2 "command failed: $(git rev-parse --sq-quote "$@")"
 +      echo >&4 "command failed: $(git rev-parse --sq-quote "$@")"
        return 1
  }
  
  # otherwise.
  
  test_must_be_empty () {
-       if test -s "$1"
+       if ! test -f "$1"
+       then
+               echo "'$1' is missing"
+               return 1
+       elif test -s "$1"
        then
                echo "'$1' is not empty, it contains:"
                cat "$1"
@@@ -1084,37 -1024,3 +1088,37 @@@ nongit () 
                "$@"
        )
  }
 +
 +# convert stdin to pktline representation; note that empty input becomes an
 +# empty packet, not a flush packet (for that you can just print 0000 yourself).
 +packetize() {
 +      cat >packetize.tmp &&
 +      len=$(wc -c <packetize.tmp) &&
 +      printf '%04x%s' "$(($len + 4))" &&
 +      cat packetize.tmp &&
 +      rm -f packetize.tmp
 +}
 +
 +# Parse the input as a series of pktlines, writing the result to stdout.
 +# Sideband markers are removed automatically, and the output is routed to
 +# stderr if appropriate.
 +#
 +# NUL bytes are converted to "\\0" for ease of parsing with text tools.
 +depacketize () {
 +      perl -e '
 +              while (read(STDIN, $len, 4) == 4) {
 +                      if ($len eq "0000") {
 +                              print "FLUSH\n";
 +                      } else {
 +                              read(STDIN, $buf, hex($len) - 4);
 +                              $buf =~ s/\0/\\0/g;
 +                              if ($buf =~ s/^[\x2\x3]//) {
 +                                      print STDERR $buf;
 +                              } else {
 +                                      $buf =~ s/^\x1//;
 +                                      print $buf;
 +                              }
 +                      }
 +              }
 +      '
 +}