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

chore: Add unit tests #461

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add test__parse_and_export_env_vars
MaxymVlasov committed Jan 27, 2023

Verified

This commit was signed with the committer’s verified signature.
MaxymVlasov Maksym Vlasov
commit 5759bb690ec2c9427de3f2b6961ee1d55115f480
52 changes: 42 additions & 10 deletions tests/shunit2/_common_test.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
#!/usr/bin/env bash

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

function oneTimeSetUp {
# Load include to test.
# 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 {
@@ -14,7 +23,8 @@ function test__HOOK_ID {
# }

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'" \
@@ -35,6 +45,8 @@ function test__parse_cmdline {
'-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' \
@@ -46,21 +58,41 @@ function test__parse_cmdline {
# Test Global ENVs changes
#

expected='--config-file=.tfsec.json --force-all-dirs --exclude-downloaded-modules --concise-output --config-file=__GIT_WORKING_DIR__/.tfsec.json'
# 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`
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;"
# 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`
expected='-get=true -get=true -upgrade'
# 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`
expected='AWS_DEFAULT_REGION="us-west-2" AWS_ACCESS_KEY_ID="anaccesskey" AWS_SECRET_ACCESS_KEY="asecretkey"'
# 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[*]}"

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'
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