Source file src/cmd/compile/internal/test/global_test.go

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

View as plain text