diff_test.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. atf_test_case simple
  2. atf_test_case unified
  3. atf_test_case header
  4. atf_test_case header_ns
  5. atf_test_case ifdef
  6. atf_test_case group_format
  7. atf_test_case side_by_side
  8. atf_test_case side_by_side_tabbed
  9. atf_test_case brief_format
  10. atf_test_case b230049
  11. atf_test_case stripcr_o
  12. atf_test_case b252515
  13. atf_test_case b278988
  14. atf_test_case Bflag
  15. atf_test_case Nflag
  16. atf_test_case tabsize
  17. atf_test_case conflicting_format
  18. atf_test_case label
  19. atf_test_case report_identical
  20. atf_test_case non_regular_file
  21. atf_test_case binary
  22. atf_test_case functionname
  23. atf_test_case noderef
  24. atf_test_case ignorecase
  25. simple_body()
  26. {
  27. atf_check -o file:$(atf_get_srcdir)/simple.out -s eq:1 \
  28. diff "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
  29. atf_check -o file:$(atf_get_srcdir)/simple_e.out -s eq:1 \
  30. diff -e "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
  31. atf_check -o file:$(atf_get_srcdir)/simple_u.out -s eq:1 \
  32. diff -u -L input1 -L input2 "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
  33. atf_check -o file:$(atf_get_srcdir)/simple_n.out -s eq:1 \
  34. diff -n "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
  35. atf_check -o inline:"Files $(atf_get_srcdir)/input1.in and $(atf_get_srcdir)/input2.in differ\n" -s eq:1 \
  36. diff -q "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input2.in"
  37. atf_check \
  38. diff -q "$(atf_get_srcdir)/input1.in" "$(atf_get_srcdir)/input1.in"
  39. atf_check -o file:$(atf_get_srcdir)/simple_i.out -s eq:1 \
  40. diff -i "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
  41. atf_check -o file:$(atf_get_srcdir)/simple_w.out -s eq:1 \
  42. diff -w "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
  43. atf_check -o file:$(atf_get_srcdir)/simple_b.out -s eq:1 \
  44. diff -b "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
  45. atf_check -o file:$(atf_get_srcdir)/simple_p.out -s eq:1 \
  46. diff --label input_c1.in --label input_c2.in -p "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
  47. }
  48. unified_body()
  49. {
  50. atf_check -o file:$(atf_get_srcdir)/unified_p.out -s eq:1 \
  51. diff -up -L input_c1.in -L input_c2.in "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
  52. atf_check -o file:$(atf_get_srcdir)/unified_9999.out -s eq:1 \
  53. diff -u9999 -L input_c1.in -L input_c2.in "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
  54. }
  55. b230049_body()
  56. {
  57. printf 'a\nb\r\nc\n' > b230049_a.in
  58. printf 'a\r\nb\r\nc\r\n' > b230049_b.in
  59. atf_check -o empty -s eq:0 \
  60. diff -up --strip-trailing-cr -L b230049_a.in -L b230049_b.in \
  61. b230049_a.in b230049_b.in
  62. }
  63. stripcr_o_body()
  64. {
  65. printf 'a\nX\nc\n' > stripcr_o_X.in
  66. printf 'a\r\nY\r\nc\r\n' > stripcr_o_Y.in
  67. atf_check -o "file:$(atf_get_srcdir)/strip_o.out" -s eq:1 \
  68. diff -L1 -L2 -u --strip-trailing-cr stripcr_o_X.in stripcr_o_Y.in
  69. }
  70. b252515_body()
  71. {
  72. printf 'a b\n' > b252515_a.in
  73. printf 'a b\n' > b252515_b.in
  74. atf_check -o empty -s eq:0 \
  75. diff -qw b252515_a.in b252515_b.in
  76. }
  77. b278988_body()
  78. {
  79. printf 'a\nb\nn' > b278988.a.in
  80. printf 'a\n\nb\nn' > b278988.b.in
  81. atf_check -o empty -s eq:0 \
  82. diff -Bw b278988.a.in b278988.b.in
  83. }
  84. header_body()
  85. {
  86. export TZ=UTC
  87. : > empty
  88. echo hello > hello
  89. touch -d 2015-04-03T01:02:03 empty
  90. touch -d 2016-12-22T11:22:33 hello
  91. atf_check -o "file:$(atf_get_srcdir)/header.out" -s eq:1 \
  92. diff -u empty hello
  93. }
  94. header_ns_body()
  95. {
  96. export TZ=UTC
  97. : > empty
  98. echo hello > hello
  99. touch -d 2015-04-03T01:02:03.123456789 empty
  100. touch -d 2016-12-22T11:22:33.987654321 hello
  101. atf_check -o "file:$(atf_get_srcdir)/header_ns.out" -s eq:1 \
  102. diff -u empty hello
  103. }
  104. ifdef_body()
  105. {
  106. atf_check -o file:$(atf_get_srcdir)/ifdef.out -s eq:1 \
  107. diff -D PLOP "$(atf_get_srcdir)/input_c1.in" \
  108. "$(atf_get_srcdir)/input_c2.in"
  109. }
  110. group_format_body()
  111. {
  112. atf_check -o file:$(atf_get_srcdir)/group-format.out -s eq:1 \
  113. diff --changed-group-format='<<<<<<< (local)
  114. %<=======
  115. %>>>>>>>> (stock)
  116. ' "$(atf_get_srcdir)/input_c1.in" "$(atf_get_srcdir)/input_c2.in"
  117. }
  118. side_by_side_body()
  119. {
  120. atf_check -o save:A printf "A\nB\nC\n"
  121. atf_check -o save:B printf "D\nB\nE\n"
  122. exp_output=$(printf "A[[:space:]]+|[[:space:]]+D\nB[[:space:]]+B\nC[[:space:]]+|[[:space:]]+E")
  123. exp_output_suppressed=$(printf "A[[:space:]]+|[[:space:]]+D\nC[[:space:]]+|[[:space:]]+E")
  124. atf_check -o match:"$exp_output" -s exit:1 \
  125. diff --side-by-side A B
  126. atf_check -o match:"$exp_output" -s exit:1 \
  127. diff -y A B
  128. atf_check -o match:"$exp_output_suppressed" -s exit:1 \
  129. diff -y --suppress-common-lines A B
  130. atf_check -o match:"$exp_output_suppressed" -s exit:1 \
  131. diff -W 65 -y --suppress-common-lines A B
  132. }
  133. side_by_side_tabbed_body()
  134. {
  135. file_a=$(atf_get_srcdir)/side_by_side_tabbed_a.in
  136. file_b=$(atf_get_srcdir)/side_by_side_tabbed_b.in
  137. atf_check -o save:diffout -s not-exit:0 \
  138. diff -y ${file_a} ${file_b}
  139. atf_check -o save:diffout_expanded -s not-exit:0 \
  140. diff -yt ${file_a} ${file_b}
  141. atf_check -o not-empty grep -Ee 'file A.+file B' diffout
  142. atf_check -o not-empty grep -Ee 'file A.+file B' diffout_expanded
  143. atf_check -o not-empty grep -Ee 'tabs.+tabs' diffout
  144. atf_check -o not-empty grep -Ee 'tabs.+tabs' diffout_expanded
  145. }
  146. brief_format_body()
  147. {
  148. atf_check mkdir A B
  149. atf_check -x "echo 1 > A/test-file"
  150. atf_check -x "echo 2 > B/test-file"
  151. atf_check cp -Rf A C
  152. atf_check cp -Rf A D
  153. atf_check -x "echo 3 > D/another-test-file"
  154. atf_check \
  155. -s exit:1 \
  156. -o inline:"Files A/test-file and B/test-file differ\n" \
  157. diff -rq A B
  158. atf_check diff -rq A C
  159. atf_check \
  160. -s exit:1 \
  161. -o inline:"Only in D: another-test-file\n" \
  162. diff -rq A D
  163. atf_check \
  164. -s exit:1 \
  165. -o inline:"Files A/another-test-file and D/another-test-file differ\n" \
  166. diff -Nrq A D
  167. }
  168. Bflag_body()
  169. {
  170. atf_check -x 'printf "A\nB\n" > A'
  171. atf_check -x 'printf "A\n\nB\n" > B'
  172. atf_check -x 'printf "A\n \nB\n" > C'
  173. atf_check -x 'printf "A\nC\nB\n" > D'
  174. atf_check -x 'printf "A\nB\nC\nD\nE\nF\nG\nH" > E'
  175. atf_check -x 'printf "A\n\nB\nC\nD\nE\nF\nX\nH" > F'
  176. atf_check -s exit:0 -o inline:"" diff -B A B
  177. atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_C.out" diff -B A C
  178. atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_D.out" diff -B A D
  179. atf_check -s exit:1 -o file:"$(atf_get_srcdir)/Bflag_F.out" diff -B E F
  180. }
  181. Nflag_body()
  182. {
  183. atf_check -x 'printf "foo" > A'
  184. atf_check -s exit:1 -o ignore -e ignore diff -N A NOFILE
  185. atf_check -s exit:1 -o ignore -e ignore diff -N NOFILE A
  186. atf_check -s exit:2 -o ignore -e ignore diff -N NOFILE1 NOFILE2
  187. }
  188. tabsize_body()
  189. {
  190. printf "\tA\n" > A
  191. printf "\tB\n" > B
  192. atf_check -s exit:1 \
  193. -o inline:"1c1\n< A\n---\n> B\n" \
  194. diff -t --tabsize 1 A B
  195. }
  196. conflicting_format_body()
  197. {
  198. printf "\tA\n" > A
  199. printf "\tB\n" > B
  200. atf_check -s exit:2 -e ignore diff -c -u A B
  201. atf_check -s exit:2 -e ignore diff -e -f A B
  202. atf_check -s exit:2 -e ignore diff -y -q A B
  203. atf_check -s exit:2 -e ignore diff -q -u A B
  204. atf_check -s exit:2 -e ignore diff -q -c A B
  205. atf_check -s exit:2 -e ignore diff --normal -c A B
  206. atf_check -s exit:2 -e ignore diff -c --normal A B
  207. atf_check -s exit:1 -o ignore -e ignore diff -u -u A B
  208. atf_check -s exit:1 -o ignore -e ignore diff -e -e A B
  209. atf_check -s exit:1 -o ignore -e ignore diff -y -y A B
  210. atf_check -s exit:1 -o ignore -e ignore diff -q -q A B
  211. atf_check -s exit:1 -o ignore -e ignore diff -c -c A B
  212. atf_check -s exit:1 -o ignore -e ignore diff --normal --normal A B
  213. }
  214. label_body()
  215. {
  216. printf "\tA\n" > A
  217. atf_check -o inline:"Files hello and world are identical\n" \
  218. -s exit:0 diff --label hello --label world -s A A
  219. atf_check -o inline:"Binary files hello and world differ\n" \
  220. -s exit:1 diff --label hello --label world `which diff` `which ls`
  221. }
  222. report_identical_head()
  223. {
  224. atf_set "require.user" unprivileged
  225. }
  226. report_identical_body()
  227. {
  228. printf "\tA\n" > A
  229. printf "\tB\n" > B
  230. atf_check -s exit:0 -o match:"are identical" \
  231. diff -s A A
  232. atf_check -s exit:1 -o not-match:"are identical" \
  233. diff -s A B
  234. chmod -r B
  235. atf_check -s exit:2 -e inline:"diff: B: Permission denied\n" \
  236. -o empty diff -s A B
  237. }
  238. non_regular_file_body()
  239. {
  240. printf "\tA\n" > A
  241. mkfifo B
  242. printf "\tA\n" > B &
  243. atf_check diff A B
  244. printf "\tB\n" > B &
  245. atf_check -s exit:1 \
  246. -o inline:"--- A\n+++ B\n@@ -1 +1 @@\n-\tA\n+\tB\n" \
  247. diff --label A --label B -u A B
  248. }
  249. binary_body()
  250. {
  251. # the NUL byte has to be after at least BUFSIZ bytes to trick asciifile()
  252. yes 012345678901234567890123456789012345678901234567890 | head -n 174 > A
  253. cp A B
  254. printf '\n\0\n' >> A
  255. printf '\nx\n' >> B
  256. atf_check -o inline:"Binary files A and B differ\n" -s exit:1 diff A B
  257. atf_check -o inline:"176c\nx\n.\n" -s exit:1 diff -ae A B
  258. }
  259. functionname_body()
  260. {
  261. atf_check -o file:$(atf_get_srcdir)/functionname_c.out -s exit:1 \
  262. diff -u -p -L functionname.in -L functionname_c.in \
  263. "$(atf_get_srcdir)/functionname.in" "$(atf_get_srcdir)/functionname_c.in"
  264. atf_check -o file:$(atf_get_srcdir)/functionname_objcm.out -s exit:1 \
  265. diff -u -p -L functionname.in -L functionname_objcm.in \
  266. "$(atf_get_srcdir)/functionname.in" "$(atf_get_srcdir)/functionname_objcm.in"
  267. atf_check -o file:$(atf_get_srcdir)/functionname_objcclassm.out -s exit:1 \
  268. diff -u -p -L functionname.in -L functionname_objcclassm.in \
  269. "$(atf_get_srcdir)/functionname.in" "$(atf_get_srcdir)/functionname_objcclassm.in"
  270. }
  271. noderef_body()
  272. {
  273. atf_check mkdir A B
  274. atf_check -x "echo 1 > A/test-file"
  275. atf_check -x "echo 1 > test-file"
  276. atf_check -x "echo 1 > test-file2"
  277. atf_check ln -s $(pwd)/test-file B/test-file
  278. atf_check -o empty -s exit:0 diff -r A B
  279. atf_check -o inline:"File A/test-file is a file while file B/test-file is a symbolic link\n" \
  280. -s exit:1 diff -r --no-dereference A B
  281. # both test files are now the same symbolic link
  282. atf_check rm A/test-file
  283. atf_check ln -s $(pwd)/test-file A/test-file
  284. atf_check -o empty -s exit:0 diff -r A B
  285. atf_check -o empty -s exit:0 diff -r --no-dereference A B
  286. # make test files different symbolic links, but same contents
  287. atf_check unlink A/test-file
  288. atf_check ln -s $(pwd)/test-file2 A/test-file
  289. atf_check -o empty -s exit:0 diff -r A B
  290. atf_check -o inline:"Symbolic links A/test-file and B/test-file differ\n" -s exit:1 diff -r --no-dereference A B
  291. }
  292. ignorecase_body()
  293. {
  294. atf_check mkdir A
  295. atf_check mkdir B
  296. atf_check -x "echo hello > A/foo"
  297. atf_check -x "echo hello > B/FOO"
  298. atf_check -o empty -s exit:0 diff -u -r --ignore-file-name-case A B
  299. }
  300. atf_init_test_cases()
  301. {
  302. atf_add_test_case simple
  303. atf_add_test_case unified
  304. atf_add_test_case header
  305. atf_add_test_case header_ns
  306. atf_add_test_case ifdef
  307. atf_add_test_case group_format
  308. atf_add_test_case side_by_side
  309. atf_add_test_case side_by_side_tabbed
  310. atf_add_test_case brief_format
  311. atf_add_test_case b230049
  312. atf_add_test_case stripcr_o
  313. atf_add_test_case b252515
  314. atf_add_test_case b278988
  315. atf_add_test_case Bflag
  316. atf_add_test_case Nflag
  317. atf_add_test_case tabsize
  318. atf_add_test_case conflicting_format
  319. atf_add_test_case label
  320. atf_add_test_case report_identical
  321. atf_add_test_case non_regular_file
  322. atf_add_test_case binary
  323. atf_add_test_case functionname
  324. atf_add_test_case noderef
  325. atf_add_test_case ignorecase
  326. }