WIP adding E2E tests - this requires a released binary of v1.16
diff --git a/tfexec/internal/e2etest/workspace_test.go b/tfexec/internal/e2etest/workspace_test.go index 0ba0a3d..7dc4c73 100644 --- a/tfexec/internal/e2etest/workspace_test.go +++ b/tfexec/internal/e2etest/workspace_test.go
@@ -5,16 +5,40 @@ import ( "context" + "io" "reflect" "testing" "github.com/hashicorp/go-version" + tfjson "github.com/hashicorp/terraform-json" "github.com/hashicorp/terraform-exec/tfexec" + "github.com/hashicorp/terraform-exec/tfexec/internal/testutil" ) const defaultWorkspace = "default" +func makeWorkspaceListOutput(current string, workspaces ...string) tfjson.WorkspaceListOutput { + output := tfjson.WorkspaceListOutput{ + FormatVersion: "1.0.0", + Diagnostics: []tfjson.Diagnostic{}, + } + + // Allow empty results + if current == "" { + return output + } + + for _, ws := range workspaces { + entry := tfjson.WorkspaceListEntry{ + Name: ws, + IsCurrent: ws == current, + } + output.Workspaces = append(output.Workspaces, entry) + } + return output +} + func TestWorkspace_default_only(t *testing.T) { runTest(t, "basic", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { assertWorkspaceList(t, tf, defaultWorkspace) @@ -139,6 +163,22 @@ }) } +func TestWorkspace_listJSON(t *testing.T) { + runTestWithVersions(t, []string{testutil.Latest_v1_16}, "workspace-list-json", func(t *testing.T, tfv *version.Version, tf *tfexec.Terraform) { + // List no workspaces when none exist + assertWorkspaceListJSON(t, tf, makeWorkspaceListOutput("")) + + // Create a workspace + newWorkspace := "new-workspace" + err := tf.WorkspaceNew(context.Background(), newWorkspace) + if err != nil { + t.Fatalf("got error creating new workspace: %s", err) + } + + assertWorkspaceListJSON(t, tf, makeWorkspaceListOutput(newWorkspace, newWorkspace)) + }) +} + func assertWorkspaceList(t *testing.T, tf *tfexec.Terraform, expectedCurrent string, expectedWorkspaces ...string) { actualWorkspaces, actualCurrent, err := tf.WorkspaceList(context.Background()) if err != nil { @@ -153,6 +193,22 @@ } } +func assertWorkspaceListJSON(t *testing.T, tf *tfexec.Terraform, expected tfjson.WorkspaceListOutput) { + w := io.Discard + output, err := tf.WorkspaceListJSON(context.Background(), w) + if err != nil { + t.Fatalf("got error querying workspace list: %s", err) + } + expectedCurrent := expected.CurrentWorkspace().Name + if output.CurrentWorkspace().Name != expectedCurrent { + t.Fatalf("expected %q workspace to be selected, got %q", expectedCurrent, output.CurrentWorkspace().Name) + } + + if !reflect.DeepEqual(*output, expected) { + t.Fatalf("expected %#v, got %#v", expected, output) + } +} + func assertWorkspaceShow(t *testing.T, tf *tfexec.Terraform, expectedWorkspace string) { actualWorkspace, err := tf.WorkspaceShow(context.Background()) if err != nil {