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