From 908fb2f8b2fc956249ff6065b737514c2157ced2 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Fri, 27 Jan 2023 21:38:13 +0200 Subject: [PATCH 1/3] Init tests --- hooks/_common.sh | 6 ++++++ tests/shunit2/_common_test.sh | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100755 tests/shunit2/_common_test.sh diff --git a/hooks/_common.sh b/hooks/_common.sh index 37b32d842..5bf86fec3 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -42,6 +42,12 @@ function common::parse_cmdline { # Used inside `common::export_provided_env_vars` function ENV_VARS=() + echo "$@" + echo + echo + echo $@ + echo + exit 1 local argv # TODO: Planned breaking change: remove `init-args`, `envs` as not self-descriptive argv=$(getopt -o a:,h:,i:,e: --long args:,hook-config:,init-args:,tf-init-args:,envs:,env-vars: -- "$@") || return diff --git a/tests/shunit2/_common_test.sh b/tests/shunit2/_common_test.sh new file mode 100755 index 000000000..31d979a57 --- /dev/null +++ b/tests/shunit2/_common_test.sh @@ -0,0 +1,31 @@ +#!/usr/bin/env bash + +function oneTimeSetUp { + # Load include to test. + . ../../hooks/_common.sh +} + +function test__HOOK_ID { + assertEquals "_common_test" $HOOK_ID +} + +# function test__initialize { +# Nothing to test +# } + +function test__parse_cmdline { + + common::parse_cmdline "args=ololo=123" + + + assertEquals "ARGS: " "_" "${ARGS[$@]}" + # assertEquals "HOOK_CONFIG: " "[dd]" "${HOOK_CONFIG[$@]}" + +} + + + + +# Load shUnit2. File populated to PATH from https://github.com/kward/shunit2 +. shunit2 + From bc4a33ff9c68572382d92663da954ac32e36d91d Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Fri, 27 Jan 2023 22:42:27 +0200 Subject: [PATCH 2/3] Init tests for `common::parse_cmdline` --- hooks/_common.sh | 6 ---- tests/shunit2/_common_test.sh | 53 +++++++++++++++++++++++++++++------ 2 files changed, 44 insertions(+), 15 deletions(-) diff --git a/hooks/_common.sh b/hooks/_common.sh index 5bf86fec3..37b32d842 100644 --- a/hooks/_common.sh +++ b/hooks/_common.sh @@ -42,12 +42,6 @@ function common::parse_cmdline { # Used inside `common::export_provided_env_vars` function ENV_VARS=() - echo "$@" - echo - echo - echo $@ - echo - exit 1 local argv # TODO: Planned breaking change: remove `init-args`, `envs` as not self-descriptive argv=$(getopt -o a:,h:,i:,e: --long args:,hook-config:,init-args:,tf-init-args:,envs:,env-vars: -- "$@") || return diff --git a/tests/shunit2/_common_test.sh b/tests/shunit2/_common_test.sh index 31d979a57..e373d6b6d 100755 --- a/tests/shunit2/_common_test.sh +++ b/tests/shunit2/_common_test.sh @@ -6,26 +6,61 @@ function oneTimeSetUp { } function test__HOOK_ID { - assertEquals "_common_test" $HOOK_ID + assertEquals "_common_test" "$HOOK_ID" } # function test__initialize { -# Nothing to test +# Nothing to test? # } function test__parse_cmdline { - common::parse_cmdline "args=ololo=123" + 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' \ + '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 + # - assertEquals "ARGS: " "_" "${ARGS[$@]}" - # assertEquals "HOOK_CONFIG: " "[dd]" "${HOOK_CONFIG[$@]}" + expected='--config-file=.tfsec.json --force-all-dirs --exclude-downloaded-modules --concise-output --config-file=__GIT_WORKING_DIR__/.tfsec.json' + 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;" + assertEquals "HOOK_CONFIG -" "$expected" "${HOOK_CONFIG[*]}" + # Extra space for `-i` + 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"' + 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' + assertEquals "FILES -" "$expected" "${FILES[*]}" } - - - # Load shUnit2. File populated to PATH from https://github.com/kward/shunit2 . shunit2 - From 5759bb690ec2c9427de3f2b6961ee1d55115f480 Mon Sep 17 00:00:00 2001 From: MaxymVlasov Date: Fri, 27 Jan 2023 23:42:15 +0200 Subject: [PATCH 3/3] Add test__parse_and_export_env_vars --- tests/shunit2/_common_test.sh | 52 ++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/tests/shunit2/_common_test.sh b/tests/shunit2/_common_test.sh index e373d6b6d..f03505933 100755 --- a/tests/shunit2/_common_test.sh +++ b/tests/shunit2/_common_test.sh @@ -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