@@ -473,6 +473,79 @@ func TestPlan_outBackend(t *testing.T) {
473473 }
474474}
475475
476+ // When using "-out" with a backend, the plan should encode the backend config
477+ // and also the selected workspace, if workspaces are supported by the backend.
478+ //
479+ // This test demonstrates that setting the workspace in the backend plan
480+ // responds to the selected workspace, versus other tests that show the same process
481+ // when defaulting to the default workspace when there's a lack of information about
482+ // the selected workspace.
483+ //
484+ // To test planning with a non-default workspace we need to use a backend that supports
485+ // workspaces. In this test the `inmem` backend is used.
486+ func TestPlan_outBackend_withWorkspace (t * testing.T ) {
487+ // Create a temporary working directory
488+ td := t .TempDir ()
489+ testCopyDir (t , testFixturePath ("plan-out-backend-workspace" ), td )
490+ t .Chdir (td )
491+
492+ // These values are coupled with the test fixture used above.
493+ expectedBackendType := "inmem"
494+ expectedWorkspace := "custom-workspace"
495+
496+ outPath := "foo"
497+ p := testProvider ()
498+ p .GetProviderSchemaResponse = & providers.GetProviderSchemaResponse {
499+ ResourceTypes : map [string ]providers.Schema {
500+ "test_instance" : {
501+ Body : & configschema.Block {
502+ Attributes : map [string ]* configschema.Attribute {
503+ "id" : {
504+ Type : cty .String ,
505+ Computed : true ,
506+ },
507+ "ami" : {
508+ Type : cty .String ,
509+ Optional : true ,
510+ },
511+ },
512+ },
513+ },
514+ },
515+ }
516+ p .PlanResourceChangeFn = func (req providers.PlanResourceChangeRequest ) providers.PlanResourceChangeResponse {
517+ return providers.PlanResourceChangeResponse {
518+ PlannedState : req .ProposedNewState ,
519+ }
520+ }
521+ view , done := testView (t )
522+ c := & PlanCommand {
523+ Meta : Meta {
524+ testingOverrides : metaOverridesForProvider (p ),
525+ View : view ,
526+ },
527+ }
528+
529+ args := []string {
530+ "-out" , outPath ,
531+ }
532+ code := c .Run (args )
533+ output := done (t )
534+ if code != 0 {
535+ t .Logf ("stdout: %s" , output .Stdout ())
536+ t .Fatalf ("plan command failed with exit code %d\n \n %s" , code , output .Stderr ())
537+ }
538+
539+ plan := testReadPlan (t , outPath )
540+
541+ if got , want := plan .Backend .Type , expectedBackendType ; got != want {
542+ t .Errorf ("wrong backend type %q; want %q" , got , want )
543+ }
544+ if got , want := plan .Backend .Workspace , expectedWorkspace ; got != want {
545+ t .Errorf ("wrong backend workspace %q; want %q" , got , want )
546+ }
547+ }
548+
476549func TestPlan_refreshFalse (t * testing.T ) {
477550 // Create a temporary working directory that is empty
478551 td := t .TempDir ()
0 commit comments