git/t/t1310-config-default.sh

37 lines
1.0 KiB
Bash
Raw Normal View History

builtin/config: introduce `--default` For some use cases, callers of the `git-config(1)` builtin would like to fallback to default values when the variable asked for does not exist. In addition, users would like to use existing type specifiers to ensure that values are parsed correctly when they do exist in the configuration. For example, to fetch a value without a type specifier and fallback to `$fallback`, the following is required: $ git config core.foo || echo "$fallback" This is fine for most values, but can be tricky for difficult-to-express `$fallback`'s, like ANSI color codes. This motivates `--get-color`, which is a one-off exception to the normal type specifier rules wherein a user specifies both the configuration variable and an optional fallback. Both are formatted according to their type specifier, which eases the burden on the user to ensure that values are correctly formatted. This commit (and those following it in this series) aim to eventually replace `--get-color` with a consistent alternative. By introducing `--default`, we allow the `--get-color` action to be promoted to a `--type=color` type specifier, retaining the "fallback" behavior via the `--default` flag introduced in this commit. For example, we aim to replace: $ git config --get-color variable [default] [...] with: $ git config --default default --type=color variable [...] Values filled by `--default` behave exactly as if they were present in the affected configuration file; they will be parsed by type specifiers without the knowledge that they are not themselves present in the configuration. Specifically, this means that the following will work: $ git config --int --default 1M does.not.exist 1048576 In subsequent commits, we will offer `--type=color`, which (in conjunction with `--default`) will be sufficient to replace `--get-color`. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
2018-04-10 08:18:26 +08:00
#!/bin/sh
test_description='Test git config in different settings (with --default)'
. ./test-lib.sh
test_expect_success 'uses --default when entry missing' '
echo quux >expect &&
git config -f config --default=quux core.foo >actual &&
test_cmp expect actual
'
test_expect_success 'does not use --default when entry present' '
echo bar >expect &&
git -c core.foo=bar config --default=baz core.foo >actual &&
test_cmp expect actual
'
test_expect_success 'canonicalizes --default with appropriate type' '
echo true >expect &&
git config -f config --default=yes --bool core.foo >actual &&
test_cmp expect actual
'
test_expect_success 'dies when --default cannot be parsed' '
test_must_fail git config -f config --type=expiry-date --default=x --get \
not.a.section 2>error &&
test_i18ngrep "failed to format default config value" error
'
test_expect_success 'does not allow --default without --get' '
test_must_fail git config --default=quux --unset a.section >output 2>&1 &&
test_i18ngrep "\-\-default is only applicable to" output
'
test_done