sed.tests 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. #!/bin/sh
  2. # SUSv3 compliant sed tests.
  3. # Copyright 2005 by Rob Landley <rob@landley.net>
  4. # Licensed under GPLv2, see file LICENSE in this source tree.
  5. . ./testing.sh
  6. # testing "description" "commands" "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 with empty match" "sed 's/z*//g'" "string\n" "" "string\n"
  44. testing "sed s//p" "sed -e s/foo/bar/p -e s/bar/baz/p" "bar\nbaz\nbaz\n" \
  45. "" "foo\n"
  46. testing "sed -n s//p" "sed -ne s/abc/def/p" "def\n" "" "abc\n"
  47. testing "sed s//g (exhaustive)" "sed -e 's/[[:space:]]*/,/g'" ",1,2,3,4,5,\n" \
  48. "" "12345\n"
  49. testing "sed s arbitrary delimiter" "sed -e 's woo boing '" "boing\n" "" "woo\n"
  50. testing "sed s chains" "sed -e s/foo/bar/ -e s/bar/baz/" "baz\n" "" "foo\n"
  51. testing "sed s chains2" "sed -e s/foo/bar/ -e s/baz/nee/" "bar\n" "" "foo\n"
  52. testing "sed s [delimiter]" "sed -e 's@[@]@@'" "onetwo" "" "one@two"
  53. testing "sed s with \\t (GNU ext)" "sed 's/\t/ /'" "one two" "" "one\ttwo"
  54. # branch
  55. testing "sed b (branch)" "sed -e 'b one;p;: one'" "foo\n" "" "foo\n"
  56. testing "sed b (branch with no label jumps to end)" "sed -e 'b;p'" \
  57. "foo\n" "" "foo\n"
  58. # test and branch
  59. testing "sed t (test/branch)" "sed -e 's/a/1/;t one;p;: one;p'" \
  60. "1\n1\nb\nb\nb\nc\nc\nc\n" "" "a\nb\nc\n"
  61. testing "sed t (test/branch clears test bit)" "sed -e 's/a/b/;:loop;t loop'" \
  62. "b\nb\nc\n" "" "a\nb\nc\n"
  63. testing "sed T (!test/branch)" "sed -e 's/a/1/;T notone;p;: notone;p'" \
  64. "1\n1\n1\nb\nb\nc\nc\n" "" "a\nb\nc\n"
  65. test x"$SKIP_KNOWN_BUGS" = x"" && {
  66. # Normal sed end-of-script doesn't print "c" because n flushed the pattern
  67. # space. If n hits EOF, pattern space is empty when script ends.
  68. # Query: how does this interact with no newline at EOF?
  69. testing "sed n (flushes pattern space, terminates early)" "sed -e 'n;p'" \
  70. "a\nb\nb\nc\n" "" "a\nb\nc\n"
  71. }
  72. # non-GNU sed: N does _not_ flush pattern space, therefore c is eaten @ script end
  73. # GNU sed: N flushes pattern space, therefore c is printed too @ script end
  74. testing "sed N (flushes pattern space (GNU behavior))" "sed -e 'N;p'" \
  75. "a\nb\na\nb\nc\n" "" "a\nb\nc\n"
  76. testing "sed N test2" "sed ':a;N;s/\n/ /;ta'" \
  77. "a b c\n" "" "a\nb\nc\n"
  78. testing "sed N test3" "sed 'N;s/\n/ /'" \
  79. "a b\nc\n" "" "a\nb\nc\n"
  80. testing "sed address match newline" 'sed "/b/N;/b\\nc/i woo"' \
  81. "a\nwoo\nb\nc\nd\n" "" "a\nb\nc\nd\n"
  82. # Multiple lines in pattern space
  83. testing "sed N (stops at end of input) and P (prints to first newline only)" \
  84. "sed -n 'N;P;p'" "a\na\nb\n" "" "a\nb\nc\n"
  85. # Hold space
  86. testing "sed G (append hold space to pattern space)" 'sed G' "a\n\nb\n\nc\n\n" \
  87. "" "a\nb\nc\n"
  88. #testing "sed g/G (swap/append hold and patter space)"
  89. #testing "sed g (swap hold/pattern space)"
  90. testing "sed d ends script iteration" \
  91. "sed -e '/ook/d;s/ook/ping/p;i woot'" "" "" "ook\n"
  92. testing "sed d ends script iteration (2)" \
  93. "sed -e '/ook/d;a\' -e 'bang'" "woot\nbang\n" "" "ook\nwoot\n"
  94. # Multiple files, with varying newlines and NUL bytes
  95. test x"$SKIP_KNOWN_BUGS" = x"" && {
  96. testing "sed embedded NUL" "sed -e 's/woo/bang/'" "\0bang\0woo\0" "" \
  97. "\0woo\0woo\0"
  98. }
  99. testing "sed embedded NUL g" "sed -e 's/woo/bang/g'" "bang\0bang\0" "" \
  100. "woo\0woo\0"
  101. test x"$SKIP_KNOWN_BUGS" = x"" && {
  102. $ECHO -e "/woo/a he\0llo" > sed.commands
  103. testing "sed NUL in command" "sed -f sed.commands" "woo\nhe\0llo\n" "" "woo"
  104. rm sed.commands
  105. }
  106. # sed has funky behavior with newlines at the end of file. Test lots of
  107. # corner cases with the optional newline appending behavior.
  108. testing "sed normal newlines" "sed -e 's/woo/bang/' input -" "bang\nbang\n" \
  109. "woo\n" "woo\n"
  110. testing "sed leave off trailing newline" "sed -e 's/woo/bang/' input -" \
  111. "bang\nbang" "woo\n" "woo"
  112. testing "sed autoinsert newline" "sed -e 's/woo/bang/' input -" "bang\nbang" \
  113. "woo" "woo"
  114. testing "sed empty file plus cat" "sed -e 's/nohit//' input -" "one\ntwo" \
  115. "" "one\ntwo"
  116. testing "sed cat plus empty file" "sed -e 's/nohit//' input -" "one\ntwo" \
  117. "one\ntwo" ""
  118. testing "sed append autoinserts newline" "sed -e '/woot/a woo' -" \
  119. "woot\nwoo\n" "" "woot"
  120. testing "sed append autoinserts newline 2" "sed -e '/oot/a woo' - input" \
  121. "woot\nwoo\nboot\nwoo\n" "boot" "woot"
  122. testing "sed append autoinserts newline 3" "sed -e '/oot/a woo' -i input && cat input" \
  123. "boot\nwoo\n" "boot" ""
  124. testing "sed insert doesn't autoinsert newline" "sed -e '/woot/i woo' -" \
  125. "woo\nwoot" "" "woot"
  126. testing "sed print autoinsert newlines" "sed -e 'p' -" "one\none" "" "one"
  127. testing "sed print autoinsert newlines two files" "sed -e 'p' input -" \
  128. "one\none\ntwo\ntwo" "one" "two"
  129. testing "sed noprint, no match, no newline" "sed -ne 's/woo/bang/' input" \
  130. "" "no\n" ""
  131. testing "sed selective matches with one nl" "sed -ne 's/woo/bang/p' input -" \
  132. "a bang\nc bang\n" "a woo\nb no" "c woo\nd no"
  133. testing "sed selective matches insert newline" \
  134. "sed -ne 's/woo/bang/p' input -" "a bang\nb bang\nd bang" \
  135. "a woo\nb woo" "c no\nd woo"
  136. testing "sed selective matches noinsert newline" \
  137. "sed -ne 's/woo/bang/p' input -" "a bang\nb bang" "a woo\nb woo" \
  138. "c no\nd no"
  139. testing "sed clusternewline" \
  140. "sed -e '/one/a 111' -e '/two/i 222' -e p input -" \
  141. "one\none\n111\n222\ntwo\ntwo" "one" "two"
  142. testing "sed subst+write" \
  143. "sed -e 's/i/z/' -e 'woutputw' input -; $ECHO -n X; cat outputw" \
  144. "thzngy\nagaznXthzngy\nagazn" "thingy" "again"
  145. rm outputw
  146. testing "sed trailing NUL" \
  147. "sed 's/i/z/' input -" \
  148. "a\0b\0\nc" "a\0b\0" "c"
  149. testing "sed escaped newline in command" \
  150. "sed 's/a/z\\
  151. z/' input" \
  152. "z\nz" "a" ""
  153. # Test end-of-file matching behavior
  154. testing "sed match EOF" "sed -e '"'$p'"'" "hello\nthere\nthere" "" \
  155. "hello\nthere"
  156. testing "sed match EOF two files" "sed -e '"'$p'"' input -" \
  157. "one\ntwo\nthree\nfour\nfour" "one\ntwo" "three\nfour"
  158. # sed match EOF inline: gnu sed 4.1.5 outputs this:
  159. #00000000 6f 6e 65 0a 6f 6f 6b 0a 6f 6f 6b 0a 74 77 6f 0a |one.ook.ook.two.|
  160. #00000010 0a 74 68 72 65 65 0a 6f 6f 6b 0a 6f 6f 6b 0a 66 |.three.ook.ook.f|
  161. #00000020 6f 75 72 |our|
  162. # which looks buggy to me.
  163. $ECHO -ne "three\nfour" > input2
  164. testing "sed match EOF inline" \
  165. "sed -e '"'$i ook'"' -i input input2 && cat input input2" \
  166. "one\nook\ntwothree\nook\nfour" "one\ntwo" ""
  167. rm input2
  168. # Test lie-to-autoconf
  169. testing "sed lie-to-autoconf" "sed --version | grep -o 'GNU sed version '" \
  170. "GNU sed version \n" "" ""
  171. # Jump to nonexistent label
  172. test x"$SKIP_KNOWN_BUGS" = x"" && {
  173. # Incompatibility: illegal jump is not detected if input is ""
  174. # (that is, no lines at all). GNU sed 4.1.5 complains even in this case
  175. testing "sed nonexistent label" "sed -e 'b walrus' 2>/dev/null || echo yes" \
  176. "yes\n" "" ""
  177. }
  178. testing "sed backref from empty s uses range regex" \
  179. "sed -e '/woot/s//eep \0 eep/'" "eep woot eep" "" "woot"
  180. testing "sed backref from empty s uses range regex with newline" \
  181. "sed -e '/woot/s//eep \0 eep/'" "eep woot eep\n" "" "woot\n"
  182. # -i with no filename
  183. touch ./- # Detect gnu failure mode here.
  184. testing "sed -i with no arg [GNUFAIL]" "sed -e '' -i 2> /dev/null || echo yes" \
  185. "yes\n" "" ""
  186. rm ./- # Clean up
  187. testing "sed s/xxx/[/" "sed -e 's/xxx/[/'" "[\n" "" "xxx\n"
  188. # Ponder this a bit more, why "woo not found" from gnu version?
  189. #testing "sed doesn't substitute in deleted line" \
  190. # "sed -e '/ook/d;s/ook//;t woo;a bang;'" "bang" "" "ook\n"
  191. # This makes both seds very unhappy. Why?
  192. #testing "sed -g (exhaustive)" "sed -e 's/[[:space:]]*/,/g'" ",1,2,3,4,5," \
  193. # "" "12345"
  194. # testing "description" "commands" "result" "infile" "stdin"
  195. testing "sed n command must reset 'substituted' bit" \
  196. "sed 's/1/x/;T;n;: next;s/3/y/;t quit;n;b next;: quit;q'" \
  197. "0\nx\n2\ny\n" "" "0\n1\n2\n3\n"
  198. testing "sed d does not break n,m matching" \
  199. "sed -n '1d;1,3p'" \
  200. "second\nthird\n" "" "first\nsecond\nthird\nfourth\n"
  201. testing "sed d does not break n,regex matching" \
  202. "sed -n '1d;1,/hir/p'" \
  203. "second\nthird\n" "" "first\nsecond\nthird\nfourth\n"
  204. testing "sed d does not break n,regex matching #2" \
  205. "sed -n '1,5d;1,/hir/p'" \
  206. "second2\nthird2\n" "" \
  207. "first\nsecond\nthird\nfourth\n""first2\nsecond2\nthird2\nfourth2\n"
  208. testing "sed 2d;2,1p (gnu compat)" \
  209. "sed -n '2d;2,1p'" \
  210. "third\n" "" \
  211. "first\nsecond\nthird\nfourth\n"
  212. # Regex means: "match / at BOL or nothing, then one or more not-slashes".
  213. # The bug was that second slash in /usr/lib was treated as "at BOL" too.
  214. testing "sed beginning (^) matches only once" \
  215. "sed 's,\(^/\|\)[^/][^/]*,>\0<,g'" \
  216. ">/usr</>lib<\n" "" \
  217. "/usr/lib\n"
  218. testing "sed c" \
  219. "sed 'crepl'" \
  220. "repl\nrepl\n" "" \
  221. "first\nsecond\n"
  222. testing "sed nested {}s" \
  223. "sed '/asd/ { p; /s/ { s/s/c/ }; p; q }'" \
  224. "qwe\nasd\nacd\nacd\n" "" \
  225. "qwe\nasd\nzxc\n"
  226. testing "sed a cmd ended by double backslash" \
  227. "sed -e '/| one /a \\
  228. | three \\\\' -e '/| one-/a \\
  229. | three-* \\\\'" \
  230. ' | one \\
  231. | three \\
  232. | two \\
  233. ' '' \
  234. ' | one \\
  235. | two \\
  236. '
  237. testing "sed a cmd understands \\n,\\t,\\r" \
  238. "sed '/1/a\\\\t\\rzero\\none\\\\ntwo\\\\\\nthree'" \
  239. "\
  240. line1
  241. \t\rzero
  242. one\\\\ntwo\\
  243. three
  244. " "" "line1\n"
  245. testing "sed i cmd understands \\n,\\t,\\r" \
  246. "sed '/1/i\\\\t\\rzero\\none\\\\ntwo\\\\\\nthree'" \
  247. "\
  248. \t\rzero
  249. one\\\\ntwo\\
  250. three
  251. line1
  252. " "" "line1\n"
  253. # first three lines are deleted; 4th line is matched and printed by "2,3" and by "4" ranges
  254. testing "sed with N skipping lines past ranges on next cmds" \
  255. "sed -n '1{N;N;d};1p;2,3p;3p;4p'" \
  256. "4\n4\n" "" "1\n2\n3\n4\n"
  257. testing "sed -i with address modifies all files, not only first" \
  258. "cp input input2; sed -i -e '1s/foo/bar/' input input2 && cat input input2; rm input2" \
  259. "bar\nbar\n" "foo\n" ""
  260. testing "sed understands \r" \
  261. "sed 's/r/\r/'" \
  262. "\rrr\n" "" "rrr\n"
  263. testing "sed -i finishes ranges correctly" \
  264. "sed '1,2d' -i input; echo \$?; cat input" \
  265. "0\n3\n4\n" "1\n2\n3\n4\n" ""
  266. testing "sed zero chars match/replace advances correctly 1" \
  267. "sed 's/l*/@/g'" \
  268. "@h@e@o@\n" "" "helllo\n"
  269. testing "sed zero chars match/replace advances correctly 2" \
  270. "sed 's [^ .]* x g'" \
  271. "x x.x\n" "" " a.b\n"
  272. testing "sed zero chars match/replace logic must not falsely trigger here 1" \
  273. "sed 's/a/A/g'" \
  274. "_AAA1AA\n" "" "_aaa1aa\n"
  275. testing "sed zero chars match/replace logic must not falsely trigger here 2" \
  276. "sed 's/ *$/_/g'" \
  277. "qwerty_\n" "" "qwerty\n"
  278. testing "sed /\$_in_regex/ should not match newlines, only end-of-line" \
  279. "sed ': testcont; /\\\\$/{ =; N; b testcont }'" \
  280. "\
  281. this is a regular line
  282. 2
  283. line with \\
  284. continuation
  285. more regular lines
  286. 5
  287. line with \\
  288. continuation
  289. " \
  290. "" "\
  291. this is a regular line
  292. line with \\
  293. continuation
  294. more regular lines
  295. line with \\
  296. continuation
  297. "
  298. testing "sed s///NUM test" \
  299. "sed -e 's/a/b/2; s/a/c/g'" \
  300. "cb\n" "" "aa\n"
  301. testing "sed /regex/,N{...} addresses work" \
  302. "sed /^2/,2{d}" \
  303. "1\n3\n4\n5\n" \
  304. "" \
  305. "1\n2\n3\n4\n5\n"
  306. testing "sed /regex/,+N{...} addresses work" \
  307. "sed /^2/,+2{d}" \
  308. "1\n5\n" \
  309. "" \
  310. "1\n2\n3\n4\n5\n"
  311. testing "sed /regex/,+N{...} -i works" \
  312. "cat - >input2; sed /^4/,+2{d} -i input input2; echo \$?; cat input input2; rm input2" \
  313. "0\n""1\n2\n3\n7\n8\n""1\n2\n7\n8\n" \
  314. "1\n2\n3\n4\n5\n6\n7\n8\n" \
  315. "1\n2\n4\n5\n6\n7\n8\n" \
  316. # GNU sed 4.2.1 would also accept "/^4/,+{d}" with the same meaning, we don't
  317. testing "sed /regex/,+0{...} -i works" \
  318. "cat - >input2; sed /^4/,+0{d} -i input input2; echo \$?; cat input input2; rm input2" \
  319. "0\n""1\n2\n3\n5\n6\n7\n8\n""1\n2\n5\n6\n7\n8\n" \
  320. "1\n2\n3\n4\n5\n6\n7\n8\n" \
  321. "1\n2\n4\n5\n6\n7\n8\n" \
  322. # GNU sed 4.2.1 would also accept "/^4/,+d" with the same meaning, we don't
  323. testing "sed /regex/,+0<cmd> -i works" \
  324. "cat - >input2; sed /^4/,+0d -i input input2; echo \$?; cat input input2; rm input2" \
  325. "0\n""1\n2\n3\n5\n6\n7\n8\n""1\n2\n5\n6\n7\n8\n" \
  326. "1\n2\n3\n4\n5\n6\n7\n8\n" \
  327. "1\n2\n4\n5\n6\n7\n8\n" \
  328. testing "sed 's///w FILE'" \
  329. "sed 's/qwe/ZZZ/wz'; cat z; rm z" \
  330. "123\nZZZ\nasd\n""ZZZ\n" \
  331. "" \
  332. "123\nqwe\nasd\n"
  333. # testing "description" "commands" "result" "infile" "stdin"
  334. exit $FAILCOUNT