diff --git a/diff-so-fancy b/diff-so-fancy index 049aa37..5fd758f 100755 --- a/diff-so-fancy +++ b/diff-so-fancy @@ -490,26 +490,12 @@ sub git_config_boolean { my $search_key = lc($_[0] || ""); my $default_value = lc($_[1] || 0); # Default to false - # If we're in a unit test, use the default (don't read the users config) - if (in_unit_test()) { - return boolean($default_value); - } - my $result = git_config($search_key,$default_value); my $ret = boolean($result); return $ret; } -# Check if we're inside of BATS -sub in_unit_test { - if ($ENV{BATS_CWD}) { - return 1; - } else { - return 0; - } -} - sub get_less_charset { my @less_char_vars = ("LESSCHARSET", "LESSCHARDEF", "LC_ALL", "LC_CTYPE", "LANG"); foreach my $key (@less_char_vars) { diff --git a/test/bugs.bats b/test/bugs.bats index 0e49052..d0934ec 100644 --- a/test/bugs.bats +++ b/test/bugs.bats @@ -1,8 +1,24 @@ #!/usr/bin/env bats -load 'test_helper/bats-support/load' -load 'test_helper/bats-assert/load' -load 'test_helper/util' +# Used by both `setup_file` and `setup`, which are special bats callbacks. +__load_imports__() { + load 'test_helper/bats-support/load' + load 'test_helper/bats-assert/load' + load 'test_helper/util' +} + +setup_file() { + __load_imports__ + setup_default_dsf_git_config +} + +setup() { + __load_imports__ +} + +teardown_file() { + teardown_default_dsf_git_config +} # https://github.com/paulirish/dotfiles/commit/6743b907ff586c28cd36e08d1e1c634e2968893e#commitcomment-13459061 @test "All removed lines are present in diff" { diff --git a/test/diff-so-fancy.bats b/test/diff-so-fancy.bats index 62d3a3d..31d9a56 100644 --- a/test/diff-so-fancy.bats +++ b/test/diff-so-fancy.bats @@ -1,13 +1,29 @@ #!/usr/bin/env bats -load 'test_helper/bats-support/load' -load 'test_helper/bats-assert/load' -load 'test_helper/util' +# Helper invoked by `setup_file` and `setup`, which are special bats callbacks. +__load_imports__() { + load 'test_helper/bats-support/load' + load 'test_helper/bats-assert/load' + load 'test_helper/util' +} -set_env +setup_file() { + __load_imports__ + set_env + setup_default_dsf_git_config + # bats fails to handle our multiline result, so we save to $output ourselves + __dsf_cached_output="$( load_fixture "ls-function" | $diff_so_fancy )" + export __dsf_cached_output +} -# bats fails to handle our multiline result, so we save to $output ourselves -output=$( load_fixture "ls-function" | $diff_so_fancy ) +setup() { + __load_imports__ + output="${__dsf_cached_output}" +} + +teardown_file() { + teardown_default_dsf_git_config +} @test "diff-so-fancy runs and exits without error" { load_fixture "ls-function" | $diff_so_fancy diff --git a/test/test_helper/util.bash b/test/test_helper/util.bash index 440d3bd..dbad4b1 100644 --- a/test/test_helper/util.bash +++ b/test/test_helper/util.bash @@ -10,17 +10,36 @@ set_env() { export LC_CTYPE="en_US.UTF-8" } +dsf_test_git_config() { + printf '%s/gitconfig' "${BATS_TMPDIR}" +} # applying colors so ANSI color values will match # FIXME: not everyone will have these set, so we need to test for that. -git config color.diff.meta "227" -git config color.diff.frag "magenta bold" -git config color.diff.commit "227 bold" -git config color.diff.old "red bold" -git config color.diff.new "green bold" -git config color.diff.whitespace "red reverse" +setup_default_dsf_git_config() { + GIT_CONFIG="$(dsf_test_git_config)" || return $? + cat > "${GIT_CONFIG}" <