From 818a95d79ca42dd3496d3aa669f98d2e85c12d56 Mon Sep 17 00:00:00 2001 From: Nils Winkler Date: Mon, 13 Apr 2020 21:10:50 +0200 Subject: [PATCH] Refactored test setup code Removed the copy/paste code, moved it into a reusable function that can be called from the `local_setup` function as needed. Also simplified the handling of the $HOME variable since @rico-chet provided a better way of doing this. --- test/bash_it/bash_it.bats | 28 +--------- test/completion/bash-it.completion.bats | 31 +---------- test/install/install.bats | 26 +-------- test/install/uninstall.bats | 26 +-------- test/lib/helpers.bats | 16 +----- test/lib/search.bats | 17 +----- test/lib/utilities.bats | 17 +----- test/plugins/ruby.plugin.bats | 16 +----- test/test_helper.bash | 70 +++++++++++++++++-------- 9 files changed, 56 insertions(+), 191 deletions(-) diff --git a/test/bash_it/bash_it.bats b/test/bash_it/bash_it.bats index 95bad253..af2f15e9 100644 --- a/test/bash_it/bash_it.bats +++ b/test/bash_it/bash_it.bats @@ -4,36 +4,10 @@ load ../test_helper load ../../lib/composure function local_setup { - mkdir -p "$BASH_IT" - lib_directory="$(cd "$(dirname "$0")" && pwd)" - echo "Bi : $BASH_IT" - echo "Lib: $lib_directory" - # Use rsync to copy Bash-it to the temp folder - # rsync is faster than cp, since we can exclude the large ".git" folder - rsync -qavrKL -d --delete-excluded --exclude=.git --exclude=enabled $lib_directory/../../../.. "$BASH_IT" - - rm -rf "$BASH_IT"/enabled - rm -rf "$BASH_IT"/aliases/enabled - rm -rf "$BASH_IT"/completion/enabled - rm -rf "$BASH_IT"/plugins/enabled + setup_test_fixture # Copy the test fixture to the Bash-it folder rsync -a "$BASH_IT/test/fixtures/bash_it/" "$BASH_IT/" - - # Don't pollute the user's actual $HOME directory - # Use a test home directory instead - export BASH_IT_TEST_CURRENT_HOME="${HOME}" - export BASH_IT_TEST_HOME="$(cd "${BASH_IT}/.." && pwd)/BASH_IT_TEST_HOME" - mkdir -p "${BASH_IT_TEST_HOME}" - export HOME="${BASH_IT_TEST_HOME}" -} - -function local_teardown { - export HOME="${BASH_IT_TEST_CURRENT_HOME}" - - rm -rf "${BASH_IT_TEST_HOME}" - - assert_equal "${BASH_IT_TEST_CURRENT_HOME}" "${HOME}" } @test "bash-it: verify that the test fixture is available" { diff --git a/test/completion/bash-it.completion.bats b/test/completion/bash-it.completion.bats index 9b111512..87c697e4 100644 --- a/test/completion/bash-it.completion.bats +++ b/test/completion/bash-it.completion.bats @@ -5,36 +5,7 @@ load ../../lib/composure load ../../completion/available/bash-it.completion function local_setup { - mkdir -p "$BASH_IT" - lib_directory="$(cd "$(dirname "$0")" && pwd)" - # Use rsync to copy Bash-it to the temp folder - # rsync is faster than cp, since we can exclude the large ".git" folder - rsync -qavrKL -d --delete-excluded --exclude=.git --exclude=enabled $lib_directory/../../../.. "$BASH_IT" - - rm -rf "$BASH_IT"/enabled - rm -rf "$BASH_IT"/aliases/enabled - rm -rf "$BASH_IT"/completion/enabled - rm -rf "$BASH_IT"/plugins/enabled - - mkdir -p "$BASH_IT"/enabled - mkdir -p "$BASH_IT"/aliases/enabled - mkdir -p "$BASH_IT"/completion/enabled - mkdir -p "$BASH_IT"/plugins/enabled - - # Don't pollute the user's actual $HOME directory - # Use a test home directory instead - export BASH_IT_TEST_CURRENT_HOME="${HOME}" - export BASH_IT_TEST_HOME="$(cd "${BASH_IT}/.." && pwd)/BASH_IT_TEST_HOME" - mkdir -p "${BASH_IT_TEST_HOME}" - export HOME="${BASH_IT_TEST_HOME}" -} - -function local_teardown { - export HOME="${BASH_IT_TEST_CURRENT_HOME}" - - rm -rf "${BASH_IT_TEST_HOME}" - - assert_equal "${BASH_IT_TEST_CURRENT_HOME}" "${HOME}" + setup_test_fixture } @test "completion bash-it: ensure that the _bash-it-comp function is available" { diff --git a/test/install/install.bats b/test/install/install.bats index 2a754e42..01776f25 100644 --- a/test/install/install.bats +++ b/test/install/install.bats @@ -14,31 +14,7 @@ case $OSTYPE in esac function local_setup { - mkdir -p "$BASH_IT" - lib_directory="$(cd "$(dirname "$0")" && pwd)" - # Use rsync to copy Bash-it to the temp folder - # rsync is faster than cp, since we can exclude the large ".git" folder - rsync -qavrKL -d --delete-excluded --exclude=.git --exclude=enabled $lib_directory/../../../.. "$BASH_IT" - - rm -rf "$BASH_IT"/enabled - rm -rf "$BASH_IT"/aliases/enabled - rm -rf "$BASH_IT"/completion/enabled - rm -rf "$BASH_IT"/plugins/enabled - - # Don't pollute the user's actual $HOME directory - # Use a test home directory instead - export BASH_IT_TEST_CURRENT_HOME="${HOME}" - export BASH_IT_TEST_HOME="$(cd "${BASH_IT}/.." && pwd)/BASH_IT_TEST_HOME" - mkdir -p "${BASH_IT_TEST_HOME}" - export HOME="${BASH_IT_TEST_HOME}" -} - -function local_teardown { - export HOME="${BASH_IT_TEST_CURRENT_HOME}" - - rm -rf "${BASH_IT_TEST_HOME}" - - assert_equal "${BASH_IT_TEST_CURRENT_HOME}" "${HOME}" + setup_test_fixture } @test "install: verify that the install script exists" { diff --git a/test/install/uninstall.bats b/test/install/uninstall.bats index 315c8209..b2439b9d 100644 --- a/test/install/uninstall.bats +++ b/test/install/uninstall.bats @@ -14,31 +14,7 @@ case $OSTYPE in esac function local_setup { - mkdir -p "$BASH_IT" - lib_directory="$(cd "$(dirname "$0")" && pwd)" - # Use rsync to copy Bash-it to the temp folder - # rsync is faster than cp, since we can exclude the large ".git" folder - rsync -qavrKL -d --delete-excluded --exclude=.git --exclude=enabled $lib_directory/../../../.. "$BASH_IT" - - rm -rf "$BASH_IT"/enabled - rm -rf "$BASH_IT"/aliases/enabled - rm -rf "$BASH_IT"/completion/enabled - rm -rf "$BASH_IT"/plugins/enabled - - # Don't pollute the user's actual $HOME directory - # Use a test home directory instead - export BASH_IT_TEST_CURRENT_HOME="${HOME}" - export BASH_IT_TEST_HOME="$(cd "${BASH_IT}/.." && pwd)/BASH_IT_TEST_HOME" - mkdir -p "${BASH_IT_TEST_HOME}" - export HOME="${BASH_IT_TEST_HOME}" -} - -function local_teardown { - export HOME="${BASH_IT_TEST_CURRENT_HOME}" - - rm -rf "${BASH_IT_TEST_HOME}" - - assert_equal "${BASH_IT_TEST_CURRENT_HOME}" "${HOME}" + setup_test_fixture } @test "uninstall: verify that the uninstall script exists" { diff --git a/test/lib/helpers.bats b/test/lib/helpers.bats index ef5ed182..c3f905e7 100644 --- a/test/lib/helpers.bats +++ b/test/lib/helpers.bats @@ -11,21 +11,7 @@ cite _about _param _example _group _author _version load ../../lib/helpers function local_setup { - mkdir -p "$BASH_IT" - lib_directory="$(cd "$(dirname "$0")" && pwd)" - # Use rsync to copy Bash-it to the temp folder - # rsync is faster than cp, since we can exclude the large ".git" folder - rsync -qavrKL -d --delete-excluded --exclude=.git --exclude=enabled $lib_directory/../../../.. "$BASH_IT" - - rm -rf "$BASH_IT"/enabled - rm -rf "$BASH_IT"/aliases/enabled - rm -rf "$BASH_IT"/completion/enabled - rm -rf "$BASH_IT"/plugins/enabled - - mkdir -p "$BASH_IT"/enabled - mkdir -p "$BASH_IT"/aliases/enabled - mkdir -p "$BASH_IT"/completion/enabled - mkdir -p "$BASH_IT"/plugins/enabled + setup_test_fixture } # TODO Create global __is_enabled function diff --git a/test/lib/search.bats b/test/lib/search.bats index e7e8b4a8..da448acc 100644 --- a/test/lib/search.bats +++ b/test/lib/search.bats @@ -18,22 +18,7 @@ cite _about _param _example _group _author _version load ../../lib/helpers function local_setup { - mkdir -p "$BASH_IT" - lib_directory="$(cd "$(dirname "$0")" && pwd)" - # Use rsync to copy Bash-it to the temp folder - # rsync is faster than cp, since we can exclude the large ".git" folder - rsync -qavrKL -d --delete-excluded --exclude=.git --exclude=enabled $lib_directory/../../../.. "$BASH_IT" - - rm -rf "$BASH_IT"/enabled - rm -rf "$BASH_IT"/aliases/enabled - rm -rf "$BASH_IT"/completion/enabled - rm -rf "$BASH_IT"/plugins/enabled - rm -rf "$BASH_IT"/tmp/cache - - mkdir -p "$BASH_IT"/enabled - mkdir -p "$BASH_IT"/aliases/enabled - mkdir -p "$BASH_IT"/completion/enabled - mkdir -p "$BASH_IT"/plugins/enabled + setup_test_fixture export OLD_PATH="$PATH" export PATH="/usr/bin:/bin:/usr/sbin" diff --git a/test/lib/utilities.bats b/test/lib/utilities.bats index 2bf415fb..6e0f4ac7 100644 --- a/test/lib/utilities.bats +++ b/test/lib/utilities.bats @@ -9,22 +9,7 @@ load ../../lib/search cite _about _param _example _group _author _version function local_setup { - mkdir -p "$BASH_IT" - lib_directory="$(cd "$(dirname "$0")" && pwd)" - # Use rsync to copy Bash-it to the temp folder - # rsync is faster than cp, since we can exclude the large ".git" folder - rsync -qavrKL -d --delete-excluded --exclude=.git --exclude=enabled $lib_directory/../../../.. "$BASH_IT" - - rm -rf "$BASH_IT"/enabled - rm -rf "$BASH_IT"/aliases/enabled - rm -rf "$BASH_IT"/completion/enabled - rm -rf "$BASH_IT"/plugins/enabled - rm -rf "$BASH_IT"/tmp/cache - - mkdir -p "$BASH_IT"/enabled - mkdir -p "$BASH_IT"/aliases/enabled - mkdir -p "$BASH_IT"/completion/enabled - mkdir -p "$BASH_IT"/plugins/enabled + setup_test_fixture } function has_match() { diff --git a/test/plugins/ruby.plugin.bats b/test/plugins/ruby.plugin.bats index 62d0e938..a45411e2 100755 --- a/test/plugins/ruby.plugin.bats +++ b/test/plugins/ruby.plugin.bats @@ -6,21 +6,7 @@ load ../../lib/composure load ../../plugins/available/ruby.plugin function local_setup { - mkdir -p "$BASH_IT" - lib_directory="$(cd "$(dirname "$0")" && pwd)" - # Use rsync to copy Bash-it to the temp folder - # rsync is faster than cp, since we can exclude the large ".git" folder - rsync -qavrKL -d --delete-excluded --exclude=.git --exclude=enabled $lib_directory/../../../.. "$BASH_IT" - - rm -rf "$BASH_IT"/enabled - rm -rf "$BASH_IT"/aliases/enabled - rm -rf "$BASH_IT"/completion/enabled - rm -rf "$BASH_IT"/plugins/enabled - - mkdir -p "$BASH_IT"/enabled - mkdir -p "$BASH_IT"/aliases/enabled - mkdir -p "$BASH_IT"/completion/enabled - mkdir -p "$BASH_IT"/plugins/enabled + setup_test_fixture export OLD_PATH="$PATH" export PATH="/usr/bin:/bin:/usr/sbin" diff --git a/test/test_helper.bash b/test/test_helper.bash index cd75f05a..c51e2b53 100644 --- a/test/test_helper.bash +++ b/test/test_helper.bash @@ -6,34 +6,12 @@ unset TODO unset SCM_CHECK unset BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE -BASH_IT_TEST_DIR="${BATS_TMPDIR}/.bash_it" - -# guard against executing this block twice due to bats internals -if [ "$BASH_IT_ROOT" != "${BASH_IT_TEST_DIR}/root" ]; then - export BASH_IT_ROOT="${BASH_IT_TEST_DIR}/root" - export BASH_IT=$BASH_IT_TEST_DIR -fi - export TEST_MAIN_DIR="${BATS_TEST_DIRNAME}/.." export TEST_DEPS_DIR="${TEST_DEPS_DIR-${TEST_MAIN_DIR}/../test_lib}" # be independent of git's system configuration export GIT_CONFIG_NOSYSTEM -# Some tools, e.g. `git` use configuration files from the $HOME directory, -# which interferes with our tests. The only way to keep `git` from doing this -# seems to set HOME explicitly to a separate location. -# Refer to https://git-scm.com/docs/git-config#FILES. -unset XDG_CONFIG_HOME -export HOME="${BATS_TMPDIR}/home" -mkdir -p "${HOME}" - -# For `git` tests to run well, user name and email need to be set. -# Refer to https://git-scm.com/docs/git-commit#_commit_information. -# This goes to the test-specific config, due to the $HOME overridden above. -git config --global user.name "John Doe" -git config --global user.email "johndoe@example.com" - load "${TEST_DEPS_DIR}/bats-support/load.bash" load "${TEST_DEPS_DIR}/bats-assert/load.bash" load "${TEST_DEPS_DIR}/bats-file/load.bash" @@ -46,9 +24,56 @@ local_teardown() { true } +# This function sets up a local test fixture, i.e. a completely +# fresh and isolated Bash-it directory. This is done to avoid +# messing with your own Bash-it source directory. +# If you need this, call it in your .bats file's `local_setup` function. +setup_test_fixture() { + mkdir -p "$BASH_IT" + lib_directory="$(cd "$(dirname "$0")" && pwd)" + # Use rsync to copy Bash-it to the temp folder + # rsync is faster than cp, since we can exclude the large ".git" folder + rsync -qavrKL -d --delete-excluded --exclude=.git --exclude=enabled $lib_directory/../../../.. "$BASH_IT" + + rm -rf "$BASH_IT"/enabled + rm -rf "$BASH_IT"/aliases/enabled + rm -rf "$BASH_IT"/completion/enabled + rm -rf "$BASH_IT"/plugins/enabled + + mkdir -p "$BASH_IT"/enabled + mkdir -p "$BASH_IT"/aliases/enabled + mkdir -p "$BASH_IT"/completion/enabled + mkdir -p "$BASH_IT"/plugins/enabled + + # Some tests use the BASH_IT_TEST_HOME variable, e.g. install/uninstall + export BASH_IT_TEST_HOME="$TEST_TEMP_DIR" +} + setup() { + TEST_TEMP_DIR="$(mktemp -d -t 'bash-it-test')" + export TEST_TEMP_DIR + + export BASH_IT_TEST_DIR="${TEST_TEMP_DIR}/.bash_it" + + export BASH_IT_ROOT="${BASH_IT_TEST_DIR}/root" + export BASH_IT=$BASH_IT_TEST_DIR + mkdir -p -- "${BASH_IT_ROOT}" + # Some tools, e.g. `git` use configuration files from the $HOME directory, + # which interferes with our tests. The only way to keep `git` from doing this + # seems to set HOME explicitly to a separate location. + # Refer to https://git-scm.com/docs/git-config#FILES. + unset XDG_CONFIG_HOME + export HOME="${TEST_TEMP_DIR}" + mkdir -p "${HOME}" + + # For `git` tests to run well, user name and email need to be set. + # Refer to https://git-scm.com/docs/git-commit#_commit_information. + # This goes to the test-specific config, due to the $HOME overridden above. + git config --global user.name "John Doe" + git config --global user.email "johndoe@example.com" + local_setup } @@ -56,6 +81,7 @@ teardown() { local_teardown rm -rf "${BASH_IT_TEST_DIR}" + temp_del "${TEST_TEMP_DIR}" } # Fail and display path of the link if it does not exist. Also fails