Test that syntax (scan/parse), type, and loader errors are recorded (in PackageInfo.Errors) and reported (via Config.TypeChecker.Error).
(t *testing.T)
| 622 | // Test that syntax (scan/parse), type, and loader errors are recorded |
| 623 | // (in PackageInfo.Errors) and reported (via Config.TypeChecker.Error). |
| 624 | func TestErrorReporting(t *testing.T) { |
| 625 | pkgs := map[string]string{ |
| 626 | "a": `package a; import (_ "b"; _ "c"); var x int = false`, |
| 627 | "b": `package b; 'syntax error!`, |
| 628 | } |
| 629 | conf := loader.Config{ |
| 630 | AllowErrors: true, |
| 631 | Build: fakeContext(pkgs), |
| 632 | } |
| 633 | var mu sync.Mutex |
| 634 | var allErrors []error |
| 635 | conf.TypeChecker.Error = func(err error) { |
| 636 | mu.Lock() |
| 637 | allErrors = append(allErrors, err) |
| 638 | mu.Unlock() |
| 639 | } |
| 640 | conf.Import("a") |
| 641 | |
| 642 | prog, err := conf.Load() |
| 643 | if err != nil { |
| 644 | t.Errorf("Load failed: %s", err) |
| 645 | } |
| 646 | if prog == nil { |
| 647 | t.Fatalf("Load returned nil *Program") |
| 648 | } |
| 649 | |
| 650 | // TODO(adonovan): test keys of ImportMap. |
| 651 | |
| 652 | // Check errors recorded in each PackageInfo. |
| 653 | for pkg, info := range prog.AllPackages { |
| 654 | switch pkg.Path() { |
| 655 | case "a": |
| 656 | // The match below is unfortunately vague, because in go1.16 the error |
| 657 | // message in go/types changed from "cannot convert ..." to "cannot use |
| 658 | // ... as ... in assignment". |
| 659 | if !hasError(info.Errors, "cannot") { |
| 660 | t.Errorf("a.Errors = %v, want bool assignment (type) error", info.Errors) |
| 661 | } |
| 662 | if !hasError(info.Errors, "could not import c") { |
| 663 | t.Errorf("a.Errors = %v, want import (loader) error", info.Errors) |
| 664 | } |
| 665 | case "b": |
| 666 | if !hasError(info.Errors, "rune literal not terminated") { |
| 667 | t.Errorf("b.Errors = %v, want unterminated literal (syntax) error", info.Errors) |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | // Check errors reported via error handler. |
| 673 | if !hasError(allErrors, "cannot") || |
| 674 | !hasError(allErrors, "rune literal not terminated") || |
| 675 | !hasError(allErrors, "could not import c") { |
| 676 | t.Errorf("allErrors = %v, want syntax, type and loader errors", allErrors) |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | func TestCycles(t *testing.T) { |
| 681 | for _, test := range []struct { |