1
2
3
4
5 package test
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
19
20 func TestScanfRemoval(t *testing.T) {
21 testenv.MustHaveGoBuild(t)
22 t.Parallel()
23
24
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
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
47 dst := filepath.Join(dir, "test")
48
49
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\n%s", err, out)
54 }
55
56
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
68 func TestDashS(t *testing.T) {
69 testenv.MustHaveGoBuild(t)
70 t.Parallel()
71
72
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
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
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\n%s", err, out)
99 }
100
101 patterns := []string{
102
103
104
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 }
117
View as plain text