Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: validate bats files with shellcheck #2092

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -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"
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
"search.exclude": {
"**/.yarn": true,
"**/.pnp.*": true
},
"files.associations": {
"*.bats": "shellscript"
}
}
62 changes: 28 additions & 34 deletions test/bash/cache.bats
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -27,7 +19,7 @@ teardown() {

@test "get_oldest_file" {

CONTAINERBASE_CACHE_DIR= \
CONTAINERBASE_CACHE_DIR='' \
run get_oldest_file
assert_failure

Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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" ]
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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"

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

Expand All @@ -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"
}
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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}"
}
22 changes: 9 additions & 13 deletions test/bash/environment.bats
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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() {
Expand All @@ -32,15 +27,15 @@ 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
assert [ "${FOO_HOME}" = "123" ]
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
Expand All @@ -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
Expand All @@ -72,6 +67,7 @@ teardown() {
assert [ -n "${TEST_ROOT_DIR}" ]
assert [ -n "${ENV_FILE}" ]

# shellcheck source=/dev/null
. "${ENV_FILE}"
assert [ "${FOO_HOME}" = "123" ]

Expand Down Expand Up @@ -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
Expand Down
Loading
Loading