grep.test 8.5 KB

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