diff --git a/.changes/unreleased/NOTES-20230906-055849.yaml b/.changes/unreleased/NOTES-20230906-055849.yaml new file mode 100644 index 000000000..a6d9ef9b3 --- /dev/null +++ b/.changes/unreleased/NOTES-20230906-055849.yaml @@ -0,0 +1,8 @@ +kind: NOTES +body: 'all: This Go module has been updated to Go 1.20 per the [Go support + policy](https://go.dev/doc/devel/release#policy). It is recommended to review + the [Go 1.20 release notes](https://go.dev/doc/go1.20) before upgrading. Any + consumers building on earlier Go versions may experience errors.' +time: 2023-09-06T05:58:49.879435-04:00 +custom: + Issue: "180" diff --git a/.github/workflows/ci-go.yml b/.github/workflows/ci-go.yml index 34d9b91e3..a703e9cb4 100644 --- a/.github/workflows/ci-go.yml +++ b/.github/workflows/ci-go.yml @@ -27,7 +27,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - go-version: [ '1.20', '1.19' ] + go-version: [ '1.21', '1.20' ] steps: - uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 - uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0 diff --git a/README.md b/README.md index 8342917c3..e03f8de35 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ When run from the root of a Terraform Provider codebase, Terraform’s testing f This project follows the [support policy](https://golang.org/doc/devel/release.html#policy) of Go as its support policy. The two latest major releases of Go are supported by the project. -Currently, that means Go **1.19** or later must be used when including this project as a dependency. +Currently, that means Go **1.20** or later must be used when including this project as a dependency. ## Contributing diff --git a/go.mod b/go.mod index 590513733..c4b803ec7 100644 --- a/go.mod +++ b/go.mod @@ -1,12 +1,11 @@ module github.com/hashicorp/terraform-plugin-testing -go 1.19 +go 1.20 require ( github.com/google/go-cmp v0.5.9 github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 github.com/hashicorp/go-hclog v1.5.0 - github.com/hashicorp/go-multierror v1.1.1 github.com/hashicorp/go-uuid v1.0.3 github.com/hashicorp/go-version v1.6.0 github.com/hashicorp/hc-install v0.6.0 @@ -35,6 +34,7 @@ require ( github.com/hashicorp/errwrap v1.0.0 // indirect github.com/hashicorp/go-checkpoint v0.5.0 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect + github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/go-plugin v1.4.10 // indirect github.com/hashicorp/terraform-registry-address v0.2.1 // indirect github.com/hashicorp/terraform-svchost v0.1.1 // indirect diff --git a/helper/acctest/random.go b/helper/acctest/random.go index abb778aa0..c26303eb6 100644 --- a/helper/acctest/random.go +++ b/helper/acctest/random.go @@ -20,10 +20,6 @@ import ( "golang.org/x/crypto/ssh" ) -func init() { - rand.Seed(time.Now().UTC().UnixNano()) -} - // Helpers for generating random tidbits for use in identifiers to prevent // collisions in acceptance tests. diff --git a/helper/resource/plan_checks.go b/helper/resource/plan_checks.go index 712e3dbdf..c64c02ba8 100644 --- a/helper/resource/plan_checks.go +++ b/helper/resource/plan_checks.go @@ -5,9 +5,9 @@ package resource import ( "context" + "errors" tfjson "github.com/hashicorp/terraform-json" - "github.com/hashicorp/terraform-plugin-testing/internal/errorshim" "github.com/hashicorp/terraform-plugin-testing/plancheck" "github.com/mitchellh/go-testing-interface" ) @@ -15,18 +15,14 @@ import ( func runPlanChecks(ctx context.Context, t testing.T, plan *tfjson.Plan, planChecks []plancheck.PlanCheck) error { t.Helper() - var result error + var result []error for _, planCheck := range planChecks { resp := plancheck.CheckPlanResponse{} planCheck.CheckPlan(ctx, plancheck.CheckPlanRequest{Plan: plan}, &resp) - if resp.Error != nil { - // TODO: Once Go 1.20 is the minimum supported version for this module, replace with `errors.Join` function - // - https://github.com/hashicorp/terraform-plugin-testing/issues/99 - result = errorshim.Join(result, resp.Error) - } + result = append(result, resp.Error) } - return result + return errors.Join(result...) } diff --git a/helper/resource/testing.go b/helper/resource/testing.go index 2c40fc396..de96ec7a2 100644 --- a/helper/resource/testing.go +++ b/helper/resource/testing.go @@ -15,7 +15,6 @@ import ( "strings" "time" - "github.com/hashicorp/go-multierror" "github.com/mitchellh/go-testing-interface" "github.com/hashicorp/terraform-plugin-go/tfprotov5" @@ -947,7 +946,7 @@ func ComposeTestCheckFunc(fs ...TestCheckFunc) TestCheckFunc { return func(s *terraform.State) error { for i, f := range fs { if err := f(s); err != nil { - return fmt.Errorf("Check %d/%d error: %s", i+1, len(fs), err) + return fmt.Errorf("Check %d/%d error: %w", i+1, len(fs), err) } } @@ -965,15 +964,15 @@ func ComposeTestCheckFunc(fs ...TestCheckFunc) TestCheckFunc { // TestCheckFuncs and aggregates failures. func ComposeAggregateTestCheckFunc(fs ...TestCheckFunc) TestCheckFunc { return func(s *terraform.State) error { - var result *multierror.Error + var result []error for i, f := range fs { if err := f(s); err != nil { - result = multierror.Append(result, fmt.Errorf("Check %d/%d error: %s", i+1, len(fs), err)) + result = append(result, fmt.Errorf("Check %d/%d error: %w", i+1, len(fs), err)) } } - return result.ErrorOrNil() + return errors.Join(result...) } } diff --git a/helper/resource/testing_test.go b/helper/resource/testing_test.go index 811bd34e9..e3a401cd1 100644 --- a/helper/resource/testing_test.go +++ b/helper/resource/testing_test.go @@ -13,7 +13,6 @@ import ( "strings" "testing" - "github.com/hashicorp/go-multierror" testinginterface "github.com/mitchellh/go-testing-interface" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -54,29 +53,27 @@ func TestParallelTest(t *testing.T) { func TestComposeAggregateTestCheckFunc(t *testing.T) { t.Parallel() + err1 := errors.New("Error 1") check1 := func(s *terraform.State) error { - return errors.New("Error 1") + return err1 } + err2 := errors.New("Error 2") check2 := func(s *terraform.State) error { - return errors.New("Error 2") + return err2 } f := ComposeAggregateTestCheckFunc(check1, check2) err := f(nil) if err == nil { - t.Fatalf("Expected errors") + t.Fatal("expected error, got none") } - multi, ok := err.(*multierror.Error) - if !ok { - t.Fatalf("unexpected type %T for err", err) + if !errors.Is(err, err1) { + t.Errorf("expected %s, got: %s", err1, err) } - if !strings.Contains(multi.Errors[0].Error(), "Error 1") { - t.Fatalf("Expected Error 1, Got %s", multi.Errors[0]) - } - if !strings.Contains(multi.Errors[1].Error(), "Error 2") { - t.Fatalf("Expected Error 2, Got %s", multi.Errors[1]) + if !errors.Is(err, err2) { + t.Errorf("expected %s, got: %s", err2, err) } } diff --git a/internal/errorshim/error_join_shim.go b/internal/errorshim/error_join_shim.go deleted file mode 100644 index b7371af82..000000000 --- a/internal/errorshim/error_join_shim.go +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -// TODO: Once Go 1.20 is the minimum supported version delete this package, replace all usages with `errors` package -// - https://github.com/hashicorp/terraform-plugin-testing/issues/99 -package errorshim - -// Copied from -> https://cs.opensource.google/go/go/+/refs/tags/go1.20.2:src/errors/join.go -func Join(errs ...error) error { - n := 0 - for _, err := range errs { - if err != nil { - n++ - } - } - if n == 0 { - return nil - } - e := &joinError{ - errs: make([]error, 0, n), - } - for _, err := range errs { - if err != nil { - e.errs = append(e.errs, err) - } - } - return e -} - -type joinError struct { - errs []error -} - -func (e *joinError) Error() string { - var b []byte - for i, err := range e.errs { - if i > 0 { - b = append(b, '\n') - } - b = append(b, err.Error()...) - } - return string(b) -} - -func (e *joinError) Unwrap() []error { - return e.errs -} diff --git a/plancheck/expect_empty_plan.go b/plancheck/expect_empty_plan.go index 92d55f465..14b65a248 100644 --- a/plancheck/expect_empty_plan.go +++ b/plancheck/expect_empty_plan.go @@ -5,9 +5,8 @@ package plancheck import ( "context" + "errors" "fmt" - - "github.com/hashicorp/terraform-plugin-testing/internal/errorshim" ) var _ PlanCheck = expectEmptyPlan{} @@ -16,17 +15,15 @@ type expectEmptyPlan struct{} // CheckPlan implements the plan check logic. func (e expectEmptyPlan) CheckPlan(ctx context.Context, req CheckPlanRequest, resp *CheckPlanResponse) { - var result error + var result []error for _, rc := range req.Plan.ResourceChanges { if !rc.Change.Actions.NoOp() { - // TODO: Once Go 1.20 is the minimum supported version for this module, replace with `errors.Join` function - // - https://github.com/hashicorp/terraform-plugin-testing/issues/99 - result = errorshim.Join(result, fmt.Errorf("expected empty plan, but %s has planned action(s): %v", rc.Address, rc.Change.Actions)) + result = append(result, fmt.Errorf("expected empty plan, but %s has planned action(s): %v", rc.Address, rc.Change.Actions)) } } - resp.Error = result + resp.Error = errors.Join(result...) } // ExpectEmptyPlan returns a plan check that asserts that there are no resource changes in the plan. diff --git a/terraform/state.go b/terraform/state.go index 1e0fcb285..9cd259e0c 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -7,6 +7,7 @@ import ( "bufio" "bytes" "encoding/json" + "errors" "fmt" "log" "os" @@ -17,7 +18,6 @@ import ( "sync" "github.com/hashicorp/go-cty/cty" - "github.com/hashicorp/go-multierror" "github.com/hashicorp/go-uuid" "github.com/mitchellh/copystructure" @@ -338,7 +338,7 @@ func (s *State) Validate() error { s.Lock() defer s.Unlock() - var result error + var result []error // !!!! FOR DEVELOPERS !!!! // @@ -360,7 +360,7 @@ func (s *State) Validate() error { key := strings.Join(ms.Path, ".") if _, ok := found[key]; ok { - result = multierror.Append(result, fmt.Errorf( + result = append(result, fmt.Errorf( strings.TrimSpace(stateValidateErrMultiModule), key)) continue } @@ -369,7 +369,7 @@ func (s *State) Validate() error { } } - return result + return errors.Join(result...) } // Remove removes the item in the state at the given address, returning diff --git a/tfversion/any.go b/tfversion/any.go index 27088e1a5..2fee9cb10 100644 --- a/tfversion/any.go +++ b/tfversion/any.go @@ -5,9 +5,8 @@ package tfversion import ( "context" + "errors" "strings" - - "github.com/hashicorp/terraform-plugin-testing/internal/errorshim" ) // Any will return a nil error and empty skip message (run the test) @@ -28,7 +27,7 @@ type anyCheck struct { // CheckTerraformVersion satisfies the TerraformVersionCheck interface. func (a anyCheck) CheckTerraformVersion(ctx context.Context, req CheckTerraformVersionRequest, resp *CheckTerraformVersionResponse) { - var joinedErrors error + var joinedErrors []error strBuilder := strings.Builder{} for _, subCheck := range a.terraformVersionChecks { @@ -42,11 +41,7 @@ func (a anyCheck) CheckTerraformVersion(ctx context.Context, req CheckTerraformV return } - if checkResp.Error != nil { - // TODO: Once Go 1.20 is the minimum supported version for this module, replace with `errors.Join` function - // - https://github.com/hashicorp/terraform-plugin-testing/issues/99 - joinedErrors = errorshim.Join(joinedErrors, checkResp.Error) - } + joinedErrors = append(joinedErrors, checkResp.Error) if checkResp.Skip != "" { strBuilder.WriteString(checkResp.Skip) @@ -54,6 +49,6 @@ func (a anyCheck) CheckTerraformVersion(ctx context.Context, req CheckTerraformV } } - resp.Error = joinedErrors + resp.Error = errors.Join(joinedErrors...) resp.Skip = strings.TrimSpace(strBuilder.String()) } diff --git a/tools/go.mod b/tools/go.mod index fcd3f7270..c1aaab58c 100644 --- a/tools/go.mod +++ b/tools/go.mod @@ -1,6 +1,6 @@ module tools -go 1.19 +go 1.20 require github.com/hashicorp/copywrite v0.16.4