github.com/euank/go@v0.0.0-20160829210321-495514729181/src/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  	"log"
    12  	"os"
    13  	"os/exec"
    14  	"path/filepath"
    15  	"strings"
    16  	"testing"
    17  )
    18  
    19  // Make sure "hello world" does not link in all the
    20  // fmt.scanf routines. See issue 6853.
    21  func TestScanfRemoval(t *testing.T) {
    22  	testenv.MustHaveGoBuild(t)
    23  
    24  	// Make a directory to work in.
    25  	dir, err := ioutil.TempDir("", "issue6853a-")
    26  	if err != nil {
    27  		log.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  		log.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("go", "build", "-o", dst, src)
    51  	out, err := cmd.CombinedOutput()
    52  	if err != nil {
    53  		log.Fatalf("could not build target: %v", err)
    54  	}
    55  
    56  	// Check destination to see if scanf code was included.
    57  	cmd = exec.Command("go", "tool", "nm", dst)
    58  	out, err = cmd.CombinedOutput()
    59  	if err != nil {
    60  		log.Fatalf("could not read target: %v", err)
    61  	}
    62  	if bytes.Contains(out, []byte("scanInt")) {
    63  		log.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  
    71  	// Make a directory to work in.
    72  	dir, err := ioutil.TempDir("", "issue14515-")
    73  	if err != nil {
    74  		log.Fatalf("could not create directory: %v", err)
    75  	}
    76  	defer os.RemoveAll(dir)
    77  
    78  	// Create source.
    79  	src := filepath.Join(dir, "test.go")
    80  	f, err := os.Create(src)
    81  	if err != nil {
    82  		log.Fatalf("could not create source file: %v", err)
    83  	}
    84  	f.Write([]byte(`
    85  package main
    86  import "fmt"
    87  func main() {
    88  	fmt.Println("hello world")
    89  }
    90  `))
    91  	f.Close()
    92  
    93  	// Compile source.
    94  	cmd := exec.Command("go", "build", "-gcflags", "-S", "-o", filepath.Join(dir, "test"), src)
    95  	out, err := cmd.CombinedOutput()
    96  	if err != nil {
    97  		log.Fatalf("could not build target: %v", err)
    98  	}
    99  
   100  	patterns := []string{
   101  		// It is hard to look for actual instructions in an
   102  		// arch-independent way. So we'll just look for
   103  		// pseudo-ops that are arch-independent.
   104  		"\tTEXT\t",
   105  		"\tFUNCDATA\t",
   106  		"\tPCDATA\t",
   107  	}
   108  	outstr := string(out)
   109  	for _, p := range patterns {
   110  		if !strings.Contains(outstr, p) {
   111  			println(outstr)
   112  			panic("can't find pattern " + p)
   113  		}
   114  	}
   115  }