install-sh: a slightly better diagnostic, and tests enhancements
authorStefano Lattarini <stefano.lattarini@gmail.com>
Wed, 25 Dec 2013 23:33:05 +0000 (00:33 +0100)
committerStefano Lattarini <stefano.lattarini@gmail.com>
Wed, 25 Dec 2013 23:42:09 +0000 (00:42 +0100)
* lib/install-sh: When called with no non-option arguments and the '-t'
option with an argument that is not an existing directory, have the
diagnostic output complain about the lack of required arguments rather
than about the bad argument passed to '-t'.
* t/install-sh-unittests.sh: Enhance to also check diagnostic printed
in cases of expected failure.

Signed-off-by: Stefano Lattarini <stefano.lattarini@gmail.com>
lib/install-sh
t/install-sh-unittests.sh

index d8de87f3061c481074a1b136d37da2d42c9e6119..0b0fdcbba69ab6dd05ca162a5328828d46ab1d54 100755 (executable)
@@ -193,15 +193,6 @@ if test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
   done
 fi
 
-if test -z "$dir_arg"; then
-  if test $# -gt 1 || test "$is_target_a_directory" = always; then
-    if test ! -d "$dst_arg"; then
-      echo "$0: $dst_arg: Is not a directory." >&2
-      exit 1
-    fi
-  fi
-fi
-
 if test $# -eq 0; then
   if test -z "$dir_arg"; then
     echo "$0: no input file specified." >&2
@@ -212,6 +203,15 @@ if test $# -eq 0; then
   exit 0
 fi
 
+if test -z "$dir_arg"; then
+  if test $# -gt 1 || test "$is_target_a_directory" = always; then
+    if test ! -d "$dst_arg"; then
+      echo "$0: $dst_arg: Is not a directory." >&2
+      exit 1
+    fi
+  fi
+fi
+
 if test -z "$dir_arg"; then
   do_exit='(exit $ret); exit $ret'
   trap "ret=129; $do_exit" 1
index 1b85c9ce38573371d7e2a4b9c644420a7336ba92..dff0c51badda1ee6c64a238c38e93408f8da7713 100644 (file)
 am_create_testdir=empty
 . test-init.sh
 
+install_sh_fail () 
+{
+  err_rx=$1; shift
+  ./install-sh ${1+"$@"} 2>stderr && { cat stderr >&2; exit 1; }
+  cat stderr >&2
+  $EGREP "install-sh:.* $err_rx" stderr || exit 1
+}
+
 get_shell_script install-sh
 
 # Basic errors.
-./install-sh && exit 1
-./install-sh -m 644 dest && exit 1
+install_sh_fail 'no input file specified'
+install_sh_fail 'no input file specified' dest
+install_sh_fail 'no input file specified' -m 644 dest
+install_sh_fail 'no input file specified' -c -t dest
 
 # Incorrect usages.
 : > bar
 : > baz
 : > qux
-./install-sh -d -t foo && exit 1
-./install-sh -d -t foo bar && exit 1
-./install-sh -t foo bar && exit 1
-./install-sh bar baz foo && exit 1
+install_sh_fail 'target directory not allowed when installing a directory' \
+                -d -t foo
+install_sh_fail 'target directory not allowed when installing a directory' \
+                -d -t foo bar
+install_sh_fail 'foo: [iI]s not a directory' -t foo bar
+install_sh_fail 'foo: [iI]s not a directory' bar baz foo
 mkdir foo
-./install-sh -d -t foo && exit 1
-./install-sh -d -t foo bar && exit 1
+install_sh_fail 'target directory not allowed when installing a directory' \
+                -d -t foo
+install_sh_fail 'target directory not allowed when installing a directory' \
+                -d -t foo bar
 rmdir foo
 rm -f bar baz qux
 
@@ -96,8 +110,8 @@ test -f d4/z
 ./install-sh -T x d3/y
 test -f x
 test -f d3/y
-./install-sh -T x d3 && exit 1
-./install-sh -T x d4// && exit 1
+install_sh_fail 'd3: [iI]s a directory' -T x d3
+install_sh_fail 'd4(//)?: [iI]s a directory' -T x d4//
 
 # Ensure that install-sh works with names that include spaces.
 touch 'a  b'
@@ -108,8 +122,8 @@ test -f 'a  b'
 
 # Ensure we do not run into 'test' operator precedence bugs with Tru64 sh.
 for c in = '(' ')' '!'; do
-  ./install-sh $c 2>stderr && { cat stderr >&2; exit 1; }
-  cat stderr >&2
+  install_sh_fail 'no input file specified' $c
+  test -f stderr # sanity check
   grep 'test: ' stderr && exit 1
   # Skip tests if the file system is not capable.
   mkdir ./$c || continue