github.com/go-asm/go@v1.21.1-0.20240213172139-40c5ead50c48/cmd/go/work/cover.go (about)

     1  // Copyright 2023 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  // Action graph execution methods related to coverage.
     6  
     7  package work
     8  
     9  import (
    10  	"context"
    11  	"encoding/json"
    12  	"fmt"
    13  	"io"
    14  	"os"
    15  	"path/filepath"
    16  
    17  	"github.com/go-asm/go/cmd/cov/covcmd"
    18  	"github.com/go-asm/go/cmd/go/base"
    19  	"github.com/go-asm/go/cmd/go/cfg"
    20  	"github.com/go-asm/go/cmd/go/str"
    21  	"github.com/go-asm/go/coverage"
    22  )
    23  
    24  // CovData invokes "go tool covdata" with the specified arguments
    25  // as part of the execution of action 'a'.
    26  func (b *Builder) CovData(a *Action, cmdargs ...any) ([]byte, error) {
    27  	cmdline := str.StringList(cmdargs...)
    28  	args := append([]string{}, cfg.BuildToolexec...)
    29  	args = append(args, base.Tool("covdata"))
    30  	args = append(args, cmdline...)
    31  	return b.Shell(a).runOut(a.Objdir, nil, args)
    32  }
    33  
    34  // BuildActionCoverMetaFile locates and returns the path of the
    35  // meta-data file written by the "go tool cover" step as part of the
    36  // build action for the "go test -cover" run action 'runAct'. Note
    37  // that if the package has no functions the meta-data file will exist
    38  // but will be empty; in this case the return is an empty string.
    39  func BuildActionCoverMetaFile(runAct *Action) (string, error) {
    40  	p := runAct.Package
    41  	for i := range runAct.Deps {
    42  		pred := runAct.Deps[i]
    43  		if pred.Mode != "build" || pred.Package == nil {
    44  			continue
    45  		}
    46  		if pred.Package.ImportPath == p.ImportPath {
    47  			metaFile := pred.Objdir + covcmd.MetaFileForPackage(p.ImportPath)
    48  			f, err := os.Open(metaFile)
    49  			if err != nil {
    50  				return "", err
    51  			}
    52  			defer f.Close()
    53  			fi, err2 := f.Stat()
    54  			if err2 != nil {
    55  				return "", err2
    56  			}
    57  			if fi.Size() == 0 {
    58  				return "", nil
    59  			}
    60  			return metaFile, nil
    61  		}
    62  	}
    63  	return "", fmt.Errorf("internal error: unable to locate build action for package %q run action", p.ImportPath)
    64  }
    65  
    66  // WriteCoveragePercent writes out to the writer 'w' a "percent
    67  // statements covered" for the package whose test-run action is
    68  // 'runAct', based on the meta-data file 'mf'. This helper is used in
    69  // cases where a user runs "go test -cover" on a package that has
    70  // functions but no tests; in the normal case (package has tests)
    71  // the percentage is written by the test binary when it runs.
    72  func WriteCoveragePercent(b *Builder, runAct *Action, mf string, w io.Writer) error {
    73  	dir := filepath.Dir(mf)
    74  	output, cerr := b.CovData(runAct, "percent", "-i", dir)
    75  	if cerr != nil {
    76  		return b.Shell(runAct).reportCmd("", "", output, cerr)
    77  	}
    78  	_, werr := w.Write(output)
    79  	return werr
    80  }
    81  
    82  // WriteCoverageProfile writes out a coverage profile fragment for the
    83  // package whose test-run action is 'runAct'; content is written to
    84  // the file 'outf' based on the coverage meta-data info found in
    85  // 'mf'. This helper is used in cases where a user runs "go test
    86  // -cover" on a package that has functions but no tests.
    87  func WriteCoverageProfile(b *Builder, runAct *Action, mf, outf string, w io.Writer) error {
    88  	dir := filepath.Dir(mf)
    89  	output, err := b.CovData(runAct, "textfmt", "-i", dir, "-o", outf)
    90  	if err != nil {
    91  		return b.Shell(runAct).reportCmd("", "", output, err)
    92  	}
    93  	_, werr := w.Write(output)
    94  	return werr
    95  }
    96  
    97  // WriteCoverMetaFilesFile writes out a summary file ("meta-files
    98  // file") as part of the action function for the "writeCoverMeta"
    99  // pseudo action employed during "go test -coverpkg" runs where there
   100  // are multiple tests and multiple packages covered. It builds up a
   101  // table mapping package import path to meta-data file fragment and
   102  // writes it out to a file where it can be read by the various test
   103  // run actions. Note that this function has to be called A) after the
   104  // build actions are complete for all packages being tested, and B)
   105  // before any of the "run test" actions for those packages happen.
   106  // This requirement is enforced by adding making this action ("a")
   107  // dependent on all test package build actions, and making all test
   108  // run actions dependent on this action.
   109  func WriteCoverMetaFilesFile(b *Builder, ctx context.Context, a *Action) error {
   110  	sh := b.Shell(a)
   111  
   112  	// Build the metafilecollection object.
   113  	var collection coverage.MetaFileCollection
   114  	for i := range a.Deps {
   115  		dep := a.Deps[i]
   116  		if dep.Mode != "build" {
   117  			panic("unexpected mode " + dep.Mode)
   118  		}
   119  		metaFilesFile := dep.Objdir + covcmd.MetaFileForPackage(dep.Package.ImportPath)
   120  		// Check to make sure the meta-data file fragment exists
   121  		//  and has content (may be empty if package has no functions).
   122  		if fi, err := os.Stat(metaFilesFile); err != nil {
   123  			continue
   124  		} else if fi.Size() == 0 {
   125  			continue
   126  		}
   127  		collection.ImportPaths = append(collection.ImportPaths, dep.Package.ImportPath)
   128  		collection.MetaFileFragments = append(collection.MetaFileFragments, metaFilesFile)
   129  	}
   130  
   131  	// Serialize it.
   132  	data, err := json.Marshal(collection)
   133  	if err != nil {
   134  		return fmt.Errorf("marshal MetaFileCollection: %v", err)
   135  	}
   136  	data = append(data, '\n') // makes -x output more readable
   137  
   138  	// Create the directory for this action's objdir and
   139  	// then write out the serialized collection
   140  	// to a file in the directory.
   141  	if err := sh.Mkdir(a.Objdir); err != nil {
   142  		return err
   143  	}
   144  	mfpath := a.Objdir + coverage.MetaFilesFileName
   145  	if err := sh.writeFile(mfpath, data); err != nil {
   146  		return fmt.Errorf("writing metafiles file: %v", err)
   147  	}
   148  
   149  	// We're done.
   150  	return nil
   151  }