Skip to content

Commit 9eb05d3

Browse files
committed
Add test case for interdiff color context trimming issue #119
Assisted-by: Cursor
1 parent ecb9554 commit 9eb05d3

2 files changed

Lines changed: 190 additions & 1 deletion

File tree

‎Makefile.am‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ TESTS = tests/newline1/run-test \
278278
tests/crlf/run-test \
279279
tests/whitespace-regression/run-test \
280280
tests/interdiff-whitespace-w/run-test \
281+
tests/interdiff-color-context/run-test \
281282
tests/patch-ignore-whitespace/run-test \
282283
tests/whitespace-w/run-test \
283284
tests/filterdiff-inplace1/run-test \
@@ -304,7 +305,8 @@ TESTS = tests/newline1/run-test \
304305
# Feel free to send me patches. :-)
305306
XFAIL_TESTS = \
306307
tests/delhunk5/run-test \
307-
tests/delhunk6/run-test
308+
tests/delhunk6/run-test \
309+
tests/interdiff-color-context/run-test
308310

309311
test-perms: src/combinediff$(EXEEXT) src/flipdiff$(EXEEXT) \
310312
src/lsdiff$(EXEEXT) src/grepdiff$(EXEEXT) src/patchview$(EXEEXT) \
Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
#!/bin/sh
2+
3+
# This is an interdiff(1) testcase.
4+
# Test: Verify that --color option doesn't break context trimming
5+
#
6+
# This test ensures that when interdiff uses the --color option,
7+
# the context trimming functionality (which prevents patch fuzz)
8+
# still works correctly. The test creates a scenario similar to
9+
# the fuzz1 test but with color output enabled.
10+
#
11+
# EXPECTED FAILURE: This test currently fails because interdiff cannot
12+
# parse colored diff output when --color=always produces ANSI escape
13+
# sequences. The test will pass when this bug is fixed.
14+
15+
. ${top_srcdir-.}/tests/common.sh
16+
17+
# Create a base file with enough lines to test context trimming
18+
cat << EOF > file
19+
LINE 1
20+
line 2
21+
line 3
22+
line 4
23+
line 5
24+
if
25+
1
26+
fi
27+
if
28+
2
29+
fi
30+
A
31+
B
32+
C
33+
D
34+
E
35+
F
36+
EOF
37+
38+
# Create first patch: change line 1 to uppercase
39+
cat << EOF > patch1
40+
--- file
41+
+++ file
42+
@@ -1,4 +1,4 @@
43+
-line 1
44+
+LINE 1
45+
line 2
46+
line 3
47+
line 4
48+
EOF
49+
50+
# Create second patch: remove some lines in the middle
51+
# This creates a situation where context trimming is needed
52+
cat << EOF > patch2
53+
--- file
54+
+++ file
55+
@@ -5,9 +5,6 @@
56+
line 5
57+
if
58+
1
59+
-fi
60+
-if
61+
-2
62+
fi
63+
A
64+
B
65+
EOF
66+
67+
# Test 1: Run interdiff without --color (baseline)
68+
${INTERDIFF} patch1 patch2 2>errors-no-color >patch1-2-no-color
69+
[ -s errors-no-color -o "$?" -gt "0" ] && { cat errors-no-color; exit 1; }
70+
71+
# Test 2: Run interdiff with --color=never (should be same as baseline)
72+
${INTERDIFF} --color=never patch1 patch2 2>errors-color-never >patch1-2-color-never
73+
[ -s errors-color-never -o "$?" -gt "0" ] && { cat errors-color-never; exit 1; }
74+
75+
# Test 3: Run interdiff with --color=always (the main test)
76+
# First check if the underlying diff supports --color
77+
if diff --color=always -u /dev/null /dev/null >/dev/null 2>&1; then
78+
${INTERDIFF} --color=always patch1 patch2 2>errors-color-always >patch1-2-color-always
79+
color_always_exit_code=$?
80+
81+
# This test should FAIL if interdiff can't handle colored diff output
82+
# The bug is that interdiff fails to parse ANSI color codes in diff output
83+
if [ -s errors-color-always -o "$color_always_exit_code" -gt "0" ]; then
84+
if grep -q "Line not understood" errors-color-always; then
85+
echo "FAIL: interdiff cannot parse colored diff output (--color=always broken)"
86+
echo "ERROR: interdiff fails with 'Line not understood' when diff produces ANSI color codes"
87+
cat errors-color-always
88+
exit 1
89+
else
90+
echo "ERROR: --color=always failed for unexpected reason"
91+
cat errors-color-always
92+
exit 1
93+
fi
94+
else
95+
color_always_supported=1
96+
echo "SUCCESS: --color=always works correctly"
97+
fi
98+
else
99+
# If diff doesn't support --color, skip the color=always test
100+
# but still test that context trimming works with other modes
101+
color_always_supported=0
102+
echo "SKIP: System diff doesn't support --color, skipping --color=always test"
103+
fi
104+
105+
# Test 4: Run interdiff with --color (auto mode, should default to never in non-tty)
106+
${INTERDIFF} --color patch1 patch2 2>errors-color-auto >patch1-2-color-auto
107+
[ -s errors-color-auto -o "$?" -gt "0" ] && { cat errors-color-auto; exit 1; }
108+
109+
# Verify that all generated patches can be applied without fuzz
110+
# This is the critical test - if context trimming is broken, patch will complain about fuzz
111+
112+
# Test the baseline patch
113+
cp file file-test1
114+
${PATCH} file-test1 < patch1-2-no-color >patch-errors1 2>&1 || { cat patch-errors1; exit 1; }
115+
if grep -i fuzz patch-errors1; then
116+
echo "ERROR: Baseline patch caused fuzz - context trimming broken"
117+
exit 1
118+
fi
119+
120+
# Test the --color=never patch (should be identical to baseline)
121+
cp file file-test2
122+
${PATCH} file-test2 < patch1-2-color-never >patch-errors2 2>&1 || { cat patch-errors2; exit 1; }
123+
if grep -i fuzz patch-errors2; then
124+
echo "ERROR: --color=never patch caused fuzz"
125+
exit 1
126+
fi
127+
128+
# Test the --color=always patch (main test) - only if color is supported and working
129+
if [ "$color_always_supported" = "1" ]; then
130+
cp file file-test3
131+
${PATCH} file-test3 < patch1-2-color-always >patch-errors3 2>&1 || { cat patch-errors3; exit 1; }
132+
if grep -i fuzz patch-errors3; then
133+
echo "ERROR: --color=always patch caused fuzz - context trimming broken with color"
134+
exit 1
135+
fi
136+
echo "SUCCESS: --color=always patch applied without fuzz"
137+
else
138+
echo "SKIP: --color=always patch test skipped (not supported or failed earlier)"
139+
fi
140+
141+
# Test the --color (auto) patch
142+
cp file file-test4
143+
${PATCH} file-test4 < patch1-2-color-auto >patch-errors4 2>&1 || { cat patch-errors4; exit 1; }
144+
if grep -i fuzz patch-errors4; then
145+
echo "ERROR: --color (auto) patch caused fuzz"
146+
exit 1
147+
fi
148+
149+
# Additional verification: ensure the patches produce the same logical result
150+
# (ignoring ANSI color codes which might be present in the --color=always version)
151+
if [ "$color_always_supported" = "1" ]; then
152+
# Strip ANSI color codes from the color=always output for comparison
153+
sed 's/\x1b\[[0-9;]*m//g' patch1-2-color-always > patch1-2-color-always-stripped
154+
155+
# Compare the logical content (the never and stripped always should be identical)
156+
if ! cmp patch1-2-color-never patch1-2-color-always-stripped; then
157+
echo "ERROR: --color=always produces different logical output than --color=never"
158+
echo "=== --color=never ==="
159+
cat patch1-2-color-never
160+
echo "=== --color=always (stripped) ==="
161+
cat patch1-2-color-always-stripped
162+
exit 1
163+
fi
164+
fi
165+
166+
# Verify that all patched files end up in the same state
167+
if ! cmp file-test1 file-test2; then
168+
echo "ERROR: Different results between no-color and --color=never"
169+
exit 1
170+
fi
171+
172+
if [ "$color_always_supported" = "1" ]; then
173+
if ! cmp file-test1 file-test3; then
174+
echo "ERROR: Different results between no-color and --color=always"
175+
exit 1
176+
fi
177+
fi
178+
179+
if ! cmp file-test1 file-test4; then
180+
echo "ERROR: Different results between no-color and --color (auto)"
181+
exit 1
182+
fi
183+
184+
# If we get here, the core functionality works (--color=never and --color auto work)
185+
# The test only fails if --color=always is broken (which it currently is)
186+
echo "SUCCESS: Context trimming works correctly with supported color modes"
187+
exit 0

0 commit comments

Comments
 (0)