-
-
Notifications
You must be signed in to change notification settings - Fork 575
chore: Add unit tests #461
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
Draft
MaxymVlasov
wants to merge
4
commits into
master
Choose a base branch
from
feat/GH-303/add_unit_tests
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+98
−0
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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 | ||
| 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 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 fromtests/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
🤖 Prompt for AI Agents