github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/cmd/compile/internal/gc/global_test.go (about)

     1  // Copyright 2015 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 gc
     6  
     7  import (
     8  	"bytes"
     9  	"internal/testenv"
    10  	"io/ioutil"
    11  	"os"
    12  	"os/exec"
    13  	"path/filepath"
    14  	"strings"
    15  	"testing"
    16  )
    17  
    18  // Make sure "hello world" does not link in all the
    19  // fmt.scanf routines. See issue 6853.
    20  func TestScanfRemoval(t *testing.T) {
    21  	testenv.MustHaveGoBuild(t)
    22  
    23  	// Make a directory to work in.
    24  	dir, err := ioutil.TempDir("", "issue6853a-")
    25  	if err != nil {
    26  		t.Fatalf("could not create directory: %v", err)
    27  	}
    28  	defer os.RemoveAll(dir)
    29  
    30  	// Create source.
    31  	src := filepath.Join(dir, "test.go")
    32  	f, err := os.Create(src)
    33  	if err != nil {
    34  		t.Fatalf("could not create source file: %v", err)
    35  	}
    36  	f.Write([]byte(`
    37  package main
    38  import "fmt"
    39  func main() {
    40  	fmt.Println("hello world")
    41  }
    42  `))
    43  	f.Close()
    44  
    45  	// Name of destination.
    46  	dst := filepath.Join(dir, "test")
    47  
    48  	// Compile source.
    49  	cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", dst, src)
    50  	out, err := cmd.CombinedOutput()
    51  	if err != nil {
    52  		t.Fatalf("could not build target: %v", err)
    53  	}
    54  
    55  	// Check destination to see if scanf code was included.
    56  	cmd = exec.Command(testenv.GoToolPath(t), "tool", "nm", dst)
    57  	out, err = cmd.CombinedOutput()
    58  	if err != nil {
    59  		t.Fatalf("could not read target: %v", err)
    60  	}
    61  	if bytes.Contains(out, []byte("scanInt")) {
    62  		t.Fatalf("scanf code not removed from helloworld")
    63  	}
    64  }
    65  
    66  // Make sure -S prints assembly code. See issue 14515.
    67  func TestDashS(t *testing.T) {
    68  	testenv.MustHaveGoBuild(t)
    69  
    70  	// Make a directory to work in.
    71  	dir, err := ioutil.TempDir("", "issue14515-")
    72  	if err != nil {
    73  		t.Fatalf("could not create directory: %v", err)
    74  	}
    75  	defer os.RemoveAll(dir)
    76  
    77  	// Create source.
    78  	src := filepath.Join(dir, "test.go")
    79  	f, err := os.Create(src)
    80  	if err != nil {
    81  		t.Fatalf("could not create source file: %v", err)
    82  	}
    83  	f.Write([]byte(`
    84  package main
    85  import "fmt"
    86  func main() {
    87  	fmt.Println("hello world")
    88  }
    89  `))
    90  	f.Close()
    91  
    92  	// Compile source.
    93  	cmd := exec.Command(testenv.GoToolPath(t), "build", "-gcflags", "-S", "-o", filepath.Join(dir, "test"), src)
    94  	out, err := cmd.CombinedOutput()
    95  	if err != nil {
    96  		t.Fatalf("could not build target: %v", err)
    97  	}
    98  
    99  	patterns := []string{
   100  		// It is hard to look for actual instructions in an
   101  		// arch-independent way. So we'll just look for
   102  		// pseudo-ops that are arch-independent.
   103  		"\tTEXT\t",
   104  		"\tFUNCDATA\t",
   105  		"\tPCDATA\t",
   106  	}
   107  	outstr := string(out)
   108  	for _, p := range patterns {
   109  		if !strings.Contains(outstr, p) {
   110  			println(outstr)
   111  			panic("can't find pattern " + p)
   112  		}
   113  	}
   114  }