xref: /aosp_15_r20/external/toybox/tests/diff.test (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1#!/bin/bash
2
3#testing "name" "command" "result" "infile" "stdin"
4
5seq 10 > left
6seq 11 > right
7
8testcmd "unknown argument" '--oops left right 2>/dev/null ; echo $?' "2\n" "" ""
9testcmd "missing" 'missing1 missing2 2>/dev/null ; echo $?' "2\n" "" ""
10
11testcmd "- -" '- - ; echo $?' "0\n" "" "whatever"
12
13testcmd "simple" "-u -L lll -L rrr left right" '--- lll
14+++ rrr
15@@ -8,3 +8,4 @@
16 8
17 9
18 10
19+11
20' "" ""
21rm left right
22
23mkdir -p tree1 tree2
24echo foo > tree1/file
25echo food > tree2/file
26
27# Debian's diff gratuitously echoes its command line with -r. No idea why.
28testcmd "-r" "-u -r -L tree1/file -L tree2/file tree1 tree2 | grep -v ^diff" \
29  '--- tree1/file
30+++ tree2/file
31@@ -1 +1 @@
32-foo
33+food
34' "" ""
35rm tree1/file tree2/file
36rmdir tree1 tree2
37
38echo -e "hello\r\nworld\r\n"> a
39echo -e "hello\nworld\n"> b
40testcmd "--strip-trailing-cr off" "-q a b" "Files a and b differ\n" "" ""
41testcmd "--strip-trailing-cr on" '-u --strip-trailing-cr a b; echo $?' \
42  "0\n" "" ""
43
44echo -e "1\n2" > aa
45echo -e "1\n3" > bb
46testcmd "line format" "--unchanged-line-format=U%l --old-line-format=D%l --new-line-format=A%l aa bb" "U1D2A3" "" ""
47testcmd "line format empty" "--unchanged-line-format= --old-line-format=D%l --new-line-format=A%l aa bb" "D2A3" "" ""
48
49ln -s aa cc
50testcmd "follow symlink" "-q -L aa -L cc aa cc" "" "" ""
51testcmd "no follow symlink" "-q --no-dereference -L aa -L cc aa cc" "File aa is a regular file while file cc is a symbolic link\n" "" ""
52ln -s ./aa dd
53testcmd "symlink differs" "-q -L cc -L dd cc dd" "" "" ""
54testcmd "symlink differs no follow" "-q --no-dereference -L cc -L dd cc dd" "Symbolic links cc and dd differ\n" "" ""
55rm aa bb cc dd
56
57mkfifo fifo1
58mkfifo fifo2
59echo -e "1\n2" > fifo1&
60echo -e "1\n3" > fifo2&
61testcmd "fifos" "-u -L fifo1 -L fifo2 fifo1 fifo2" '--- fifo1
62+++ fifo2
63@@ -1,2 +1,2 @@
64 1
65-2
66+3
67' "" ""
68
69echo -e "1\n2" > fifo1&
70echo -e "1\n3" > file1
71ln -s file1 link1
72
73testcmd "fifo symlinked file" "-u -L fifo1 -L link1 fifo1 link1" '--- fifo1
74+++ link1
75@@ -1,2 +1,2 @@
76 1
77-2
78+3
79' "" ""
80
81testcmd "fifo symlinked file no follow" "-u -L fifo1 -L link1 fifo1 link1 --no-dereference" "File fifo1 is a fifo while file link1 is a symbolic link\n" "" ""
82testcmd "symlinked file stdin no follow" "-u -L link1 -L - link1 - --no-dereference" "File link1 is a symbolic link while file - is a fifo\n" "" "test"
83rm fifo1 fifo2 link1 file1
84
85echo -e 'int bar() {
86}
87
88int foo() {
89}
90
91int baz() {
92  1
93  {2
94  3
95  4
96  foo
97}
98'> a
99echo -e 'int barbar() {
100}
101
102int foo() {
103}
104
105int baz() {
106  1a
107  {2
108  3
109  4
110  bar
111}
112'> b
113testcmd 'show function' "--show-function-line=' {$' -U1 -L lll -L rrr a b" \
114'--- lll
115+++ rrr
116@@ -1,2 +1,2 @@
117-int bar() {
118+int barbar() {
119 }
120@@ -7,3 +7,3 @@ int foo() {
121 int baz() {
122-  1
123+  1a
124   {2
125@@ -11,3 +11,3 @@ int baz() {
126   4
127-  foo
128+  bar
129 }
130' \
131'' ''
132rm a b
133
134seq 1 100000 > one
135seq 1 4 100000 > two
136testcmd 'big hunk' '-u --label nope --label nope one two' \
137  "$(echo -e '--- nope\n+++ nope\n@@ -1,100000 +1,25000 @@'; for i in $(seq 1 100000); do (((i-1)&3)) && echo "-$i" || echo " $i"; done)\n" '' ''
138rm one two
139