tidy up show and schema
diff --git a/tfexec/terraform.go b/tfexec/terraform.go index a5038c3..87d0479 100644 --- a/tfexec/terraform.go +++ b/tfexec/terraform.go
@@ -502,13 +502,13 @@ return outputs, nil } -func (t *Terraform) Show(ctx context.Context, args ...string) (*tfjson.State, error) { +func (tf *Terraform) StateShow(ctx context.Context) (*tfjson.State, error) { var ret tfjson.State var errBuf strings.Builder var outBuf bytes.Buffer - showCmd := t.ShowCmd(ctx, args...) + showCmd := tf.StateShowCmd(ctx) showCmd.Stderr = &errBuf showCmd.Stdout = &outBuf @@ -534,13 +534,13 @@ return &ret, nil } -func (t *Terraform) ProvidersSchema(ctx context.Context, args ...string) (*tfjson.ProviderSchemas, error) { +func (tf *Terraform) ProvidersSchema(ctx context.Context) (*tfjson.ProviderSchemas, error) { var ret tfjson.ProviderSchemas var errBuf strings.Builder var outBuf bytes.Buffer - schemaCmd := t.ProvidersSchemaCmd(ctx, args...) + schemaCmd := tf.ProvidersSchemaCmd(ctx) schemaCmd.Stderr = &errBuf schemaCmd.Stdout = &outBuf
diff --git a/tfexec/terraform_cmd.go b/tfexec/terraform_cmd.go index 552e14e..ee6e005 100644 --- a/tfexec/terraform_cmd.go +++ b/tfexec/terraform_cmd.go
@@ -278,11 +278,11 @@ return tf.buildTerraformCmd(ctx, args...) } -func (t *Terraform) ShowCmd(ctx context.Context, args ...string) *exec.Cmd { +func (tf *Terraform) StateShowCmd(ctx context.Context, args ...string) *exec.Cmd { allArgs := []string{"show", "-json", "-no-color"} allArgs = append(allArgs, args...) - return t.buildTerraformCmd(ctx, allArgs...) + return tf.buildTerraformCmd(ctx, allArgs...) } func (t *Terraform) ProvidersSchemaCmd(ctx context.Context, args ...string) *exec.Cmd {
diff --git a/tfexec/terraform_test.go b/tfexec/terraform_test.go index 1c074e7..c13b1fd 100644 --- a/tfexec/terraform_test.go +++ b/tfexec/terraform_test.go
@@ -77,6 +77,23 @@ } } +func TestApplyCmd(t *testing.T) { + tf, err := NewTerraform("/dev/null", "") + if err != nil { + t.Fatal(err) + } + + applyCmd := tf.ApplyCmd(context.Background(), Backup("testbackup"), LockTimeout("200s"), State("teststate"), StateOut("teststateout"), VarFile("testvarfile"), Lock(false), Parallelism(99), Refresh(false), Target("target1"), Target("target2"), Var("var1=foo"), Var("var2=bar"), DirOrPlan("testfile")) + + actual := strings.TrimPrefix(applyCmd.String(), applyCmd.Path+" ") + + expected := "apply -no-color -auto-approve -input=false -backup=testbackup -lock-timeout=200s -state=teststate -state-out=teststateout -var-file=testvarfile -lock=false -parallelism=99 -refresh=false -target=target1 -target=target2 -var 'var1=foo' -var 'var2=bar' testfile" + + if actual != expected { + t.Fatalf("expected arguments of ApplyCmd:\n%s\n actual arguments:\n%s\n", expected, actual) + } +} + func TestDestroyCmd(t *testing.T) { tf, err := NewTerraform("/dev/null", "") if err != nil { @@ -174,7 +191,43 @@ } } -func TestShow(t *testing.T) { +func TestStateShowCmd(t *testing.T) { + tf, err := NewTerraform("/dev/null", "") + if err != nil { + t.Fatal(err) + } + + // defaults + showCmd := tf.StateShowCmd(context.Background()) + + actual := strings.TrimPrefix(showCmd.String(), showCmd.Path+" ") + + expected := "show -json -no-color" + + if actual != expected { + t.Fatalf("expected default arguments of ShowCmd:\n%s\n actual arguments:\n%s\n", expected, actual) + } +} + +func TestProvidersSchemaCmd(t *testing.T) { + tf, err := NewTerraform("/dev/null", "") + if err != nil { + t.Fatal(err) + } + + // defaults + schemaCmd := tf.ProvidersSchemaCmd(context.Background()) + + actual := strings.TrimPrefix(schemaCmd.String(), schemaCmd.Path+" ") + + expected := "providers schema -json -no-color" + + if actual != expected { + t.Fatalf("expected default arguments of ProvidersSchemaCmd:\n%s\n actual arguments:\n%s\n", expected, actual) + } +} + +func TestStateShow(t *testing.T) { td := testTempDir(t) defer os.RemoveAll(td) @@ -218,7 +271,7 @@ t.Fatalf("error running Init in test directory: %s", err) } - actual, err := tf.Show(context.Background()) + actual, err := tf.StateShow(context.Background()) if err != nil { t.Fatal(err) } @@ -245,7 +298,7 @@ // FIXME: Parse this in the actual command and return ErrNoInit expected := "Error: Could not satisfy plugin requirements" - _, err = tf.Show(context.Background()) + _, err = tf.StateShow(context.Background()) if err == nil { t.Fatal("expected Show to error, but it did not") } else { @@ -281,23 +334,6 @@ } } -func TestApplyCmd(t *testing.T) { - tf, err := NewTerraform("/dev/null", "") - if err != nil { - t.Fatal(err) - } - - applyCmd := tf.ApplyCmd(context.Background(), Backup("testbackup"), LockTimeout("200s"), State("teststate"), StateOut("teststateout"), VarFile("testvarfile"), Lock(false), Parallelism(99), Refresh(false), Target("target1"), Target("target2"), Var("var1=foo"), Var("var2=bar"), DirOrPlan("testfile")) - - actual := strings.TrimPrefix(applyCmd.String(), applyCmd.Path+" ") - - expected := "apply -no-color -auto-approve -input=false -backup=testbackup -lock-timeout=200s -state=teststate -state-out=teststateout -var-file=testvarfile -lock=false -parallelism=99 -refresh=false -target=target1 -target=target2 -var 'var1=foo' -var 'var2=bar' testfile" - - if actual != expected { - t.Fatalf("expected arguments of ApplyCmd:\n%s\n actual arguments:\n%s\n", expected, actual) - } -} - func testTempDir(t *testing.T) string { d, err := ioutil.TempDir("", "tf") if err != nil {