GoPLS Viewer

Home|gopls/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
10package main_test
11
12import (
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
24const (
25    // Data directory, also the package directory for the test.
26    testdata = "testdata"
27)
28
29var 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
37func TestCover(t *testing.T) {
38    testenv.NeedsTool(t"go")
39
40    tmpdirerr := ioutil.TempDir("""TestCover")
41    if err != nil {
42        t.Fatal(err)
43    }
44    defer func() {
45        if debug {
46            fmt.Printf("test files left in %s\n"tmpdir)
47        } else {
48            os.RemoveAll(tmpdir)
49        }
50    }()
51
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")
57
58    for _f := range []string{testMaintestTest} {
59        dataerr := ioutil.ReadFile(filepath.Join(testdatafilepath.Base(f)))
60        if err != nil {
61            t.Fatal(err)
62        }
63        if err := ioutil.WriteFile(fdata0644); err != nil {
64            t.Fatal(err)
65        }
66    }
67
68    // Read in the test file (testTest) and write it, with LINEs specified, to coverInput.
69    fileerr := ioutil.ReadFile(testTest)
70    if err != nil {
71        t.Fatal(err)
72    }
73    lines := bytes.Split(file, []byte("\n"))
74    for iline := range lines {
75        lines[i] = bytes.Replace(line, []byte("LINE"), []byte(fmt.Sprint(i+1)), -1)
76    }
77    err = ioutil.WriteFile(coverInputbytes.Join(lines, []byte("\n")), 0666)
78    if err != nil {
79        t.Fatal(err)
80    }
81
82    // go build -o testcover
83    cmd := exec.Command("go""build""-o"testcover)
84    run(cmdt)
85
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"coverOutputcoverInput)
88    run(cmdt)
89
90    // defer removal of ./testdata/test_cover.go
91    if !debug {
92        defer os.Remove(coverOutput)
93    }
94
95    // go run ./testdata/main.go ./testdata/test.go
96    cmd = exec.Command("go""run"testMaincoverOutput)
97    run(cmdt)
98}
99
100func run(c *exec.Cmdt *testing.T) {
101    c.Stdout = os.Stdout
102    c.Stderr = os.Stderr
103    err := c.Run()
104    if err != nil {
105        t.Fatal(err)
106    }
107}
108
MembersX
debug
TestCover.tmpdir
TestCover.coverOutput
TestCover.RangeStmt_1385.f
TestCover.RangeStmt_1385.BlockStmt.err
TestCover.lines
run.t
exec
TestCover.RangeStmt_1834.i
run.c
run.err
TestCover.RangeStmt_1385.BlockStmt.data
testing
testenv
TestCover.t
TestCover.err
TestCover.testcover
TestCover.testMain
TestCover.coverInput
TestCover.cmd
testdata
TestCover
TestCover.testTest
TestCover.file
TestCover.RangeStmt_1834.line
run
Members
X