grep.tests 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/bin/sh
  2. # grep tests.
  3. # Copyright 2005 by Rob Landley <rob@landley.net>
  4. # Licensed under GPL v2, see file LICENSE for details.
  5. # AUDIT:
  6. . testing.sh
  7. # testing "test name" "options" "expected result" "file input" "stdin"
  8. # file input will be file called "input"
  9. # test can create a file "actual" instead of writing to stdout
  10. # Test exit status
  11. testing "grep (exit with error)" "grep nonexistent 2> /dev/null ; echo \$?" \
  12. "1\n" "" ""
  13. testing "grep (exit success)" "grep grep $0 > /dev/null 2>&1 ; echo \$?" "0\n" \
  14. "" ""
  15. # Test various data sources and destinations
  16. testing "grep (default to stdin)" "grep two" "two\n" "" \
  17. "one\ntwo\nthree\nthree\nthree\n"
  18. testing "grep - (specify stdin)" "grep two -" "two\n" "" \
  19. "one\ntwo\nthree\nthree\nthree\n"
  20. testing "grep input (specify file)" "grep two input" "two\n" \
  21. "one\ntwo\nthree\nthree\nthree\n" ""
  22. testing "grep (no newline at EOL)" "grep bug" "bug" "bug" ""
  23. # Note that this assumes actual is empty.
  24. testing "grep input actual (two files)" "grep two input actual 2> /dev/null" \
  25. "input:two\n" "one\ntwo\nthree\nthree\nthree\n" ""
  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. optional FEATURE_GREP_EGREP_ALIAS
  62. testing "grep -E supports extended regexps" "grep -E fo+" "foo\n" "" \
  63. "b\ar\nfoo\nbaz"
  64. testing "grep is also egrep" "egrep foo" "foo\n" "" "foo\nbar\n"
  65. testing "egrep is not case insensitive" \
  66. "egrep foo ; [ \$? -ne 0 ] && echo yes" "yes\n" "" "FOO\n"
  67. exit $FAILCOUNT