grep.tests 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. # GNU grep 2.5.3 outputs a new line character after the located string
  22. # even if there is no new line character in the input
  23. testing "grep (no newline at EOL)" "grep bug input" "bug\n" "bug" ""
  24. >empty
  25. testing "grep two files" "grep two input empty 2>/dev/null" \
  26. "input:two\n" "one\ntwo\nthree\nthree\nthree\n" ""
  27. rm empty
  28. testing "grep - infile (specify stdin and file)" "grep two - input" \
  29. "(standard input):two\ninput:two\n" "one\ntwo\nthree\n" \
  30. "one\ntwo\ntoo\nthree\nthree\n"
  31. # Check if we see the correct return value if both stdin and non-existing file
  32. # are given.
  33. testing "grep - nofile (specify stdin and nonexisting file)" \
  34. "grep two - nonexistent 2> /dev/null ; echo \$?" \
  35. "(standard input):two\n(standard input):two\n2\n" \
  36. "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
  37. testing "grep -q - nofile (specify stdin and nonexisting file, no match)" \
  38. "grep -q nomatch - nonexistent 2> /dev/null ; echo \$?" \
  39. "2\n" "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
  40. # SUSv3: If the -q option is specified, the exit status shall be zero
  41. # if an input line is selected, even if an error was detected.
  42. testing "grep -q - nofile (specify stdin and nonexisting file, match)" \
  43. "grep -q two - nonexistent ; echo \$?" \
  44. "0\n" "" "one\ntwo\ntwo\nthree\nthree\nthree\n"
  45. # Test various command line options
  46. # -s no error messages
  47. testing "grep -s nofile (nonexisting file, no match)" \
  48. "grep -s nomatch nonexistent ; echo \$?" "2\n" "" ""
  49. testing "grep -s nofile - (stdin and nonexisting file, match)" \
  50. "grep -s domatch nonexistent - ; echo \$?" \
  51. "(standard input):domatch\n2\n" "" "nomatch\ndomatch\nend\n"
  52. testing "grep handles NUL in files" "grep -a foo input" "\0foo\n" "\0foo\n\n" ""
  53. testing "grep handles NUL on stdin" "grep -a foo" "\0foo\n" "" "\0foo\n\n"
  54. testing "grep matches NUL" "grep . input > /dev/null 2>&1 ; echo \$?" \
  55. "0\n" "\0\n" ""
  56. # -e regex
  57. testing "grep handles multiple regexps" "grep -e one -e two input ; echo \$?" \
  58. "one\ntwo\n0\n" "one\ntwo\n" ""
  59. testing "grep -F handles multiple expessions" "grep -F -e one -e two input ; echo \$?" \
  60. "one\ntwo\n0\n" "one\ntwo\n" ""
  61. # -f file/-
  62. testing "grep can read regexps from stdin" "grep -f - input ; echo \$?" \
  63. "two\nthree\n0\n" "tw\ntwo\nthree\n" "tw.\nthr\n"
  64. optional FEATURE_GREP_EGREP_ALIAS
  65. testing "grep -E supports extended regexps" "grep -E fo+" "foo\n" "" \
  66. "b\ar\nfoo\nbaz"
  67. testing "grep is also egrep" "egrep foo" "foo\n" "" "foo\nbar\n"
  68. testing "egrep is not case insensitive" \
  69. "egrep foo ; [ \$? -ne 0 ] && echo yes" "yes\n" "" "FOO\n"
  70. exit $FAILCOUNT