From 70fed06ee377322a9134ef8b77165db112c1e599 Mon Sep 17 00:00:00 2001 From: Suhas Karanth Date: Sun, 16 Aug 2020 12:10:38 +0530 Subject: [PATCH] Check errors on loading package (#130) --- pkg/moq/moq.go | 6 ++++++ pkg/moq/moq_test.go | 11 +++++++++++ pkg/moq/testpackages/_parseerror/service/service.go | 8 ++++++++ 3 files changed, 25 insertions(+) create mode 100644 pkg/moq/testpackages/_parseerror/service/service.go diff --git a/pkg/moq/moq.go b/pkg/moq/moq.go index 2a55991..d7ffe1e 100644 --- a/pkg/moq/moq.go +++ b/pkg/moq/moq.go @@ -222,6 +222,12 @@ func pkgInfoFromPath(srcDir string, mode packages.LoadMode) (*packages.Package, if len(pkgs) > 1 { return nil, errors.New("More than one package was found") } + if errs := pkgs[0].Errors; len(errs) != 0 { + if len(errs) == 1 { + return nil, errs[0] + } + return nil, fmt.Errorf("%s (and %d more errors)", errs[0], len(errs)-1) + } return pkgs[0], nil } diff --git a/pkg/moq/moq_test.go b/pkg/moq/moq_test.go index e750a68..79eaa6c 100644 --- a/pkg/moq/moq_test.go +++ b/pkg/moq/moq_test.go @@ -544,6 +544,17 @@ func TestImportedPackageWithSameName(t *testing.T) { } } +func TestParseError(t *testing.T) { + _, err := New(Config{SrcDir: "testpackages/_parseerror/service"}) + if err == nil { + t.Errorf("expected error but got nil") + return + } + if !strings.Contains(err.Error(), `could not import github.com/matryer/notexist (invalid package name: "")`) { + t.Errorf("unexpected error: %s", err.Error()) + } +} + // normalize normalizes \r\n (windows) and \r (mac) // into \n (unix) func normalize(d []byte) []byte { diff --git a/pkg/moq/testpackages/_parseerror/service/service.go b/pkg/moq/testpackages/_parseerror/service/service.go new file mode 100644 index 0000000..7c88423 --- /dev/null +++ b/pkg/moq/testpackages/_parseerror/service/service.go @@ -0,0 +1,8 @@ +package service + +import "github.com/matryer/notexist" + +// Service does something good with computers. +type Service interface { + DoSomething(notexist.SomeType) error +}