github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/cmd/link/internal/ld/ld_test.go (about) 1 // Copyright 2018 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package ld 6 7 import ( 8 "internal/testenv" 9 "io/ioutil" 10 "os" 11 "os/exec" 12 "strings" 13 "testing" 14 ) 15 16 func TestUndefinedRelocErrors(t *testing.T) { 17 t.Parallel() 18 testenv.MustHaveGoBuild(t) 19 dir, err := ioutil.TempDir("", "go-build") 20 if err != nil { 21 t.Fatal(err) 22 } 23 defer os.RemoveAll(dir) 24 25 out, err := exec.Command(testenv.GoToolPath(t), "build", "./testdata/issue10978").CombinedOutput() 26 if err == nil { 27 t.Fatal("expected build to fail") 28 } 29 30 wantErrors := map[string]int{ 31 // Main function has dedicated error message. 32 "function main is undeclared in the main package": 1, 33 34 // Single error reporting per each symbol. 35 // This way, duplicated messages are not reported for 36 // multiple relocations with a same name. 37 "main.defined1: relocation target main.undefined not defined": 1, 38 "main.defined2: relocation target main.undefined not defined": 1, 39 } 40 unexpectedErrors := map[string]int{} 41 42 for _, l := range strings.Split(string(out), "\n") { 43 if strings.HasPrefix(l, "#") || l == "" { 44 continue 45 } 46 matched := "" 47 for want := range wantErrors { 48 if strings.Contains(l, want) { 49 matched = want 50 break 51 } 52 } 53 if matched != "" { 54 wantErrors[matched]-- 55 } else { 56 unexpectedErrors[l]++ 57 } 58 } 59 60 for want, n := range wantErrors { 61 switch { 62 case n > 0: 63 t.Errorf("unmatched error: %s (x%d)", want, n) 64 case n < 0: 65 t.Errorf("extra errors: %s (x%d)", want, -n) 66 } 67 } 68 for unexpected, n := range unexpectedErrors { 69 t.Errorf("unexpected error: %s (x%d)", unexpected, n) 70 } 71 }