| 1 |
#! /bin/sh |
| 2 |
# Test various combinations of command-line options. |
| 3 |
# |
| 4 |
# Copyright (C) 2001, 2006, 2009-2025 Free Software Foundation, Inc. |
| 5 |
# |
| 6 |
# Copying and distribution of this file, with or without modification, |
| 7 |
# are permitted in any medium without royalty provided the copyright |
| 8 |
# notice and this notice are preserved. |
| 9 |
# |
| 10 |
# This set of tests was started by Julian Foad. |
| 11 |
|
| 12 |
. "${srcdir=.}/init.sh"; path_prepend_ ../src |
| 13 |
|
| 14 |
failures=0 |
| 15 |
|
| 16 |
# grep_test INPUT EXPECTED_OUTPUT PATTERN_AND_OPTIONS... |
| 17 |
# Run "grep" with the given INPUT, pattern and options, and check that |
| 18 |
# the output is EXPECTED_OUTPUT. If not, print a message and set 'failures'. |
| 19 |
# "/" represents a newline within INPUT and EXPECTED_OUTPUT. |
| 20 |
grep_test () |
| 21 |
{ |
| 22 |
INPUT="$1" |
| 23 |
EXPECT="$2" |
| 24 |
shift 2 |
| 25 |
OUTPUT=$(printf %s "$INPUT" | tr "/" "\n" | grep "$@" | tr "\n" "/") |
| 26 |
if test "$OUTPUT" != "$EXPECT" || test "$VERBOSE" = "yes"; then |
| 27 |
echo "Testing: grep $@" |
| 28 |
test "$LC_ALL" != C && test "$LC_ALL" != "" && echo " LC_ALL: \"$LC_ALL\"" |
| 29 |
echo " input: \"$INPUT\"" |
| 30 |
echo " output: \"$OUTPUT\"" |
| 31 |
fi |
| 32 |
if test "$OUTPUT" != "$EXPECT"; then |
| 33 |
echo " expect: \"$EXPECT\"" |
| 34 |
echo "FAIL" |
| 35 |
failures=1 |
| 36 |
fi |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
# Test "--only-matching" ("-o") option |
| 41 |
|
| 42 |
# "-o" with "-i" should output an exact copy of the matching input text. |
| 43 |
grep_test "WordA/wordB/WORDC/" "Word/word/WORD/" "word" -o -i |
| 44 |
grep_test "WordA/wordB/WORDC/" "Word/word/WORD/" "Word" -o -i |
| 45 |
grep_test "WordA/wordB/WORDC/" "Word/word/WORD/" "WORD" -o -i |
| 46 |
|
| 47 |
# Should display the line number (-n), octet offset (-b), or file name |
| 48 |
# (-H) of every match, not just of the first match on each input line. |
| 49 |
# Check it both with and without -i because of the separate code paths. |
| 50 |
# Also check what it does when lines of context are specified. |
| 51 |
grep_test "wA wB/wC/" "1:wA/1:wB/2:wC/" "w." -o -n |
| 52 |
grep_test "wA wB/wC/" "1:wA/1:wB/2:wC/" "w." -o -n -i |
| 53 |
grep_test "wA wB/wC/" "1:wA/1:wB/2:wC/" "w." -o -n -3 2>/dev/null |
| 54 |
grep_test "XwA YwB/ZwC/" "1:wA/5:wB/9:wC/" "w." -o -b |
| 55 |
grep_test "XwA YwB/ZwC/" "1:wA/5:wB/9:wC/" "w." -o -b -i |
| 56 |
grep_test "XwA YwB/ZwC/" "1:wA/5:wB/9:wC/" "w." -o -b -3 2>/dev/null |
| 57 |
grep_test "XwA YwB/ZwC/" "1:w/5:w/9:w/" "w" -F -o -b |
| 58 |
grep_test "XwA YwB/ZwC/" "1:w/5:w/9:w/" "w" -F -o -b -i |
| 59 |
grep_test "XwA YwB/ZwC/" "1:w/5:w/9:w/" "w" -F -o -b -3 2>/dev/null |
| 60 |
grep_test "wA wB/" "(standard input):wA/(standard input):wB/" "w." -o -H |
| 61 |
grep_test "wA wB/" "(standard input):wA/(standard input):wB/" "w." -o -H -i |
| 62 |
grep_test "wA wB/" "(standard input):wA/(standard input):wB/" "w." -o -H -3 2>/dev/null |
| 63 |
|
| 64 |
# Combination of -h and -H |
| 65 |
grep_test "wA wB/" "wA wB/" "w." |
| 66 |
grep_test "wA wB/" "wA wB/" "w." -h |
| 67 |
grep_test "wA wB/" "wA wB/" "w." -H -h |
| 68 |
grep_test "wA wB/" "(standard input):wA wB/" "w." -H |
| 69 |
grep_test "wA wB/" "(standard input):wA wB/" "w." -h -H |
| 70 |
|
| 71 |
# End of a previous match should not match a "start of ..." expression. |
| 72 |
grep_test "word_word/" "word_/" "^word_*" -o |
| 73 |
grep_test "wordword/" "word/" "\<word" -o |
| 74 |
|
| 75 |
|
| 76 |
# Test "--color" option |
| 77 |
|
| 78 |
CB="[01;31m[K" |
| 79 |
CE="[m[K" |
| 80 |
|
| 81 |
# "--color" with "-i" should output an exact copy of the matching input text. |
| 82 |
grep_test "WordA/wordb/WORDC/" "${CB}Word${CE}A/${CB}word${CE}b/${CB}WORD${CE}C/" "word" --color=always -i |
| 83 |
grep_test "WordA/wordb/WORDC/" "${CB}Word${CE}A/${CB}word${CE}b/${CB}WORD${CE}C/" "Word" --color=always -i |
| 84 |
grep_test "WordA/wordb/WORDC/" "${CB}Word${CE}A/${CB}word${CE}b/${CB}WORD${CE}C/" "WORD" --color=always -i |
| 85 |
|
| 86 |
# End of a previous match should not match a "start of ..." expression. |
| 87 |
grep_test "word_word/" "${CB}word_${CE}word/" "^word_*" --color=always |
| 88 |
grep_test "wordword/" "${CB}word${CE}word/" "\<word" --color=always |
| 89 |
|
| 90 |
|
| 91 |
# Test combination of "-m" with "-A" and anchors. |
| 92 |
# Based on a report from Pavol Gono. |
| 93 |
grep_test "4/40/" "4/40/" "^4$" -m1 -A99 |
| 94 |
grep_test "4/04/" "4/04/" "^4$" -m1 -A99 |
| 95 |
grep_test "4/444/" "4/444/" "^4$" -m1 -A99 |
| 96 |
grep_test "4/40/" "4/40/" "^4" -m1 -A99 |
| 97 |
grep_test "4/04/" "4/04/" "^4" -m1 -A99 |
| 98 |
grep_test "4/444/" "4/444/" "^4" -m1 -A99 |
| 99 |
grep_test "4/40/" "4/40/" "4$" -m1 -A99 |
| 100 |
grep_test "4/04/" "4/04/" "4$" -m1 -A99 |
| 101 |
grep_test "4/444/" "4/444/" "4$" -m1 -A99 |
| 102 |
|
| 103 |
|
| 104 |
# Test for "-F -w" bugs. Thanks to Gordon Lack for these two. |
| 105 |
grep_test "A/CX/B/C/" "A/B/C/" -wF -e A -e B -e C |
| 106 |
grep_test "LIN7C 55327/" "" -wF -e 5327 -e 5532 |
| 107 |
|
| 108 |
# Test for non-empty matches following empty ones. |
| 109 |
grep_test 'xyz/' 'y/' -o 'y*' |
| 110 |
grep_test 'xyz/' "x${CB}y${CE}z/" --color=always 'y*' |
| 111 |
|
| 112 |
# Test for increasing/decreasing-length word matches, |
| 113 |
# for independence from pattern order within the pattern list, |
| 114 |
# and for preferring the longest match at a given position. |
| 115 |
x0='a bb ccc dddd/' |
| 116 |
x1='dddd ccc bb a/' |
| 117 |
x2='bcd abcd abc bc bcd abc/' |
| 118 |
x3='bc abcd bc/' |
| 119 |
y0="a ${CB}bb${CE} ${CB}ccc${CE} dddd/" |
| 120 |
y1="dddd ${CB}ccc${CE} ${CB}bb${CE} a/" |
| 121 |
y2="bcd abcd abc ${CB}bc${CE} bcd abc/" |
| 122 |
y3="${CB}bc${CE} abcd ${CB}bc${CE}/" |
| 123 |
grep_test "$x0" "$y0" -E --color=always -e bb -e cc -e ccc |
| 124 |
grep_test "$x0" "$y0" -F --color=always -e bb -e cc -e ccc |
| 125 |
grep_test "$x0" "$y0" -E --color=always -e bb -e ccc -e cc |
| 126 |
grep_test "$x0" "$y0" -F --color=always -e bb -e ccc -e cc |
| 127 |
grep_test "$x0" "$y0" -E -w --color=always -e bb -e ccc |
| 128 |
grep_test "$x0" "$y0" -F -w --color=always -e bb -e ccc |
| 129 |
grep_test "$x0" "$y0" -E -w --color=always -e ccc -e bb |
| 130 |
grep_test "$x0" "$y0" -F -w --color=always -e ccc -e bb |
| 131 |
grep_test "$x1" "$y1" -E -w --color=always -e bb -e ccc |
| 132 |
grep_test "$x1" "$y1" -F -w --color=always -e bb -e ccc |
| 133 |
grep_test "$x1" "$y1" -E -w --color=always -e ccc -e bb |
| 134 |
grep_test "$x1" "$y1" -F -w --color=always -e ccc -e bb |
| 135 |
grep_test "$x2" "$y2" -E -w --color=always bc |
| 136 |
grep_test "$x2" "$y2" -F -w --color=always bc |
| 137 |
grep_test "$x3" "$y3" -E -w --color=always bc |
| 138 |
grep_test "$x3" "$y3" -F -w --color=always bc |
| 139 |
|
| 140 |
# Bug#25655 |
| 141 |
grep_test .tar/ .tar/ -e '\.tar' -e '\.tbz' |
| 142 |
grep_test .tar/ .tar/ -o -e '\.tar' -e 'tar' |
| 143 |
grep_test '$*.[^\/' '$*.[^\/' -o -e '\$\*\.\[\^\\' -e abc |
| 144 |
grep_test '$*.[^\/(+?{|/' '$*.[^\/(+?{|/' -o -E \ |
| 145 |
-e '\$\*\.\[\^\\' -e '\(\+\?\{\|' |
| 146 |
|
| 147 |
# Skip the rest of the tests - known to fail. TAA. |
| 148 |
Exit $failures |
| 149 |
|
| 150 |
# The rest of this file is meant to be executed under this locale. |
| 151 |
LC_ALL=cs_CZ.UTF-8; export LC_ALL |
| 152 |
# If the UTF-8 locale doesn't work, skip these tests silently. |
| 153 |
test "`locale charmap 2>/dev/null`" = UTF-8 || Exit $failures |
| 154 |
|
| 155 |
# Test character class erroneously matching a '[' character. |
| 156 |
grep_test "[/" "" "[[:alpha:]]" -E |
| 157 |
|
| 158 |
for mode in F G E; do |
| 159 |
# Hint: pipe the output of these tests in |
| 160 |
# "| LESS= LESSCHARSET=ascii less". |
| 161 |
# LETTER N WITH TILDE is U+00F1 and U+00D1. |
| 162 |
# LETTER Y WITH DIAERESIS is U+00FF and U+0178. |
| 163 |
grep_test 'añÿb/AÑŸB/' 'ñÿ/ÑŸ/' 'ñÿ' -o -i -$mode |
| 164 |
grep_test 'añÿb/AÑŸB/' 'ñÿ/ÑŸ/' 'ÑŸ' -o -i -$mode |
| 165 |
grep_test 'añÿb/AÑŸB/' "a${CB}ñÿ${CE}b/A${CB}ÑŸ${CE}B/" 'ñÿ' --color=always -i -$mode |
| 166 |
grep_test 'añÿb/AÑŸB/' "a${CB}ñÿ${CE}b/A${CB}ÑŸ${CE}B/" 'ÑŸ' --color=always -i -$mode |
| 167 |
|
| 168 |
# POSIX (about -i): ... each character in the string is matched |
| 169 |
# against the pattern, not only the character, but also its case |
| 170 |
# counterpart (if any), shall be matched. |
| 171 |
# The following were chosen because of their trickiness due to the |
| 172 |
# differing UTF-8 octet length of their counterpart and to the |
| 173 |
# non-reflexivity of their mapping. |
| 174 |
# Beware of homographs! Look carefully at the actual octets. |
| 175 |
|
| 176 |
# lc(U+0130 LATIN CAPITAL LETTER I WITH DOT ABOVE) = U+0069 LATIN SMALL LETTER I |
| 177 |
grep_test 'aİb/' "a${CB}İ${CE}b/" 'i' --color=always -i -$mode |
| 178 |
grep_test 'aib/' '' 'İ' --color=always -i -$mode |
| 179 |
grep_test 'aİb/' '' 'I' --color=always -i -$mode |
| 180 |
# uc(U+0131 LATIN SMALL LETTER DOTLESS I) = U+0049 LATIN CAPITAL LETTER I |
| 181 |
grep_test 'aıb/' "a${CB}ı${CE}b/" 'I' --color=always -i -$mode |
| 182 |
grep_test 'aIb/' '' 'ı' --color=always -i -$mode |
| 183 |
grep_test 'aıb/' '' 'i' --color=always -i -$mode |
| 184 |
# uc(U+017F LATIN SMALL LETTER LONG S) = U+0053 LATIN CAPITAL LETTER S |
| 185 |
grep_test 'aſb/' "a${CB}ſ${CE}b/" 'S' --color=always -i -$mode |
| 186 |
grep_test 'aSb/' '' 'ſ' --color=always -i -$mode |
| 187 |
grep_test 'aſb/' '' 's' --color=always -i -$mode |
| 188 |
# uc(U+1FBE GREEK PROSGEGRAMMENI) = U+0399 GREEK CAPITAL LETTER IOTA |
| 189 |
grep_test 'aιb/' "a${CB}ι${CE}b/" 'Ι' --color=always -i -$mode |
| 190 |
grep_test 'aΙb/' '' 'ι' --color=always -i -$mode |
| 191 |
grep_test 'aιb/' '' 'ι' --color=always -i -$mode |
| 192 |
# lc(U+2126 OHM SIGN) = U+03C9 GREEK SMALL LETTER OMEGA |
| 193 |
grep_test 'aΩb/' "a${CB}Ω${CE}b/" 'ω' --color=always -i -$mode |
| 194 |
grep_test 'aωb/' '' 'Ω' --color=always -i -$mode |
| 195 |
grep_test 'aΩb/' '' 'Ω' --color=always -i -$mode |
| 196 |
# lc(U+212A KELVIN SIGN) = U+006B LATIN SMALL LETTER K |
| 197 |
grep_test 'aKb/' "a${CB}K${CE}b/" 'k' --color=always -i -$mode |
| 198 |
grep_test 'akb/' '' 'K' --color=always -i -$mode |
| 199 |
grep_test 'aKb/' '' 'K' --color=always -i -$mode |
| 200 |
# lc(U+212B ANGSTROM SIGN) = U+00E5 LATIN SMALL LETTER A WITH RING ABOVE |
| 201 |
grep_test 'aÅb/' "a${CB}Å${CE}b/" 'å' --color=always -i -$mode |
| 202 |
grep_test 'aåb/' '' 'Å' --color=always -i -$mode |
| 203 |
grep_test 'aÅb/' '' 'Å' --color=always -i -$mode |
| 204 |
done |
| 205 |
|
| 206 |
|
| 207 |
# Any tests inserted right here will be performed under an UTF-8 locale. |
| 208 |
# Insert them before LC_ALL is set above to avoid this. |
| 209 |
# Leave this comment last. |
| 210 |
|
| 211 |
Exit $failures |