From 1cbd7cb1c78da094675ad4b031c292abd6cf5b55 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Thu, 24 Oct 2024 15:43:58 +0300 Subject: [PATCH] refactor: simplify the Packages.Errors implementation --- internal/code/packages.go | 3 +-- internal/code/packages_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/internal/code/packages.go b/internal/code/packages.go index 17fd5bb6fe4..3b08b01cac7 100644 --- a/internal/code/packages.go +++ b/internal/code/packages.go @@ -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) diff --git a/internal/code/packages_test.go b/internal/code/packages_test.go index d390741c568..2485df8cc41 100644 --- a/internal/code/packages_test.go +++ b/internal/code/packages_test.go @@ -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) { @@ -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