Fix >= 0.12.0 version detection for versions with prereleases

This commit is contained in:
Alex Castle
2020-12-03 23:00:01 -05:00
parent c08b1a2996
commit 8d921a7dc7
2 changed files with 56 additions and 37 deletions

View File

@@ -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
}
@@ -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!

View File

@@ -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",
@@ -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.