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