1#!/bin/sh
23
test_description='Test wacky input to git config'
4. ./test-lib.sh
56
# Leaving off the newline is intentional!
7setup() {
8(printf "[section]\n" &&
9printf " key = foo") >.git/config
10}
1112
# 'check section.key value' verifies that the entry for section.key is
13# 'value'
14check() {
15echo "$2" >expected
16git config --get "$1" >actual 2>&1
17test_cmp actual expected
18}
1920
# 'check section.key regex value' verifies that the entry for
21# section.key *that matches 'regex'* is 'value'
22check_regex() {
23echo "$3" >expected
24git config --get "$1" "$2" >actual 2>&1
25test_cmp actual expected
26}
2728
test_expect_success 'modify same key' '
29setup &&
30git config section.key bar &&
31check section.key bar
32'
3334
test_expect_success 'add key in same section' '
35setup &&
36git config section.other bar &&
37check section.key foo &&
38check section.other bar
39'
4041
test_expect_success 'add key in different section' '
42setup &&
43git config section2.key bar &&
44check section.key foo &&
45check section2.key bar
46'
4748
SECTION="test.q\"s\\sq'sp e.key"
49test_expect_success 'make sure git config escapes section names properly' '
50git config "$SECTION" bar &&
51check "$SECTION" bar
52'
5354
LONG_VALUE=$(printf "x%01021dx a" 7)
55test_expect_success 'do not crash on special long config line' '
56setup &&
57git config section.key "$LONG_VALUE" &&
58check section.key "$LONG_VALUE"
59'
6061
setup_many() {
62setup &&
63# This time we want the newline so that we can tack on more
64# entries.
65echo >>.git/config &&
66# Semi-efficient way of concatenating 5^5 = 3125 lines. Note
67# that because 'setup' already put one line, this means 3126
68# entries for section.key in the config file.
69cat >5to1 <<-\EOF &&
70key = foo
71key = foo
72key = foo
73key = foo
74key = foo
75EOF
76cat 5to1 5to1 5to1 5to1 5to1 >5to2 && # 25
77cat 5to2 5to2 5to2 5to2 5to2 >5to3 && # 125
78cat 5to3 5to3 5to3 5to3 5to3 >5to4 && # 635
79cat 5to4 5to4 5to4 5to4 5to4 >>.git/config # 3125
80}
8182
test_expect_success 'get many entries' '
83setup_many &&
84git config --get-all section.key >actual &&
85test_line_count = 3126 actual
86'
8788
test_expect_success 'get many entries by regex' '
89setup_many &&
90git config --get-regexp "sec.*ke." >actual &&
91test_line_count = 3126 actual
92'
9394
test_expect_success 'add and replace one of many entries' '
95setup_many &&
96git config --add section.key bar &&
97check_regex section.key "b.*r" bar &&
98git config section.key beer "b.*r" &&
99check_regex section.key "b.*r" beer
100'
101102
test_expect_success 'replace many entries' '
103setup_many &&
104git config --replace-all section.key bar &&
105check section.key bar
106'
107108
test_expect_success 'unset many entries' '
109setup_many &&
110git config --unset-all section.key &&
111test_must_fail git config section.key
112'
113114
test_expect_success '--add appends new value after existing empty value' '
115cat >expect <<-\EOF &&
116117
118
fool
119roll
120EOF
121cp .git/config .git/config.old &&
122test_when_finished "mv .git/config.old .git/config" &&
123cat >.git/config <<-\EOF &&
124[foo]
125baz
126baz =
127baz = fool
128EOF
129git config --add foo.baz roll &&
130git config --get-all foo.baz >output &&
131test_cmp expect output
132'
133134
test_done