Skip to content

Commit 3c4fb51

Browse files
Merge pull request #420 from rwe/quarantest
test: use isolated, temporary `GIT_CONFIG`
2 parents b1deaee + f156545 commit 3c4fb51

File tree

4 files changed

+70
-33
lines changed

4 files changed

+70
-33
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: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
11
#!/usr/bin/env bats
22

3-
load 'test_helper/bats-support/load'
4-
load 'test_helper/bats-assert/load'
5-
load 'test_helper/util'
3+
# Used by both `setup_file` and `setup`, which are special bats callbacks.
4+
__load_imports__() {
5+
load 'test_helper/bats-support/load'
6+
load 'test_helper/bats-assert/load'
7+
load 'test_helper/util'
8+
}
9+
10+
setup_file() {
11+
__load_imports__
12+
setup_default_dsf_git_config
13+
}
14+
15+
setup() {
16+
__load_imports__
17+
}
18+
19+
teardown_file() {
20+
teardown_default_dsf_git_config
21+
}
622

723
# https://github.com/paulirish/dotfiles/commit/6743b907ff586c28cd36e08d1e1c634e2968893e#commitcomment-13459061
824
@test "All removed lines are present in diff" {

test/diff-so-fancy.bats

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
#!/usr/bin/env bats
22

3-
load 'test_helper/bats-support/load'
4-
load 'test_helper/bats-assert/load'
5-
load 'test_helper/util'
3+
# Helper invoked by `setup_file` and `setup`, which are special bats callbacks.
4+
__load_imports__() {
5+
load 'test_helper/bats-support/load'
6+
load 'test_helper/bats-assert/load'
7+
load 'test_helper/util'
8+
}
69

7-
set_env
10+
setup_file() {
11+
__load_imports__
12+
set_env
13+
setup_default_dsf_git_config
14+
# bats fails to handle our multiline result, so we save to $output ourselves
15+
__dsf_cached_output="$( load_fixture "ls-function" | $diff_so_fancy )"
16+
export __dsf_cached_output
17+
}
818

9-
# bats fails to handle our multiline result, so we save to $output ourselves
10-
output=$( load_fixture "ls-function" | $diff_so_fancy )
19+
setup() {
20+
__load_imports__
21+
output="${__dsf_cached_output}"
22+
}
23+
24+
teardown_file() {
25+
teardown_default_dsf_git_config
26+
}
1127

1228
@test "diff-so-fancy runs and exits without error" {
1329
load_fixture "ls-function" | $diff_so_fancy

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)