github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/go/testdata/script/test_generated_main.txt (about)

     1  # Tests that the generated test main file has a generated code comment.
     2  # This is needed by analyzers that access source files through 'go list'.
     3  # Verifies golang.org/issue/31971.
     4  # TODO(jayconrod): This test is brittle. We should write _testmain.go as
     5  # a build action instead of with an ad-hoc WriteFile call
     6  # in internal/test/test.go. Then we could just grep 'go get -n'.
     7  go test x_test.go
     8  
     9  -- x_test.go --
    10  package x
    11  
    12  import (
    13  	"os"
    14  	"path/filepath"
    15  	"io/ioutil"
    16  	"regexp"
    17  	"testing"
    18  )
    19  
    20  func Test(t *testing.T) {
    21  	exePath, err := os.Executable()
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	testmainPath := filepath.Join(filepath.Dir(exePath), "_testmain.go")
    26  	source, err := ioutil.ReadFile(testmainPath)
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	if matched, err := regexp.Match(`(?m)^// Code generated .* DO NOT EDIT\.$`, source); err != nil {
    31  		t.Fatal(err)
    32  	} else if !matched {
    33  		t.Error("_testmain.go does not have generated code comment")
    34  	}
    35  }