Skip to content

Commit

Permalink
Add tests for truncatePreReleaseVersion, containsBucketRequiredError
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Oct 5, 2023
1 parent bb079fd commit 72284a7
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 22 deletions.
43 changes: 43 additions & 0 deletions tfexec/terraform_version_test.go
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"os"
"testing"

"github.com/hashicorp/go-version"
)

func TestTerraformCLIVersion(t *testing.T) {
Expand Down Expand Up @@ -87,3 +89,44 @@ func TestAccTerraformCLIVersion(t *testing.T) {
}
fmt.Printf("got = %s\n", got)
}

func TestTruncatePreReleaseVersion(t *testing.T) {
cases := []struct {
desc string
v string
want string
ok bool
}{
{
desc: "pre-release",
v: "1.6.0-rc1",
want: "1.6.0",
ok: true,
},
{
desc: "not pre-release",
v: "1.6.0",
want: "1.6.0",
ok: true,
},
}

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
v, err := version.NewVersion(tc.v)
if err != nil {
t.Fatalf("failed to parse version: %s", err)
}
got, err := truncatePreReleaseVersion(v)
if tc.ok && err != nil {
t.Fatalf("unexpected err: %s", err)
}
if !tc.ok && err == nil {
t.Fatalf("expected to return an error, but no error, got = %s", got)
}
if tc.ok && got.String() != tc.want {
t.Errorf("got: %s, want: %s", got, tc.want)
}
})
}
}
22 changes: 0 additions & 22 deletions tfmigrate/test_helper.go
Expand Up @@ -6,28 +6,6 @@ import "regexp"
// error message for missing bucket key in the s3 backend differs
// depending on the Terraform version.
// Define a helper function to hide the difference.
//
// # Terraform v1.5
//
// ```
// Error: "bucket": required field is not set
// ```
//
// # Terraform v1.6
//
// ```
//
// Error: Missing Required Value
//
// on main.tf line 4, in terraform:
// 4: backend "s3" {
//
// The attribute "bucket" is required by the backend.
//
// Refer to the backend documentation for additional information which
// attributes are required.
//
// ```
const testBucketRequiredErrorLegacyTF = `Error: "bucket": required field is not set`
const testBucketRequiredErrorTF16 = `The attribute "bucket" is required by the backend`

Expand Down
50 changes: 50 additions & 0 deletions tfmigrate/test_helper_test.go
@@ -0,0 +1,50 @@
package tfmigrate

import (
"errors"
"testing"
)

func TestContainsBucketRequiredError(t *testing.T) {
cases := []struct {
desc string
msg string
want bool
}{
{
desc: "terraform v1.5",
msg: `Error: "bucket": required field is not set`,
want: true,
},
{
desc: "terraform v1.6",
msg: `
Error: Missing Required Value
on main.tf line 4, in terraform:
4: backend "s3" {
The attribute "bucket" is required by the backend.
Refer to the backend documentation for additional information which
attributes are required.
`,
want: true,
},
{
desc: "unknown",
msg: `Error: unknown`,
want: false,
},
}

for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
got := containsBucketRequiredError(errors.New(tc.msg))
if got != tc.want {
t.Errorf("got: %t, want: %t", got, tc.want)
}
})
}
}

0 comments on commit 72284a7

Please sign in to comment.