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