diff --git a/src/usr/local/buildpack/util.sh b/src/usr/local/buildpack/util.sh index e95a8bab8..cac4f84e7 100644 --- a/src/usr/local/buildpack/util.sh +++ b/src/usr/local/buildpack/util.sh @@ -9,6 +9,7 @@ export ENV_FILE=/usr/local/etc/env export ROOT_DIR=/usr/local # shellcheck disable=SC2153 export USER_HOME="/home/${USER_NAME}" +export BASH_RC=/etc/bash.bashrc # source the helper files # shellcheck source=/dev/null diff --git a/src/usr/local/buildpack/utils/environment.sh b/src/usr/local/buildpack/utils/environment.sh index 5cd44d275..296762074 100644 --- a/src/usr/local/buildpack/utils/environment.sh +++ b/src/usr/local/buildpack/utils/environment.sh @@ -52,14 +52,24 @@ function export_tool_env () { } function export_tool_path () { + local additional_path=$1 + local add_to_end=${2:false} local install_dir + install_dir=$(get_install_dir) - if [[ -z "${TOOL_NAME+x}" ]]; then + + if [[ -z "${TOOL_NAME}" ]]; then echo "No TOOL_NAME defined - skipping: ${TOOL_NAME}" >&2 exit 1; fi - export PATH="$1:$PATH" - echo export PATH="$1:\$PATH" >> "$install_dir"/env.d/"${TOOL_NAME}".sh + + if [ "${add_to_end}" = true ]; then + export PATH="$PATH:$additional_path" + echo export PATH="\$PATH:$additional_path" >> "${install_dir}/env.d/${TOOL_NAME}.sh" + else + export PATH="$additional_path:$PATH" + echo export PATH="$additional_path:\$PATH" >> "${install_dir}/env.d/${TOOL_NAME}.sh" + fi } function get_tool_version_env () { @@ -99,7 +109,7 @@ if [ -d "${USER_HOME}/env.d" ]; then fi EOM - cat >> /etc/bash.bashrc <<- EOM + cat >> "${BASH_RC}" <<- EOM if [[ -r "$ENV_FILE" && -z "${BUILDPACK+x}" ]]; then . $ENV_FILE fi diff --git a/test/bash/environment.bats b/test/bash/environment.bats index cf30174d9..3b1aae707 100644 --- a/test/bash/environment.bats +++ b/test/bash/environment.bats @@ -2,21 +2,22 @@ setup() { load '../../node_modules/bats-support/load' load '../../node_modules/bats-assert/load' - TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" >/dev/null 2>&1 && pwd)" - TEST_ROOT_DIR=$(mktemp -u) + TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" >/dev/null 2>&1 && pwd)" + TEST_ROOT_DIR=$(mktemp -u) - load "$TEST_DIR/../../src/usr/local/buildpack/util.sh" + load "$TEST_DIR/../../src/usr/local/buildpack/util.sh" - # load test overwrites - load "$TEST_DIR/util.sh" + # load test overwrites + load "$TEST_DIR/util.sh" - # set directories for test - ROOT_DIR="${TEST_ROOT_DIR}/root" - USER_HOME="${TEST_ROOT_DIR}/user" - ENV_FILE="${TEST_ROOT_DIR}/env" + # set directories for test + ROOT_DIR="${TEST_ROOT_DIR}/root" + USER_HOME="${TEST_ROOT_DIR}/user" + ENV_FILE="${TEST_ROOT_DIR}/env" + BASH_RC="${TEST_ROOT_DIR}/bash.bashrc" - # set default test user - TEST_ROOT_USER=1000 + # set default test user + TEST_ROOT_USER=1000 } teardown() { @@ -53,6 +54,8 @@ teardown() { mkdir -p "${TEST_ROOT_DIR}/user/env.d" + setup_env_files + TOOL_NAME= run export_tool_env assert_failure @@ -62,31 +65,29 @@ teardown() { assert_success assert_output --partial "FOO_HOME=\${FOO_HOME-123}" - # Below cases cannot be tested unless we have flexible setup methods - - # unset FOO_HOME - # assert [ -z "${FOO_HOME}" ] - # assert [ -n "${TEST_ROOT_DIR}" ] - # assert [ -n "${ENV_FILE}" ] + unset FOO_HOME + assert [ -z "${FOO_HOME}" ] + assert [ -n "${TEST_ROOT_DIR}" ] + assert [ -n "${ENV_FILE}" ] - # . "/usr/local/etc/env" - # assert [ "${FOO_HOME}" = "123" ] + . "${ENV_FILE}" + assert [ "${FOO_HOME}" = "123" ] - # unset FOO_HOME + unset FOO_HOME - # BASH_ENV="${TEST_ROOT_DIR}/usr/local/etc/env" \ - # ENV="${TEST_ROOT_DIR}/usr/local/etc/env" \ - # run bash -c 'env | grep FOO' - # assert_success - # assert_output --partial FOO_HOME=123 + BASH_ENV="${ENV_FILE}" \ + ENV="${ENV_FILE}" \ + run bash -c 'env | grep FOO' + assert_success + assert_output --partial FOO_HOME=123 - # unset FOO_HOME + unset FOO_HOME - # BASH_ENV="${TEST_ROOT_DIR}/usr/local/etc/env" \ - # ENV="${TEST_ROOT_DIR}/usr/local/etc/env" \ - # run bash -c "sh -c 'env | grep FOO'" - # assert_success - # assert_output --partial FOO_HOME=123 + BASH_ENV="${ENV_FILE}" \ + ENV="${ENV_FILE}" \ + run bash -c "sh -c 'env | grep FOO'" + assert_success + assert_output --partial FOO_HOME=123 } @test "handles complex setting and getting the tool path correctly" { @@ -95,48 +96,44 @@ teardown() { local TOOL_VERSION=1.2.3 mkdir -p "${TEST_ROOT_DIR}/user/env.d" + setup_env_files local old_path=$PATH - # TODO(Chumper): This test should fail TOOL_NAME= run export_tool_path - # assert_failure - assert_success + assert_failure export_tool_path /foo assert echo "${PATH}" | grep "/foo:" - # Append to the end is not implemented yet - - # export_tool_path /foo true - # assert echo "${PATH}" | grep ":/foo" - # export PATH=$old_path + export_tool_path /foo true + assert echo "${PATH}" | grep ":/foo" + export PATH=$old_path - BASH_ENV="${TEST_ROOT_DIR}/usr/local/etc/env" \ - ENV="${TEST_ROOT_DIR}/usr/local/etc/env" \ + BASH_ENV="${ENV_FILE}" \ + ENV="${ENV_FILE}" \ run bash -c 'env | grep PATH' assert_success assert_output --partial "/foo:" - BASH_ENV="${TEST_ROOT_DIR}/usr/local/etc/env" \ - ENV="${TEST_ROOT_DIR}/usr/local/etc/env" \ + BASH_ENV="${ENV_FILE}" \ + ENV="${ENV_FILE}" \ run bash -c 'sh -c "env | grep PATH"' assert_success assert_output --partial "/foo:" - # Append to the end is not implemented yet + BASH_ENV="${ENV_FILE}" \ + ENV="${ENV_FILE}" \ + run bash -c 'env | grep PATH' + assert_success + assert_output --partial ":/foo" + export PATH=$old_path - # BASH_ENV="${TEST_ROOT_DIR}/usr/local/etc/env" \ - # ENV="${TEST_ROOT_DIR}/usr/local/etc/env" \ - # run bash -c 'env | grep PATH' - # assert_success - # assert_output --partial :/foo - # export PATH=$old_path + BASH_ENV="${ENV_FILE}" \ + ENV="${ENV_FILE}" \ + run bash -c 'sh -c "env | grep PATH"' + assert_success + assert_output --partial ":/foo" - # BASH_ENV="${TEST_ROOT_DIR}/usr/local/etc/env" \ - # ENV="${TEST_ROOT_DIR}/usr/local/etc/env" \ - # run bash -c 'sh -c "env | grep PATH"' - # assert_success - # assert_output --partial :/foo export PATH=$old_path }