.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.1.1-0.20210319172145-bda8f5cee399 / cmd / cover / cover_test.go
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.
4
5 // No testdata on Android.
6
7 //go:build !android
8 // +build !android
9
10 package main_test
11
12 import (
13         "bytes"
14         "fmt"
15         "io/ioutil"
16         "os"
17         "os/exec"
18         "path/filepath"
19         "testing"
20
21         "golang.org/x/tools/internal/testenv"
22 )
23
24 const (
25         // Data directory, also the package directory for the test.
26         testdata = "testdata"
27 )
28
29 var debug = false // Keeps the rewritten files around if set.
30
31 // Run this shell script, but do it in Go so it can be run by "go test".
32 //
33 //      replace the word LINE with the line number < testdata/test.go > testdata/test_line.go
34 //      go build -o ./testcover
35 //      ./testcover -mode=count -var=CoverTest -o ./testdata/test_cover.go testdata/test_line.go
36 //      go run ./testdata/main.go ./testdata/test.go
37 //
38 func TestCover(t *testing.T) {
39         testenv.NeedsTool(t, "go")
40
41         tmpdir, err := ioutil.TempDir("", "TestCover")
42         if err != nil {
43                 t.Fatal(err)
44         }
45         defer func() {
46                 if debug {
47                         fmt.Printf("test files left in %s\n", tmpdir)
48                 } else {
49                         os.RemoveAll(tmpdir)
50                 }
51         }()
52
53         testcover := filepath.Join(tmpdir, "testcover.exe")
54         testMain := filepath.Join(tmpdir, "main.go")
55         testTest := filepath.Join(tmpdir, "test.go")
56         coverInput := filepath.Join(tmpdir, "test_line.go")
57         coverOutput := filepath.Join(tmpdir, "test_cover.go")
58
59         for _, f := range []string{testMain, testTest} {
60                 data, err := ioutil.ReadFile(filepath.Join(testdata, filepath.Base(f)))
61                 if err != nil {
62                         t.Fatal(err)
63                 }
64                 if err := ioutil.WriteFile(f, data, 0644); err != nil {
65                         t.Fatal(err)
66                 }
67         }
68
69         // Read in the test file (testTest) and write it, with LINEs specified, to coverInput.
70         file, err := ioutil.ReadFile(testTest)
71         if err != nil {
72                 t.Fatal(err)
73         }
74         lines := bytes.Split(file, []byte("\n"))
75         for i, line := range lines {
76                 lines[i] = bytes.Replace(line, []byte("LINE"), []byte(fmt.Sprint(i+1)), -1)
77         }
78         err = ioutil.WriteFile(coverInput, bytes.Join(lines, []byte("\n")), 0666)
79         if err != nil {
80                 t.Fatal(err)
81         }
82
83         // go build -o testcover
84         cmd := exec.Command("go", "build", "-o", testcover)
85         run(cmd, t)
86
87         // ./testcover -mode=count -var=coverTest -o ./testdata/test_cover.go testdata/test_line.go
88         cmd = exec.Command(testcover, "-mode=count", "-var=coverTest", "-o", coverOutput, coverInput)
89         run(cmd, t)
90
91         // defer removal of ./testdata/test_cover.go
92         if !debug {
93                 defer os.Remove(coverOutput)
94         }
95
96         // go run ./testdata/main.go ./testdata/test.go
97         cmd = exec.Command("go", "run", testMain, coverOutput)
98         run(cmd, t)
99 }
100
101 func run(c *exec.Cmd, t *testing.T) {
102         c.Stdout = os.Stdout
103         c.Stderr = os.Stderr
104         err := c.Run()
105         if err != nil {
106                 t.Fatal(err)
107         }
108 }