github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/runtime/coverage/ts_test.go (about)

     1  // Copyright 2022 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 coverage
     6  
     7  import (
     8  	"internal/goexperiment"
     9  	"os"
    10  	"path/filepath"
    11  	"strings"
    12  	"testing"
    13  	_ "unsafe"
    14  )
    15  
    16  //go:linkname testing_testGoCoverDir testing.testGoCoverDir
    17  func testing_testGoCoverDir() string
    18  
    19  // TestTestSupport does a basic verification of the functionality in
    20  // runtime/coverage.processCoverTestDir (doing this here as opposed to
    21  // relying on other test paths will provide a better signal when
    22  // running "go test -cover" for this package).
    23  func TestTestSupport(t *testing.T) {
    24  	if !goexperiment.CoverageRedesign {
    25  		return
    26  	}
    27  	if testing.CoverMode() == "" {
    28  		return
    29  	}
    30  	t.Logf("testing.testGoCoverDir() returns %s mode=%s\n",
    31  		testing_testGoCoverDir(), testing.CoverMode())
    32  
    33  	textfile := filepath.Join(t.TempDir(), "file.txt")
    34  	var sb strings.Builder
    35  	err := processCoverTestDirInternal(testing_testGoCoverDir(), textfile,
    36  		testing.CoverMode(), "", &sb)
    37  	if err != nil {
    38  		t.Fatalf("bad: %v", err)
    39  	}
    40  
    41  	// Check for existence of text file.
    42  	if inf, err := os.Open(textfile); err != nil {
    43  		t.Fatalf("problems opening text file %s: %v", textfile, err)
    44  	} else {
    45  		inf.Close()
    46  	}
    47  
    48  	// Check for percent output with expected tokens.
    49  	strout := sb.String()
    50  	want1 := "runtime/coverage"
    51  	want2 := "of statements"
    52  	if !strings.Contains(strout, want1) ||
    53  		!strings.Contains(strout, want2) {
    54  		t.Logf("output from run: %s\n", strout)
    55  		t.Fatalf("percent output missing key tokens: %q and %q",
    56  			want1, want2)
    57  	}
    58  }