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 <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Oct 21, 2018 at 16:02 UTC
a5db0b77b916014e732155d116575bdffbcbe79b
2 files changed
+42
-60
t/t1300-config.sh
+19
-60
@@ -76,15 +76,11 @@ EOF
76
test_expect_success 'non-match result' 'test_cmp expect .git/config'
77
78
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
80
'
81
82
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
84
'
85
86
test_expect_success 'subsections are not canonicalized by git-config' '
@@ -94,12 +90,8 @@ test_expect_success 'subsections are not canonicalized by git-config' '
90
[section "SubSection"]
91
key = two
92
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
95
'
96
97
cat > .git/config <<\EOF
@@ -212,9 +204,7 @@ test_expect_success 'really really mean test' '
204
'
205
206
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
208
'
209
210
cat > expect << EOF
@@ -251,15 +241,11 @@ test_expect_success 'non-match' '
241
'
242
243
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
245
'
246
247
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
249
'
250
251
test_expect_success 'multi-valued get-all returns all' '
@@ -520,21 +506,11 @@ test_expect_success 'editing stdin is an error' '
506
507
test_expect_success 'refer config from subdirectory' '
508
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
510
'
511
512
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
514
'
515
516
cat > expect << EOF
@@ -688,16 +664,13 @@ test_expect_success numbers '
664
665
test_expect_success '--int is at least 64 bits' '
666
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
669
'
670
671
test_expect_success 'invalid unit' '
672
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 &&
674
test_must_fail git config --int --get aninvalid.unit 2>actual &&
675
test_i18ngrep "bad numeric config value .1auto. for .aninvalid.unit. in file .git/config: invalid unit" actual
676
'
@@ -1039,9 +1012,7 @@ test_expect_success '--null --get-regexp' '
1012
1013
test_expect_success 'inner whitespace kept verbatim' '
1014
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
1016
'
1017
1018
test_expect_success SYMLINKS 'symlinked configuration' '
@@ -1809,21 +1780,15 @@ big = 1M
1780
EOF
1781
1782
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
1784
'
1785
1786
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
1788
'
1789
1790
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
1792
'
1793
1794
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' '
1807
'
1808
1809
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
1811
'
1812
1813
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
1815
'
1816
1817
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
1819
'
1820
1821
test_expect_success '--type rejects unknown specifiers' '
t/test-lib-functions.sh
+23
@@ -747,6 +747,29 @@ test_cmp() {
747
$GIT_TEST_CMP "$@"
748
}
749
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
+
773
# test_cmp_bin - helper to compare binary files
774
775
test_cmp_bin() {