github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/tools/cmd/cover/cover_test.go (about)

     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  // +build !android
     8  
     9  package main_test
    10  
    11  import (
    12  	"bytes"
    13  	"fmt"
    14  	"io/ioutil"
    15  	"os"
    16  	"os/exec"
    17  	"path/filepath"
    18  	"testing"
    19  )
    20  
    21  const (
    22  	// Data directory, also the package directory for the test.
    23  	testdata = "testdata"
    24  
    25  	// Binaries we compile.
    26  	testcover = "./testcover.exe"
    27  )
    28  
    29  var (
    30  	// Files we use.
    31  	testMain    = filepath.Join(testdata, "main.go")
    32  	testTest    = filepath.Join(testdata, "test.go")
    33  	coverInput  = filepath.Join(testdata, "test_line.go")
    34  	coverOutput = filepath.Join(testdata, "test_cover.go")
    35  )
    36  
    37  var debug = false // Keeps the rewritten files around if set.
    38  
    39  // Run this shell script, but do it in Go so it can be run by "go test".
    40  //
    41  //	replace the word LINE with the line number < testdata/test.go > testdata/test_line.go
    42  // 	go build -o ./testcover
    43  // 	./testcover -mode=count -var=CoverTest -o ./testdata/test_cover.go testdata/test_line.go
    44  //	go run ./testdata/main.go ./testdata/test.go
    45  //
    46  func TestCover(t *testing.T) {
    47  	// Read in the test file (testTest) and write it, with LINEs specified, to coverInput.
    48  	file, err := ioutil.ReadFile(testTest)
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  	lines := bytes.Split(file, []byte("\n"))
    53  	for i, line := range lines {
    54  		lines[i] = bytes.Replace(line, []byte("LINE"), []byte(fmt.Sprint(i+1)), -1)
    55  	}
    56  	err = ioutil.WriteFile(coverInput, bytes.Join(lines, []byte("\n")), 0666)
    57  	if err != nil {
    58  		t.Fatal(err)
    59  	}
    60  
    61  	// defer removal of test_line.go
    62  	if !debug {
    63  		defer os.Remove(coverInput)
    64  	}
    65  
    66  	// go build -o testcover
    67  	cmd := exec.Command("go", "build", "-o", testcover)
    68  	run(cmd, t)
    69  
    70  	// defer removal of testcover
    71  	defer os.Remove(testcover)
    72  
    73  	// ./testcover -mode=count -var=coverTest -o ./testdata/test_cover.go testdata/test_line.go
    74  	cmd = exec.Command(testcover, "-mode=count", "-var=coverTest", "-o", coverOutput, coverInput)
    75  	run(cmd, t)
    76  
    77  	// defer removal of ./testdata/test_cover.go
    78  	if !debug {
    79  		defer os.Remove(coverOutput)
    80  	}
    81  
    82  	// go run ./testdata/main.go ./testdata/test.go
    83  	cmd = exec.Command("go", "run", testMain, coverOutput)
    84  	run(cmd, t)
    85  }
    86  
    87  func run(c *exec.Cmd, t *testing.T) {
    88  	c.Stdout = os.Stdout
    89  	c.Stderr = os.Stderr
    90  	err := c.Run()
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  }