Skip to content

Commit

Permalink
all: Update Go Module to Go 1.20 and remove go-multierror direct depe…
Browse files Browse the repository at this point in the history
…ndency (#181)

Reference: https://pkg.go.dev/errors#Join
Reference: https://pkg.go.dev/math/rand#Seed
Reference: #99
Reference: #180

Previously from `golangci-lint` after Go 1.20 upgrade:

```
helper/acctest/random.go:24:2: SA1019: rand.Seed has been deprecated since Go 1.20 and an alternative has been available since Go 1.0: As of Go 1.20 there is no reason to call Seed with a random value. Programs that call Seed with a known value to get a specific sequence of results should use New(NewSource(seed)) to obtain a local random generator. (staticcheck)
        rand.Seed(time.Now().UTC().UnixNano())
        ^
```

Fully removing the go-multierror dependency will require some other upstream updates, e.g.

```
# github.com/hashicorp/go-multierror
github.com/hashicorp/terraform-plugin-testing/helper/resource
github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema
github.com/hashicorp/go-multierror
```
  • Loading branch information
bflad authored Sep 6, 2023
1 parent 6caf2e3 commit f5d6460
Show file tree
Hide file tree
Showing 13 changed files with 42 additions and 101 deletions.
8 changes: 8 additions & 0 deletions .changes/unreleased/NOTES-20230906-055849.yaml
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion .github/workflows/ci-go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions helper/acctest/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
12 changes: 4 additions & 8 deletions helper/resource/plan_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,24 @@ 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"
)

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...)
}
9 changes: 4 additions & 5 deletions helper/resource/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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...)
}
}

Expand Down
21 changes: 9 additions & 12 deletions helper/resource/testing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}

Expand Down
47 changes: 0 additions & 47 deletions internal/errorshim/error_join_shim.go

This file was deleted.

11 changes: 4 additions & 7 deletions plancheck/expect_empty_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ package plancheck

import (
"context"
"errors"
"fmt"

"github.com/hashicorp/terraform-plugin-testing/internal/errorshim"
)

var _ PlanCheck = expectEmptyPlan{}
Expand All @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions terraform/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"bufio"
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"os"
Expand All @@ -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"

Expand Down Expand Up @@ -338,7 +338,7 @@ func (s *State) Validate() error {
s.Lock()
defer s.Unlock()

var result error
var result []error

// !!!! FOR DEVELOPERS !!!!
//
Expand All @@ -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
}
Expand All @@ -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
Expand Down
13 changes: 4 additions & 9 deletions tfversion/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -42,18 +41,14 @@ 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)
strBuilder.WriteString("\n")
}
}

resp.Error = joinedErrors
resp.Error = errors.Join(joinedErrors...)
resp.Skip = strings.TrimSpace(strBuilder.String())
}
2 changes: 1 addition & 1 deletion tools/go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module tools

go 1.19
go 1.20

require github.com/hashicorp/copywrite v0.16.4

Expand Down

0 comments on commit f5d6460

Please sign in to comment.