Skip to content

Commit 96386fa

Browse files
dschopeff
authored andcommitted
add -i: verify in the tests that colors can be overridden
Now that the Perl version produces the same output as the built-in version (mostly fixing bugs in the latter), let's add a regression test to verify that it stays this way. Note that we only `grep` for the colored error message instead of verifying that the entire `stderr` consists of just this one line: when running the test script using the `-x` option to trace the commands, the sub-shell in `force_color` causes those commands to be traced into `err.raw` (unless running in Bash where we set the `BASH_XTRACEFD` variable to avoid that). Also note that the color reset in the `<BLUE>+<RESET><BLUE>new<RESET>` line might look funny and unnecessary, as the corresponding `old` line does not reset the color after the diff marker only to turn the color back on right away. However, this is a (necessary) side effect of the white-space check: in `emit_line_ws_markup()`, we first emit the diff marker via `emit_line_0()` and then the rest of the line via `ws_check_emit()`. To leave them somewhat decoupled, the color has to be reset after the diff marker to allow for the rest of the line to start with another color (or inverted, in case of white-space issues). Finally, we have to simulate hunk editing: the `git add -p` command cannot rely on the internal diff machinery for coloring after letting the user edit a hunk; It has to "re-color" the edited hunk. This is the primary reason why that command is interested in the exact values of the `color.diff.*` settings in the first place. To test this re-coloring, we therefore have to pretend to edit a hunk and then show that hunk in the regression test. Co-authored-by: Jeff King <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 890b68b commit 96386fa

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

t/t3701-add-interactive.sh

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,90 @@ test_expect_success 'diffs can be colorized' '
589589
grep "$(printf "\\033")" output
590590
'
591591

592+
test_expect_success 'colors can be overridden' '
593+
git reset --hard &&
594+
test_when_finished "git rm -f color-test" &&
595+
test_write_lines context old more-context >color-test &&
596+
git add color-test &&
597+
test_write_lines context new more-context another-one >color-test &&
598+
599+
echo trigger an error message >input &&
600+
force_color git \
601+
-c color.interactive.error=blue \
602+
add -i 2>err.raw <input &&
603+
test_decode_color <err.raw >err &&
604+
grep "<BLUE>Huh (trigger)?<RESET>" err &&
605+
606+
test_write_lines help quit >input &&
607+
force_color git \
608+
-c color.interactive.header=red \
609+
-c color.interactive.help=green \
610+
-c color.interactive.prompt=yellow \
611+
add -i >actual.raw <input &&
612+
test_decode_color <actual.raw >actual &&
613+
cat >expect <<-\EOF &&
614+
<RED> staged unstaged path<RESET>
615+
1: +3/-0 +2/-1 color-test
616+
617+
<RED>*** Commands ***<RESET>
618+
1: <YELLOW>s<RESET>tatus 2: <YELLOW>u<RESET>pdate 3: <YELLOW>r<RESET>evert 4: <YELLOW>a<RESET>dd untracked
619+
5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>q<RESET>uit 8: <YELLOW>h<RESET>elp
620+
<YELLOW>What now<RESET>> <GREEN>status - show paths with changes<RESET>
621+
<GREEN>update - add working tree state to the staged set of changes<RESET>
622+
<GREEN>revert - revert staged set of changes back to the HEAD version<RESET>
623+
<GREEN>patch - pick hunks and update selectively<RESET>
624+
<GREEN>diff - view diff between HEAD and index<RESET>
625+
<GREEN>add untracked - add contents of untracked files to the staged set of changes<RESET>
626+
<RED>*** Commands ***<RESET>
627+
1: <YELLOW>s<RESET>tatus 2: <YELLOW>u<RESET>pdate 3: <YELLOW>r<RESET>evert 4: <YELLOW>a<RESET>dd untracked
628+
5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>q<RESET>uit 8: <YELLOW>h<RESET>elp
629+
<YELLOW>What now<RESET>> Bye.
630+
EOF
631+
test_cmp expect actual &&
632+
633+
: exercise recolor_hunk by editing and then look at the hunk again &&
634+
test_write_lines s e K q >input &&
635+
force_color git \
636+
-c color.interactive.prompt=yellow \
637+
-c color.diff.meta=italic \
638+
-c color.diff.frag=magenta \
639+
-c color.diff.context=cyan \
640+
-c color.diff.old=bold \
641+
-c color.diff.new=blue \
642+
-c core.editor=touch \
643+
add -p >actual.raw <input &&
644+
test_decode_color <actual.raw >actual.decoded &&
645+
sed "s/index [0-9a-f]*\\.\\.[0-9a-f]* 100644/<INDEX-LINE>/" <actual.decoded >actual &&
646+
cat >expect <<-\EOF &&
647+
<ITALIC>diff --git a/color-test b/color-test<RESET>
648+
<ITALIC><INDEX-LINE><RESET>
649+
<ITALIC>--- a/color-test<RESET>
650+
<ITALIC>+++ b/color-test<RESET>
651+
<MAGENTA>@@ -1,3 +1,4 @@<RESET>
652+
<CYAN> context<RESET>
653+
<BOLD>-old<RESET>
654+
<BLUE>+<RESET><BLUE>new<RESET>
655+
<CYAN> more-context<RESET>
656+
<BLUE>+<RESET><BLUE>another-one<RESET>
657+
<YELLOW>(1/1) Stage this hunk [y,n,q,a,d,s,e,?]? <RESET><BOLD>Split into 2 hunks.<RESET>
658+
<MAGENTA>@@ -1,3 +1,3 @@<RESET>
659+
<CYAN> context<RESET>
660+
<BOLD>-old<RESET>
661+
<BLUE>+<RESET><BLUE>new<RESET>
662+
<CYAN> more-context<RESET>
663+
<YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? <RESET><MAGENTA>@@ -3 +3,2 @@<RESET>
664+
<CYAN> more-context<RESET>
665+
<BLUE>+<RESET><BLUE>another-one<RESET>
666+
<YELLOW>(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? <RESET><MAGENTA>@@ -1,3 +1,3 @@<RESET>
667+
<CYAN> context<RESET>
668+
<BOLD>-old<RESET>
669+
<BLUE>+new<RESET>
670+
<CYAN> more-context<RESET>
671+
<YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? <RESET>
672+
EOF
673+
test_cmp expect actual
674+
'
675+
592676
test_expect_success 'colorized diffs respect diff.wsErrorHighlight' '
593677
git reset --hard &&
594678

0 commit comments

Comments
 (0)