Check errors on loading package (#130)

This commit is contained in:
Suhas Karanth 2020-08-16 12:10:38 +05:30 committed by GitHub
parent c5b1da6fe9
commit 70fed06ee3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 0 deletions

View File

@ -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
}

View File

@ -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 {

View File

@ -0,0 +1,8 @@
package service
import "github.com/matryer/notexist"
// Service does something good with computers.
type Service interface {
DoSomething(notexist.SomeType) error
}