github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/src/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  	testenv.MustHaveGoBuild(t)
    18  	dir, err := ioutil.TempDir("", "go-build")
    19  	if err != nil {
    20  		t.Fatal(err)
    21  	}
    22  	defer os.RemoveAll(dir)
    23  
    24  	out, err := exec.Command(testenv.GoToolPath(t), "build", "./testdata/issue10978").CombinedOutput()
    25  	if err == nil {
    26  		t.Fatal("expected build to fail")
    27  	}
    28  
    29  	wantErrors := map[string]int{
    30  		// Main function has dedicated error message.
    31  		"function main is undeclared in the main package": 1,
    32  
    33  		// Single error reporting per each symbol.
    34  		// This way, duplicated messages are not reported for
    35  		// multiple relocations with a same name.
    36  		"main.defined1: relocation target main.undefined not defined": 1,
    37  		"main.defined2: relocation target main.undefined not defined": 1,
    38  	}
    39  	unexpectedErrors := map[string]int{}
    40  
    41  	for _, l := range strings.Split(string(out), "\n") {
    42  		if strings.HasPrefix(l, "#") || l == "" {
    43  			continue
    44  		}
    45  		matched := ""
    46  		for want := range wantErrors {
    47  			if strings.Contains(l, want) {
    48  				matched = want
    49  				break
    50  			}
    51  		}
    52  		if matched != "" {
    53  			wantErrors[matched]--
    54  		} else {
    55  			unexpectedErrors[l]++
    56  		}
    57  	}
    58  
    59  	for want, n := range wantErrors {
    60  		switch {
    61  		case n > 0:
    62  			t.Errorf("unmatched error: %s (x%d)", want, n)
    63  		case n < 0:
    64  			t.Errorf("extra errors: %s (x%d)", want, -n)
    65  		}
    66  	}
    67  	for unexpected, n := range unexpectedErrors {
    68  		t.Errorf("unexpected error: %s (x%d)", unexpected, n)
    69  	}
    70  }