From 7aa34fd11a98fbba04d5ce42302be597f26ebd9d Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Thu, 20 Jun 2019 21:15:59 -0700 Subject: [PATCH] Prepend flags, append positional args (#11) * Add tests * add debug info --- .travis.yml | 6 ++++++ Makefile | 7 +++++-- main.go | 10 +++++++--- test/Makefile | 2 ++ test/tf_cli_args_init.bats | 20 ++++++++++++++++++++ test/tf_cli_args_plan.bats | 22 ++++++++++++++++++++++ test/tf_var.bats | 34 ++++++++++++++++++++++++++++++++++ 7 files changed, 96 insertions(+), 5 deletions(-) create mode 100644 test/Makefile create mode 100644 test/tf_cli_args_init.bats create mode 100644 test/tf_cli_args_plan.bats create mode 100644 test/tf_var.bats diff --git a/.travis.yml b/.travis.yml index 4a8847d..80f8f7c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,11 @@ addons: - make - curl +before_install: + - sudo add-apt-repository ppa:duggan/bats --yes + - sudo apt-get update -qq + - sudo apt-get install -qq bats + install: - make init - make go/deps-build @@ -19,6 +24,7 @@ script: - make go/test - make go/lint - make go/build-all +- make test TF_ENV=../release/tfenv_linux_amd64 - ls -l release/ deploy: diff --git a/Makefile b/Makefile index 3005441..25b11a4 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,9 @@ PATH:=$(PATH):$(GOPATH)/bin -include $(shell curl -sSL -o .build-harness "https://git.io/build-harness"; echo .build-harness) -build: go/build - @exit 0 +.PHONY : test +test: + $(MAKE) -C $(@) +release/tfenv: main.go + $(MAKE) go/build diff --git a/main.go b/main.go index 93df442..9843cec 100644 --- a/main.go +++ b/main.go @@ -81,10 +81,11 @@ func main() { // Combine parameters into something like `-backend-config=role_arn=xxx` arg = "-backend-config=" + arg - if len(pair[1]) > 0 && pair[1] != "true" { + if len(pair[1]) > 0 { arg += "=" + pair[1] } - tfCliArgsInit = append(tfCliArgsInit, arg) + // Prepend flags + tfCliArgsInit = append([]string{arg}, tfCliArgsInit...) } else if reTfCliOption.MatchString(pair[0]) { // `TF_CLI_ARGS_plan`: Map `TF_CLI_PLAN_SOMETHING=value` to `-something=value` match := reTfCliOption.FindStringSubmatch(pair[0]) @@ -98,6 +99,7 @@ func main() { if len(pair[1]) > 0 && pair[1] != "true" { arg += "=" + pair[1] } + // Prepend flags switch cmd { case "init": tfCliArgsInit = append([]string{arg}, tfCliArgsInit...) @@ -137,7 +139,9 @@ func main() { if len(pair[1]) > 0 && pair[1] != "true" { arg += "=" + pair[1] } - tfCliArgs = append(tfCliArgs, arg) + // Prepend flags + tfCliArgs = append([]string{arg}, tfCliArgs...) + } else if !reBlacklist.MatchString(pair[0]) && reWhitelist.MatchString(pair[0]) { // Process the blacklist for exclusions, then the whitelist for inclusions // Strip off TF_VAR_ prefix so we can simplify normalization diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..8a1327e --- /dev/null +++ b/test/Makefile @@ -0,0 +1,2 @@ +all: + bats --tap . diff --git a/test/tf_cli_args_init.bats b/test/tf_cli_args_init.bats new file mode 100644 index 0000000..334cafc --- /dev/null +++ b/test/tf_cli_args_init.bats @@ -0,0 +1,20 @@ +function setup() { + export TF_CLI_INIT_FROM_MODULE="git::https://https://github.com/cloudposse/terraform-null-label?ref=master" + export TF_CLI_INIT_BACKEND=false + export TF_CLI_INIT="module/" + export TF_ENV=${TF_ENV:-../release/tfenv} +} + +function teardown() { + unset TF_CLI_INIT_FROM_MODULE + unset TF_CLI_INIT_BACKEND + unset TF_CLI_INIT + unset TF_ENV +} + +@test "TF_CLI_ARGS_init works" { + which ${TF_ENV} + ${TF_ENV} printenv TF_CLI_ARGS_init >&2 + [ "$(${TF_ENV} printenv TF_CLI_ARGS_init)" != "" ] + [ "$(${TF_ENV} printenv TF_CLI_ARGS_init)" == "-backend=${TF_CLI_INIT_BACKEND} -from-module=${TF_CLI_INIT_FROM_MODULE} ${TF_CLI_INIT}" ] +} diff --git a/test/tf_cli_args_plan.bats b/test/tf_cli_args_plan.bats new file mode 100644 index 0000000..af4c236 --- /dev/null +++ b/test/tf_cli_args_plan.bats @@ -0,0 +1,22 @@ +function setup() { + export TF_CLI_PLAN_AUTO_APPROVE="true" + export TF_CLI_PLAN_NO_COLOR="true" + export TF_CLI_PLAN_OUT="plan.txt" + export TF_CLI_PLAN="module/" + export TF_ENV=${TF_ENV:-../release/tfenv} +} + +function teardown() { + unset TF_CLI_PLAN_AUTO_APPROVE + unset TF_CLI_PLAN_NO_COLOR + unset TF_CLI_PLAN_OUT + unset TF_CLI_PLAN + unset TF_ENV +} + +@test "TF_CLI_ARGS_plan works" { + which ${TF_ENV} + ${TF_ENV} printenv TF_CLI_ARGS_plan >&2 + [ "$(${TF_ENV} printenv TF_CLI_ARGS_plan)" != "" ] + [ "$(${TF_ENV} printenv TF_CLI_ARGS_plan)" == "-no-color -out=${TF_CLI_PLAN_OUT} -auto-approve $TF_CLI_PLAN" ] +} diff --git a/test/tf_var.bats b/test/tf_var.bats new file mode 100644 index 0000000..754d1d6 --- /dev/null +++ b/test/tf_var.bats @@ -0,0 +1,34 @@ +function setup() { + export FOOBAR=123 + export Blah=true + export _something=good + export TF_ENV=${TF_ENV:-../release/tfenv} +} + +function teardown() { + unset FOOBAR + unset Blah + unset _something + unset TF_ENV +} + +@test "TF_VAR_foobar works" { + which ${TF_ENV} + ${TF_ENV} printenv TF_VAR_foobar >&2 + [ "$(${TF_ENV} printenv TF_VAR_foobar)" != "" ] + [ "$(${TF_ENV} printenv TF_VAR_foobar)" == "${FOOBAR}" ] +} + +@test "TF_VAR_blah works" { + which ${TF_ENV} + ${TF_ENV} printenv TF_VAR_blah >&2 + [ "$(${TF_ENV} printenv TF_VAR_blah)" != "" ] + [ "$(${TF_ENV} printenv TF_VAR_blah)" == "${Blah}" ] +} + +@test "TF_VAR_something works" { + which ${TF_ENV} + ${TF_ENV} printenv TF_VAR_something >&2 + [ "$(${TF_ENV} printenv TF_VAR_something)" != "" ] + [ "$(${TF_ENV} printenv TF_VAR_something)" == "${_something}" ] +}