Skip to content

Commit a5db0b7

Browse files
pcloudsgitster
authored andcommitted
t1300: extract and use test_cmp_config()
In many config-related tests it's common to check if a config variable has expected value and we want to print the differences when the test fails. Doing it the normal way is three lines of shell code. Let's add a function do to all this (and a little more). This function has uses outside t1300 as well but I'm not going to convert them all. And it will be used in the next commit where per-worktree config feature is introduced. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c4df23f commit a5db0b7

File tree

2 files changed

+42
-60
lines changed

2 files changed

+42
-60
lines changed

t/t1300-config.sh

Lines changed: 19 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,11 @@ EOF
7676
test_expect_success 'non-match result' 'test_cmp expect .git/config'
7777

7878
test_expect_success 'find mixed-case key by canonical name' '
79-
echo Second >expect &&
80-
git config cores.whatever >actual &&
81-
test_cmp expect actual
79+
test_cmp_config Second cores.whatever
8280
'
8381

8482
test_expect_success 'find mixed-case key by non-canonical name' '
85-
echo Second >expect &&
86-
git config CoReS.WhAtEvEr >actual &&
87-
test_cmp expect actual
83+
test_cmp_config Second CoReS.WhAtEvEr
8884
'
8985

9086
test_expect_success 'subsections are not canonicalized by git-config' '
@@ -94,12 +90,8 @@ test_expect_success 'subsections are not canonicalized by git-config' '
9490
[section "SubSection"]
9591
key = two
9692
EOF
97-
echo one >expect &&
98-
git config section.subsection.key >actual &&
99-
test_cmp expect actual &&
100-
echo two >expect &&
101-
git config section.SubSection.key >actual &&
102-
test_cmp expect actual
93+
test_cmp_config one section.subsection.key &&
94+
test_cmp_config two section.SubSection.key
10395
'
10496

10597
cat > .git/config <<\EOF
@@ -212,9 +204,7 @@ test_expect_success 'really really mean test' '
212204
'
213205

214206
test_expect_success 'get value' '
215-
echo alpha >expect &&
216-
git config beta.haha >actual &&
217-
test_cmp expect actual
207+
test_cmp_config alpha beta.haha
218208
'
219209

220210
cat > expect << EOF
@@ -251,15 +241,11 @@ test_expect_success 'non-match' '
251241
'
252242

253243
test_expect_success 'non-match value' '
254-
echo wow >expect &&
255-
git config --get nextsection.nonewline !for >actual &&
256-
test_cmp expect actual
244+
test_cmp_config wow --get nextsection.nonewline !for
257245
'
258246

259247
test_expect_success 'multi-valued get returns final one' '
260-
echo "wow2 for me" >expect &&
261-
git config --get nextsection.nonewline >actual &&
262-
test_cmp expect actual
248+
test_cmp_config "wow2 for me" --get nextsection.nonewline
263249
'
264250

265251
test_expect_success 'multi-valued get-all returns all' '
@@ -520,21 +506,11 @@ test_expect_success 'editing stdin is an error' '
520506

521507
test_expect_success 'refer config from subdirectory' '
522508
mkdir x &&
523-
(
524-
cd x &&
525-
echo strasse >expect &&
526-
git config --get --file ../other-config ein.bahn >actual &&
527-
test_cmp expect actual
528-
)
529-
509+
test_cmp_config -C x strasse --get --file ../other-config ein.bahn
530510
'
531511

532512
test_expect_success 'refer config from subdirectory via --file' '
533-
(
534-
cd x &&
535-
git config --file=../other-config --get ein.bahn >actual &&
536-
test_cmp expect actual
537-
)
513+
test_cmp_config -C x strasse --file=../other-config --get ein.bahn
538514
'
539515

540516
cat > expect << EOF
@@ -688,16 +664,13 @@ test_expect_success numbers '
688664

689665
test_expect_success '--int is at least 64 bits' '
690666
git config giga.watts 121g &&
691-
echo 129922760704 >expect &&
692-
git config --int --get giga.watts >actual &&
693-
test_cmp expect actual
667+
echo >expect &&
668+
test_cmp_config 129922760704 --int --get giga.watts
694669
'
695670

696671
test_expect_success 'invalid unit' '
697672
git config aninvalid.unit "1auto" &&
698-
echo 1auto >expect &&
699-
git config aninvalid.unit >actual &&
700-
test_cmp expect actual &&
673+
test_cmp_config 1auto aninvalid.unit &&
701674
test_must_fail git config --int --get aninvalid.unit 2>actual &&
702675
test_i18ngrep "bad numeric config value .1auto. for .aninvalid.unit. in file .git/config: invalid unit" actual
703676
'
@@ -1039,9 +1012,7 @@ test_expect_success '--null --get-regexp' '
10391012

10401013
test_expect_success 'inner whitespace kept verbatim' '
10411014
git config section.val "foo bar" &&
1042-
echo "foo bar" >expect &&
1043-
git config section.val >actual &&
1044-
test_cmp expect actual
1015+
test_cmp_config "foo bar" section.val
10451016
'
10461017

10471018
test_expect_success SYMLINKS 'symlinked configuration' '
@@ -1809,21 +1780,15 @@ big = 1M
18091780
EOF
18101781

18111782
test_expect_success 'identical modern --type specifiers are allowed' '
1812-
git config --type=int --type=int core.big >actual &&
1813-
echo 1048576 >expect &&
1814-
test_cmp expect actual
1783+
test_cmp_config 1048576 --type=int --type=int core.big
18151784
'
18161785

18171786
test_expect_success 'identical legacy --type specifiers are allowed' '
1818-
git config --int --int core.big >actual &&
1819-
echo 1048576 >expect &&
1820-
test_cmp expect actual
1787+
test_cmp_config 1048576 --int --int core.big
18211788
'
18221789

18231790
test_expect_success 'identical mixed --type specifiers are allowed' '
1824-
git config --int --type=int core.big >actual &&
1825-
echo 1048576 >expect &&
1826-
test_cmp expect actual
1791+
test_cmp_config 1048576 --int --type=int core.big
18271792
'
18281793

18291794
test_expect_success 'non-identical modern --type specifiers are not allowed' '
@@ -1842,21 +1807,15 @@ test_expect_success 'non-identical mixed --type specifiers are not allowed' '
18421807
'
18431808

18441809
test_expect_success '--type allows valid type specifiers' '
1845-
echo "true" >expect &&
1846-
git config --type=bool core.foo >actual &&
1847-
test_cmp expect actual
1810+
test_cmp_config true --type=bool core.foo
18481811
'
18491812

18501813
test_expect_success '--no-type unsets type specifiers' '
1851-
echo "10" >expect &&
1852-
git config --type=bool --no-type core.number >actual &&
1853-
test_cmp expect actual
1814+
test_cmp_config 10 --type=bool --no-type core.number
18541815
'
18551816

18561817
test_expect_success 'unset type specifiers may be reset to conflicting ones' '
1857-
echo 1048576 >expect &&
1858-
git config --type=bool --no-type --type=int core.big >actual &&
1859-
test_cmp expect actual
1818+
test_cmp_config 1048576 --type=bool --no-type --type=int core.big
18601819
'
18611820

18621821
test_expect_success '--type rejects unknown specifiers' '

t/test-lib-functions.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,29 @@ test_cmp() {
747747
$GIT_TEST_CMP "$@"
748748
}
749749

750+
# Check that the given config key has the expected value.
751+
#
752+
# test_cmp_config [-C <dir>] <expected-value>
753+
# [<git-config-options>...] <config-key>
754+
#
755+
# for example to check that the value of core.bar is foo
756+
#
757+
# test_cmp_config foo core.bar
758+
#
759+
test_cmp_config() {
760+
local GD &&
761+
if test "$1" = "-C"
762+
then
763+
shift &&
764+
GD="-C $1" &&
765+
shift
766+
fi &&
767+
printf "%s\n" "$1" >expect.config &&
768+
shift &&
769+
git $GD config "$@" >actual.config &&
770+
test_cmp expect.config actual.config
771+
}
772+
750773
# test_cmp_bin - helper to compare binary files
751774

752775
test_cmp_bin() {

0 commit comments

Comments
 (0)