Source file src/internal/analysis/gotestchecker/run_test.go

     1  // Copyright 2026 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 gotestchecker
     6  
     7  import (
     8  	"errors"
     9  	"go/analysis"
    10  	"os"
    11  	"path/filepath"
    12  	"runtime"
    13  	"slices"
    14  	"strings"
    15  	"testing"
    16  
    17  	"internal/analysis/gotestchecker/protocol"
    18  )
    19  
    20  // TestTrimmedStacks checks that the failure messages of panics in an
    21  // analyzer, and in the encoding of its facts, show the stack of the
    22  // analyzer (or fact type), but not that of the driver.
    23  //
    24  // TODO(adonovan): make this a go test script test, when we can.
    25  func TestTrimmedStacks(t *testing.T) {
    26  	dir := t.TempDir()
    27  	file := filepath.Join(dir, "a.go")
    28  	if err := os.WriteFile(file, []byte("package a\n"), 0666); err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	for _, test := range []struct {
    32  		name string
    33  		a    *analysis.Analyzer
    34  		want string // a frame that the message must show
    35  	}{
    36  		{
    37  			name: "run",
    38  			a:    &analysis.Analyzer{Name: "panicky", Doc: "panic", Run: panicky},
    39  			want: "gotestchecker.panicky(",
    40  		},
    41  		{
    42  			name: "facts",
    43  			a: &analysis.Analyzer{
    44  				Name:      "facty",
    45  				Doc:       "export a fact that cannot be encoded",
    46  				Run:       facty,
    47  				FactTypes: []analysis.Fact{new(badFact)},
    48  			},
    49  			want: "gotestchecker.(*factSet).Encode(",
    50  		},
    51  	} {
    52  		t.Run(test.name, func(t *testing.T) {
    53  			out := filepath.Join(dir, test.name+".pass")
    54  			areq := &protocol.AnalyzeRequest{
    55  				ID:          "a",
    56  				ImportPath:  "a",
    57  				Dir:         dir,
    58  				GoFiles:     []string{file},
    59  				GoVersion:   "go1.28",
    60  				GOARCH:      runtime.GOARCH,
    61  				TypeImports: map[string]string{},
    62  				Facts:       map[int]map[string]string{},
    63  				Outputs:     map[int]string{0: out},
    64  				Report:      []int{0},
    65  			}
    66  			if err := analyze(areq, []Var{{Symbol: "a.A", Value: test.a}}, nil); err != nil {
    67  				t.Fatal(err)
    68  			}
    69  			pass, err := protocol.ReadPass(out)
    70  			if err != nil {
    71  				t.Fatal(err)
    72  			}
    73  			if pass.Failure == nil {
    74  				t.Fatal("pass did not fail")
    75  			}
    76  			msg := pass.Failure.Message
    77  			if !strings.Contains(msg, test.want) {
    78  				t.Errorf("failure message lacks %q:\n%s", test.want, msg)
    79  			}
    80  			for _, unwanted := range []string{
    81  				"panic(",                 // the call of panic
    82  				"runtime/debug.Stack(",   // the call of debug.Stack
    83  				"gotestchecker.runPass(", // and the driver's frames
    84  				"gotestchecker.encodeFacts(",
    85  				"gotestchecker.analyze(",
    86  			} {
    87  				if strings.Contains(msg, unwanted) {
    88  					t.Errorf("failure message contains %q:\n%s", unwanted, msg)
    89  				}
    90  			}
    91  		})
    92  	}
    93  }
    94  
    95  func panicky(*analysis.Pass) (any, error) { panic("oops") }
    96  
    97  func facty(pass *analysis.Pass) (any, error) {
    98  	pass.ExportPackageFact(new(badFact))
    99  	return nil, nil
   100  }
   101  
   102  // A badFact is a fact type whose values cannot be encoded.
   103  type badFact struct{}
   104  
   105  func (*badFact) AFact() {}
   106  
   107  func (*badFact) GobEncode() ([]byte, error) { return nil, errors.New("cannot encode") }
   108  
   109  // TestPrerequisiteFailure checks that the failure of a prerequisite
   110  // is recorded only in its own pass file, and that the dependent pass is
   111  // merely skipped.
   112  //
   113  // TODO(adonovan): make this a go test script test, when we can.
   114  func TestPrerequisiteFailure(t *testing.T) {
   115  	dir := t.TempDir()
   116  	file := filepath.Join(dir, "a.go")
   117  	if err := os.WriteFile(file, []byte("package a\n"), 0666); err != nil {
   118  		t.Fatal(err)
   119  	}
   120  	prereq := &analysis.Analyzer{Name: "panicky", Doc: "panic", Run: panicky}
   121  	dependent := &analysis.Analyzer{
   122  		Name:     "dependent",
   123  		Doc:      "require panicky",
   124  		Requires: []*analysis.Analyzer{prereq},
   125  		Run:      func(*analysis.Pass) (any, error) { return nil, nil },
   126  	}
   127  	vars := []Var{{Symbol: "a.A", Value: dependent}}
   128  	g := newGraph(vars)
   129  	var (
   130  		iprereq    = slices.Index(g.analyzers, prereq)
   131  		idependent = slices.Index(g.analyzers, dependent)
   132  	)
   133  	areq := &protocol.AnalyzeRequest{
   134  		ID:          "a",
   135  		ImportPath:  "a",
   136  		Dir:         dir,
   137  		GoFiles:     []string{file},
   138  		GoVersion:   "go1.28",
   139  		GOARCH:      runtime.GOARCH,
   140  		TypeImports: map[string]string{},
   141  		Facts:       map[int]map[string]string{},
   142  		Outputs: map[int]string{
   143  			iprereq:    filepath.Join(dir, "prereq.pass"),
   144  			idependent: filepath.Join(dir, "dependent.pass"),
   145  		},
   146  		Report: []int{idependent},
   147  	}
   148  	if err := analyze(areq, vars, nil); err != nil {
   149  		t.Fatal(err)
   150  	}
   151  	pass, err := protocol.ReadPass(areq.Outputs[iprereq])
   152  	if err != nil {
   153  		t.Fatal(err)
   154  	}
   155  	if pass.Failure == nil || !strings.Contains(pass.Failure.Message, "panic: oops") {
   156  		t.Errorf("prerequisite pass: got failure %v, want panic", pass.Failure)
   157  	}
   158  	pass, err = protocol.ReadPass(areq.Outputs[idependent])
   159  	if err != nil {
   160  		t.Fatal(err)
   161  	}
   162  	if pass.Failure != nil || !pass.Skipped {
   163  		t.Errorf("dependent pass: got failure %v, skipped %t; want skipped", pass.Failure, pass.Skipped)
   164  	}
   165  }
   166  

View as plain text