Skip to content

Commit

Permalink
refactor: replace multierror with stdlib errors (#5220)
Browse files Browse the repository at this point in the history
* refactor: replace multierror with stdlib errors

Replace multierror lib with go errors.Join

* lint: fix linter issues

* lint: fix linter issues

* lint: fix linter issues
  • Loading branch information
kruskall authored Jul 30, 2024
1 parent b87f4e4 commit e0d77f0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 76 deletions.
31 changes: 0 additions & 31 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3992,37 +3992,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.


--------------------------------------------------------------------------------
Dependency : github.com/joeshaw/multierror
Version: v0.0.0-20140124173710-69b34d4ec901
Licence type (autodetected): MIT
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/joeshaw/[email protected]/LICENSE:

The MIT License (MIT)

Copyright (c) 2014 Joe Shaw

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.


--------------------------------------------------------------------------------
Dependency : github.com/josephspurrier/goversioninfo
Version: v0.0.0-20190209210621-63e6d1acd3dd
Expand Down
63 changes: 21 additions & 42 deletions dev-tools/mage/integtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
package mage

import (
"errors"
"fmt"
"os"
"path/filepath"
"strconv"

"github.com/joeshaw/multierror"
"github.com/magefile/mage/mg"
"github.com/pkg/errors"
)

const (
Expand Down Expand Up @@ -85,7 +84,7 @@ func (steps IntegrationTestSteps) Setup(env map[string]string) error {
// errors ignored
_ = steps.teardownFrom(prev, env)
}
return errors.Wrapf(err, "%s setup failed", step.Name())
return fmt.Errorf("%s setup failed: %w", step.Name(), err)
}
}
return nil
Expand All @@ -101,16 +100,16 @@ func (steps IntegrationTestSteps) Teardown(env map[string]string) error {
}

func (steps IntegrationTestSteps) teardownFrom(start int, env map[string]string) error {
var errs multierror.Errors
var errs []error
for i := start; i >= 0; i-- {
if mg.Verbose() {
fmt.Printf("Teardown %s...\n", steps[i].Name())
}
if err := steps[i].Teardown(env); err != nil {
errs = append(errs, errors.Wrapf(err, "%s teardown failed", steps[i].Name()))
errs = append(errs, fmt.Errorf("%s teardown failed: %w", steps[i].Name(), err))
}
}
return errs.Err()
return errors.Join(errs...)
}

// IntegrationTester is interface used by the actual test runner.
Expand Down Expand Up @@ -149,34 +148,19 @@ func NewIntegrationRunners(path string, passInEnv map[string]string) (Integratio
}
dir := filepath.Join(cwd, path)

// Load the overall steps to use (skipped inside of test environment, as they are never ran on the inside).
// These steps are duplicated per scenario.
var steps IntegrationTestSteps
if !IsInIntegTestEnv() {
for _, step := range globalIntegrationTestSetupSteps {
use, err := step.Use(dir)
if err != nil {
return nil, errors.Wrapf(err, "%s step failed on Use", step.Name())
}
if use {
steps = append(steps, step)
}
}
}

// Create the runners (can only be multiple).
var runners IntegrationRunners
for _, t := range globalIntegrationTesters {
use, err := t.Use(dir)
if err != nil {
return nil, errors.Wrapf(err, "%s tester failed on Use", t.Name())
return nil, fmt.Errorf("%s tester failed on Use: %w", t.Name(), err)
}
if !use {
continue
}
runner, err := initRunner(t, dir, passInEnv)
if err != nil {
return nil, errors.Wrapf(err, "initializing %s runner", t.Name())
return nil, fmt.Errorf("initializing %s runner: %w", t.Name(), err)
}
runners = append(runners, runner)
}
Expand All @@ -192,7 +176,7 @@ func NewIntegrationRunners(path string, passInEnv map[string]string) (Integratio
}
runner, err := initRunner(tester, dir, passInEnv)
if err != nil {
return nil, errors.Wrapf(err, "initializing docker runner")
return nil, fmt.Errorf("initializing docker runner: %w", err)
}
runners = append(runners, runner)
}
Expand Down Expand Up @@ -243,38 +227,33 @@ func initRunner(tester IntegrationTester, dir string, passInEnv map[string]strin
}

// Test actually performs the test.
func (r *IntegrationRunner) Test(mageTarget string, test func() error) (err error) {
func (r *IntegrationRunner) Test(mageTarget string, test func() error) error {
// Inside the testing environment just run the test.
if IsInIntegTestEnv() {
err = r.tester.InsideTest(test)
return
return r.tester.InsideTest(test)
}

// Honor the TEST_ENVIRONMENT value if set.
if testEnvVar, isSet := os.LookupEnv("TEST_ENVIRONMENT"); isSet {
var enabled bool
enabled, err = strconv.ParseBool(testEnvVar)
enabled, err := strconv.ParseBool(testEnvVar)
if err != nil {
err = errors.Wrap(err, "failed to parse TEST_ENVIRONMENT value")
return
return fmt.Errorf("failed to parse TEST_ENVIRONMENT value: %w", err)
}
if !enabled {
err = fmt.Errorf("TEST_ENVIRONMENT=%s", testEnvVar)
return
return fmt.Errorf("TEST_ENVIRONMENT=%s", testEnvVar)
}
}

// log missing requirements and do nothing
err = r.tester.HasRequirements()
err := r.tester.HasRequirements()
if err != nil {
// log error; and return (otherwise on machines without requirements it will mark the tests as failed)
fmt.Printf("skipping test run with %s due to missing requirements: %s\n", r.tester.Name(), err)
err = nil
return
//nolint:nilerr // log error; and return (otherwise on machines without requirements it will mark the tests as failed)
return nil
}

if err = r.steps.Setup(r.env); err != nil {
return
if err := r.steps.Setup(r.env); err != nil {
return err
}

// catch any panics to run teardown
Expand Down Expand Up @@ -306,18 +285,18 @@ func (r *IntegrationRunner) Test(mageTarget string, test func() error) (err erro
err = teardownErr
}
}
return
return err
}

// Test runs the test on each runner and collects the errors.
func (r IntegrationRunners) Test(mageTarget string, test func() error) error {
var errs multierror.Errors
var errs []error
for _, runner := range r {
if err := runner.Test(mageTarget, test); err != nil {
errs = append(errs, err)
}
}
return errs.Err()
return errors.Join(errs...)
}

func passThroughEnvs(env map[string]string, passthrough ...string) {
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ require (
github.com/hectane/go-acl v0.0.0-20190604041725-da78bae5fc95
github.com/jaypipes/ghw v0.12.0
github.com/jedib0t/go-pretty/v6 v6.4.6
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901
github.com/josephspurrier/goversioninfo v0.0.0-20190209210621-63e6d1acd3dd
github.com/kardianos/service v1.2.1-0.20210728001519-a323c3813bc7
github.com/magefile/mage v1.15.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1099,8 +1099,6 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 h1:rp+c0RAYOWj8l6qbCUTSiRLG/iKnW3K3/QfPPuSsBt4=
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901/go.mod h1:Z86h9688Y0wesXCyonoVr47MasHilkuLMqGhRZ4Hpak=
github.com/josephspurrier/goversioninfo v0.0.0-20190209210621-63e6d1acd3dd h1:KikNiFwUO3QLyeKyN4k9yBH9Pcu/gU/yficWi61cJIw=
github.com/josephspurrier/goversioninfo v0.0.0-20190209210621-63e6d1acd3dd/go.mod h1:eJTEwMjXb7kZ633hO3Ln9mBUCOjX2+FlTljvpl9SYdE=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
Expand Down

0 comments on commit e0d77f0

Please sign in to comment.