Skip to content

Commit e0d77f0

Browse files
authored
refactor: replace multierror with stdlib errors (elastic#5220)
* 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
1 parent b87f4e4 commit e0d77f0

File tree

4 files changed

+21
-76
lines changed

4 files changed

+21
-76
lines changed

NOTICE.txt

-31
Original file line numberDiff line numberDiff line change
@@ -3992,37 +3992,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39923992
SOFTWARE.
39933993

39943994

3995-
--------------------------------------------------------------------------------
3996-
Dependency : github.com/joeshaw/multierror
3997-
Version: v0.0.0-20140124173710-69b34d4ec901
3998-
Licence type (autodetected): MIT
3999-
--------------------------------------------------------------------------------
4000-
4001-
Contents of probable licence file $GOMODCACHE/github.com/joeshaw/multierror@v0.0.0-20140124173710-69b34d4ec901/LICENSE:
4002-
4003-
The MIT License (MIT)
4004-
4005-
Copyright (c) 2014 Joe Shaw
4006-
4007-
Permission is hereby granted, free of charge, to any person obtaining a copy
4008-
of this software and associated documentation files (the "Software"), to deal
4009-
in the Software without restriction, including without limitation the rights
4010-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
4011-
copies of the Software, and to permit persons to whom the Software is
4012-
furnished to do so, subject to the following conditions:
4013-
4014-
The above copyright notice and this permission notice shall be included in
4015-
all copies or substantial portions of the Software.
4016-
4017-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
4018-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
4019-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
4020-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
4021-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
4022-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
4023-
THE SOFTWARE.
4024-
4025-
40263995
--------------------------------------------------------------------------------
40273996
Dependency : github.com/josephspurrier/goversioninfo
40283997
Version: v0.0.0-20190209210621-63e6d1acd3dd

dev-tools/mage/integtest.go

+21-42
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@
55
package mage
66

77
import (
8+
"errors"
89
"fmt"
910
"os"
1011
"path/filepath"
1112
"strconv"
1213

13-
"github.com/joeshaw/multierror"
1414
"github.com/magefile/mage/mg"
15-
"github.com/pkg/errors"
1615
)
1716

1817
const (
@@ -85,7 +84,7 @@ func (steps IntegrationTestSteps) Setup(env map[string]string) error {
8584
// errors ignored
8685
_ = steps.teardownFrom(prev, env)
8786
}
88-
return errors.Wrapf(err, "%s setup failed", step.Name())
87+
return fmt.Errorf("%s setup failed: %w", step.Name(), err)
8988
}
9089
}
9190
return nil
@@ -101,16 +100,16 @@ func (steps IntegrationTestSteps) Teardown(env map[string]string) error {
101100
}
102101

103102
func (steps IntegrationTestSteps) teardownFrom(start int, env map[string]string) error {
104-
var errs multierror.Errors
103+
var errs []error
105104
for i := start; i >= 0; i-- {
106105
if mg.Verbose() {
107106
fmt.Printf("Teardown %s...\n", steps[i].Name())
108107
}
109108
if err := steps[i].Teardown(env); err != nil {
110-
errs = append(errs, errors.Wrapf(err, "%s teardown failed", steps[i].Name()))
109+
errs = append(errs, fmt.Errorf("%s teardown failed: %w", steps[i].Name(), err))
111110
}
112111
}
113-
return errs.Err()
112+
return errors.Join(errs...)
114113
}
115114

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

152-
// Load the overall steps to use (skipped inside of test environment, as they are never ran on the inside).
153-
// These steps are duplicated per scenario.
154-
var steps IntegrationTestSteps
155-
if !IsInIntegTestEnv() {
156-
for _, step := range globalIntegrationTestSetupSteps {
157-
use, err := step.Use(dir)
158-
if err != nil {
159-
return nil, errors.Wrapf(err, "%s step failed on Use", step.Name())
160-
}
161-
if use {
162-
steps = append(steps, step)
163-
}
164-
}
165-
}
166-
167151
// Create the runners (can only be multiple).
168152
var runners IntegrationRunners
169153
for _, t := range globalIntegrationTesters {
170154
use, err := t.Use(dir)
171155
if err != nil {
172-
return nil, errors.Wrapf(err, "%s tester failed on Use", t.Name())
156+
return nil, fmt.Errorf("%s tester failed on Use: %w", t.Name(), err)
173157
}
174158
if !use {
175159
continue
176160
}
177161
runner, err := initRunner(t, dir, passInEnv)
178162
if err != nil {
179-
return nil, errors.Wrapf(err, "initializing %s runner", t.Name())
163+
return nil, fmt.Errorf("initializing %s runner: %w", t.Name(), err)
180164
}
181165
runners = append(runners, runner)
182166
}
@@ -192,7 +176,7 @@ func NewIntegrationRunners(path string, passInEnv map[string]string) (Integratio
192176
}
193177
runner, err := initRunner(tester, dir, passInEnv)
194178
if err != nil {
195-
return nil, errors.Wrapf(err, "initializing docker runner")
179+
return nil, fmt.Errorf("initializing docker runner: %w", err)
196180
}
197181
runners = append(runners, runner)
198182
}
@@ -243,38 +227,33 @@ func initRunner(tester IntegrationTester, dir string, passInEnv map[string]strin
243227
}
244228

245229
// Test actually performs the test.
246-
func (r *IntegrationRunner) Test(mageTarget string, test func() error) (err error) {
230+
func (r *IntegrationRunner) Test(mageTarget string, test func() error) error {
247231
// Inside the testing environment just run the test.
248232
if IsInIntegTestEnv() {
249-
err = r.tester.InsideTest(test)
250-
return
233+
return r.tester.InsideTest(test)
251234
}
252235

253236
// Honor the TEST_ENVIRONMENT value if set.
254237
if testEnvVar, isSet := os.LookupEnv("TEST_ENVIRONMENT"); isSet {
255-
var enabled bool
256-
enabled, err = strconv.ParseBool(testEnvVar)
238+
enabled, err := strconv.ParseBool(testEnvVar)
257239
if err != nil {
258-
err = errors.Wrap(err, "failed to parse TEST_ENVIRONMENT value")
259-
return
240+
return fmt.Errorf("failed to parse TEST_ENVIRONMENT value: %w", err)
260241
}
261242
if !enabled {
262-
err = fmt.Errorf("TEST_ENVIRONMENT=%s", testEnvVar)
263-
return
243+
return fmt.Errorf("TEST_ENVIRONMENT=%s", testEnvVar)
264244
}
265245
}
266246

267247
// log missing requirements and do nothing
268-
err = r.tester.HasRequirements()
248+
err := r.tester.HasRequirements()
269249
if err != nil {
270-
// log error; and return (otherwise on machines without requirements it will mark the tests as failed)
271250
fmt.Printf("skipping test run with %s due to missing requirements: %s\n", r.tester.Name(), err)
272-
err = nil
273-
return
251+
//nolint:nilerr // log error; and return (otherwise on machines without requirements it will mark the tests as failed)
252+
return nil
274253
}
275254

276-
if err = r.steps.Setup(r.env); err != nil {
277-
return
255+
if err := r.steps.Setup(r.env); err != nil {
256+
return err
278257
}
279258

280259
// catch any panics to run teardown
@@ -306,18 +285,18 @@ func (r *IntegrationRunner) Test(mageTarget string, test func() error) (err erro
306285
err = teardownErr
307286
}
308287
}
309-
return
288+
return err
310289
}
311290

312291
// Test runs the test on each runner and collects the errors.
313292
func (r IntegrationRunners) Test(mageTarget string, test func() error) error {
314-
var errs multierror.Errors
293+
var errs []error
315294
for _, runner := range r {
316295
if err := runner.Test(mageTarget, test); err != nil {
317296
errs = append(errs, err)
318297
}
319298
}
320-
return errs.Err()
299+
return errors.Join(errs...)
321300
}
322301

323302
func passThroughEnvs(env map[string]string, passthrough ...string) {

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ require (
3434
github.com/hectane/go-acl v0.0.0-20190604041725-da78bae5fc95
3535
github.com/jaypipes/ghw v0.12.0
3636
github.com/jedib0t/go-pretty/v6 v6.4.6
37-
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901
3837
github.com/josephspurrier/goversioninfo v0.0.0-20190209210621-63e6d1acd3dd
3938
github.com/kardianos/service v1.2.1-0.20210728001519-a323c3813bc7
4039
github.com/magefile/mage v1.15.0

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -1099,8 +1099,6 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y
10991099
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
11001100
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
11011101
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
1102-
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 h1:rp+c0RAYOWj8l6qbCUTSiRLG/iKnW3K3/QfPPuSsBt4=
1103-
github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901/go.mod h1:Z86h9688Y0wesXCyonoVr47MasHilkuLMqGhRZ4Hpak=
11041102
github.com/josephspurrier/goversioninfo v0.0.0-20190209210621-63e6d1acd3dd h1:KikNiFwUO3QLyeKyN4k9yBH9Pcu/gU/yficWi61cJIw=
11051103
github.com/josephspurrier/goversioninfo v0.0.0-20190209210621-63e6d1acd3dd/go.mod h1:eJTEwMjXb7kZ633hO3Ln9mBUCOjX2+FlTljvpl9SYdE=
11061104
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=

0 commit comments

Comments
 (0)