-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add version helper with tests (#293)
* refactor: add version helper with tests * chore: add more test for umask setting
- Loading branch information
Showing
4 changed files
with
178 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/bin/bash | ||
|
||
# Will set the version of the given tool to the given version in the versions folder | ||
function set_tool_version () { | ||
local tool=${1:-$TOOL_NAME} | ||
local version=${2:-$TOOL_VERSION} | ||
|
||
check tool true | ||
check version true | ||
|
||
local version_path | ||
version_path=$(get_version_path) | ||
|
||
# set umask for subshell and enter version | ||
# will only affect if we write the file initially | ||
# umask 117 -> chmod 660 | ||
(umask 117 && echo "${version}" > "${version_path}/${tool}") | ||
} | ||
|
||
# Gets the version of the tool behind $TOOL_NAME or the first argument | ||
# if it is set, empty otherwise | ||
function get_tool_version () { | ||
local version_path | ||
local tool=${1:-$TOOL_NAME} | ||
check tool | ||
|
||
version_path=$(get_version_path) | ||
|
||
cat "${version_path}/${tool}" 2>&- | ||
} | ||
|
||
# Gets the version env var for the given tool | ||
# e.g | ||
# get_tool_version_env foo-bar | ||
# returns | ||
# FOO_BAR_VERSION | ||
function get_tool_version_env () { | ||
local tool=${1//-/_} | ||
check tool true | ||
|
||
tool=${tool^^}_VERSION | ||
echo "${tool}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
|
||
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) | ||
|
||
load "$TEST_DIR/../../src/usr/local/buildpack/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 default test user | ||
TEST_ROOT_USER=1000 | ||
} | ||
|
||
teardown() { | ||
rm -rf "${TEST_ROOT_DIR}" | ||
} | ||
|
||
@test "set and get versions" { | ||
|
||
run setup_directories | ||
assert_success | ||
|
||
run set_tool_version | ||
assert_failure | ||
|
||
TOOL_NAME=foo TOOL_VERSION= \ | ||
run set_tool_version | ||
assert_failure | ||
|
||
TOOL_NAME= TOOL_VERSION= \ | ||
run set_tool_version | ||
assert_failure | ||
|
||
run set_tool_version foo | ||
assert_failure | ||
|
||
run set_tool_version "" 1.2.3 | ||
assert_failure | ||
|
||
TOOL_NAME=foo TOOL_VERSION=1.2.3 \ | ||
run set_tool_version | ||
assert_success | ||
|
||
TOOL_NAME=foo TOOL_VERSION=1.2.3 \ | ||
run get_tool_version | ||
assert_success | ||
assert_output "1.2.3" | ||
|
||
TOOL_NAME=foo TOOL_VERSION=1.2.4 \ | ||
run set_tool_version foobar | ||
assert_success | ||
|
||
TOOL_NAME=foo TOOL_VERSION=1.2.4 \ | ||
run get_tool_version foobar | ||
assert_success | ||
assert_output "1.2.4" | ||
|
||
TOOL_NAME=foo TOOL_VERSION=1.2.4 \ | ||
run set_tool_version foobar1 1.2.5 | ||
assert_success | ||
|
||
TOOL_NAME=foo TOOL_VERSION=1.2.4 \ | ||
run get_tool_version foobar1 | ||
assert_success | ||
assert_output "1.2.5" | ||
|
||
TOOL_NAME=bar TOOL_VERSION=1.2.4 \ | ||
run set_tool_version "" 1.1.1 | ||
assert_success | ||
|
||
TOOL_NAME=bar TOOL_VERSION=1.2.4 \ | ||
run get_tool_version | ||
assert_success | ||
assert_output "1.1.1" | ||
} | ||
|
||
@test "can set version with different umask" { | ||
local version_path=$(get_version_path) | ||
|
||
run setup_directories | ||
assert_success | ||
|
||
touch "${version_path}/foo" | ||
chmod 777 "${version_path}/foo" | ||
|
||
run set_tool_version foo 1.2.3 | ||
assert_success | ||
|
||
run get_tool_version foo | ||
assert_success | ||
assert_output "1.2.3" | ||
|
||
assert [ $(stat --format '%a' "${version_path}/foo") -eq 777 ] | ||
|
||
run set_tool_version bar 1.2.4 | ||
assert_success | ||
|
||
run get_tool_version bar | ||
assert_success | ||
assert_output "1.2.4" | ||
|
||
assert [ $(stat --format '%a' "${version_path}/bar") -eq 660 ] | ||
} | ||
|
||
@test "get_tool_version_env" { | ||
|
||
run get_tool_version_env | ||
assert_failure | ||
|
||
run get_tool_version_env "" | ||
assert_failure | ||
|
||
run get_tool_version_env foo-bar | ||
assert_success | ||
assert_output "FOO_BAR_VERSION" | ||
|
||
run get_tool_version_env foo | ||
assert_success | ||
assert_output "FOO_VERSION" | ||
|
||
run get_tool_version_env foo---bar | ||
assert_success | ||
assert_output "FOO___BAR_VERSION" | ||
} |