]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
tests: provide 100% coverage for echo
authorBernhard Voelker <mail@bernhard-voelker.de>
Mon, 24 Sep 2018 21:47:39 +0000 (23:47 +0200)
committerPádraig Brady <P@draigBrady.com>
Sat, 27 Oct 2018 12:02:54 +0000 (05:02 -0700)
*  src/echo.c (usage): Assert that STATUS is always EXIT_SUCCESS.
* tests/misc/echo.sh: Add further tests for all hex and escape and
escape characters.

To get coverage statistics, run:
  make coverage -j 4 TESTS=tests/misc/echo.sh SUBDIRS=.
  xdg-open doc/coverage/src/echo.c.gcov.frameset.html

src/echo.c
tests/misc/echo.sh

index 2aee5acfbdd04696af174a01aca5452e05f4ce4b..062f9038cf725b2931fdf27555a329200d5bd0b8 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <config.h>
 #include <stdio.h>
+#include <assert.h>
 #include <sys/types.h>
 #include "system.h"
 
@@ -34,35 +35,35 @@ enum { DEFAULT_ECHO_TO_XPG = false };
 void
 usage (int status)
 {
-  if (status != EXIT_SUCCESS)
-    emit_try_help ();
-  else
-    {
-      printf (_("\
+  /* STATUS should always be EXIT_SUCCESS (unlike in most other
+     utilities which would call emit_try_help otherwise).  */
+  assert (status == EXIT_SUCCESS);
+
+  printf (_("\
 Usage: %s [SHORT-OPTION]... [STRING]...\n\
   or:  %s LONG-OPTION\n\
 "), program_name, program_name);
-      fputs (_("\
+  fputs (_("\
 Echo the STRING(s) to standard output.\n\
 \n\
   -n             do not output the trailing newline\n\
 "), stdout);
-      fputs (_(DEFAULT_ECHO_TO_XPG
-               ? N_("\
+  fputs (_(DEFAULT_ECHO_TO_XPG
+           ? N_("\
   -e             enable interpretation of backslash escapes (default)\n\
   -E             disable interpretation of backslash escapes\n")
-               : N_("\
+           : N_("\
   -e             enable interpretation of backslash escapes\n\
   -E             disable interpretation of backslash escapes (default)\n")),
-             stdout);
-      fputs (HELP_OPTION_DESCRIPTION, stdout);
-      fputs (VERSION_OPTION_DESCRIPTION, stdout);
-      fputs (_("\
+         stdout);
+  fputs (HELP_OPTION_DESCRIPTION, stdout);
+  fputs (VERSION_OPTION_DESCRIPTION, stdout);
+  fputs (_("\
 \n\
 If -e is in effect, the following sequences are recognized:\n\
 \n\
 "), stdout);
-      fputs (_("\
+  fputs (_("\
   \\\\      backslash\n\
   \\a      alert (BEL)\n\
   \\b      backspace\n\
@@ -74,13 +75,12 @@ If -e is in effect, the following sequences are recognized:\n\
   \\t      horizontal tab\n\
   \\v      vertical tab\n\
 "), stdout);
-      fputs (_("\
+  fputs (_("\
   \\0NNN   byte with octal value NNN (1 to 3 digits)\n\
   \\xHH    byte with hexadecimal value HH (1 to 2 digits)\n\
 "), stdout);
-      printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
-      emit_ancillary_info (PROGRAM_NAME);
-    }
+  printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
+  emit_ancillary_info (PROGRAM_NAME);
   exit (status);
 }
 
index 53e87fb71a9cc19d7f9a6d80cd8e684b3b3876ab..34b23c46f673b06040add58286a672b60509fcb9 100755 (executable)
@@ -63,4 +63,37 @@ foo
 EOF
 compare exp out || fail=1
 
+# Further test coverage.
+# Output a literal '-' (on a line itself).
+$prog - > out || fail=1
+# Output a literal backslash '\', no newline.
+$prog -n -e '\\' >> out || fail=1
+# Output an empty line (merely to have a newline after the previous test).
+$prog >> out || fail=1
+# Test other characters escaped by a backslash:
+# \a  hex 07  alert, bell
+# \b  hex 08  backspace
+# \e  hex 1b  escape
+# \f  hex 0c  form feed
+# \n  hex 0a  new line
+# \r  hex 0d  carriage return
+# \t  hex 09  horizontal tab
+# \v  hex 0b  vertical tab
+# Convert output, yet checking the exit status of $prog.
+{ $prog -n -e '\a\b\e\f\n\r\t\v' || touch fail; } | od -tx1 >> out || fail=1
+test '!' -f fail || fail=1
+# Output hex values which contain hexadecimal characters to test hextobin().
+# Hex values 4a through 4f are ASCII "JKLMNO".
+$prog -n -e '\x4a\x4b\x4c\x4d\x4e\x4f\x4A\x4B\x4C\x4D\x4E\x4F' >> out || fail=1
+# Output another newline.
+$prog >> out || fail=1
+cat <<\EOF > exp
+-
+\
+0000000 07 08 1b 0c 0a 0d 09 0b
+0000010
+JKLMNOJKLMNO
+EOF
+compare exp out || fail=1
+
 Exit $fail