diff mbox series

[1/2] t4034: extract out `diff_with_opts`

Message ID 20240302095751.123138-2-karthik.188@gmail.com (mailing list archive)
State New, archived
Headers show
Series Support diff.wordDiff config | expand

Commit Message

Karthik Nayak March 2, 2024, 9:57 a.m. UTC
The current `word_diff` function in t4034 uses the arguments provided to
obtain the diff and compare them.

Let's rename this to `diff_with_opts` and add a new function `word_diff`
which is specific to the `--word-diff=` option. This function will act
as a wrapper around `diff_with_opts`.

In the following commit, when we introduce a config option for
`--word-diff`, we'd want to test the existing tests also with the config
option and at this point we can expand `word_diff` to test both config
and command line options.

Signed-off-by: Karthik Nayak <karthik.188@gmail.com>
---
 t/t4034-diff-words.sh | 54 +++++++++++++++++++++++--------------------
 1 file changed, 29 insertions(+), 25 deletions(-)
diff mbox series

Patch

diff --git a/t/t4034-diff-words.sh b/t/t4034-diff-words.sh
index 74586f3813..4f70aa6e9f 100755
--- a/t/t4034-diff-words.sh
+++ b/t/t4034-diff-words.sh
@@ -51,7 +51,7 @@  cat >expect.non-whitespace-is-word <<-EOF
 	<GREEN>aeff = aeff * ( aaa )<RESET>
 EOF
 
-word_diff () {
+diff_with_opts () {
 	pre=$(git rev-parse --short $(git hash-object pre)) &&
 	post=$(git rev-parse --short $(git hash-object post)) &&
 	test_must_fail git diff --no-index "$@" pre post >output &&
@@ -60,6 +60,10 @@  word_diff () {
 	test_cmp expected output.decrypted
 }
 
+word_diff () {
+	diff_with_opts "--word-diff=$1" $(echo "$@" | cut -d' ' -s -f 2-)
+}
+
 test_language_driver () {
 	lang=$1
 	test_expect_success "diff driver '$lang'" '
@@ -67,11 +71,11 @@  test_language_driver () {
 			"$TEST_DIRECTORY/t4034/'"$lang"'/post" \
 			"$TEST_DIRECTORY/t4034/'"$lang"'/expect" . &&
 		echo "* diff='"$lang"'" >.gitattributes &&
-		word_diff --color-words
+		diff_with_opts --color-words
 	'
 	test_expect_success "diff driver '$lang' in Islandic" '
 		LANG=is_IS.UTF-8 LANGUAGE=is LC_ALL="$is_IS_locale" \
-		word_diff --color-words
+		diff_with_opts --color-words
 	'
 }
 
@@ -81,7 +85,7 @@  test_expect_success setup '
 	git config diff.color.func magenta
 '
 
-test_expect_success 'set up pre and post with runs of whitespace' '
+test_expect_success 'setup pre and post with runs of whitespace' '
 	cp pre.simple pre &&
 	cp post.simple post
 '
@@ -101,9 +105,9 @@  test_expect_success 'word diff with runs of whitespace' '
 
 		<GREEN>aeff = aeff * ( aaa )<RESET>
 	EOF
-	word_diff --color-words &&
-	word_diff --word-diff=color &&
-	word_diff --color --word-diff=color
+	diff_with_opts --color-words &&
+	word_diff color &&
+	diff_with_opts --color --word-diff=color
 '
 
 test_expect_success '--word-diff=porcelain' '
@@ -127,7 +131,7 @@  test_expect_success '--word-diff=porcelain' '
 		+aeff = aeff * ( aaa )
 		~
 	EOF
-	word_diff --word-diff=porcelain
+	word_diff porcelain
 '
 
 test_expect_success '--word-diff=plain' '
@@ -145,8 +149,8 @@  test_expect_success '--word-diff=plain' '
 
 		{+aeff = aeff * ( aaa )+}
 	EOF
-	word_diff --word-diff=plain &&
-	word_diff --word-diff=plain --no-color
+	diff_with_opts --word-diff=plain &&
+	word_diff plain --no-color
 '
 
 test_expect_success '--word-diff=plain --color' '
@@ -164,7 +168,7 @@  test_expect_success '--word-diff=plain --color' '
 
 		<GREEN>{+aeff = aeff * ( aaa )+}<RESET>
 	EOF
-	word_diff --word-diff=plain --color
+	word_diff plain --color
 '
 
 test_expect_success 'word diff without context' '
@@ -181,17 +185,17 @@  test_expect_success 'word diff without context' '
 
 		<GREEN>aeff = aeff * ( aaa )<RESET>
 	EOF
-	word_diff --color-words --unified=0
+	diff_with_opts --color-words --unified=0
 '
 
 test_expect_success 'word diff with a regular expression' '
 	cp expect.letter-runs-are-words expect &&
-	word_diff --color-words="[a-z]+"
+	diff_with_opts --color-words="[a-z]+"
 '
 
 test_expect_success 'word diff with zero length matches' '
 	cp expect.letter-runs-are-words expect &&
-	word_diff --color-words="[a-z${LF}]*"
+	diff_with_opts --color-words="[a-z${LF}]*"
 '
 
 test_expect_success 'set up a diff driver' '
@@ -204,12 +208,12 @@  test_expect_success 'set up a diff driver' '
 
 test_expect_success 'option overrides .gitattributes' '
 	cp expect.letter-runs-are-words expect &&
-	word_diff --color-words="[a-z]+"
+	diff_with_opts --color-words="[a-z]+"
 '
 
 test_expect_success 'use regex supplied by driver' '
 	cp expect.non-whitespace-is-word expect &&
-	word_diff --color-words
+	diff_with_opts --color-words
 '
 
 test_expect_success 'set up diff.wordRegex option' '
@@ -218,7 +222,7 @@  test_expect_success 'set up diff.wordRegex option' '
 
 test_expect_success 'command-line overrides config' '
 	cp expect.letter-runs-are-words expect &&
-	word_diff --color-words="[a-z]+"
+	diff_with_opts --color-words="[a-z]+"
 '
 
 test_expect_success 'command-line overrides config: --word-diff-regex' '
@@ -236,12 +240,12 @@  test_expect_success 'command-line overrides config: --word-diff-regex' '
 
 		<GREEN>{+aeff = aeff * ( aaa+}<RESET> )
 	EOF
-	word_diff --color --word-diff-regex="[a-z]+"
+	diff_with_opts --color --word-diff-regex="[a-z]+"
 '
 
 test_expect_success '.gitattributes override config' '
 	cp expect.non-whitespace-is-word expect &&
-	word_diff --color-words
+	diff_with_opts --color-words
 '
 
 test_expect_success 'setup: remove diff driver regex' '
@@ -263,7 +267,7 @@  test_expect_success 'use configured regex' '
 
 		<GREEN>aeff = aeff * ( aaa<RESET> )
 	EOF
-	word_diff --color-words
+	diff_with_opts --color-words
 '
 
 test_expect_success 'test parsing words for newline' '
@@ -279,7 +283,7 @@  test_expect_success 'test parsing words for newline' '
 		<CYAN>@@ -1 +1 @@<RESET>
 		aaa (aaa) <GREEN>aaa<RESET>
 	EOF
-	word_diff --color-words="a+"
+	diff_with_opts --color-words="a+"
 '
 
 test_expect_success 'test when words are only removed at the end' '
@@ -295,7 +299,7 @@  test_expect_success 'test when words are only removed at the end' '
 		<CYAN>@@ -1 +1 @@<RESET>
 		(<RED>:<RESET>
 	EOF
-	word_diff --color-words=.
+	diff_with_opts --color-words=.
 '
 
 test_expect_success '--word-diff=none' '
@@ -312,7 +316,7 @@  test_expect_success '--word-diff=none' '
 		-(:
 		+(
 	EOF
-	word_diff --word-diff=plain --word-diff=none
+	word_diff plain --word-diff=none
 '
 
 test_expect_success 'unset default driver' '
@@ -363,7 +367,7 @@  test_expect_success 'word-diff with diff.sbe' '
 	[-b-]{+c+}
 	EOF
 	test_config diff.suppress-blank-empty true &&
-	word_diff --word-diff=plain
+	word_diff plain
 '
 
 test_expect_success 'word-diff with no newline at EOF' '
@@ -379,7 +383,7 @@  test_expect_success 'word-diff with no newline at EOF' '
 	@@ -1 +1 @@
 	a a [-a-]{+ab+} a a
 	EOF
-	word_diff --word-diff=plain
+	word_diff plain
 '
 
 test_expect_success 'setup history with two files' '