Yet more sed tests. Passing these is a to-do item for 1.1.2 or 1.2, not a
[oweals/busybox.git] / testsuite / sed.tests
1 #!/bin/sh
2
3 # SUSv3 compliant sed tests.
4 # Copyright 2005 by Rob Landley <rob@landley.net>
5 # Licensed under GPL v2, see file LICENSE for details.
6
7 [ -z "$COMMAND" ] && COMMAND=sed
8 . testing.sh
9
10 # testing "description" "arguments" "result" "infile" "stdin"
11
12 # Corner cases
13 testing "sed no files (stdin)" '""' "hello\n" "" "hello\n"
14 testing "sed explicit stdin" '"" -' "hello\n" "" "hello\n"
15 testing "sed handles empty lines" "-e 's/\$/@/'" "@\n" "" "\n"
16 testing "sed stdin twice" '"" - -' "hello" "" "hello"
17
18 # Trailing EOF.
19 #       Match $, at end of each file or all files?
20
21 # -e corner cases
22 #       without -e
23 #       multiple -e
24 #               interact with a
25 #       -eee arg1 arg2 arg3
26 # -f corner cases
27 #       -e -f -e
28 # -n corner cases
29 #       no newline at EOF?
30 # -r corner cases
31 #       Just make sure it works.
32 # -i corner cases:
33 #       sed -i -
34 #       permissions
35 #       -i on a symlink
36 #       on a directory
37 #       With $ last-line test
38 # Continue with \
39 #       End of script with trailing \
40
41 # command list
42 testing "sed accepts blanks before command" "-e '1 d'" "" "" ""
43 testing "sed accepts newlines in -e" "-e 'i\
44 1
45 a\
46 3'" "1\n2\n3\n" "" "2\n"
47 testing "sed accepts multiple -e" "-e 'i\' -e '1' -e 'a\' -e '3'" \
48         "1\n2\n3\n" "" "2\n"
49
50 # substitutions
51 testing "sed -n" "-n -e s/foo/bar/ -e s/bar/baz/" "" "" "foo\n"
52 testing "sed s//p" "-e s/foo/bar/p -e s/bar/baz/p" "bar\nbaz\nbaz\n" \
53         "" "foo\n"
54 testing "sed -n s//p" "-ne s/abc/def/p" "def\n" "" "abc\n"
55 testing "sed s//g (exhaustive)" "-e 's/[[:space:]]*/,/g'" ",1,2,3,4,5,\n" \
56         "" "12345\n"
57 testing "sed s arbitrary delimiter" "-e 's woo boing '" "boing\n" "" "woo\n"
58 testing "sed s chains" "-e s/foo/bar/ -e s/bar/baz/" "baz\n" "" "foo\n"
59 testing "sed s chains2" "-e s/foo/bar/ -e s/baz/nee/" "bar\n" "" "foo\n"
60 testing "sed s [delimiter]" "-e 's@[@]@@'" "onetwo" "" "one@two"
61
62 # branch
63 testing "sed b (branch)" "-e 'b one;p;: one'" "foo\n" "" "foo\n"
64 testing "sed b (branch with no label jumps to end)" "-e 'b;p'" \
65         "foo\n" "" "foo\n"
66
67 # test and branch
68 testing "sed t (test/branch)" "-e 's/a/1/;t one;p;: one;p'" \
69         "1\n1\nb\nb\nb\nc\nc\nc\n" "" "a\nb\nc\n"
70 testing "sed t (test/branch clears test bit)" "-e 's/a/b/;:loop;t loop'" \
71         "b\nb\nc\n" "" "a\nb\nc\n"
72 testing "sed T (!test/branch)" "-e 's/a/1/;T notone;p;: notone;p'" \
73         "1\n1\n1\nb\nb\nc\nc\n" "" "a\nb\nc\n"
74
75 # Normal sed end-of-script doesn't print "c" because n flushed the pattern
76 # space.  If n hits EOF, pattern space is empty when script ends.
77 # Query: how does this interact with no newline at EOF?
78 testing "sed n (flushes pattern space, terminates early)" "-e 'n;p'" \
79         "a\nb\nb\nc\n" "" "a\nb\nc\n"
80 # N does _not_ flush pattern space, therefore c is still in there @ script end.
81 testing "sed N (doesn't flush pattern space when terminating)" "-e 'N;p'" \
82         "a\nb\na\nb\nc\n" "" "a\nb\nc\n"
83 testing "sed address match newline" '"/b/N;/b\\nc/i woo"' "a\nwoo\nb\nc\nd\n" \
84         "" "a\nb\nc\nd\n"
85
86 # Multiple lines in pattern space
87 testing "sed N (stops at end of input) and P (prints to first newline only)" \
88         "-n 'N;P;p'" "a\na\nb\n" "" "a\nb\nc\n"
89
90 # Hold space
91 testing "sed G (append hold space to pattern space)" 'G' "a\n\nb\n\nc\n\n" \
92         "" "a\nb\nc\n"
93 #testing "sed g/G (swap/append hold and patter space)"
94 #testing "sed g (swap hold/pattern space)"
95
96 testing "sed d ends script iteration" \
97         "-e '/ook/d;s/ook/ping/p;i woot'" "" "" "ook\n"
98 testing "sed d ends script iteration (2)" \
99         "-e '/ook/d;a\' -e 'bang'" "woot\nbang\n" "" "ook\nwoot\n"
100
101 # Multiple files, with varying newlines and NUL bytes
102 testing "sed embedded NUL" "-e 's/woo/bang/'" "\0bang\0woo\0" "" "\0woo\0woo\0"
103 testing "sed embedded NUL g" "-e 's/woo/bang/g'" "bang\0bang\0" "" "woo\0woo\0"
104 echo -e "/woo/a he\0llo" > sed.commands
105 testing "sed NUL in command" "-f sed.commands" "woo\nhe\0llo\n" "" "woo"
106 rm sed.commands
107
108 # sed has funky behavior with newlines at the end of file.  Test lots of
109 # corner cases with the optional newline appending behavior.
110
111 testing "sed normal newlines" "-e 's/woo/bang/' input -" "bang\nbang\n" \
112         "woo\n" "woo\n"
113 testing "sed leave off trailing newline" "-e 's/woo/bang/' input -" \
114         "bang\nbang" "woo\n" "woo"
115 testing "sed autoinsert newline" "-e 's/woo/bang/' input -" "bang\nbang" \
116         "woo" "woo"
117 testing "sed empty file plus cat" "-e 's/nohit//' input -" "one\ntwo" \
118         "" "one\ntwo" 
119 testing "sed cat plus empty file" "-e 's/nohit//' input -" "one\ntwo" \
120         "one\ntwo" ""
121 testing "sed append autoinserts newline" "-e '/woot/a woo' -" "woot\nwoo\n" \
122         "" "woot"
123 testing "sed insert doesn't autoinsert newline" "-e '/woot/i woo' -" \
124         "woo\nwoot" "" "woot"
125 testing "sed print autoinsert newlines" "-e 'p' -" "one\none" "" "one"
126 testing "sed print autoinsert newlines two files" "-e 'p' input -" \
127         "one\none\ntwo\ntwo" "one" "two"
128 testing "sed noprint, no match, no newline" "-ne 's/woo/bang/' input" \
129         "" "no\n" ""
130 testing "sed selective matches with one nl" "-ne 's/woo/bang/p' input -" \
131         "a bang\nc bang\n" "a woo\nb no" "c woo\nd no"
132 testing "sed selective matches insert newline" "-ne 's/woo/bang/p' input -" \
133         "a bang\nb bang\nd bang" "a woo\nb woo" "c no\nd woo"
134 testing "sed selective matches noinsert newline" "-ne 's/woo/bang/p' input -" \
135         "a bang\nb bang" "a woo\nb woo" "c no\nd no"
136 testing "sed clusternewline" "-e '/one/a 111' -e '/two/i 222' -e p input -" \
137         "one\none\n111\n222\ntwo\ntwo" "one" "two" 
138
139 # Test end-of-file matching behavior
140
141 testing "sed match EOF" " -e '"'$p'"'" "hello\nthere\nthere" "" "hello\nthere"
142 testing "sed match EOF two files" " -e '"'$p'"' input -" \
143         "one\ntwo\nthree\nfour\nfour" "one\ntwo" "three\nfour"
144 echo -ne "three\nfour" > input2
145 testing "sed match EOF inline" \
146         " -e '"'$i ook'"' -i input input2 && cat input input2" \
147         "one\nook\ntwothree\nook\nfour" "one\ntwo" ""
148 rm input2
149
150 # Test lie-to-autoconf
151
152 testing "sed lie-to-autoconf" "--version | grep -o 'GNU sed version '" \
153         "GNU sed version \n" "" ""
154
155 # Jump to nonexistent label
156 testing "sed nonexistent label" "-e 'b walrus' 2> /dev/null || echo yes" \
157         "yes\n" "" ""
158
159 testing "sed backref from empty s uses range regex" \
160         "-e '/woot/s//eep \0 eep/'" "eep woot eep" "" "woot"
161
162 testing "sed backref from empty s uses range regex with newline" \
163         "-e '/woot/s//eep \0 eep/'" "eep woot eep\n" "" "woot\n"
164
165 # -i with no filename
166
167 touch ./-  # Detect gnu failure mode here.
168 testing "sed -i with no arg [GNUFAIL]" "-e '' -i 2> /dev/null || echo yes" \
169         "yes\n" "" ""
170 rm ./-     # Clean up
171
172 # Ponder this a bit more, why "woo not found" from gnu version?
173 #testing "sed doesn't substitute in deleted line" \
174 #       "-e '/ook/d;s/ook//;t woo;a bang;'" "bang" "" "ook\n"
175
176 # This makes both seds very unhappy.  Why?
177 #testing "sed -g (exhaustive)" "sed -e 's/[[:space:]]*/,/g'" ",1,2,3,4,5," \
178 #       "" "12345"
179
180 exit $FAILCOUNT