sed.tests 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #!/bin/sh
  2. # SUSv3 compliant sed tests.
  3. # Copyright 2005 by Rob Landley <rob@landley.net>
  4. # Licensed under GPL v2, see file LICENSE for details.
  5. . ./testing.sh
  6. # testing "description" "arguments" "result" "infile" "stdin"
  7. # Corner cases
  8. testing "sed no files (stdin)" 'sed ""' "hello\n" "" "hello\n"
  9. testing "sed explicit stdin" 'sed "" -' "hello\n" "" "hello\n"
  10. testing "sed handles empty lines" "sed -e 's/\$/@/'" "@\n" "" "\n"
  11. testing "sed stdin twice" 'sed "" - -' "hello" "" "hello"
  12. # Trailing EOF.
  13. # Match $, at end of each file or all files?
  14. # -e corner cases
  15. # without -e
  16. # multiple -e
  17. # interact with a
  18. # -eee arg1 arg2 arg3
  19. # -f corner cases
  20. # -e -f -e
  21. # -n corner cases
  22. # no newline at EOF?
  23. # -r corner cases
  24. # Just make sure it works.
  25. # -i corner cases:
  26. # sed -i -
  27. # permissions
  28. # -i on a symlink
  29. # on a directory
  30. # With $ last-line test
  31. # Continue with \
  32. # End of script with trailing \
  33. # command list
  34. testing "sed accepts blanks before command" "sed -e '1 d'" "" "" ""
  35. testing "sed accepts newlines in -e" "sed -e 'i\
  36. 1
  37. a\
  38. 3'" "1\n2\n3\n" "" "2\n"
  39. testing "sed accepts multiple -e" "sed -e 'i\' -e '1' -e 'a\' -e '3'" \
  40. "1\n2\n3\n" "" "2\n"
  41. # substitutions
  42. testing "sed -n" "sed -n -e s/foo/bar/ -e s/bar/baz/" "" "" "foo\n"
  43. testing "sed s//p" "sed -e s/foo/bar/p -e s/bar/baz/p" "bar\nbaz\nbaz\n" \
  44. "" "foo\n"
  45. testing "sed -n s//p" "sed -ne s/abc/def/p" "def\n" "" "abc\n"
  46. test x"$SKIP_KNOWN_BUGS" = x"" && {
  47. testing "sed s//g (exhaustive)" "sed -e 's/[[:space:]]*/,/g'" ",1,2,3,4,5,\n" \
  48. "" "12345\n"
  49. }
  50. testing "sed s arbitrary delimiter" "sed -e 's woo boing '" "boing\n" "" "woo\n"
  51. testing "sed s chains" "sed -e s/foo/bar/ -e s/bar/baz/" "baz\n" "" "foo\n"
  52. testing "sed s chains2" "sed -e s/foo/bar/ -e s/baz/nee/" "bar\n" "" "foo\n"
  53. testing "sed s [delimiter]" "sed -e 's@[@]@@'" "onetwo" "" "one@two"
  54. testing "sed s with \\t (GNU ext)" "sed 's/\t/ /'" "one two" "" "one\ttwo"
  55. # branch
  56. testing "sed b (branch)" "sed -e 'b one;p;: one'" "foo\n" "" "foo\n"
  57. testing "sed b (branch with no label jumps to end)" "sed -e 'b;p'" \
  58. "foo\n" "" "foo\n"
  59. # test and branch
  60. testing "sed t (test/branch)" "sed -e 's/a/1/;t one;p;: one;p'" \
  61. "1\n1\nb\nb\nb\nc\nc\nc\n" "" "a\nb\nc\n"
  62. testing "sed t (test/branch clears test bit)" "sed -e 's/a/b/;:loop;t loop'" \
  63. "b\nb\nc\n" "" "a\nb\nc\n"
  64. testing "sed T (!test/branch)" "sed -e 's/a/1/;T notone;p;: notone;p'" \
  65. "1\n1\n1\nb\nb\nc\nc\n" "" "a\nb\nc\n"
  66. test x"$SKIP_KNOWN_BUGS" = x"" && {
  67. # Normal sed end-of-script doesn't print "c" because n flushed the pattern
  68. # space. If n hits EOF, pattern space is empty when script ends.
  69. # Query: how does this interact with no newline at EOF?
  70. testing "sed n (flushes pattern space, terminates early)" "sed -e 'n;p'" \
  71. "a\nb\nb\nc\n" "" "a\nb\nc\n"
  72. # N does _not_ flush pattern space, therefore c is still in there @ script end.
  73. testing "sed N (doesn't flush pattern space when terminating)" "sed -e 'N;p'" \
  74. "a\nb\na\nb\nc\n" "" "a\nb\nc\n"
  75. }
  76. testing "sed address match newline" 'sed "/b/N;/b\\nc/i woo"' \
  77. "a\nwoo\nb\nc\nd\n" "" "a\nb\nc\nd\n"
  78. # Multiple lines in pattern space
  79. testing "sed N (stops at end of input) and P (prints to first newline only)" \
  80. "sed -n 'N;P;p'" "a\na\nb\n" "" "a\nb\nc\n"
  81. # Hold space
  82. testing "sed G (append hold space to pattern space)" 'sed G' "a\n\nb\n\nc\n\n" \
  83. "" "a\nb\nc\n"
  84. #testing "sed g/G (swap/append hold and patter space)"
  85. #testing "sed g (swap hold/pattern space)"
  86. testing "sed d ends script iteration" \
  87. "sed -e '/ook/d;s/ook/ping/p;i woot'" "" "" "ook\n"
  88. testing "sed d ends script iteration (2)" \
  89. "sed -e '/ook/d;a\' -e 'bang'" "woot\nbang\n" "" "ook\nwoot\n"
  90. # Multiple files, with varying newlines and NUL bytes
  91. test x"$SKIP_KNOWN_BUGS" = x"" && {
  92. testing "sed embedded NUL" "sed -e 's/woo/bang/'" "\0bang\0woo\0" "" \
  93. "\0woo\0woo\0"
  94. }
  95. testing "sed embedded NUL g" "sed -e 's/woo/bang/g'" "bang\0bang\0" "" \
  96. "woo\0woo\0"
  97. test x"$SKIP_KNOWN_BUGS" = x"" && {
  98. $ECHO -e "/woo/a he\0llo" > sed.commands
  99. testing "sed NUL in command" "sed -f sed.commands" "woo\nhe\0llo\n" "" "woo"
  100. rm sed.commands
  101. }
  102. # sed has funky behavior with newlines at the end of file. Test lots of
  103. # corner cases with the optional newline appending behavior.
  104. testing "sed normal newlines" "sed -e 's/woo/bang/' input -" "bang\nbang\n" \
  105. "woo\n" "woo\n"
  106. testing "sed leave off trailing newline" "sed -e 's/woo/bang/' input -" \
  107. "bang\nbang" "woo\n" "woo"
  108. testing "sed autoinsert newline" "sed -e 's/woo/bang/' input -" "bang\nbang" \
  109. "woo" "woo"
  110. testing "sed empty file plus cat" "sed -e 's/nohit//' input -" "one\ntwo" \
  111. "" "one\ntwo"
  112. testing "sed cat plus empty file" "sed -e 's/nohit//' input -" "one\ntwo" \
  113. "one\ntwo" ""
  114. test x"$SKIP_KNOWN_BUGS" = x"" && {
  115. testing "sed append autoinserts newline" "sed -e '/woot/a woo' -" \
  116. "woot\nwoo\n" "" "woot"
  117. }
  118. testing "sed insert doesn't autoinsert newline" "sed -e '/woot/i woo' -" \
  119. "woo\nwoot" "" "woot"
  120. testing "sed print autoinsert newlines" "sed -e 'p' -" "one\none" "" "one"
  121. testing "sed print autoinsert newlines two files" "sed -e 'p' input -" \
  122. "one\none\ntwo\ntwo" "one" "two"
  123. testing "sed noprint, no match, no newline" "sed -ne 's/woo/bang/' input" \
  124. "" "no\n" ""
  125. testing "sed selective matches with one nl" "sed -ne 's/woo/bang/p' input -" \
  126. "a bang\nc bang\n" "a woo\nb no" "c woo\nd no"
  127. testing "sed selective matches insert newline" \
  128. "sed -ne 's/woo/bang/p' input -" "a bang\nb bang\nd bang" \
  129. "a woo\nb woo" "c no\nd woo"
  130. testing "sed selective matches noinsert newline" \
  131. "sed -ne 's/woo/bang/p' input -" "a bang\nb bang" "a woo\nb woo" \
  132. "c no\nd no"
  133. test x"$SKIP_KNOWN_BUGS" = x"" && {
  134. testing "sed clusternewline" \
  135. "sed -e '/one/a 111' -e '/two/i 222' -e p input -" \
  136. "one\none\n111\n222\ntwo\ntwo" "one" "two"
  137. }
  138. testing "sed subst+write" \
  139. "sed -e 's/i/z/' -e 'woutputw' input -; $ECHO -n X; cat outputw" \
  140. "thzngy\nagaznXthzngy\nagazn" "thingy" "again"
  141. rm outputw
  142. testing "sed trailing NUL" \
  143. "sed 's/i/z/' input -" \
  144. "a\0b\0\nc" "a\0b\0" "c"
  145. testing "sed escaped newline in command" \
  146. "sed 's/a/z\\
  147. z/' input" \
  148. "z\nz" "a" ""
  149. # Test end-of-file matching behavior
  150. testing "sed match EOF" "sed -e '"'$p'"'" "hello\nthere\nthere" "" \
  151. "hello\nthere"
  152. testing "sed match EOF two files" "sed -e '"'$p'"' input -" \
  153. "one\ntwo\nthree\nfour\nfour" "one\ntwo" "three\nfour"
  154. # sed match EOF inline: gnu sed 4.1.5 outputs this:
  155. #00000000 6f 6e 65 0a 6f 6f 6b 0a 6f 6f 6b 0a 74 77 6f 0a |one.ook.ook.two.|
  156. #00000010 0a 74 68 72 65 65 0a 6f 6f 6b 0a 6f 6f 6b 0a 66 |.three.ook.ook.f|
  157. #00000020 6f 75 72 |our|
  158. # which looks buggy to me.
  159. $ECHO -ne "three\nfour" > input2
  160. testing "sed match EOF inline" \
  161. "sed -e '"'$i ook'"' -i input input2 && cat input input2" \
  162. "one\nook\ntwothree\nook\nfour" "one\ntwo" ""
  163. rm input2
  164. # Test lie-to-autoconf
  165. testing "sed lie-to-autoconf" "sed --version | grep -o 'GNU sed version '" \
  166. "GNU sed version \n" "" ""
  167. # Jump to nonexistent label
  168. test x"$SKIP_KNOWN_BUGS" = x"" && {
  169. # Incompatibility: illegal jump is not detected if input is ""
  170. # (that is, no lines at all). GNU sed 4.1.5 complains even in this case
  171. testing "sed nonexistent label" "sed -e 'b walrus' 2>/dev/null || echo yes" \
  172. "yes\n" "" ""
  173. }
  174. testing "sed backref from empty s uses range regex" \
  175. "sed -e '/woot/s//eep \0 eep/'" "eep woot eep" "" "woot"
  176. testing "sed backref from empty s uses range regex with newline" \
  177. "sed -e '/woot/s//eep \0 eep/'" "eep woot eep\n" "" "woot\n"
  178. # -i with no filename
  179. touch ./- # Detect gnu failure mode here.
  180. testing "sed -i with no arg [GNUFAIL]" "sed -e '' -i 2> /dev/null || echo yes" \
  181. "yes\n" "" ""
  182. rm ./- # Clean up
  183. testing "sed s/xxx/[/" "sed -e 's/xxx/[/'" "[\n" "" "xxx\n"
  184. # Ponder this a bit more, why "woo not found" from gnu version?
  185. #testing "sed doesn't substitute in deleted line" \
  186. # "sed -e '/ook/d;s/ook//;t woo;a bang;'" "bang" "" "ook\n"
  187. # This makes both seds very unhappy. Why?
  188. #testing "sed -g (exhaustive)" "sed -e 's/[[:space:]]*/,/g'" ",1,2,3,4,5," \
  189. # "" "12345"
  190. # testing "description" "arguments" "result" "infile" "stdin"
  191. testing "sed n command must reset 'substituted' bit" \
  192. "sed 's/1/x/;T;n;: next;s/3/y/;t quit;n;b next;: quit;q'" \
  193. "0\nx\n2\ny\n" "" "0\n1\n2\n3\n"
  194. testing "sed d does not break n,m matching" \
  195. "sed -n '1d;1,3p'" \
  196. "second\nthird\n" "" "first\nsecond\nthird\nfourth\n"
  197. testing "sed d does not break n,regex matching" \
  198. "sed -n '1d;1,/hir/p'" \
  199. "second\nthird\n" "" "first\nsecond\nthird\nfourth\n"
  200. testing "sed d does not break n,regex matching #2" \
  201. "sed -n '1,5d;1,/hir/p'" \
  202. "second2\nthird2\n" "" \
  203. "first\nsecond\nthird\nfourth\n""first2\nsecond2\nthird2\nfourth2\n"
  204. testing "sed 2d;2,1p (gnu compat)" \
  205. "sed -n '2d;2,1p'" \
  206. "third\n" "" \
  207. "first\nsecond\nthird\nfourth\n"
  208. # Regex means: "match / at BOL or nothing, then one or more not-slashes".
  209. # The bug was that second slash in /usr/lib was treated as "at BOL" too.
  210. testing "sed beginning (^) matches only once" \
  211. "sed 's,\(^/\|\)[^/][^/]*,>\0<,g'" \
  212. ">/usr</>lib<\n" "" \
  213. "/usr/lib\n"
  214. testing "sed c" \
  215. "sed 'crepl'" \
  216. "repl\nrepl\n" "" \
  217. "first\nsecond\n"
  218. testing "sed nested {}s" \
  219. "sed '/asd/ { p; /s/ { s/s/c/ }; p; q }'" \
  220. "qwe\nasd\nacd\nacd\n" "" \
  221. "qwe\nasd\nzxc\n"
  222. testing "sed a cmd ended by double backslash" \
  223. "sed -e '/| one /a \\
  224. | three \\\\' -e '/| one-/a \\
  225. | three-* \\\\'" \
  226. ' | one \\
  227. | three \\
  228. | two \\
  229. ' '' \
  230. ' | one \\
  231. | two \\
  232. '
  233. # testing "description" "arguments" "result" "infile" "stdin"
  234. exit $FAILCOUNT