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