diff.tests 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #!/bin/sh
  2. # Copyright 2008 by Denys Vlasenko
  3. # Licensed under GPLv2, see file LICENSE in this source tree.
  4. . ./testing.sh
  5. # testing "test name" "commands" "expected result" "file input" "stdin"
  6. # diff outputs date/time in the header, which should not be analysed
  7. # NB: sed has tab character in s command!
  8. TRIM_TAB="sed 's/ .*//'"
  9. testing "diff of stdin" \
  10. "diff -u - input | $TRIM_TAB" \
  11. "\
  12. --- -
  13. +++ input
  14. @@ -1 +1,3 @@
  15. +qwe
  16. asd
  17. +zxc
  18. " \
  19. "qwe\nasd\nzxc\n" \
  20. "asd\n"
  21. testing "diff of stdin, no newline in the file" \
  22. "diff -u - input | $TRIM_TAB" \
  23. "\
  24. --- -
  25. +++ input
  26. @@ -1 +1,3 @@
  27. +qwe
  28. asd
  29. +zxc
  30. \\ No newline at end of file
  31. " \
  32. "qwe\nasd\nzxc" \
  33. "asd\n"
  34. # we also test that stdin is in fact NOT read
  35. testing "diff of stdin, twice" \
  36. 'diff - -; echo $?; wc -c' \
  37. "0\n5\n" \
  38. "" \
  39. "stdin"
  40. testing "diff of empty file against stdin" \
  41. "diff -u - input | $TRIM_TAB" \
  42. "\
  43. --- -
  44. +++ input
  45. @@ -1 +0,0 @@
  46. -a
  47. " \
  48. "" \
  49. "a\n"
  50. testing "diff of empty file against nonempty one" \
  51. "diff -u - input | $TRIM_TAB" \
  52. "\
  53. --- -
  54. +++ input
  55. @@ -0,0 +1 @@
  56. +a
  57. " \
  58. "a\n" \
  59. ""
  60. testing "diff -b treats EOF as whitespace" \
  61. 'diff -ub - input; echo $?' \
  62. "0\n" \
  63. "abc" \
  64. "abc "
  65. testing "diff -b treats all spaces as equal" \
  66. 'diff -ub - input; echo $?' \
  67. "0\n" \
  68. "a \t c\n" \
  69. "a\t \tc\n"
  70. testing "diff -B ignores changes whose lines are all blank" \
  71. 'diff -uB - input; echo $?' \
  72. "0\n" \
  73. "a\n" \
  74. "\na\n\n"
  75. testing "diff -B does not ignore changes whose lines are not all blank" \
  76. "diff -uB - input | $TRIM_TAB" \
  77. "\
  78. --- -
  79. +++ input
  80. @@ -1,3 +1 @@
  81. -
  82. -b
  83. -
  84. +a
  85. " \
  86. "a\n" \
  87. "\nb\n\n"
  88. testing "diff -B ignores blank single line change" \
  89. 'diff -qB - input; echo $?' \
  90. "0\n" \
  91. "\n1\n" \
  92. "1\n"
  93. testing "diff -B does not ignore non-blank single line change" \
  94. 'diff -qB - input; echo $?' \
  95. "Files - and input differ\n1\n" \
  96. "0\n" \
  97. "1\n"
  98. testing "diff always takes context from old file" \
  99. "diff -ub - input | $TRIM_TAB" \
  100. "\
  101. --- -
  102. +++ input
  103. @@ -1 +1,3 @@
  104. +abc
  105. a c
  106. +def
  107. " \
  108. "abc\na c\ndef\n" \
  109. "a c\n"
  110. # testing "test name" "commands" "expected result" "file input" "stdin"
  111. # clean up
  112. rm -rf diff1 diff2
  113. mkdir diff1 diff2 diff2/subdir
  114. echo qwe >diff1/-
  115. echo asd >diff2/subdir/-
  116. optional FEATURE_DIFF_DIR
  117. testing "diff diff1 diff2/subdir" \
  118. "diff -ur diff1 diff2/subdir | $TRIM_TAB" \
  119. "\
  120. --- diff1/-
  121. +++ diff2/subdir/-
  122. @@ -1 +1 @@
  123. -qwe
  124. +asd
  125. " \
  126. "" ""
  127. SKIP=
  128. # using directory structure from prev test...
  129. optional FEATURE_DIFF_DIR
  130. testing "diff dir dir2/file/-" \
  131. "diff -ur diff1 diff2/subdir/- | $TRIM_TAB" \
  132. "\
  133. --- diff1/-
  134. +++ diff2/subdir/-
  135. @@ -1 +1 @@
  136. -qwe
  137. +asd
  138. " \
  139. "" ""
  140. SKIP=
  141. # using directory structure from prev test...
  142. mkdir diff1/test
  143. mkfifo diff2/subdir/test
  144. optional FEATURE_DIFF_DIR
  145. testing "diff of dir and fifo" \
  146. "diff -ur diff1 diff2/subdir | $TRIM_TAB" \
  147. "\
  148. --- diff1/-
  149. +++ diff2/subdir/-
  150. @@ -1 +1 @@
  151. -qwe
  152. +asd
  153. Only in diff2/subdir: test
  154. " \
  155. "" ""
  156. SKIP=
  157. # using directory structure from prev test...
  158. rmdir diff1/test
  159. echo >diff1/test
  160. optional FEATURE_DIFF_DIR
  161. testing "diff of file and fifo" \
  162. "diff -ur diff1 diff2/subdir | $TRIM_TAB" \
  163. "\
  164. --- diff1/-
  165. +++ diff2/subdir/-
  166. @@ -1 +1 @@
  167. -qwe
  168. +asd
  169. File diff2/subdir/test is not a regular file or directory and was skipped
  170. " \
  171. "" ""
  172. SKIP=
  173. # using directory structure from prev test...
  174. mkfifo diff1/test2
  175. optional FEATURE_DIFF_DIR
  176. testing "diff -rN does not read non-regular files" \
  177. "diff -urN diff1 diff2/subdir | $TRIM_TAB" \
  178. "\
  179. --- diff1/-
  180. +++ diff2/subdir/-
  181. @@ -1 +1 @@
  182. -qwe
  183. +asd
  184. File diff2/subdir/test is not a regular file or directory and was skipped
  185. File diff1/test2 is not a regular file or directory and was skipped
  186. " \
  187. "" ""
  188. SKIP=
  189. # clean up
  190. rm -rf diff1 diff2
  191. # NOT using directory structure from prev test...
  192. mkdir diff1 diff2
  193. echo qwe >diff1/-
  194. echo rty >diff2/-
  195. optional FEATURE_DIFF_DIR
  196. testing "diff diff1 diff2/" \
  197. "diff -ur diff1 diff2/ | $TRIM_TAB; diff -ur .///diff1 diff2//// | $TRIM_TAB" \
  198. "\
  199. --- diff1/-
  200. +++ diff2/-
  201. @@ -1 +1 @@
  202. -qwe
  203. +rty
  204. --- .///diff1/-
  205. +++ diff2////-
  206. @@ -1 +1 @@
  207. -qwe
  208. +rty
  209. " \
  210. "" ""
  211. SKIP=
  212. # clean up
  213. rm -rf diff1 diff2
  214. exit $FAILCOUNT