Skip to content

Commit

Permalink
refactor: simplify the Packages.Errors implementation (#3344)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored Oct 24, 2024
1 parent b16d7d2 commit 4367bb9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 1 addition & 2 deletions internal/code/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ func (p *Packages) ModTidy() error {

// Errors returns any errors that were returned by Load, either from the call itself or any of the loaded packages.
func (p *Packages) Errors() PkgErrors {
var res []error //nolint:prealloc
res = append(res, p.loadErrors...)
res := append([]error{}, p.loadErrors...)
for _, pkg := range p.packages {
for _, err := range pkg.Errors {
res = append(res, err)
Expand Down
18 changes: 18 additions & 0 deletions internal/code/packages_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package code

import (
"errors"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/tools/go/packages"
)

func TestPackages(t *testing.T) {
Expand Down Expand Up @@ -40,6 +42,22 @@ func TestPackages(t *testing.T) {
})
}

func TestPackagesErrors(t *testing.T) {
loadFirstErr := errors.New("first")
loadSecondErr := errors.New("second")
packageErr := packages.Error{Msg: "package"}
p := &Packages{
loadErrors: []error{loadFirstErr, loadSecondErr},
packages: map[string]*packages.Package{"github.com/99designs/gqlgen/internal/code/testdata/a": {
Errors: []packages.Error{packageErr},
}},
}

errs := p.Errors()

assert.Equal(t, PkgErrors([]error{loadFirstErr, loadSecondErr, packageErr}), errs)
}

func TestNameForPackage(t *testing.T) {
var p Packages

Expand Down

0 comments on commit 4367bb9

Please sign in to comment.