From 72284a7b2cca8de88e472f92d389abcdce671287 Mon Sep 17 00:00:00 2001 From: Masayuki Morita Date: Thu, 5 Oct 2023 10:34:19 +0900 Subject: [PATCH] Add tests for truncatePreReleaseVersion, containsBucketRequiredError --- tfexec/terraform_version_test.go | 43 +++++++++++++++++++++++++++ tfmigrate/test_helper.go | 22 -------------- tfmigrate/test_helper_test.go | 50 ++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 22 deletions(-) create mode 100644 tfmigrate/test_helper_test.go diff --git a/tfexec/terraform_version_test.go b/tfexec/terraform_version_test.go index 61adddb..5aca27f 100644 --- a/tfexec/terraform_version_test.go +++ b/tfexec/terraform_version_test.go @@ -5,6 +5,8 @@ import ( "fmt" "os" "testing" + + "github.com/hashicorp/go-version" ) func TestTerraformCLIVersion(t *testing.T) { @@ -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) + } + }) + } +} diff --git a/tfmigrate/test_helper.go b/tfmigrate/test_helper.go index 92a0de0..1660adc 100644 --- a/tfmigrate/test_helper.go +++ b/tfmigrate/test_helper.go @@ -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` diff --git a/tfmigrate/test_helper_test.go b/tfmigrate/test_helper_test.go new file mode 100644 index 0000000..3c50c59 --- /dev/null +++ b/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) + } + }) + } +}