From 0f271d0581c85f992a0f821e09138181a28a3123 Mon Sep 17 00:00:00 2001 From: Michael Kriese Date: Wed, 31 Jan 2024 14:04:33 +0100 Subject: [PATCH] test: validate bats files with shellcheck --- .github/workflows/build.yml | 9 ++++-- .lintstagedrc.json | 6 ++-- .vscode/settings.json | 3 ++ test/bash/cache.bats | 62 ++++++++++++++++-------------------- test/bash/environment.bats | 22 ++++++------- test/bash/filesystem.bats | 56 +++++++++++++++----------------- test/bash/java.bats | 22 ++++--------- test/bash/linking.bats | 24 +++++--------- test/bash/prepare.bats | 14 ++------ test/bash/util.bats | 19 ++++++----- test/bash/util.sh | 7 ++-- test/bash/v2/defaults.bats | 12 +++---- test/bash/v2/filesystem.bats | 23 ++++++------- test/bash/version.bats | 22 ++++--------- 14 files changed, 130 insertions(+), 171 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a2ff265b6..53e1e5940 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -15,6 +15,7 @@ env: BUILDX_NO_DEFAULT_LOAD: 1 CONTAINERBASE_VERSION: ${{ github.sha }} APT_HTTP_PROXY: http://172.17.0.1:8000 + HUSKY: 0 permissions: contents: read @@ -64,8 +65,12 @@ jobs: fail_on_error: true reporter: github-check filter_mode: nofilter - path: src - pattern: '*.sh' + path: | + src + .husky + pattern: | + *.sh + *.bats check_all_files_with_shebangs: 'true' bats: diff --git a/.lintstagedrc.json b/.lintstagedrc.json index 1f9c07490..a5e4f69e8 100644 --- a/.lintstagedrc.json +++ b/.lintstagedrc.json @@ -1,7 +1,9 @@ { "*.sh": "shellcheck", + "*.bats": "shellcheck", ".husky/*": "shellcheck", "src/usr/local/bin/*": "shellcheck", - "*.{js,ts,cjs,mjs}": "eslint --fix", - "*": "prettier --ignore-unknown --write" + "src/usr/local/containerbase/bin/*": "shellcheck", + "*.{js,ts,cjs,mjs}": ["eslint --fix", "prettier --write"], + "!*.{js,ts,cjs,mjs}": "prettier --ignore-unknown --write" } diff --git a/.vscode/settings.json b/.vscode/settings.json index 0af2d31a7..212b64ad9 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,5 +4,8 @@ "search.exclude": { "**/.yarn": true, "**/.pnp.*": true + }, + "files.associations": { + "*.bats": "shellscript" } } diff --git a/test/bash/cache.bats b/test/bash/cache.bats index 9c98aaafe..808ee91cb 100644 --- a/test/bash/cache.bats +++ b/test/bash/cache.bats @@ -1,24 +1,16 @@ +# shellcheck disable=SC2034,SC2148,SC2155 setup() { - load $BATS_SUPPORT_LOAD_PATH - load $BATS_ASSERT_LOAD_PATH + load "$BATS_SUPPORT_LOAD_PATH" + load "$BATS_ASSERT_LOAD_PATH" - 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/containerbase/util.sh" + load "$TEST_DIR/../../src/usr/local/containerbase/util.sh" - # load test overwrites - load "$TEST_DIR/util.sh" - - # set directories for test - ROOT_DIR="${TEST_ROOT_DIR}/root" - BIN_DIR="${TEST_ROOT_DIR}/bin" - USER_HOME="${TEST_ROOT_DIR}/user" - ENV_FILE="${TEST_ROOT_DIR}/env" - - # set default test user - TEST_ROOT_USER=1000 + # load test overwrites + load "$TEST_DIR/util.sh" } teardown() { @@ -27,7 +19,7 @@ teardown() { @test "get_oldest_file" { - CONTAINERBASE_CACHE_DIR= \ + CONTAINERBASE_CACHE_DIR='' \ run get_oldest_file assert_failure @@ -54,7 +46,7 @@ teardown() { @test "get_cache_fill_level" { local TEST_FILL_LEVEL=88 - CONTAINERBASE_CACHE_DIR= \ + CONTAINERBASE_CACHE_DIR='' \ run get_cache_fill_level assert_failure @@ -63,11 +55,12 @@ teardown() { mkdir -p "${CONTAINERBASE_CACHE_DIR}" local real_fill_level=$(get_cache_fill_level) - assert test "[[ "$real_fill_level" =~ ^[0-9]+$ ]]" + assert test "[[ ""$real_fill_level"" =~ ^[0-9]+$ ]]" # overwrite function to verify deletion + # shellcheck disable=SC2317 function get_cache_fill_level () { - echo $TEST_FILL_LEVEL + echo "$TEST_FILL_LEVEL" } run get_cache_fill_level @@ -101,17 +94,17 @@ teardown() { sleep 0.01 touch "${CONTAINERBASE_CACHE_DIR}/a" - CONTAINERBASE_CACHE_DIR= \ - CONTAINERBASE_MAX_ALLOCATED_DISK= \ + CONTAINERBASE_CACHE_DIR='' \ + CONTAINERBASE_MAX_ALLOCATED_DISK='' \ run cleanup_cache assert_success - CONTAINERBASE_CACHE_DIR= \ + CONTAINERBASE_CACHE_DIR='' \ CONTAINERBASE_MAX_ALLOCATED_DISK=20 \ run cleanup_cache assert_success - CONTAINERBASE_MAX_ALLOCATED_DISK= \ + CONTAINERBASE_MAX_ALLOCATED_DISK='' \ run cleanup_cache assert_success assert [ -e "${CONTAINERBASE_CACHE_DIR}/a" ] @@ -162,11 +155,12 @@ teardown() { assert_success assert_line "${CONTAINERBASE_CACHE_DIR}/foobar" - CONTAINERBASE_CACHE_DIR= \ + CONTAINERBASE_CACHE_DIR='' \ tmp_file=$(download_file "${file}") rm "${tmp_file}" - assert test "[[ "${tmp_file}" =~ "\/containerbase.tar.xz" ]]" + # shellcheck disable=SC1001,SC2140 + assert test "[[ ""${tmp_file}"" =~ "\/containerbase.tar.xz" ]]" } @test "get_from_url" { @@ -207,7 +201,7 @@ teardown() { local checksum="233c335a7f10e9f0dfd7e9d0cda802a38c15a7f13b6678c55980814f22799a70590d56888a819b6591881ec1939240d9dbe68e7e495021b4d6c6a49cdee24d80" local file="https://github.com/containerbase/base/releases/download/7.10.0/containerbase.tar.xz" - run get_from_url "${file}" $(basename "${file}") "${checksum}" "sha512sum" + run get_from_url "${file}" "$(basename "${file}")" "${checksum}" "sha512sum" assert_success assert_line --regexp "^${CONTAINERBASE_CACHE_DIR}/[0-9a-f]{64}/containerbase\.tar\.xz" @@ -220,7 +214,7 @@ teardown() { rm -rf "${CONTAINERBASE_CACHE_DIR}" # wrong checksum - run get_from_url "${file}" $(basename "${file}") "123" "sha256sum" + run get_from_url "${file}" "$(basename "${file}")" "123" "sha256sum" assert_failure assert_output --partial "Retries left: 2" assert_output --partial "Retries left: 1" @@ -245,7 +239,7 @@ teardown() { local checksum="233c335a7f10e9f0dfd7e9d0cda802a38c15a7f13b6678c55980814f22799a70590d56888a819b6591881ec1939240d9dbe68e7e495021b4d6c6a49cdee24d80" local file="https://github.com/containerbase/base/releases/download/7.10.0/containerbase.tar.xz" - run --separate-stderr get_from_url "${file}" $(basename "${file}") "${checksum}" "sha512sum" + run --separate-stderr get_from_url "${file}" "$(basename "${file}")" "${checksum}" "sha512sum" assert_success assert_output --regexp "^${CONTAINERBASE_CACHE_DIR}/[0-9a-f]{64}/containerbase\.tar\.xz" @@ -259,7 +253,7 @@ teardown() { echo "a" >> "${file_path}" # corrupt file in cache - run get_from_url "${file}" $(basename "${file}") "${checksum}" "sha512sum" + run get_from_url "${file}" "$(basename "${file}")" "${checksum}" "sha512sum" assert_success assert_output --partial "Cached file is corrupt" } @@ -294,8 +288,8 @@ teardown() { assert_output --regexp "^${CONTAINERBASE_CACHE_DIR}/${path_checksum}/folder\.tar\.zst" # delete cache entry - rm -rf ${CONTAINERBASE_CACHE_DIR}/${key_checksum} - rm -rf ${CONTAINERBASE_CACHE_DIR}/${path_checksum} + rm -rf "${CONTAINERBASE_CACHE_DIR:?}/${key_checksum}" + rm -rf "${CONTAINERBASE_CACHE_DIR:?}/${path_checksum}" } @test "restore_folder_from_cache" { @@ -358,6 +352,6 @@ teardown() { assert [ -e "${tool_folder}/b" ] # delete cache entry - rm -rf ${CONTAINERBASE_CACHE_DIR}/${key_checksum} - rm -rf ${CONTAINERBASE_CACHE_DIR}/${path_checksum} + rm -rf "${CONTAINERBASE_CACHE_DIR:?}/${key_checksum}" + rm -rf "${CONTAINERBASE_CACHE_DIR:?}/${path_checksum}" } diff --git a/test/bash/environment.bats b/test/bash/environment.bats index c01a71c09..a2cae32e0 100644 --- a/test/bash/environment.bats +++ b/test/bash/environment.bats @@ -1,6 +1,8 @@ +# shellcheck disable=SC2034,SC2148,SC2155 + setup() { - load $BATS_SUPPORT_LOAD_PATH - load $BATS_ASSERT_LOAD_PATH + load "$BATS_SUPPORT_LOAD_PATH" + load "$BATS_ASSERT_LOAD_PATH" TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" >/dev/null 2>&1 && pwd)" TEST_ROOT_DIR=$(mktemp -u) @@ -11,14 +13,7 @@ setup() { load "$TEST_DIR/util.sh" # set directories for test - ROOT_DIR="${TEST_ROOT_DIR}/root" - BIN_DIR="${TEST_ROOT_DIR}/bin" - 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 } teardown() { @@ -32,7 +27,7 @@ teardown() { mkdir -p "${TEST_ROOT_DIR}/user/env.d" - TOOL_NAME= run export_tool_env + TOOL_NAME='' run export_tool_env assert_failure export_tool_env FOO_HOME 123 @@ -40,7 +35,7 @@ teardown() { run cat "${install_dir}/env.d/foo.sh" assert_success assert_output --partial "FOO_HOME=\${FOO_HOME-123}" - assert [ $(stat --format '%a' "${install_dir}/env.d/foo.sh") -eq 664 ] + assert [ "$(stat --format '%a' "${install_dir}/env.d/foo.sh")" -eq 664 ] run reset_tool_env assert_success @@ -58,7 +53,7 @@ teardown() { setup_env_files - TOOL_NAME= run export_tool_env + TOOL_NAME='' run export_tool_env assert_failure export_tool_env FOO_HOME 123 @@ -72,6 +67,7 @@ teardown() { assert [ -n "${TEST_ROOT_DIR}" ] assert [ -n "${ENV_FILE}" ] + # shellcheck source=/dev/null . "${ENV_FILE}" assert [ "${FOO_HOME}" = "123" ] @@ -102,7 +98,7 @@ teardown() { local old_path=$PATH - TOOL_NAME= run export_tool_path + TOOL_NAME='' run export_tool_path assert_failure export_tool_path /foo diff --git a/test/bash/filesystem.bats b/test/bash/filesystem.bats index 0232b8273..b09e3d3c2 100644 --- a/test/bash/filesystem.bats +++ b/test/bash/filesystem.bats @@ -1,6 +1,8 @@ +# shellcheck disable=SC2034,SC2148,SC2155 + setup() { - load $BATS_SUPPORT_LOAD_PATH - load $BATS_ASSERT_LOAD_PATH + load "$BATS_SUPPORT_LOAD_PATH" + load "$BATS_ASSERT_LOAD_PATH" TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" >/dev/null 2>&1 && pwd)" TEST_ROOT_DIR=$(mktemp -u) @@ -9,14 +11,6 @@ setup() { # load test overwrites load "$TEST_DIR/util.sh" - - # set directories for test - ROOT_DIR="${TEST_ROOT_DIR}/root" - BIN_DIR="${TEST_ROOT_DIR}/bin" - USER_HOME="${TEST_ROOT_DIR}/user" - - # set default test user - TEST_ROOT_USER=1000 } teardown() { @@ -41,8 +35,8 @@ teardown() { assert_output "${TEST_ROOT_DIR}/user/foo/1.2.3" assert [ -d "${TEST_ROOT_DIR}/user/foo/1.2.3" ] - assert [ $(stat --format '%a' "${TEST_ROOT_DIR}/user/foo") -eq 775 ] - assert [ $(stat --format '%a' "${TEST_ROOT_DIR}/user/foo/1.2.3") -eq 775 ] + assert [ "$(stat --format '%a' "${TEST_ROOT_DIR}/user/foo")" -eq 775 ] + assert [ "$(stat --format '%a' "${TEST_ROOT_DIR}/user/foo/1.2.3")" -eq 775 ] } @test "can create a versioned tool path as root" { @@ -55,8 +49,8 @@ teardown() { assert_output "${TEST_ROOT_DIR}/root/foo/1.2.3" assert [ -d "${TEST_ROOT_DIR}/root/foo/1.2.3" ] - assert [ $(stat --format '%a' "${TEST_ROOT_DIR}/root/foo") -eq 755 ] - assert [ $(stat --format '%a' "${TEST_ROOT_DIR}/root/foo/1.2.3") -eq 755 ] + assert [ "$(stat --format '%a' "${TEST_ROOT_DIR}/root/foo")" -eq 755 ] + assert [ "$(stat --format '%a' "${TEST_ROOT_DIR}/root/foo/1.2.3")" -eq 755 ] } @test "finds the versioned tool path" { @@ -114,7 +108,7 @@ teardown() { run find_tool_env assert_output "${TEST_ROOT_DIR}/root/env.d/foo.sh" - TOOL_NAME= run find_tool_env + TOOL_NAME='' run find_tool_env assert_failure } @@ -125,13 +119,13 @@ teardown() { assert_success assert [ -d "${install_dir}/tools" ] - assert [ $(stat --format '%a' "${install_dir}/tools") -eq 775 ] + assert [ "$(stat --format '%a' "${install_dir}/tools")" -eq 775 ] assert [ -d "${install_dir}/versions" ] - assert [ $(stat --format '%a' "${install_dir}/versions") -eq 775 ] + assert [ "$(stat --format '%a' "${install_dir}/versions")" -eq 775 ] assert [ -d "${BIN_DIR}" ] - assert [ $(stat --format '%a' "${BIN_DIR}") -eq 775 ] + assert [ "$(stat --format '%a' "${BIN_DIR}")" -eq 775 ] assert [ -d "${install_dir}/env.d" ] - assert [ $(stat --format '%a' "${install_dir}/env.d") -eq 775 ] + assert [ "$(stat --format '%a' "${install_dir}/env.d")" -eq 775 ] } @test "creates a folder with correct permissions" { @@ -145,26 +139,26 @@ teardown() { assert_success assert [ -d "${install_dir}/foo" ] - assert [ $(stat --format '%a' "${install_dir}/foo") -eq "${USER_UMASK}" ] + assert [ "$(stat --format '%a' "${install_dir}/foo")" -eq "${USER_UMASK}" ] run create_folder "${install_dir}/foo2" 777 assert_success assert [ -d "${install_dir}/foo2" ] - assert [ $(stat --format '%a' "${install_dir}/foo2") -eq "777" ] + assert [ "$(stat --format '%a' "${install_dir}/foo2")" -eq "777" ] TEST_ROOT_USER=0 run create_folder "${install_dir}/bar" assert_success assert [ -d "${install_dir}/bar" ] - assert [ $(stat --format '%a' "${install_dir}/bar") -eq "${ROOT_UMASK}" ] + assert [ "$(stat --format '%a' "${install_dir}/bar")" -eq "${ROOT_UMASK}" ] run create_folder "${install_dir}/bar2" 777 assert_success assert [ -d "${install_dir}/bar2" ] - assert [ $(stat --format '%a' "${install_dir}/bar2") -eq "777" ] + assert [ "$(stat --format '%a' "${install_dir}/bar2")" -eq "777" ] } @test "creates deep folder with correct permissions" { @@ -174,23 +168,23 @@ teardown() { run create_folder "${install_dir}/test/foo/bar/baz" assert [ -d "${install_dir}/test" ] - assert [ $(stat --format '%a' "${install_dir}/test") -eq "${USER_UMASK}" ] + assert [ "$(stat --format '%a' "${install_dir}/test")" -eq "${USER_UMASK}" ] assert [ -d "${install_dir}/test/foo" ] - assert [ $(stat --format '%a' "${install_dir}/test/foo") -eq "${USER_UMASK}" ] + assert [ "$(stat --format '%a' "${install_dir}/test/foo")" -eq "${USER_UMASK}" ] assert [ -d "${install_dir}/test/foo/bar" ] - assert [ $(stat --format '%a' "${install_dir}/test/foo/bar") -eq "${USER_UMASK}" ] + assert [ "$(stat --format '%a' "${install_dir}/test/foo/bar")" -eq "${USER_UMASK}" ] assert [ -d "${install_dir}/test/foo/bar/baz" ] - assert [ $(stat --format '%a' "${install_dir}/test/foo/bar/baz") -eq "${USER_UMASK}" ] + assert [ "$(stat --format '%a' "${install_dir}/test/foo/bar/baz")" -eq "${USER_UMASK}" ] TEST_ROOT_USER=0 run create_folder "${install_dir}/bar/foo/bar/baz" assert [ -d "${install_dir}/bar" ] - assert [ $(stat --format '%a' "${install_dir}/bar") -eq "${ROOT_UMASK}" ] + assert [ "$(stat --format '%a' "${install_dir}/bar")" -eq "${ROOT_UMASK}" ] assert [ -d "${install_dir}/bar/foo" ] - assert [ $(stat --format '%a' "${install_dir}/bar/foo") -eq "${ROOT_UMASK}" ] + assert [ "$(stat --format '%a' "${install_dir}/bar/foo")" -eq "${ROOT_UMASK}" ] assert [ -d "${install_dir}/bar/foo/bar" ] - assert [ $(stat --format '%a' "${install_dir}/bar/foo/bar") -eq "${ROOT_UMASK}" ] + assert [ "$(stat --format '%a' "${install_dir}/bar/foo/bar")" -eq "${ROOT_UMASK}" ] assert [ -d "${install_dir}/bar/foo/bar/baz" ] - assert [ $(stat --format '%a' "${install_dir}/bar/foo/bar/baz") -eq "${ROOT_UMASK}" ] + assert [ "$(stat --format '%a' "${install_dir}/bar/foo/bar/baz")" -eq "${ROOT_UMASK}" ] } diff --git a/test/bash/java.bats b/test/bash/java.bats index 7e749114e..0b42bbcdf 100644 --- a/test/bash/java.bats +++ b/test/bash/java.bats @@ -1,8 +1,8 @@ # shellcheck disable=SC2034,SC2148 setup() { - load $BATS_SUPPORT_LOAD_PATH - load $BATS_ASSERT_LOAD_PATH + load "$BATS_SUPPORT_LOAD_PATH" + load "$BATS_ASSERT_LOAD_PATH" TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" >/dev/null 2>&1 && pwd)" TEST_ROOT_DIR=$(mktemp -u) @@ -15,20 +15,12 @@ setup() { # load test overwrites load "$TEST_DIR/util.sh" - # set directories for test - ROOT_DIR="${TEST_ROOT_DIR}/root" - BIN_DIR="${TEST_ROOT_DIR}/bin" - USER_HOME="${TEST_ROOT_DIR}/user" - ENV_FILE="${TEST_ROOT_DIR}/env" - setup_directories # copy containerbase files cp -r "$TEST_DIR/../../src/usr/local/containerbase" "${ROOT_DIR}/containerbase" - # set default test user is root - TEST_ROOT_USER=1000 - # load helm + # load java load "$TEST_DIR/../../src/usr/local/containerbase/utils/java.sh" } @@ -98,8 +90,8 @@ teardown() { assert_success assert_output "Gradle settings already found" - assert [ $(stat --format '%a' "${USER_HOME}/.gradle") -eq 775 ] - assert [ $(stat --format '%a' "${USER_HOME}/.gradle/gradle.properties") -eq 664 ] + assert [ "$(stat --format '%a' "${USER_HOME}/.gradle")" -eq 775 ] + assert [ "$(stat --format '%a' "${USER_HOME}/.gradle/gradle.properties")" -eq 664 ] } @test "create_maven_settings" { @@ -111,6 +103,6 @@ teardown() { assert_success assert_output "Maven settings already found" - assert [ $(stat --format '%a' "${USER_HOME}/.m2") -eq 775 ] - assert [ $(stat --format '%a' "${USER_HOME}/.m2/settings.xml") -eq 664 ] + assert [ "$(stat --format '%a' "${USER_HOME}/.m2")" -eq 775 ] + assert [ "$(stat --format '%a' "${USER_HOME}/.m2/settings.xml")" -eq 664 ] } diff --git a/test/bash/linking.bats b/test/bash/linking.bats index b20fa2057..d7082312f 100644 --- a/test/bash/linking.bats +++ b/test/bash/linking.bats @@ -1,7 +1,8 @@ +# shellcheck disable=SC2148 setup() { - load $BATS_SUPPORT_LOAD_PATH - load $BATS_ASSERT_LOAD_PATH + load "$BATS_SUPPORT_LOAD_PATH" + load "$BATS_ASSERT_LOAD_PATH" TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" >/dev/null 2>&1 && pwd)" TEST_ROOT_DIR=$(mktemp -u) @@ -11,16 +12,7 @@ setup() { # load test overwrites load "$TEST_DIR/util.sh" - # set directories for test - ROOT_DIR="${TEST_ROOT_DIR}/root" - BIN_DIR="${TEST_ROOT_DIR}/bin" - USER_HOME="${TEST_ROOT_DIR}/user" - ENV_FILE="${TEST_ROOT_DIR}/env" - setup_directories - - # set default test user - TEST_ROOT_USER=1000 } teardown() { @@ -43,7 +35,7 @@ teardown() { assert_success assert [ -f "${BIN_DIR}/git" ] - echo "#!/bin/bash\n\necho 'foobar'" > "${USER_HOME}/bin2/foobar" + printf "#!/bin/bash\n\necho 'foobar'" > "${USER_HOME}/bin2/foobar" chmod +x "${USER_HOME}/bin2/foobar" run link_wrapper foobar "${USER_HOME}/bin2/foobar" @@ -51,7 +43,7 @@ teardown() { assert [ -f "${BIN_DIR}/foobar" ] rm "${BIN_DIR}/foobar" - echo "#!/bin/bash\n\necho 'foobar'" > "${USER_HOME}/bin3/foobar" + printf "#!/bin/bash\n\necho 'foobar'" > "${USER_HOME}/bin3/foobar" chmod +x "${USER_HOME}/bin3/foobar" run link_wrapper foobar "${USER_HOME}/bin3" @@ -63,7 +55,7 @@ teardown() { @test "shell_wrapper" { mkdir -p "${USER_HOME}/bin" - echo "#!/bin/bash\n\necho 'foobar'" > "${USER_HOME}/bin/foobar" + printf "#!/bin/bash\n\necho 'foobar'" > "${USER_HOME}/bin/foobar" chmod +x "${USER_HOME}/bin/foobar" run shell_wrapper @@ -77,10 +69,10 @@ teardown() { run shell_wrapper ls assert_success assert [ -f "${BIN_DIR}/ls" ] - assert [ $(stat --format '%a' "${BIN_DIR}/ls") -eq 775 ] + assert [ "$(stat --format '%a' "${BIN_DIR}/ls")" -eq 775 ] PATH="${USER_HOME}/bin":$PATH run shell_wrapper foobar assert_success assert [ -f "${BIN_DIR}/foobar" ] - assert [ $(stat --format '%a' "${BIN_DIR}/ls") -eq 775 ] + assert [ "$(stat --format '%a' "${BIN_DIR}/ls")" -eq 775 ] } diff --git a/test/bash/prepare.bats b/test/bash/prepare.bats index 90a4fcfd1..6b3f53731 100644 --- a/test/bash/prepare.bats +++ b/test/bash/prepare.bats @@ -1,7 +1,8 @@ +# shellcheck disable=SC2148 setup() { - load $BATS_SUPPORT_LOAD_PATH - load $BATS_ASSERT_LOAD_PATH + load "$BATS_SUPPORT_LOAD_PATH" + load "$BATS_ASSERT_LOAD_PATH" TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" >/dev/null 2>&1 && pwd)" TEST_ROOT_DIR=$(mktemp -u) @@ -11,19 +12,10 @@ setup() { # load test overwrites load "$TEST_DIR/util.sh" - # set directories for test - ROOT_DIR="${TEST_ROOT_DIR}/root" - BIN_DIR="${TEST_ROOT_DIR}/bin" - USER_HOME="${TEST_ROOT_DIR}/user" - ENV_FILE="${TEST_ROOT_DIR}/env" - setup_directories # copy containerbase files cp -r "$TEST_DIR/../../src/usr/local/containerbase" "${ROOT_DIR}/containerbase" - - # set default test user is root - TEST_ROOT_USER=1000 } teardown() { diff --git a/test/bash/util.bats b/test/bash/util.bats index 372cdc061..180695360 100644 --- a/test/bash/util.bats +++ b/test/bash/util.bats @@ -1,7 +1,8 @@ +# shellcheck disable=SC2148,SC2034 setup() { - load $BATS_SUPPORT_LOAD_PATH - load $BATS_ASSERT_LOAD_PATH + load "$BATS_SUPPORT_LOAD_PATH" + load "$BATS_ASSERT_LOAD_PATH" TEST_DIR="$( cd "$( dirname "$BATS_TEST_FILENAME" )" >/dev/null 2>&1 && pwd )" @@ -49,7 +50,7 @@ teardown() { assert_output --partial "No USER_NAME defined" assert_failure - USER_NAME= run require_user + USER_NAME='' run require_user assert_output --partial "No USER_NAME defined" assert_failure @@ -178,7 +179,8 @@ teardown() { } @test "require_distro" { - if ! check_command "lsb_release" || [ ! $(lsb_release -si) =~ "Ubuntu" ]; then + # shellcheck disable=SC2074 + if ! check_command "lsb_release" || [ ! "$(lsb_release -si)" =~ "Ubuntu" ]; then skip "not ubuntu" fi @@ -186,7 +188,8 @@ teardown() { } @test "get_distro" { - if ! check_command "lsb_release" || [ ! $(lsb_release -si) =~ "Ubuntu" ]; then + # shellcheck disable=SC2074 + if ! check_command "lsb_release" || [ ! "$(lsb_release -si)" =~ "Ubuntu" ]; then skip "not ubuntu" fi @@ -196,13 +199,13 @@ teardown() { @test "ignore_tool" { # This is fine as the tool is normally checked before - TOOL_NAME= \ - IGNORED_TOOLS= \ + TOOL_NAME='' \ + IGNORED_TOOLS='' \ run ignore_tool assert_output "1" TOOL_NAME=foo \ - IGNORED_TOOLS= \ + IGNORED_TOOLS='' \ run ignore_tool assert_output "0" diff --git a/test/bash/util.sh b/test/bash/util.sh index 20e596ab7..6a855cece 100644 --- a/test/bash/util.sh +++ b/test/bash/util.sh @@ -6,9 +6,11 @@ export CONTAINERBASE_DIR="${TEST_DIR}/../../src/usr/local/containerbase" export ROOT_DIR="${TEST_ROOT_DIR}/root" export BIN_DIR="${TEST_ROOT_DIR}/bin" -export USER_HOME="${TEST_ROOT_DIR}/userhome" +export USER_HOME="${TEST_ROOT_DIR}/user" export ENV_FILE="${TEST_ROOT_DIR}/env" +# set default test user +export TEST_ROOT_USER=1000 # Overwrite is_root function to check a test root user # instead of the effective caller @@ -26,8 +28,9 @@ function link_cli_tool () { if [[ "${ARCHITECTURE}" = "aarch64" ]];then arch=arm64 fi - mkdir -p "${BIN_DIR}" export PATH="${BIN_DIR}:${PATH}" ln -sf "${CONTAINERBASE_DIR}/bin/containerbase-cli-${arch}" "${BIN_DIR}/containerbase-cli" } + +mkdir -p "${BIN_DIR}" "${ROOT_DIR}" link_cli_tool diff --git a/test/bash/v2/defaults.bats b/test/bash/v2/defaults.bats index 37b48f42c..14c2d1352 100644 --- a/test/bash/v2/defaults.bats +++ b/test/bash/v2/defaults.bats @@ -1,6 +1,8 @@ +# shellcheck disable=SC2034,SC2148 + setup() { - load $BATS_SUPPORT_LOAD_PATH - load $BATS_ASSERT_LOAD_PATH + load "$BATS_SUPPORT_LOAD_PATH" + load "$BATS_ASSERT_LOAD_PATH" TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" >/dev/null 2>&1 && pwd)" TEST_ROOT_DIR=$(mktemp -u) @@ -12,12 +14,6 @@ setup() { # load test overwrites load "$TEST_DIR/../util.sh" - - # set directories for test - ROOT_DIR="${TEST_ROOT_DIR}/root" - - # set default test user - TEST_ROOT_USER=1000 } teardown() { diff --git a/test/bash/v2/filesystem.bats b/test/bash/v2/filesystem.bats index 18b69cf13..18198902a 100644 --- a/test/bash/v2/filesystem.bats +++ b/test/bash/v2/filesystem.bats @@ -1,6 +1,8 @@ +# shellcheck disable=SC2034,SC2148 + setup() { - load $BATS_SUPPORT_LOAD_PATH - load $BATS_ASSERT_LOAD_PATH + load "$BATS_SUPPORT_LOAD_PATH" + load "$BATS_ASSERT_LOAD_PATH" TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" >/dev/null 2>&1 && pwd)" TEST_ROOT_DIR=$(mktemp -u) @@ -12,13 +14,6 @@ setup() { # load test overrides load "$TEST_DIR/../util.sh" - - # set directories for test - ROOT_DIR="${TEST_ROOT_DIR}/root" - BIN_DIR="${TEST_ROOT_DIR}/bin" - - # set default test user - TEST_ROOT_USER=1000 } teardown() { @@ -49,7 +44,7 @@ teardown() { run create_tool_path assert_success assert_output "${TEST_ROOT_DIR}/root/tools/foo" - assert [ $(stat --format '%a' "${TEST_ROOT_DIR}/root/tools/foo") -eq 775 ] + assert [ "$(stat --format '%a' "${TEST_ROOT_DIR}/root/tools/foo")" -eq 775 ] # user run find_tool_path @@ -73,8 +68,8 @@ teardown() { assert_output "${TEST_ROOT_DIR}/root/tools/foo/1.2.3" assert [ -d "${TEST_ROOT_DIR}/root/tools/foo/1.2.3" ] - assert [ $(stat --format '%a' "${TEST_ROOT_DIR}/root/tools/foo") -eq 775 ] - assert [ $(stat --format '%a' "${TEST_ROOT_DIR}/root/tools/foo/1.2.3") -eq 755 ] + assert [ "$(stat --format '%a' "${TEST_ROOT_DIR}/root/tools/foo")" -eq 775 ] + assert [ "$(stat --format '%a' "${TEST_ROOT_DIR}/root/tools/foo/1.2.3")" -eq 755 ] } @test "overwrite: can create a versioned tool path as user" { @@ -85,6 +80,6 @@ teardown() { assert_output "${TEST_ROOT_DIR}/root/tools/foo/1.2.3" assert [ -d "${TEST_ROOT_DIR}/root/tools/foo/1.2.3" ] - assert [ $(stat --format '%a' "${TEST_ROOT_DIR}/root/tools/foo") -eq 775 ] - assert [ $(stat --format '%a' "${TEST_ROOT_DIR}/root/tools/foo/1.2.3") -eq 775 ] + assert [ "$(stat --format '%a' "${TEST_ROOT_DIR}/root/tools/foo")" -eq 775 ] + assert [ "$(stat --format '%a' "${TEST_ROOT_DIR}/root/tools/foo/1.2.3")" -eq 775 ] } diff --git a/test/bash/version.bats b/test/bash/version.bats index a94da02c7..5521c8113 100644 --- a/test/bash/version.bats +++ b/test/bash/version.bats @@ -1,7 +1,8 @@ +# shellcheck disable=SC2148,SC2155 setup() { - load $BATS_SUPPORT_LOAD_PATH - load $BATS_ASSERT_LOAD_PATH + load "$BATS_SUPPORT_LOAD_PATH" + load "$BATS_ASSERT_LOAD_PATH" TEST_DIR="$(cd "$(dirname "$BATS_TEST_FILENAME")" >/dev/null 2>&1 && pwd)" TEST_ROOT_DIR=$(mktemp -u) @@ -10,15 +11,6 @@ setup() { # load test overwrites load "$TEST_DIR/util.sh" - - # set directories for test - ROOT_DIR="${TEST_ROOT_DIR}/root" - BIN_DIR="${TEST_ROOT_DIR}/bin" - USER_HOME="${TEST_ROOT_DIR}/user" - ENV_FILE="${TEST_ROOT_DIR}/env" - - # set default test user - TEST_ROOT_USER=1000 } teardown() { @@ -37,11 +29,11 @@ teardown() { run set_tool_version assert_failure - TOOL_NAME=foo TOOL_VERSION= \ + TOOL_NAME=foo TOOL_VERSION='' \ run set_tool_version assert_failure - TOOL_NAME= TOOL_VERSION= \ + TOOL_NAME='' TOOL_VERSION='' \ run set_tool_version assert_failure @@ -104,7 +96,7 @@ teardown() { assert_success assert_output "1.2.3" - assert [ $(stat --format '%a' "${version_path}/foo") -eq 777 ] + assert [ "$(stat --format '%a' "${version_path}/foo")" -eq 777 ] run set_tool_version bar 1.2.4 assert_success @@ -113,7 +105,7 @@ teardown() { assert_success assert_output "1.2.4" - assert [ $(stat --format '%a' "${version_path}/bar") -eq 660 ] + assert [ "$(stat --format '%a' "${version_path}/bar")" -eq 660 ] } @test "get_tool_version_env" {