Skip to content

Commit

Permalink
feat: path can be appended to the end (#285)
Browse files Browse the repository at this point in the history
* refactor: extract bashrc location

* fix: add more functionality to the envionment funcs

* feat: path can be added to the end

* chore: move quotation to the end

Co-authored-by: Michael Kriese <[email protected]>
  • Loading branch information
Chumper and viceice authored Feb 8, 2022
1 parent 4366e2e commit 120f451
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 59 deletions.
1 change: 1 addition & 0 deletions src/usr/local/buildpack/util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 14 additions & 4 deletions src/usr/local/buildpack/utils/environment.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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
Expand Down
107 changes: 52 additions & 55 deletions test/bash/environment.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -53,6 +54,8 @@ teardown() {

mkdir -p "${TEST_ROOT_DIR}/user/env.d"

setup_env_files

TOOL_NAME= run export_tool_env
assert_failure

Expand All @@ -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" {
Expand All @@ -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
}

0 comments on commit 120f451

Please sign in to comment.