Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify the Packages.Errors implementation #3344

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading