Ashwini Sharma | 577377c | 2013-08-01 01:52:32 -0500 | [diff] [blame] | 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 | |
Ashwini Sharma | 577377c | 2013-08-01 01:52:32 -0500 | [diff] [blame] | 8 | #testing "name" "command" "result" "infile" "stdin" |
| 9 | |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 10 | testing "-c" "grep -c 123 input" "3\n" "123\ncount 123\n123\nfasdfasdf" "" |
Ashwini Sharma | 577377c | 2013-08-01 01:52:32 -0500 | [diff] [blame] | 11 | |
Ashwini Sharma | 577377c | 2013-08-01 01:52:32 -0500 | [diff] [blame] | 12 | echo -e "this is test" > foo |
| 13 | echo -e "this is test2" > foo2 |
| 14 | echo -e "this is foo3" > foo3 |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 15 | testing "-l" "grep -l test foo foo2 foo3" "foo\nfoo2\n" "" "" |
Ashwini Sharma | 577377c | 2013-08-01 01:52:32 -0500 | [diff] [blame] | 16 | rm foo foo2 foo3 |
| 17 | |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 18 | testing "-q" "grep -q test input && echo yes" "yes\n" "this is a test\n" "" |
| 19 | testing "-E" "grep -E '[0-9]' input" "1234123asdfas123123\n1\n" \ |
Rob Landley | 3595eff | 2013-08-01 15:22:52 -0500 | [diff] [blame] | 20 | "1234123asdfas123123\nabc\n1\nabcde" "" |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 21 | testing "-e" "grep -e '[0-9]' input" "1234123asdfas123123\n1\n" \ |
Rob Landley | 3595eff | 2013-08-01 15:22:52 -0500 | [diff] [blame] | 22 | "1234123asdfas123123\nabc\n1\nabcde" "" |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 23 | testing "-e -e" "grep -e one -e two -e three input" \ |
Rob Landley | 9f088a1 | 2013-08-12 04:28:03 -0500 | [diff] [blame] | 24 | "two\ntwo\nthree\none\n" "two\ntwo\nthree\nand\none\n" "" |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 25 | testing "-F" "grep -F is input" "this is test\nthis is test2\n" \ |
Rob Landley | 3595eff | 2013-08-01 15:22:52 -0500 | [diff] [blame] | 26 | "this is test\nthis is test2\ntest case" "" |
Ashwini Sharma | 577377c | 2013-08-01 01:52:32 -0500 | [diff] [blame] | 27 | |
| 28 | echo -e "this is test\nthis is test2\ntest case" > foo |
| 29 | echo -e "hello this is test" > foo2 |
| 30 | echo -e "hi hello" > foo3 |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 31 | testing "-H" "grep -H is foo foo2 foo3" "foo:this is test\nfoo:this is test2\nfoo2:hello this is test\n" "" "" |
Rob Landley | 3595eff | 2013-08-01 15:22:52 -0500 | [diff] [blame] | 32 | rm foo foo2 foo3 |
Ashwini Sharma | 577377c | 2013-08-01 01:52:32 -0500 | [diff] [blame] | 33 | |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 34 | testing "-b" "grep -b is input" "0:this is test\n13:this is test2\n" \ |
Rob Landley | 3595eff | 2013-08-01 15:22:52 -0500 | [diff] [blame] | 35 | "this is test\nthis is test2\ntest case" "" |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 36 | testing "-i" "grep -i is input" "thisIs test\nthis is test2\n" \ |
Rob Landley | 3595eff | 2013-08-01 15:22:52 -0500 | [diff] [blame] | 37 | "thisIs test\nthis is test2\ntest case" "" |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 38 | testing "-n" "grep -n is input" "1:this is test\n2:this is test2\n" \ |
Rob Landley | 3595eff | 2013-08-01 15:22:52 -0500 | [diff] [blame] | 39 | "this is test\nthis is test2\ntest case" "" |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 40 | testing "-o" "grep -o is input" "is\nis\nis\nis\n" \ |
Rob Landley | 3595eff | 2013-08-01 15:22:52 -0500 | [diff] [blame] | 41 | "this is test\nthis is test2\ntest case" "" |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 42 | testing "-s" "grep -hs hello asdf input 2>&1" "hello\n" "hello\n" "" |
| 43 | testing "-v" "grep -v abc input" "1234123asdfas123123\n1ABa\n" \ |
Rob Landley | 3595eff | 2013-08-01 15:22:52 -0500 | [diff] [blame] | 44 | "1234123asdfas123123\n1ABabc\nabc\n1ABa\nabcde" "" |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 45 | testing "-w" "grep -w abc input" "abc\n123 abc\nabc 123\n123 abc 456\n" \ |
Rob Landley | 73e3a64 | 2015-11-01 02:33:51 -0600 | [diff] [blame] | 46 | "1234123asdfas123123\n1ABabc\nabc\n1ABa\nabcde\n123 abc\nabc 123\n123 abc 456\n" "" |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 47 | testing "-x" "grep -x abc input" "abc\n" \ |
Rob Landley | 7b72ab4 | 2013-08-04 15:04:08 -0500 | [diff] [blame] | 48 | "aabcc\nabc\n" "" |
| 49 | |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 50 | testing "-H (standard input)" "grep -H abc" "(standard input):abc\n" \ |
Rob Landley | 7b72ab4 | 2013-08-04 15:04:08 -0500 | [diff] [blame] | 51 | "" "abc\n" |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 52 | testing "-l (standard input)" "grep -l abc" "(standard input)\n" \ |
Rob Landley | 7b72ab4 | 2013-08-04 15:04:08 -0500 | [diff] [blame] | 53 | "" "abc\n" |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 54 | testing "-n two inputs" "grep -hn def - input" "2:def\n2:def\n" \ |
Rob Landley | 7b72ab4 | 2013-08-04 15:04:08 -0500 | [diff] [blame] | 55 | "abc\ndef\n" "abc\ndef\n" |
| 56 | |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 57 | testing "pattern with newline" "grep 'abc |
Rob Landley | 7b72ab4 | 2013-08-04 15:04:08 -0500 | [diff] [blame] | 58 | def' input" "aabcc\nddeff\n" \ |
| 59 | "aaaa\naabcc\n\dddd\nddeff\nffff\n" "" |
| 60 | |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 61 | testing "-lH" "grep -lH abc input" "input\n" "abc\n" "" |
| 62 | testing "-cn" "grep -cn abc input" "1\n" "abc\n" "" |
| 63 | testing "-cH" "grep -cH abc input" "input:1\n" "abc\n" "" |
| 64 | testing "-qs" "grep -qs abc none input && echo yes" "yes\n" "abc\n" "" |
| 65 | testing "-hl" "grep -hl abc input" "input\n" "abc\n" "" |
| 66 | testing "-b stdin" "grep -b one" "0:one\n4:one\n8:one\n" "" "one\none\none\n" |
| 67 | testing "-o overlap" "grep -bo aaa" "1:aaa\n" "" "baaaa\n" |
Rob Landley | 9f088a1 | 2013-08-12 04:28:03 -0500 | [diff] [blame] | 68 | # nonobvious: -co counts lines, not matches |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 69 | testing "-co" "grep -co one input" "1\n" "one one one\n" "" |
| 70 | testing "-nom" "grep -nom 2 one" "1:one\n1:one\n1:one\n2:one\n2:one\n" \ |
Rob Landley | 7b72ab4 | 2013-08-04 15:04:08 -0500 | [diff] [blame] | 71 | "" "one one one\none one\none" |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 72 | testing "-vo" "grep -vo one input" "two\nthree\n" "onetwoonethreeone\n" "" |
| 73 | testing "no newline" "grep -h one input -" \ |
Rob Landley | 7b72ab4 | 2013-08-04 15:04:08 -0500 | [diff] [blame] | 74 | "hello one\nthere one\n" "hello one" "there one" |
Rob Landley | 133cc5b | 2013-08-11 01:03:26 -0500 | [diff] [blame] | 75 | |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 76 | testing "-e multi" "grep -e one -ethree input" \ |
Rob Landley | 133cc5b | 2013-08-11 01:03:26 -0500 | [diff] [blame] | 77 | "three\none\n" "three\ntwo\none\n" "" |
| 78 | # Suppress filenames for recursive test because dunno order they'd occur in |
| 79 | mkdir sub |
| 80 | echo -e "one\ntwo\nthree" > sub/one |
| 81 | echo -e "three\ntwo\none" > sub/two |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 82 | testing "-hr" "grep -hr one sub" "one\none\n" "" "" |
| 83 | testing "-r file" "grep -r three sub/two" "three\n" "" "" |
| 84 | testing "-r dir" "grep -r one sub | sort" "sub/one:one\nsub/two:one\n" \ |
Rob Landley | fce85e9 | 2013-08-19 03:17:51 -0500 | [diff] [blame] | 85 | "" "" |
Rob Landley | 133cc5b | 2013-08-11 01:03:26 -0500 | [diff] [blame] | 86 | rm -rf sub |
| 87 | |
Rob Landley | 73e3a64 | 2015-11-01 02:33:51 -0600 | [diff] [blame] | 88 | # -x exact match trumps -F's "empty string matches whole line" behavior |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 89 | testing "-Fx ''" "grep -Fx '' input" "" "one one one\n" "" |
| 90 | testing "-F ''" "grep -F '' input" "one one one\n" "one one one\n" "" |
| 91 | testing "-F -e blah -e ''" "grep -F -e blah -e '' input" "one one one\n" \ |
Rob Landley | fce85e9 | 2013-08-19 03:17:51 -0500 | [diff] [blame] | 92 | "one one one\n" "" |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 93 | testing "-e blah -e ''" "grep -e blah -e '' input" "one one one\n" \ |
Rob Landley | fce85e9 | 2013-08-19 03:17:51 -0500 | [diff] [blame] | 94 | "one one one\n" "" |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 95 | testing "-w ''" "grep -w '' input" "" "one one one\n" "" |
| 96 | testing "-w '' 2" "grep -w '' input" "one two\n" "one two\n" "" |
| 97 | testing "-w \\1" "grep -wo '\\(x\\)\\1'" "xx\n" "" "xx" |
| 98 | testing "-o ''" "grep -o '' input" "" "one one one\n" "" |
| 99 | testing "backref" 'grep -e "a\(b\)" -e "b\(c\)\1"' "bcc\nab\n" \ |
Rob Landley | fce85e9 | 2013-08-19 03:17:51 -0500 | [diff] [blame] | 100 | "" "bcc\nbcb\nab\n" |
Rob Landley | b97d821 | 2016-01-30 18:28:39 -0600 | [diff] [blame] | 101 | |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 102 | testing "-A" "grep -A 2 yes" "yes\nno\nno\n--\nyes\nno\nno\nyes\nno\n" \ |
Rob Landley | b97d821 | 2016-01-30 18:28:39 -0600 | [diff] [blame] | 103 | "" "yes\nno\nno\nno\nyes\nno\nno\nyes\nno" |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 104 | testing "-B" "grep -B 1 yes" "no\nyes\n--\nno\nyes\nno\nyes\n" \ |
Rob Landley | 36aed25 | 2016-01-30 22:07:58 -0600 | [diff] [blame] | 105 | "" "no\nno\nno\nyes\nno\nno\nyes\nno\nyes" |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 106 | testing "-C" "grep -C 1 yes" \ |
Rob Landley | 36aed25 | 2016-01-30 22:07:58 -0600 | [diff] [blame] | 107 | "yes\nno\n--\nno\nyes\nno\nno\nyes\nno\nyes\nno\n" \ |
| 108 | "" "yes\nno\nno\nno\nyes\nno\nno\nyes\nno\nyes\nno\nno" |
Rob Landley | 336c44a | 2016-03-02 15:20:04 -0600 | [diff] [blame] | 109 | testing "-HnC" "grep -HnC1 two" \ |
Rob Landley | c02619b | 2016-02-04 14:10:36 -0600 | [diff] [blame] | 110 | "(standard input)-1-one\n(standard input):2:two\n(standard input)-3-three\n" \ |
| 111 | "" "one\ntwo\nthree" |
Rob Landley | 12adb41 | 2016-07-23 01:32:23 -0500 | [diff] [blame] | 112 | |
| 113 | # Context lines weren't showing -b |
| 114 | testing "-HnbB1" "grep -HnbB1 f input" \ |
| 115 | "input-3-8-three\ninput:4:14:four\ninput:5:19:five\n" \ |
| 116 | "one\ntwo\nthree\nfour\nfive\n" "" |
| 117 | |
| 118 | testing "-q match overrides error" \ |
| 119 | "grep -q hello missing input 2>/dev/null && echo yes" "yes\n" "hello\n" "" |
| 120 | testing "-q not found is 1" \ |
| 121 | 'grep -q hello input || echo $?' "1\n" "" "" |
| 122 | testing "-q missing is 2" \ |
| 123 | 'grep -q hello missing missing 2>/dev/null || echo $?' "2\n" "" "" |
| 124 | testing "-q missing survives exists but not found" \ |
| 125 | 'grep -q hello missing missing input 2>/dev/null || echo $?' "2\n" "" "" |
| 126 | testing "not found retained past match" \ |
| 127 | 'grep hello missing input 2>/dev/null || echo $?' \ |
| 128 | "input:hello\n2\n" "hello\n" "" |
| 129 | touch empty |
| 130 | testing "one match good enough for 0" \ |
| 131 | 'grep hello input empty && echo $?' 'input:hello\n0\n' 'hello\n' '' |
| 132 | rm empty |
| 133 | |
| 134 | testing "-o ''" "grep -o ''" "" "" "one two three\none two\none\n" |
| 135 | testing '' "grep -o -e '' -e two" "two\ntwo\n" "" \ |
| 136 | "one two three\none two\none\n" |
| 137 | |
| 138 | echo "one\ntwo\nthree" > test |
| 139 | testing "-l trumps -C" "grep -l -C1 two test input" "test\ninput\n" \ |
| 140 | "three\ntwo\none\n" "" |
| 141 | rm test |
| 142 | |
| 143 | # match after NUL byte |
| 144 | testing "match after NUL byte" "grep -a two" "one\0and two three\n" \ |
| 145 | "" 'one\0and two three' |