grep.tests 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/sh
  2. # Copyright 2005 by Rob Landley <rob@landley.net>
  3. # Licensed under GPL v2, see file LICENSE for details.
  4. # AUDIT:
  5. . testing.sh
  6. # testing "test name" "options" "expected result" "file input" "stdin"
  7. # file input will be file called "input"
  8. # test can create a file "actual" instead of writing to stdout
  9. # Test exit status
  10. testing "grep (exit with error)" "grep nonexistent 2> /dev/null ; echo \$?" \
  11. "1\n" "" ""
  12. testing "grep (exit success)" "grep grep $0 > /dev/null 2>&1 ; echo \$?" "0\n" \
  13. "" ""
  14. # Test various data sources and destinations
  15. testing "grep (default to stdin)" "grep two" "two\n" "" \
  16. "one\ntwo\nthree\nthree\nthree\n"
  17. testing "grep - (specify stdin)" "grep two -" "two\n" "" \
  18. "one\ntwo\nthree\nthree\nthree\n"
  19. testing "grep input (specify file)" "grep two input" "two\n" \
  20. "one\ntwo\nthree\nthree\nthree\n" ""
  21. testing "grep (no newline at EOL)" "grep bug" "bug" "bug" ""
  22. >empty
  23. testing "grep two files" "grep two input empty 2>/dev/null" \
  24. "input:two\n" "one\ntwo\nthree\nthree\nthree\n" ""
  25. rm empty
  26. testing "grep - infile (specify stdin and file)" "grep two - input" \
  27. "(standard input):two\ninput:two\n" "one\ntwo\nthree\n" \
  28. "one\ntwo\ntoo\nthree\nthree\n"
  29. # Check if we see the correct return value if both stdin and non-existing file
  30. # are given.
  31. testing "grep - nofile (specify stdin and nonexisting file)" \
  32. "grep two - nonexistent 2> /dev/null ; echo \$?" \
  33. "(standard input):two\n(standard input):two\n2\n" \
  34. "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
  35. testing "grep -q - nofile (specify stdin and nonexisting file, no match)" \
  36. "grep -q nomatch - nonexistent 2> /dev/null ; echo \$?" \
  37. "2\n" "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
  38. # SUSv3: If the -q option is specified, the exit status shall be zero
  39. # if an input line is selected, even if an error was detected.
  40. testing "grep -q - nofile (specify stdin and nonexisting file, match)" \
  41. "grep -q two - nonexistent ; echo \$?" \
  42. "0\n" "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
  43. # Test various command line options
  44. # -s no error messages
  45. testing "grep -s nofile (nonexisting file, no match)" \
  46. "grep -s nomatch nonexistent ; echo \$?" "2\n" "" ""
  47. testing "grep -s nofile - (stdin and nonexisting file, match)" \
  48. "grep -s domatch nonexistent - ; echo \$?" \
  49. "(standard input):domatch\n2\n" "" "nomatch\ndomatch\nend\n"
  50. # This doesn't match GNU behaviour (Binary file input matches)
  51. # acts like GNU grep -a
  52. testing "grep handles binary files" "grep foo input" "foo\n" "\0foo\n\n" ""
  53. # This doesn't match GNU behaviour (Binary file (standard input) matches)
  54. # acts like GNU grep -a
  55. testing "grep handles binary stdin" "grep foo" "foo\n" "" "\0foo\n\n"
  56. testing "grep matches NUL" "grep . input > /dev/null 2>&1 ; echo \$?" \
  57. "0\n" "\0\n" ""
  58. # -e regex
  59. testing "grep handles multiple regexps" "grep -e one -e two input ; echo \$?" \
  60. "one\ntwo\n0\n" "one\ntwo\n" ""
  61. testing "grep -F handles multiple expessions" "grep -F -e one -e two input ; echo \$?" \
  62. "one\ntwo\n0\n" "one\ntwo\n" ""
  63. optional FEATURE_GREP_EGREP_ALIAS
  64. testing "grep -E supports extended regexps" "grep -E fo+" "foo\n" "" \
  65. "b\ar\nfoo\nbaz"
  66. testing "grep is also egrep" "egrep foo" "foo\n" "" "foo\nbar\n"
  67. testing "egrep is not case insensitive" \
  68. "egrep foo ; [ \$? -ne 0 ] && echo yes" "yes\n" "" "FOO\n"
  69. exit $FAILCOUNT