grep.test 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #!/bin/bash
  2. # TODO: several tests need to check both fast and slow paths
  3. [ -f testing.sh ] && . testing.sh
  4. # Copyright 2013 by Kyungsu Kim <kaspyx@gmail.com>
  5. # Copyright 2013 by Kyungwan Han <asura321@gmail.com>
  6. #testing "name" "command" "result" "infile" "stdin"
  7. testcmd "-c" "-c 123 input" "3\n" "123\ncount 123\n123\nfasdfasdf" ""
  8. echo -e "this is test" > foo
  9. echo -e "this is test2" > foo2
  10. echo -e "this is foo3" > foo3
  11. testcmd "-l" "-l test foo foo2 foo3" "foo\nfoo2\n" "" ""
  12. testcmd "-L" "-L test foo foo2 foo3" "foo3\n" "" ""
  13. rm foo foo2 foo3
  14. testcmd "-q" "-q test input && echo yes" "yes\n" "this is a test\n" ""
  15. testcmd "-E" "-E '[0-9]' input" "1234123asdfas123123\n1\n" \
  16. "1234123asdfas123123\nabc\n1\nabcde" ""
  17. testcmd "-e" "-e '[0-9]' input" "1234123asdfas123123\n1\n" \
  18. "1234123asdfas123123\nabc\n1\nabcde" ""
  19. testcmd "-e -e" "-e one -e two -e three input" \
  20. "two\ntwo\nthree\none\n" "two\ntwo\nthree\nand\none\n" ""
  21. testcmd "-F" "-F is input" "this is test\nthis is test2\n" \
  22. "this is test\nthis is test2\ntest case" ""
  23. testcmd "-Fo ''" "-Fo ''" "" "" "hello\n"
  24. testcmd "-Fw ''" "-Fw ''" "" "" "hello\n"
  25. testcmd "-Fw '' 2" "-Fw ''" "\n" "" "\n"
  26. testcmd "-F is really fixed" "-F '.[x]'" "c.[x]d\n" "" "axb\nc.[x]d\n"
  27. echo -e "this is test\nthis is test2\ntest case" > foo
  28. echo -e "hello this is test" > foo2
  29. echo -e "hi hello" > foo3
  30. testcmd "-H" "-H is foo foo2 foo3" \
  31. "foo:this is test\nfoo:this is test2\nfoo2:hello this is test\n" "" ""
  32. rm foo foo2 foo3
  33. testcmd "-b" "-b is input" "0:this is test\n13:this is test2\n" \
  34. "this is test\nthis is test2\ntest case" ""
  35. testcmd "-i" "-i is input" "thisIs test\nthis is test2\n" \
  36. "thisIs test\nthis is test2\ntest case" ""
  37. testcmd "-n" "-n is input" "1:this is test\n2:this is test2\n" \
  38. "this is test\nthis is test2\ntest case" ""
  39. testcmd "-o" "-o is input" "is\nis\nis\nis\n" \
  40. "this is test\nthis is test2\ntest case" ""
  41. testcmd "-s" "-hs hello asdf input 2>&1" "hello\n" "hello\n" ""
  42. testcmd "-v" "-v abc input" "1234123asdfas123123\n1ABa\n" \
  43. "1234123asdfas123123\n1ABabc\nabc\n1ABa\nabcde" ""
  44. testcmd "-w" "-w abc input" "abc\n123 abc\nabc 123\n123 abc 456\n" \
  45. "1234123asdfas123123\n1ABabc\nabc\n1ABa\nabcde\n123 abc\nabc 123\n123 abc 456\n" ""
  46. testcmd "-x" "-x abc input" "abc\n" "aabcc\nabc\n" ""
  47. testcmd "-H (standard input)" "-H abc" "(standard input):abc\n" "" "abc\n"
  48. testcmd "-l (standard input)" "-l abc" "(standard input)\n" "" "abc\n"
  49. testcmd "-n two inputs" "-hn def - input" "2:def\n2:def\n" \
  50. "abc\ndef\n" "abc\ndef\n"
  51. testcmd "pattern with newline" $'"abc\ndef" input' "aabcc\nddeff\n" \
  52. "aaaa\naabcc\n\dddd\nddeff\nffff\n" ""
  53. testcmd "-lH" "-lH abc input" "input\n" "abc\n" ""
  54. testcmd "-cn" "-cn abc input" "1\n" "abc\n" ""
  55. testcmd "-cH" "-cH abc input" "input:1\n" "abc\n" ""
  56. testcmd "-qs" "-qs abc none input && echo yes" "yes\n" "abc\n" ""
  57. testcmd "-hl" "-hl abc input" "input\n" "abc\n" ""
  58. testcmd "-b stdin" "-b one" "0:one\n4:one\n8:one\n" "" "one\none\none\n"
  59. testcmd "-o overlap" "-bo aaa" "1:aaa\n" "" "baaaa\n"
  60. # nonobvious: -co counts lines, not matches
  61. testcmd "-co" "-co one input" "1\n" "one one one\n" ""
  62. testcmd "-nom" "-nom 2 one" "1:one\n1:one\n1:one\n2:one\n2:one\n" \
  63. "" "one one one\none one\none"
  64. toyonly testcmd "-vo" "-vo one input" "two\nthree\n" "onetwoonethreeone\n" ""
  65. testcmd "no newline" "-h one input -" \
  66. "hello one\nthere one\n" "hello one" "there one"
  67. testcmd "-e multi" "-e one -ethree input" "three\none\n" "three\ntwo\none\n" ""
  68. # Suppress filenames for recursive test because dunno order they'd occur in
  69. mkdir sub
  70. echo -e "one\ntwo\nthree" > sub/one
  71. echo -e "three\ntwo\none" > sub/two
  72. testcmd "-hr" "-hr one sub" "one\none\n" "" ""
  73. testcmd "-r file" "-r three sub/two" "three\n" "" ""
  74. testcmd "-r dir" "-r one sub | sort" "sub/one:one\nsub/two:one\n" \
  75. "" ""
  76. rm -rf sub
  77. # -x exact match overrides -F's "empty string matches whole line" behavior
  78. testcmd "-Fx ''" "-Fx '' input" "" "one one one\n" ""
  79. testcmd "-F ''" "-F '' input" "one one one\n" "one one one\n" ""
  80. testcmd "-F -e blah -e ''" "-F -e blah -e '' input" "one one one\n" \
  81. "one one one\n" ""
  82. testcmd "-Fxv -e subset" "-Fxv -e bbswitch-dkms -e dkms" "" "" "bbswitch-dkms\n"
  83. testcmd "-e blah -e ''" "-e blah -e '' input" "one one one\n" "one one one\n" ""
  84. testcmd "-w ''" "-w '' input" "" "one one one\n" ""
  85. testcmd "-w '' 2" "-w '' input" "one two\n" "one two\n" ""
  86. testcmd "-w \\1" "-wo '\\(x\\)\\1'" "xx\n" "" "xx"
  87. testcmd "-o ''" "-o '' input" "" "one one one\n" ""
  88. testcmd "backref" '-e "a\(b\)" -e "b\(c\)\1"' "bcc\nab\n" "" "bcc\nbcb\nab\n"
  89. testcmd "-A" "-A 2 yes" "yes\nno\nno\n--\nyes\nno\nno\nyes\nno\n" \
  90. "" "yes\nno\nno\nno\nyes\nno\nno\nyes\nno"
  91. testcmd "-B" "-B 1 yes" "no\nyes\n--\nno\nyes\nno\nyes\n" \
  92. "" "no\nno\nno\nyes\nno\nno\nyes\nno\nyes"
  93. testcmd "-C" "-C 1 yes" "yes\nno\n--\nno\nyes\nno\nno\nyes\nno\nyes\nno\n" \
  94. "" "yes\nno\nno\nno\nyes\nno\nno\nyes\nno\nyes\nno\nno"
  95. testcmd "-HnC" "-HnC1 two" \
  96. "(standard input)-1-one\n(standard input):2:two\n(standard input)-3-three\n" \
  97. "" "one\ntwo\nthree"
  98. # Context lines weren't showing -b
  99. testcmd "-HnbB1" "-HnbB1 f input" \
  100. "input-3-8-three\ninput:4:14:four\ninput:5:19:five\n" \
  101. "one\ntwo\nthree\nfour\nfive\n" ""
  102. testcmd "-q match overrides error" \
  103. "-q hello missing input 2>/dev/null && echo yes" "yes\n" "hello\n" ""
  104. testcmd "-q not found is 1" '-q hello input || echo $?' "1\n" "x" ""
  105. testcmd "-q missing is 2" \
  106. '-q hello missing missing 2>/dev/null || echo $?' "2\n" "" ""
  107. testcmd "-q missing survives exists but not found" \
  108. '-q hello missing missing input 2>/dev/null || echo $?' "2\n" "" ""
  109. testcmd "not found retained past match" \
  110. 'hello missing input 2>/dev/null || echo $?' "input:hello\n2\n" "hello\n" ""
  111. touch empty
  112. testcmd "one match good enough for 0" \
  113. 'hello input empty && echo $?' 'input:hello\n0\n' 'hello\n' ''
  114. rm empty
  115. testcmd "-o ''" "-o ''" "" "" "one two three\none two\none\n"
  116. testcmd '' "-o -e '' -e two" "two\ntwo\n" "" "one two three\none two\none\n"
  117. echo "one\ntwo\nthree" > test
  118. testcmd "-l overrides -C" "-l -C1 two test input" "test\ninput\n" \
  119. "three\ntwo\none\n" ""
  120. rm test
  121. # match after NUL byte
  122. testcmd "match after NUL byte" "-a two" "one\0and two three\n" \
  123. "" 'one\0and two three'
  124. # BREs versus EREs
  125. testcmd "implicit BRE |" "'uno|dos'" "uno|dos\n" "" "uno\ndos\nuno|dos\n"
  126. testcmd "explicit BRE |" "-e 'uno|dos'" "uno|dos\n" "" "uno\ndos\nuno|dos\n"
  127. testcmd "explicit ERE |" "-E 'uno|dos'" "uno\ndos\nuno|dos\n" \
  128. "" "uno\ndos\nuno|dos\n"
  129. testcmd "" "-o -e iss -e ipp" "iss\niss\nipp\n" "" "mississippi"
  130. testcmd "" "-o -e gum -e rgu" "rgu\n" "" "argument"
  131. testcmd "early failure" '--what 2>/dev/null || echo $?' "2\n" "" ""
  132. testcmd "" 'abc ; echo $?' "abcdef\n0\n" "" "abcdef\n"
  133. testcmd "" 'abc doesnotexist input 2>/dev/null; echo $?' \
  134. "input:abcdef\n2\n" "abcdef\n" ""
  135. mkdir sub
  136. ln -s nope sub/link
  137. testcmd "" '-r walrus sub 2>/dev/null; echo $?' "1\n" "" ""
  138. rm -rf sub
  139. # --exclude-dir
  140. mkdir -p sub/yes
  141. echo "hello world" > sub/yes/test
  142. mkdir sub/no
  143. echo "hello world" > sub/no/test
  144. testcmd "--exclude-dir" '--exclude-dir=no -r world sub' \
  145. "sub/yes/test:hello world\n" "" ""
  146. rm -rf sub
  147. # -r and -R differ in that -R will dereference symlinks to directories.
  148. mkdir dir
  149. echo "hello" > dir/f
  150. mkdir sub
  151. ln -s ../dir sub/link
  152. testcmd "" "-rh hello sub 2>/dev/null || echo err" "err\n" "" ""
  153. testcmd "" "-Rh hello sub" "hello\n" "" ""
  154. rm -rf sub real
  155. # -F multiple matches
  156. testcmd "-F multiple" "-F h input" "this is hello\nthis is world\n" \
  157. "missing\nthis is hello\nthis is world\nmissing" ""
  158. testcmd "-Fi multiple" "-Fi h input" "this is HELLO\nthis is WORLD\n" \
  159. "missing\nthis is HELLO\nthis is WORLD\nmissing" ""
  160. testcmd "-F empty multiple" "-Fi '' input" \
  161. "missing\nthis is HELLO\nthis is WORLD\nmissing\n" \
  162. "missing\nthis is HELLO\nthis is WORLD\nmissing" ""
  163. testcmd "-Fx" "-Fx h input" "h\n" \
  164. "missing\nH\nthis is hello\nthis is world\nh\nmissing" ""
  165. testcmd "-Fix" "-Fix h input" "H\nh\n" \
  166. "missing\nH\nthis is HELLO\nthis is WORLD\nh\nmissing" ""
  167. testcmd "-F bucket sort" "-F '\.zip'" '\\.zip\n' '' '\\.zip\n'
  168. testcmd "-f /dev/null" "-f /dev/null" "" "" "hello\n"
  169. # -z doesn't apply to the \n in -e or -f patterns
  170. # Because x\n becomes "x" and "" the second of which matches every line.
  171. testcmd '-z patter\n' "-ze $'x\n' | xxd -pc0" \
  172. '6f6e650a74776f0a74687265650a00\n' '' 'one\ntwo\nthree\n'
  173. testcmd "-z patter\n 2" "-zof input | xxd -pc0" "69007400\n" "i\nt" "hi\nthere"
  174. testcmd '-lZ' '-lZ ^t input' 'input\0' 'one\ntwo' ''
  175. # other implementations get this wrong without -a, but right with -a???
  176. toyonly testcmd '-l ^ after \0' '-l ^t' '' 'one\0two' ''
  177. testcmd "print zero length match" "'[0-9]*'" "abc\n" "" "abc\n"
  178. testcmd "-o skip zero length match" "-o '[0-9]*'" "1234\n" "" "a1234b"
  179. # Bit of a hack, but other greps insert gratuitous \e[K clear-to-EOL
  180. testcmd "--color highlights all matches" \
  181. "--color=always def | grep -o '[[][0-9;]*[Km]def.[[]m' | wc -l" \
  182. "2\n" "" "abcdefghidefjkl\n"
  183. seq 1 100002 | base64 > testfile
  184. testing "speed" "timeout 5 grep -f testfile testfile 2>/dev/null | wc -l" \
  185. "10332\n" "" ""
  186. rm -f testfile
  187. # Fast path tests
  188. testcmd 'initial \' '\\.jar' 'bell.jar\n' '' 'bell.jar\n'
  189. testcmd '^$' '^\$' '\n' '' 'one\n\ntwo\n'
  190. testcmd 'middle ^ not special' 'a^' 'a^b\n' '' 'a^b\nb^a\n'
  191. # Quoted to protect it from the shell, grep should just see '$b'
  192. testcmd 'middle $ not special' "'\$b'" 'a$b\n' '' 'a$b\nb$a\n'
  193. testcmd 'grep -of' '-of input' 'abc\n' 'a.c\n' 'abcdef\n'