1 // Copyright 2013 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.
5 // No testdata on Android.
20 "golang.org/x/tools/internal/testenv"
24 // Data directory, also the package directory for the test.
28 var debug = false // Keeps the rewritten files around if set.
30 // Run this shell script, but do it in Go so it can be run by "go test".
32 // replace the word LINE with the line number < testdata/test.go > testdata/test_line.go
33 // go build -o ./testcover
34 // ./testcover -mode=count -var=CoverTest -o ./testdata/test_cover.go testdata/test_line.go
35 // go run ./testdata/main.go ./testdata/test.go
37 func TestCover(t *testing.T) {
38 testenv.NeedsTool(t, "go")
40 tmpdir, err := ioutil.TempDir("", "TestCover")
46 fmt.Printf("test files left in %s\n", tmpdir)
52 testcover := filepath.Join(tmpdir, "testcover.exe")
53 testMain := filepath.Join(tmpdir, "main.go")
54 testTest := filepath.Join(tmpdir, "test.go")
55 coverInput := filepath.Join(tmpdir, "test_line.go")
56 coverOutput := filepath.Join(tmpdir, "test_cover.go")
58 for _, f := range []string{testMain, testTest} {
59 data, err := ioutil.ReadFile(filepath.Join(testdata, filepath.Base(f)))
63 if err := ioutil.WriteFile(f, data, 0644); err != nil {
68 // Read in the test file (testTest) and write it, with LINEs specified, to coverInput.
69 file, err := ioutil.ReadFile(testTest)
73 lines := bytes.Split(file, []byte("\n"))
74 for i, line := range lines {
75 lines[i] = bytes.Replace(line, []byte("LINE"), []byte(fmt.Sprint(i+1)), -1)
77 err = ioutil.WriteFile(coverInput, bytes.Join(lines, []byte("\n")), 0666)
82 // go build -o testcover
83 cmd := exec.Command("go", "build", "-o", testcover)
86 // ./testcover -mode=count -var=coverTest -o ./testdata/test_cover.go testdata/test_line.go
87 cmd = exec.Command(testcover, "-mode=count", "-var=coverTest", "-o", coverOutput, coverInput)
90 // defer removal of ./testdata/test_cover.go
92 defer os.Remove(coverOutput)
95 // go run ./testdata/main.go ./testdata/test.go
96 cmd = exec.Command("go", "run", testMain, coverOutput)
100 func run(c *exec.Cmd, t *testing.T) {