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