* fix: Fail apply command if the plan file was generated for a workspace that isn't the selected workspace.
* Add change file
* test: Update test helper to include Workspace name in plan representation
* fix: Make error message more generic, so is applicable to backend and cloud blocks.
* fix: Make error message specific to backend or cloud block
* test: Add separate tests for backend/cloud usage
* test: Update remaining tests to include a value for Workspace in mocked plans
* Apply suggestions from code review
Co-authored-by: Radek Simko <radeksimko@users.noreply.github.com>
* fix: Panic when a plan file has missing workspace data
* test: Update test to match changes in error text
---------
Co-authored-by: Radek Simko <radeksimko@users.noreply.github.com>
// Check the workspace name in the plan matches the current workspace
currentWorkspace,err:=m.Workspace()
iferr!=nil{
diags=diags.Append(fmt.Errorf("error determining current workspace when initializing a backend from the plan file: %w",err))
returnnil,diags
}
varplannedWorkspacestring
varisCloudbool
switch{
caseplan.StateStore!=nil:
plannedWorkspace=plan.StateStore.Workspace
isCloud=false
caseplan.Backend!=nil:
plannedWorkspace=plan.Backend.Workspace
isCloud=plan.Backend.Type=="cloud"
default:
panic(fmt.Sprintf("Workspace data missing from plan file. Current workspace is %q. This is a bug in Terraform and should be reported.",currentWorkspace))
t.Fatalf("expected an error but got none: %s",diags.ErrWithWarnings())
}
expectedMsgs:=[]string{
fmt.Sprintf("The plan file describes changes to the %q workspace, but the %q workspace is currently in use",
planWorkspace,
otherWorkspace,
),
fmt.Sprintf(`If you'd like to continue to use the plan file, make sure the cloud block in your configuration contains the workspace name %q`,planWorkspace),
}
for_,msg:=rangeexpectedMsgs{
if!strings.Contains(diags.Err().Error(),msg){
t.Fatalf("expected error to include `%s`, but got:\n%s",
msg,
diags.Err())
}
}
})
}
// init a backend using -backend-config options multiple times