fix checkpoint disable propagation fixes #2
diff --git a/tfexec/terraform.go b/tfexec/terraform.go index 9ee9501..97d745a 100644 --- a/tfexec/terraform.go +++ b/tfexec/terraform.go
@@ -62,10 +62,9 @@ var tfenv []string // always propagate CHECKPOINT_DISABLE env var unless it is - // explicitly overridden - c := os.Getenv("CHECKPOINT_DISABLE") - if c != "" { - tfenv = append(tfenv, "CHECKPOINT_DISABLE="+c) + // explicitly overridden with tf.SetEnv + if _, ok := env["CHECKPOINT_DISABLE"]; !ok { + env["CHECKPOINT_DISABLE"] = os.Getenv("CHECKPOINT_DISABLE") } for k, v := range env {
diff --git a/tfexec/terraform_test.go b/tfexec/terraform_test.go index b8216ff..dfe4419 100644 --- a/tfexec/terraform_test.go +++ b/tfexec/terraform_test.go
@@ -19,6 +19,43 @@ const testStateJsonFileName = "state.json" const testTerraformStateFileName = "terraform.tfstate" +func TestCheckpointDisablePropagation(t *testing.T) { + td := testTempDir(t) + defer os.RemoveAll(td) + + tf, err := NewTerraform(td, "") + if err != nil { + t.Fatal(err) + } + + // case 1: env var is set in environment and not overridden + os.Setenv("CHECKPOINT_DISABLE", "1") + defer os.Unsetenv("CHECKPOINT_DISABLE") + tf.SetEnv(map[string]string{ + "FOOBAR": "1", + }) + initCmd := tf.InitCmd(context.Background()) + expected := []string{"FOOBAR=1", "CHECKPOINT_DISABLE=1", "TF_LOG="} + actual := initCmd.Env + + if !reflect.DeepEqual(actual, expected) { + t.Fatalf("expected command env to be %s, but it was %s", expected, actual) + } + + // case 2: env var is set in environment and overridden with SetEnv + tf.SetEnv(map[string]string{ + "CHECKPOINT_DISABLE": "", + "FOOBAR": "1", + }) + initCmd = tf.InitCmd(context.Background()) + expected = []string{"CHECKPOINT_DISABLE=", "FOOBAR=1", "TF_LOG="} + actual = initCmd.Env + + if !reflect.DeepEqual(actual, expected) { + t.Fatalf("expected command env to be %s, but it was %s", expected, actual) + } +} + func TestInitCmd(t *testing.T) { td := testTempDir(t) defer os.RemoveAll(td)