github.com/sean-/go@v0.0.0-20151219100004-97f854cd7bb6/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"
    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  		log.Fatalf("could not create directory: %v", err)
    27  	}
    28  	defer os.RemoveAll(dir)
    29  
    30  	// Create source.
    31  	src := path.Join(dir, "test.go")
    32  	f, err := os.Create(src)
    33  	if err != nil {
    34  		log.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 := path.Join(dir, "test")
    47  
    48  	// Compile source.
    49  	cmd := exec.Command("go", "build", "-o", dst, src)
    50  	out, err := cmd.CombinedOutput()
    51  	if err != nil {
    52  		log.Fatalf("could not build target: %v", err)
    53  	}
    54  
    55  	// Check destination to see if scanf code was included.
    56  	cmd = exec.Command("go", "tool", "nm", dst)
    57  	out, err = cmd.CombinedOutput()
    58  	if err != nil {
    59  		log.Fatalf("could not read target: %v", err)
    60  	}
    61  	if bytes.Index(out, []byte("scanInt")) != -1 {
    62  		log.Fatalf("scanf code not removed from helloworld")
    63  	}
    64  }