grep.tests 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #!/bin/sh
  2. # Copyright 2005 by Rob Landley <rob@landley.net>
  3. # Licensed under GPLv2, see file LICENSE in this source tree.
  4. # AUDIT:
  5. . ./testing.sh
  6. # testing "test name" "commands" "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. optional EXTRA_COMPAT
  53. testing "grep handles NUL in files" "grep -a foo input" "\0foo\n" "\0foo\n\n" ""
  54. testing "grep handles NUL on stdin" "grep -a foo" "\0foo\n" "" "\0foo\n\n"
  55. testing "grep matches NUL" "grep . input > /dev/null 2>&1 ; echo \$?" \
  56. "0\n" "\0\n" ""
  57. SKIP=
  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. testing "grep -F handles -i" "grep -F -i foo input ; echo \$?" \
  64. "FOO\n0\n" "FOO\n" ""
  65. # -f file/-
  66. testing "grep can read regexps from stdin" "grep -f - input ; echo \$?" \
  67. "two\nthree\n0\n" "tw\ntwo\nthree\n" "tw.\nthr\n"
  68. # -x (whole line match)
  69. testing "grep -x (full match)" "grep -x foo input ; echo \$?" \
  70. "foo\n0\n" "foo\n" ""
  71. testing "grep -x (partial match 1)" "grep -x foo input ; echo \$?" \
  72. "1\n" "foo bar\n" ""
  73. testing "grep -x (partial match 2)" "grep -x foo input ; echo \$?" \
  74. "1\n" "bar foo\n" ""
  75. testing "grep -x -F (full match)" "grep -x -F foo input ; echo \$?" \
  76. "foo\n0\n" "foo\n" ""
  77. testing "grep -x -F (partial match 1)" "grep -x -F foo input ; echo \$?" \
  78. "1\n" "foo bar\n" ""
  79. testing "grep -x -F (partial match 2)" "grep -x -F foo input ; echo \$?" \
  80. "1\n" "bar foo\n" ""
  81. # -L "show filenames which do not match" has inverted exitcode (if it printed something, it's "success")
  82. testing "grep -L exitcode 0" "grep -L qwe input; echo \$?" \
  83. "input\n0\n" "asd\n" ""
  84. testing "grep -L exitcode 0 #2" "grep -L qwe input -; echo \$?" \
  85. "(standard input)\n0\n" "qwe\n" "asd\n"
  86. testing "grep -L exitcode 1" "grep -L qwe input; echo \$?" \
  87. "1\n" "qwe\n" ""
  88. optional EGREP
  89. testing "grep -E supports extended regexps" "grep -E fo+" "foo\n" "" \
  90. "b\ar\nfoo\nbaz"
  91. testing "grep is also egrep" "egrep foo" "foo\n" "" "foo\nbar\n"
  92. testing "egrep is not case insensitive" \
  93. "egrep foo ; [ \$? -ne 0 ] && echo yes" "yes\n" "" "FOO\n"
  94. testing "grep -E -o prints all matches" \
  95. "grep -E -o '([[:xdigit:]]{2}[:-]){5}[[:xdigit:]]{2}'" \
  96. "00:19:3E:00:AA:5E\n00:1D:60:3D:3A:FB\n00:22:43:49:FB:AA\n" \
  97. "" "00:19:3E:00:AA:5E 00:1D:60:3D:3A:FB 00:22:43:49:FB:AA\n"
  98. SKIP=
  99. testing "grep -o does not loop forever" \
  100. 'grep -o "[^/]*$"' \
  101. "test\n" \
  102. "" "/var/test\n"
  103. testing "grep -o does not loop forever on zero-length match" \
  104. 'grep -o "" | head -n1' \
  105. "" \
  106. "" "test\n"
  107. testing "grep -f EMPTY_FILE" \
  108. "grep -f input" \
  109. "" \
  110. "" \
  111. "test\n"
  112. testing "grep -v -f EMPTY_FILE" \
  113. "grep -v -f input" \
  114. "test\n" \
  115. "" \
  116. "test\n"
  117. testing "grep -vxf EMPTY_FILE" \
  118. "grep -vxf input" \
  119. "test\n" \
  120. "" \
  121. "test\n"
  122. testing "grep -Fw matches only words" \
  123. "grep -Fw foo input" \
  124. "" \
  125. "foop\n" \
  126. ""
  127. testing "grep -Fw doesn't stop on 1st mismatch" \
  128. "grep -Fw foo input" \
  129. "foop foo\n" \
  130. "foop foo\n" \
  131. ""
  132. testing "grep -w doesn't stop on 1st mismatch" \
  133. "grep -w foo input" \
  134. "foop foo\n" \
  135. "foop foo\n" \
  136. ""
  137. testing "grep -w ^str doesn't match str not at the beginning" \
  138. "grep -w ^str input" \
  139. "" \
  140. "strstr\n" \
  141. ""
  142. testing "grep -w ^ doesn't hang" \
  143. "grep -w ^ input" \
  144. "" \
  145. "anything\n" \
  146. ""
  147. testing "grep -w word doesn't match wordword" \
  148. "grep -w word input" \
  149. "" \
  150. "wordword\n" \
  151. ""
  152. testing "grep -F -w w doesn't match ww" \
  153. "grep -F -w w input" \
  154. "" \
  155. "ww\n" \
  156. ""
  157. testing "grep -w word match second word" \
  158. "grep -w word input" \
  159. "bword,word\n""wordb,word\n""bwordb,word\n" \
  160. "bword,word\n""wordb,word\n""bwordb,word\n" \
  161. ""
  162. testing "grep -x -v -e EXP1 -e EXP2 finds nothing if either EXP matches" \
  163. "grep -x -v -e '.*aa.*' -e 'bb.*'; echo \$?" \
  164. "1\n" \
  165. "" \
  166. " aa bb cc\n"
  167. testing "grep PATTERN can be a newline-delimited list" \
  168. 'grep -Fv "$(printf "foo\nbar\n")"' \
  169. "baz\n" \
  170. "" \
  171. "foo\nbar\nbaz\n"
  172. testing "grep -e PATTERN can be a newline-delimited list" \
  173. 'grep -Fv -e "$(printf "foo\nbar\n")"' \
  174. "baz\n" \
  175. "" \
  176. "foo\nbar\nbaz\n"
  177. # -r on symlink to dir should recurse into dir
  178. mkdir -p grep.testdir/foo
  179. echo bar > grep.testdir/foo/file
  180. ln -s foo grep.testdir/symfoo
  181. testing "grep -r on symlink to dir" \
  182. "grep -r . grep.testdir/symfoo" \
  183. "grep.testdir/symfoo/file:bar\n" \
  184. "" ""
  185. rm -Rf grep.testdir
  186. # But -r on dir/symlink_to_dir should not recurse into symlink_to_dir
  187. mkdir -p grep.testdir/foo
  188. echo bar > grep.testdir/foo/file
  189. ln -s foo grep.testdir/symfoo
  190. testing "grep -r on dir/symlink to dir" \
  191. "grep -r . grep.testdir" \
  192. "grep.testdir/foo/file:bar\n" \
  193. "" ""
  194. rm -Rf grep.testdir
  195. # testing "test name" "commands" "expected result" "file input" "stdin"
  196. # file input will be file called "input"
  197. # test can create a file "actual" instead of writing to stdout
  198. exit $FAILCOUNT