Skip to content
Draft
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
98 changes: 98 additions & 0 deletions tests/shunit2/_common_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/usr/bin/env bash

# shellcheck disable=SC2034 # shunit2 constant
readonly SHUNIT_TEST_PREFIX="_common.sh__"

function oneTimeSetUp {
# Load stuff for tests.
. ../../hooks/_common.sh
# Global ENV vars in --args
Comment on lines +6 to +9
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Make hook script sourcing path robust
Using a relative path (../../hooks/_common.sh) assumes the test is always run from tests/shunit2. To avoid breakage if the working directory changes, compute the script’s directory via ${BASH_SOURCE[0]} and source from there.

 function oneTimeSetUp {
-  . ../../hooks/_common.sh
+  local script_dir
+  script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+  . "$script_dir"/../../hooks/_common.sh
   # Global ENV vars in --args
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function oneTimeSetUp {
# Load stuff for tests.
. ../../hooks/_common.sh
# Global ENV vars in --args
function oneTimeSetUp {
# Load stuff for tests.
local script_dir
script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
. "$script_dir"/../../hooks/_common.sh
# Global ENV vars in --args
}
🤖 Prompt for AI Agents
In tests/shunit2/_common_test.sh around lines 6 to 9, the script sources
../../hooks/_common.sh using a relative path, which breaks if the working
directory changes. Modify the script to determine its own directory using
${BASH_SOURCE[0]} and then source _common.sh relative to that directory to
ensure the path is always correct regardless of where the test is run from.

export CONFIG_FILE=.tflint.hcl
export CONFIG_NAME=.tflint
export CONFIG_EXT=hcl
# Disable hooks clolrs
export PRE_COMMIT_COLOR="never"
}

function test__HOOK_ID {
assertEquals "_common_test" "$HOOK_ID"
}

# function test__initialize {
# Nothing to test?
# }

function test__parse_cmdline {
# Pass test parameters to the function being tested
# shellcheck disable=SC2016 # IT should not expand
common::parse_cmdline \
'--args=--config-file=.tfsec.json' \
"--hook-config='.totalHourlyCost >= 0.1'" \
'--init-args=-get=true' \
'--tf-init-args=-get=true' \
'--envs=AWS_DEFAULT_REGION="us-west-2"' \
'--env-vars=AWS_ACCESS_KEY_ID="anaccesskey"' \
\
'-a --force-all-dirs' \
'-h ".totalHourlyCost|tonumber > 1"' \
'-i -upgrade' \
'-e AWS_SECRET_ACCESS_KEY="asecretkey"' \
\
'--args=--exclude-downloaded-modules' \
'-h ".projects[].diff.totalMonthlyCost|tonumber!=10000"' \
'-h [.projects[].diff.totalMonthlyCost | select (.!=null) | tonumber] | add > 1000' \
'--hook-config=--retry-once-with-cleanup=true' \
'-a --concise-output' \
'--args=--config-file=__GIT_WORKING_DIR__/.tfsec.json' \
'--hook-config=--retry-once-with-cleanup=true' \
'--args=--config=__GIT_WORKING_DIR__/${CONFIG_FILE}' \
'-a --config=__GIT_WORKING_DIR__/${CONFIG_NAME}.${CONFIG_EXT}' \
'environment/qa/backends.tf' \
'environment/qa/main.tf' \
'modules/aws-environment/lambdas.tf' \
'environment/qa/data.tf' \
'environment/qa/outputs.tf' \
'environment/qa/versions.tf'

#
# Test Global ENVs changes
#

# shellcheck disable=SC2016 # IT should not expand
local expected='--config-file=.tfsec.json --force-all-dirs --exclude-downloaded-modules --concise-output --config-file=__GIT_WORKING_DIR__/.tfsec.json --config=__GIT_WORKING_DIR__/${CONFIG_FILE} --config=__GIT_WORKING_DIR__/${CONFIG_NAME}.${CONFIG_EXT}'
assertEquals "ARGS -" "$expected" "${ARGS[*]}"
# Extra space for `-h`. No matter, because in function it spitted to array by spaces
local expected="'.totalHourlyCost >= 0.1'; \".totalHourlyCost|tonumber > 1\"; \".projects[].diff.totalMonthlyCost|tonumber!=10000\"; [.projects[].diff.totalMonthlyCost | select (.!=null) | tonumber] | add > 1000; --retry-once-with-cleanup=true; --retry-once-with-cleanup=true;"
assertEquals "HOOK_CONFIG -" "$expected" "${HOOK_CONFIG[*]}"
# Extra space for `-i`.No matter, because in function it spitted to array by spaces
local expected='-get=true -get=true -upgrade'
assertEquals "TF_INIT_ARGS -" "$expected" "${TF_INIT_ARGS[*]}"
# Extra space for `-e`. No matter, because in function it spitted to array by spaces
local expected='AWS_DEFAULT_REGION="us-west-2" AWS_ACCESS_KEY_ID="anaccesskey" AWS_SECRET_ACCESS_KEY="asecretkey"'
assertEquals "ENV_VARS -" "$expected" "${ENV_VARS[*]}"

local expected='environment/qa/backends.tf environment/qa/main.tf modules/aws-environment/lambdas.tf environment/qa/data.tf environment/qa/outputs.tf environment/qa/versions.tf'
assertEquals "FILES -" "$expected" "${FILES[*]}"
}

function test__parse_and_export_env_vars {
# Init "GLOBAL ENV"
# shellcheck disable=SC2016 # IT should not expand
local ARGS=(
'--args=--config=__GIT_WORKING_DIR__/${CONFIG_FILE}'
'--args=--config=__GIT_WORKING_DIR__/${CONFIG_NAME}.${CONFIG_EXT}'
'--args=--module'
)
# Pass test parameters to the function being tested (only GLOBAL ENV - ARGS)
common::parse_and_export_env_vars

local expected=(
'--args=--config=__GIT_WORKING_DIR__/.tflint.hcl'
'--args=--config=__GIT_WORKING_DIR__/.tflint.hcl'
'--args=--module'
)
assertEquals "${expected[*]}" "${ARGS[*]}"
}

# Load shUnit2. File populated to PATH from https://github.com/kward/shunit2
. shunit2
Loading