Skip to content

Commit f156545

Browse files
committed
test: use completely isolated, temporary GIT_CONFIG
This writes a temp git config file during setup/teardown, and exports GIT_CONFIG=… to that and sets GIT_CONFIG_NOSYSTEM=1. This ensures that any git invocations _only_ use that configuration. Note that none of the tests currently test anything other than the default config; however, if they do, the generation of this test config should be moved into `setup()` rather than `setup_file()` so that it can be modified and restored between tests.
1 parent 7fb648e commit f156545

File tree

4 files changed

+42
-27
lines changed

4 files changed

+42
-27
lines changed

diff-so-fancy

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -490,26 +490,12 @@ sub git_config_boolean {
490490
my $search_key = lc($_[0] || "");
491491
my $default_value = lc($_[1] || 0); # Default to false
492492

493-
# If we're in a unit test, use the default (don't read the users config)
494-
if (in_unit_test()) {
495-
return boolean($default_value);
496-
}
497-
498493
my $result = git_config($search_key,$default_value);
499494
my $ret = boolean($result);
500495

501496
return $ret;
502497
}
503498

504-
# Check if we're inside of BATS
505-
sub in_unit_test {
506-
if ($ENV{BATS_CWD}) {
507-
return 1;
508-
} else {
509-
return 0;
510-
}
511-
}
512-
513499
sub get_less_charset {
514500
my @less_char_vars = ("LESSCHARSET", "LESSCHARDEF", "LC_ALL", "LC_CTYPE", "LANG");
515501
foreach my $key (@less_char_vars) {

test/bugs.bats

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@ __load_imports__() {
99

1010
setup_file() {
1111
__load_imports__
12+
setup_default_dsf_git_config
1213
}
1314

1415
setup() {
1516
__load_imports__
1617
}
1718

19+
teardown_file() {
20+
teardown_default_dsf_git_config
21+
}
22+
1823
# https://github.com/paulirish/dotfiles/commit/6743b907ff586c28cd36e08d1e1c634e2968893e#commitcomment-13459061
1924
@test "All removed lines are present in diff" {
2025
output=$( load_fixture "chromium-modaltoelement" | $diff_so_fancy )

test/diff-so-fancy.bats

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,19 @@ __load_imports__() {
1010
setup_file() {
1111
__load_imports__
1212
set_env
13+
setup_default_dsf_git_config
1314
# bats fails to handle our multiline result, so we save to $output ourselves
14-
__dfs_cached_output="$( load_fixture "ls-function" | $diff_so_fancy )"
15-
export __dfs_cached_output
15+
__dsf_cached_output="$( load_fixture "ls-function" | $diff_so_fancy )"
16+
export __dsf_cached_output
1617
}
1718

1819
setup() {
1920
__load_imports__
20-
output="${__dfs_cached_output}"
21+
output="${__dsf_cached_output}"
22+
}
23+
24+
teardown_file() {
25+
teardown_default_dsf_git_config
2126
}
2227

2328
@test "diff-so-fancy runs and exits without error" {

test/test_helper/util.bash

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,36 @@ set_env() {
1010
export LC_CTYPE="en_US.UTF-8"
1111
}
1212

13+
dsf_test_git_config() {
14+
printf '%s/gitconfig' "${BATS_TMPDIR}"
15+
}
1316

1417
# applying colors so ANSI color values will match
1518
# FIXME: not everyone will have these set, so we need to test for that.
16-
git config color.diff.meta "227"
17-
git config color.diff.frag "magenta bold"
18-
git config color.diff.commit "227 bold"
19-
git config color.diff.old "red bold"
20-
git config color.diff.new "green bold"
21-
git config color.diff.whitespace "red reverse"
19+
setup_default_dsf_git_config() {
20+
GIT_CONFIG="$(dsf_test_git_config)" || return $?
21+
cat > "${GIT_CONFIG}" <<EOF || return $?
22+
[color "diff"]
23+
meta = 227
24+
frag = magenta bold
25+
commit = 227 bold
26+
old = red bold
27+
new = green bold
28+
whitespace = red reverse
29+
30+
[color "diff-highlight"]
31+
oldNormal = red bold
32+
oldHighlight = red bold 52
33+
newNormal = green bold
34+
newHighlight = green bold 22
35+
EOF
36+
export GIT_CONFIG
37+
export GIT_CONFIG_NOSYSTEM=1
38+
}
2239

23-
git config color.diff-highlight.oldNormal "red bold"
24-
git config color.diff-highlight.oldHighlight "red bold 52"
25-
git config color.diff-highlight.newNormal "green bold"
26-
git config color.diff-highlight.newHighlight "green bold 22"
40+
teardown_default_dsf_git_config() {
41+
local test_config
42+
test_config="$(dsf_test_git_config)" || return $?
43+
[ ! -f "${test_config}" ] || rm -f "${test_config}"
44+
GIT_CONFIG=/dev/null
45+
}

0 commit comments

Comments
 (0)