Skip to content

Commit

Permalink
Fix >= 0.12.0 version detection for versions with prereleases
Browse files Browse the repository at this point in the history
  • Loading branch information
acastle committed Dec 4, 2020
1 parent c08b1a2 commit 8d921a7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 37 deletions.
4 changes: 1 addition & 3 deletions server/events/runtime/plan_step_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (p *PlanStepRunner) buildPlanCmd(ctx models.ProjectCommandContext, extraArg
// actually used in the configuration. Since there's no way for us to detect
// if the configuration is using those variables, we don't set them.
func (p *PlanStepRunner) tfVars(ctx models.ProjectCommandContext, tfVersion *version.Version) []string {
if vTwelveAndUp.Check(tfVersion) {
if tfVersion.GreaterThanOrEqual(version.Must(version.NewVersion("0.12.0"))) {
return nil
}

Expand Down Expand Up @@ -299,8 +299,6 @@ func (p *PlanStepRunner) runRemotePlan(
return output, err
}

var vTwelveAndUp = MustConstraint(">=0.12-a")

// remoteOpsErr01114 is the error terraform plan will return if this project is
// using TFE remote operations in TF 0.11.14.
var remoteOpsErr01114 = `Error: Saving a generated plan is currently not supported!
Expand Down
89 changes: 55 additions & 34 deletions server/events/runtime/plan_step_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,40 +618,9 @@ func TestRun_OutputOnErr(t *testing.T) {
// being used.
func TestRun_NoOptionalVarsIn012(t *testing.T) {
RegisterMockTestingT(t)
terraform := mocks.NewMockClient()

tfVersion, _ := version.NewVersion("0.12.0")
s := runtime.PlanStepRunner{
TerraformExecutor: terraform,
DefaultTFVersion: tfVersion,
}

When(terraform.RunCommandWithVersion(
matchers.AnyPtrToLoggingSimpleLogger(),
AnyString(),
AnyStringSlice(),
matchers2.AnyMapOfStringToString(),
matchers2.AnyPtrToGoVersionVersion(),
AnyString())).ThenReturn("output", nil)

output, err := s.Run(models.ProjectCommandContext{
Workspace: "default",
RepoRelDir: ".",
User: models.User{Username: "username"},
EscapedCommentArgs: []string{"comment", "args"},
Pull: models.PullRequest{
Num: 2,
},
BaseRepo: models.Repo{
FullName: "owner/repo",
Owner: "owner",
Name: "repo",
},
}, []string{"extra", "args"}, "/path", map[string]string(nil))
Ok(t, err)
Equals(t, "output", output)

expPlanArgs := []string{"plan",
expPlanArgs := []string{
"plan",
"-input=false",
"-refresh",
"-no-color",
Expand All @@ -662,7 +631,59 @@ func TestRun_NoOptionalVarsIn012(t *testing.T) {
"comment",
"args",
}
terraform.VerifyWasCalledOnce().RunCommandWithVersion(nil, "/path", expPlanArgs, map[string]string(nil), tfVersion, "default")

cases := []struct {
name string
tfVersion string
}{
{
"stable version",
"0.12.0",
},
{
"with prerelease",
"0.14.0-rc1",
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
terraform := mocks.NewMockClient()
When(terraform.RunCommandWithVersion(
matchers.AnyPtrToLoggingSimpleLogger(),
AnyString(),
AnyStringSlice(),
matchers2.AnyMapOfStringToString(),
matchers2.AnyPtrToGoVersionVersion(),
AnyString())).ThenReturn("output", nil)

tfVersion, _ := version.NewVersion(c.tfVersion)
s := runtime.PlanStepRunner{
TerraformExecutor: terraform,
DefaultTFVersion: tfVersion,
}

output, err := s.Run(models.ProjectCommandContext{
Workspace: "default",
RepoRelDir: ".",
User: models.User{Username: "username"},
EscapedCommentArgs: []string{"comment", "args"},
Pull: models.PullRequest{
Num: 2,
},
BaseRepo: models.Repo{
FullName: "owner/repo",
Owner: "owner",
Name: "repo",
},
}, []string{"extra", "args"}, "/path", map[string]string(nil))
Ok(t, err)
Equals(t, "output", output)

terraform.VerifyWasCalledOnce().RunCommandWithVersion(nil, "/path", expPlanArgs, map[string]string(nil), tfVersion, "default")
})
}

}

// Test plans if using remote ops.
Expand Down

0 comments on commit 8d921a7

Please sign in to comment.