diff.tests 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #!/bin/sh
  2. # Copyright 2008 by Denys Vlasenko
  3. # Licensed under GPL v2, see file LICENSE for details.
  4. . ./testing.sh
  5. # testing "test name" "options" "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 nonempty one" \
  41. "diff -u - input | $TRIM_TAB" \
  42. "\
  43. --- -
  44. +++ input
  45. @@ -0,0 +1 @@
  46. +a
  47. " \
  48. "a\n" \
  49. ""
  50. testing "diff -b treats EOF as whitespace" \
  51. 'diff -ub - input; echo $?' \
  52. "0\n" \
  53. "abc" \
  54. "abc "
  55. testing "diff -b treats all spaces as equal" \
  56. 'diff -ub - input; echo $?' \
  57. "0\n" \
  58. "a \t c\n" \
  59. "a\t \tc\n"
  60. testing "diff -B ignores changes whose lines are all blank" \
  61. 'diff -uB - input; echo $?' \
  62. "0\n" \
  63. "a\n" \
  64. "\na\n\n"
  65. testing "diff -B does not ignore changes whose lines are not all blank" \
  66. "diff -uB - input | $TRIM_TAB" \
  67. "\
  68. --- -
  69. +++ input
  70. @@ -1,3 +1 @@
  71. -
  72. -b
  73. -
  74. +a
  75. " \
  76. "a\n" \
  77. "\nb\n\n"
  78. testing "diff always takes context from old file" \
  79. "diff -ub - input | $TRIM_TAB" \
  80. "\
  81. --- -
  82. +++ input
  83. @@ -1 +1,3 @@
  84. +abc
  85. a c
  86. +def
  87. " \
  88. "abc\na c\ndef\n" \
  89. "a c\n"
  90. # testing "test name" "options" "expected result" "file input" "stdin"
  91. rm -rf diff1 diff2
  92. mkdir diff1 diff2 diff2/subdir
  93. echo qwe >diff1/-
  94. echo asd >diff2/subdir/-
  95. optional FEATURE_DIFF_DIR
  96. testing "diff diff1 diff2/subdir" \
  97. "diff -ur diff1 diff2/subdir | $TRIM_TAB" \
  98. "\
  99. --- diff1/-
  100. +++ diff2/subdir/-
  101. @@ -1 +1 @@
  102. -qwe
  103. +asd
  104. " \
  105. "" ""
  106. SKIP=
  107. # using directory structure from prev test...
  108. optional FEATURE_DIFF_DIR
  109. testing "diff dir dir2/file/-" \
  110. "diff -ur diff1 diff2/subdir/- | $TRIM_TAB" \
  111. "\
  112. --- diff1/-
  113. +++ diff2/subdir/-
  114. @@ -1 +1 @@
  115. -qwe
  116. +asd
  117. " \
  118. "" ""
  119. SKIP=
  120. # using directory structure from prev test...
  121. mkdir diff1/test
  122. mkfifo diff2/subdir/test
  123. optional FEATURE_DIFF_DIR
  124. testing "diff of dir and fifo" \
  125. "diff -ur diff1 diff2/subdir | $TRIM_TAB" \
  126. "\
  127. --- diff1/-
  128. +++ diff2/subdir/-
  129. @@ -1 +1 @@
  130. -qwe
  131. +asd
  132. Only in diff2/subdir: test
  133. " \
  134. "" ""
  135. SKIP=
  136. # using directory structure from prev test...
  137. rmdir diff1/test
  138. echo >diff1/test
  139. optional FEATURE_DIFF_DIR
  140. testing "diff of file and fifo" \
  141. "diff -ur diff1 diff2/subdir | $TRIM_TAB" \
  142. "\
  143. --- diff1/-
  144. +++ diff2/subdir/-
  145. @@ -1 +1 @@
  146. -qwe
  147. +asd
  148. File diff2/subdir/test is not a regular file or directory and was skipped
  149. " \
  150. "" ""
  151. SKIP=
  152. # using directory structure from prev test...
  153. mkfifo diff1/test2
  154. optional FEATURE_DIFF_DIR
  155. testing "diff -rN does not read non-regular files" \
  156. "diff -urN diff1 diff2/subdir | $TRIM_TAB" \
  157. "\
  158. --- diff1/-
  159. +++ diff2/subdir/-
  160. @@ -1 +1 @@
  161. -qwe
  162. +asd
  163. File diff2/subdir/test is not a regular file or directory and was skipped
  164. File diff1/test2 is not a regular file or directory and was skipped
  165. " \
  166. "" ""
  167. SKIP=
  168. # clean up
  169. rm -rf diff1 diff2
  170. exit $FAILCOUNT