Skip to content

Commit a471833

Browse files
lilyballgitster
authored andcommitted
test-lib: extend test_decode_color to handle more color codes
Enhance the test_decode_color function to handle all common color codes, including background colors and escapes that contain multiple codes. This change necessitates changing <WHITE> to <BOLD>, so update t4034 as well. This change is necessary for the next commit in order to test background colors properly. Signed-off-by: Kevin Ballard <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5b57413 commit a471833

File tree

2 files changed

+77
-44
lines changed

2 files changed

+77
-44
lines changed

t/t4034-diff-words.sh

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ aeff = aeff * ( aaa )
3535
EOF
3636

3737
cat > expect <<\EOF
38-
<WHITE>diff --git a/pre b/post<RESET>
39-
<WHITE>index 330b04f..5ed8eff 100644<RESET>
40-
<WHITE>--- a/pre<RESET>
41-
<WHITE>+++ b/post<RESET>
38+
<BOLD>diff --git a/pre b/post<RESET>
39+
<BOLD>index 330b04f..5ed8eff 100644<RESET>
40+
<BOLD>--- a/pre<RESET>
41+
<BOLD>+++ b/post<RESET>
4242
<CYAN>@@ -1,3 +1,7 @@<RESET>
4343
<RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
4444
@@ -122,10 +122,10 @@ test_expect_success '--word-diff=plain --no-color' '
122122
'
123123

124124
cat > expect <<EOF
125-
<WHITE>diff --git a/pre b/post<RESET>
126-
<WHITE>index 330b04f..5ed8eff 100644<RESET>
127-
<WHITE>--- a/pre<RESET>
128-
<WHITE>+++ b/post<RESET>
125+
<BOLD>diff --git a/pre b/post<RESET>
126+
<BOLD>index 330b04f..5ed8eff 100644<RESET>
127+
<BOLD>--- a/pre<RESET>
128+
<BOLD>+++ b/post<RESET>
129129
<CYAN>@@ -1,3 +1,7 @@<RESET>
130130
<RED>[-h(4)-]<RESET><GREEN>{+h(4),hh[44]+}<RESET>
131131
@@ -143,10 +143,10 @@ test_expect_success '--word-diff=plain --color' '
143143
'
144144

145145
cat > expect <<\EOF
146-
<WHITE>diff --git a/pre b/post<RESET>
147-
<WHITE>index 330b04f..5ed8eff 100644<RESET>
148-
<WHITE>--- a/pre<RESET>
149-
<WHITE>+++ b/post<RESET>
146+
<BOLD>diff --git a/pre b/post<RESET>
147+
<BOLD>index 330b04f..5ed8eff 100644<RESET>
148+
<BOLD>--- a/pre<RESET>
149+
<BOLD>+++ b/post<RESET>
150150
<CYAN>@@ -1 +1 @@<RESET>
151151
<RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
152152
<CYAN>@@ -3,0 +4,4 @@<RESET> <RESET><MAGENTA>a = b + c<RESET>
@@ -163,10 +163,10 @@ test_expect_success 'word diff without context' '
163163
'
164164

165165
cat > expect <<\EOF
166-
<WHITE>diff --git a/pre b/post<RESET>
167-
<WHITE>index 330b04f..5ed8eff 100644<RESET>
168-
<WHITE>--- a/pre<RESET>
169-
<WHITE>+++ b/post<RESET>
166+
<BOLD>diff --git a/pre b/post<RESET>
167+
<BOLD>index 330b04f..5ed8eff 100644<RESET>
168+
<BOLD>--- a/pre<RESET>
169+
<BOLD>+++ b/post<RESET>
170170
<CYAN>@@ -1,3 +1,7 @@<RESET>
171171
h(4),<GREEN>hh<RESET>[44]
172172
@@ -199,10 +199,10 @@ test_expect_success 'option overrides .gitattributes' '
199199
'
200200

201201
cat > expect <<\EOF
202-
<WHITE>diff --git a/pre b/post<RESET>
203-
<WHITE>index 330b04f..5ed8eff 100644<RESET>
204-
<WHITE>--- a/pre<RESET>
205-
<WHITE>+++ b/post<RESET>
202+
<BOLD>diff --git a/pre b/post<RESET>
203+
<BOLD>index 330b04f..5ed8eff 100644<RESET>
204+
<BOLD>--- a/pre<RESET>
205+
<BOLD>+++ b/post<RESET>
206206
<CYAN>@@ -1,3 +1,7 @@<RESET>
207207
h(4)<GREEN>,hh[44]<RESET>
208208
@@ -231,10 +231,10 @@ test_expect_success 'command-line overrides config' '
231231
'
232232

233233
cat > expect <<\EOF
234-
<WHITE>diff --git a/pre b/post<RESET>
235-
<WHITE>index 330b04f..5ed8eff 100644<RESET>
236-
<WHITE>--- a/pre<RESET>
237-
<WHITE>+++ b/post<RESET>
234+
<BOLD>diff --git a/pre b/post<RESET>
235+
<BOLD>index 330b04f..5ed8eff 100644<RESET>
236+
<BOLD>--- a/pre<RESET>
237+
<BOLD>+++ b/post<RESET>
238238
<CYAN>@@ -1,3 +1,7 @@<RESET>
239239
h(4),<GREEN>{+hh+}<RESET>[44]
240240
@@ -260,10 +260,10 @@ test_expect_success 'remove diff driver regex' '
260260
'
261261

262262
cat > expect <<\EOF
263-
<WHITE>diff --git a/pre b/post<RESET>
264-
<WHITE>index 330b04f..5ed8eff 100644<RESET>
265-
<WHITE>--- a/pre<RESET>
266-
<WHITE>+++ b/post<RESET>
263+
<BOLD>diff --git a/pre b/post<RESET>
264+
<BOLD>index 330b04f..5ed8eff 100644<RESET>
265+
<BOLD>--- a/pre<RESET>
266+
<BOLD>+++ b/post<RESET>
267267
<CYAN>@@ -1,3 +1,7 @@<RESET>
268268
h(4),<GREEN>hh[44<RESET>]
269269
@@ -282,10 +282,10 @@ echo 'aaa (aaa)' > pre
282282
echo 'aaa (aaa) aaa' > post
283283

284284
cat > expect <<\EOF
285-
<WHITE>diff --git a/pre b/post<RESET>
286-
<WHITE>index c29453b..be22f37 100644<RESET>
287-
<WHITE>--- a/pre<RESET>
288-
<WHITE>+++ b/post<RESET>
285+
<BOLD>diff --git a/pre b/post<RESET>
286+
<BOLD>index c29453b..be22f37 100644<RESET>
287+
<BOLD>--- a/pre<RESET>
288+
<BOLD>+++ b/post<RESET>
289289
<CYAN>@@ -1 +1 @@<RESET>
290290
aaa (aaa) <GREEN>aaa<RESET>
291291
EOF
@@ -301,10 +301,10 @@ echo '(:' > pre
301301
echo '(' > post
302302

303303
cat > expect <<\EOF
304-
<WHITE>diff --git a/pre b/post<RESET>
305-
<WHITE>index 289cb9d..2d06f37 100644<RESET>
306-
<WHITE>--- a/pre<RESET>
307-
<WHITE>+++ b/post<RESET>
304+
<BOLD>diff --git a/pre b/post<RESET>
305+
<BOLD>index 289cb9d..2d06f37 100644<RESET>
306+
<BOLD>--- a/pre<RESET>
307+
<BOLD>+++ b/post<RESET>
308308
<CYAN>@@ -1 +1 @@<RESET>
309309
(<RED>:<RESET>
310310
EOF

t/test-lib.sh

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,47 @@ test_set_editor () {
238238
}
239239

240240
test_decode_color () {
241-
sed -e 's/.\[1m/<WHITE>/g' \
242-
-e 's/.\[31m/<RED>/g' \
243-
-e 's/.\[32m/<GREEN>/g' \
244-
-e 's/.\[33m/<YELLOW>/g' \
245-
-e 's/.\[34m/<BLUE>/g' \
246-
-e 's/.\[35m/<MAGENTA>/g' \
247-
-e 's/.\[36m/<CYAN>/g' \
248-
-e 's/.\[m/<RESET>/g'
241+
awk '
242+
function name(n) {
243+
if (n == 0) return "RESET";
244+
if (n == 1) return "BOLD";
245+
if (n == 30) return "BLACK";
246+
if (n == 31) return "RED";
247+
if (n == 32) return "GREEN";
248+
if (n == 33) return "YELLOW";
249+
if (n == 34) return "BLUE";
250+
if (n == 35) return "MAGENTA";
251+
if (n == 36) return "CYAN";
252+
if (n == 37) return "WHITE";
253+
if (n == 40) return "BLACK";
254+
if (n == 41) return "BRED";
255+
if (n == 42) return "BGREEN";
256+
if (n == 43) return "BYELLOW";
257+
if (n == 44) return "BBLUE";
258+
if (n == 45) return "BMAGENTA";
259+
if (n == 46) return "BCYAN";
260+
if (n == 47) return "BWHITE";
261+
}
262+
{
263+
while (match($0, /\x1b\[[0-9;]*m/) != 0) {
264+
printf "%s<", substr($0, 1, RSTART-1);
265+
codes = substr($0, RSTART+2, RLENGTH-3);
266+
if (length(codes) == 0)
267+
printf "%s", name(0)
268+
else {
269+
n = split(codes, ary, ";");
270+
sep = "";
271+
for (i = 1; i <= n; i++) {
272+
printf "%s%s", sep, name(ary[i]);
273+
sep = ";"
274+
}
275+
}
276+
printf ">";
277+
$0 = substr($0, RSTART + RLENGTH, length($0) - RSTART - RLENGTH + 1);
278+
}
279+
print
280+
}
281+
'
249282
}
250283

251284
q_to_nul () {

0 commit comments

Comments
 (0)