diff.tests 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. testing "diff diff1 diff2/subdir" \
  96. "diff -ur diff1 diff2/subdir | $TRIM_TAB" \
  97. "\
  98. --- diff1/-
  99. +++ diff2/subdir/-
  100. @@ -1 +1 @@
  101. -qwe
  102. +asd
  103. " \
  104. "" ""
  105. # using directory structure from prev test...
  106. testing "diff dir dir2/file/-" \
  107. "diff -ur diff1 diff2/subdir/- | $TRIM_TAB" \
  108. "\
  109. --- diff1/-
  110. +++ diff2/subdir/-
  111. @@ -1 +1 @@
  112. -qwe
  113. +asd
  114. " \
  115. "" ""
  116. # using directory structure from prev test...
  117. mkdir diff1/test
  118. mkfifo diff2/subdir/test
  119. testing "diff of dir and fifo" \
  120. "diff -ur diff1 diff2/subdir | $TRIM_TAB" \
  121. "\
  122. --- diff1/-
  123. +++ diff2/subdir/-
  124. @@ -1 +1 @@
  125. -qwe
  126. +asd
  127. Only in diff2/subdir: test
  128. " \
  129. "" ""
  130. # using directory structure from prev test...
  131. rmdir diff1/test
  132. echo >diff1/test
  133. testing "diff of file and fifo" \
  134. "diff -ur diff1 diff2/subdir | $TRIM_TAB" \
  135. "\
  136. --- diff1/-
  137. +++ diff2/subdir/-
  138. @@ -1 +1 @@
  139. -qwe
  140. +asd
  141. File diff2/subdir/test is not a regular file or directory and was skipped
  142. " \
  143. "" ""
  144. # using directory structure from prev test...
  145. mkfifo diff1/test2
  146. testing "diff -rN does not read non-regular files" \
  147. "diff -urN diff1 diff2/subdir | $TRIM_TAB" \
  148. "\
  149. --- diff1/-
  150. +++ diff2/subdir/-
  151. @@ -1 +1 @@
  152. -qwe
  153. +asd
  154. File diff2/subdir/test is not a regular file or directory and was skipped
  155. File diff1/test2 is not a regular file or directory and was skipped
  156. " \
  157. "" ""
  158. # clean up
  159. rm -rf diff1 diff2
  160. exit $FAILCOUNT