Source file src/cmd/vet/vet_test.go

     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
     6  
     7  import (
     8  	"bytes"
     9  	"errors"
    10  	"fmt"
    11  	"internal/testenv"
    12  	"log"
    13  	"os"
    14  	"os/exec"
    15  	"path"
    16  	"path/filepath"
    17  	"regexp"
    18  	"strconv"
    19  	"strings"
    20  	"sync"
    21  	"testing"
    22  )
    23  
    24  // TestMain executes the test binary as the vet command if
    25  // GO_VETTEST_IS_VET is set, and runs the tests otherwise.
    26  func TestMain(m *testing.M) {
    27  	if os.Getenv("GO_VETTEST_IS_VET") != "" {
    28  		main()
    29  		os.Exit(0)
    30  	}
    31  
    32  	os.Setenv("GO_VETTEST_IS_VET", "1") // Set for subprocesses to inherit.
    33  	os.Exit(m.Run())
    34  }
    35  
    36  // vetPath returns the path to the "vet" binary to run.
    37  func vetPath(t testing.TB) string {
    38  	t.Helper()
    39  	testenv.MustHaveExec(t)
    40  
    41  	vetPathOnce.Do(func() {
    42  		vetExePath, vetPathErr = os.Executable()
    43  	})
    44  	if vetPathErr != nil {
    45  		t.Fatal(vetPathErr)
    46  	}
    47  	return vetExePath
    48  }
    49  
    50  var (
    51  	vetPathOnce sync.Once
    52  	vetExePath  string
    53  	vetPathErr  error
    54  )
    55  
    56  func vetCmd(t *testing.T, arg, pkg string) *exec.Cmd {
    57  	cmd := testenv.Command(t, testenv.GoToolPath(t), "vet", "-vettool="+vetPath(t), arg, path.Join("cmd/vet/testdata", pkg))
    58  	cmd.Env = os.Environ()
    59  	return cmd
    60  }
    61  
    62  func TestVet(t *testing.T) {
    63  	t.Parallel()
    64  	for _, pkg := range []string{
    65  		"appends",
    66  		"asm",
    67  		"assign",
    68  		"atomic",
    69  		"bool",
    70  		"buildtag",
    71  		"cgo",
    72  		"composite",
    73  		"copylock",
    74  		"deadcode",
    75  		"directive",
    76  		"httpresponse",
    77  		"lostcancel",
    78  		"method",
    79  		"nilfunc",
    80  		"print",
    81  		"shift",
    82  		"slog",
    83  		"structtag",
    84  		"testingpkg",
    85  		// "testtag" has its own test
    86  		"unmarshal",
    87  		"unsafeptr",
    88  		"unused",
    89  	} {
    90  		pkg := pkg
    91  		t.Run(pkg, func(t *testing.T) {
    92  			t.Parallel()
    93  
    94  			// Skip cgo test on platforms without cgo.
    95  			if pkg == "cgo" && !cgoEnabled(t) {
    96  				return
    97  			}
    98  
    99  			cmd := vetCmd(t, "-printfuncs=Warn,Warnf", pkg)
   100  
   101  			// The asm test assumes amd64.
   102  			if pkg == "asm" {
   103  				cmd.Env = append(cmd.Env, "GOOS=linux", "GOARCH=amd64")
   104  			}
   105  
   106  			dir := filepath.Join("testdata", pkg)
   107  			gos, err := filepath.Glob(filepath.Join(dir, "*.go"))
   108  			if err != nil {
   109  				t.Fatal(err)
   110  			}
   111  			asms, err := filepath.Glob(filepath.Join(dir, "*.s"))
   112  			if err != nil {
   113  				t.Fatal(err)
   114  			}
   115  			var files []string
   116  			files = append(files, gos...)
   117  			files = append(files, asms...)
   118  
   119  			errchk(cmd, files, t)
   120  		})
   121  	}
   122  
   123  	// The loopclosure analyzer (aka "rangeloop" before CL 140578)
   124  	// is a no-op for files whose version >= go1.22, so we use a
   125  	// go.mod file in the rangeloop directory to "downgrade".
   126  	//
   127  	// TOOD(adonovan): delete when go1.21 goes away.
   128  	t.Run("loopclosure", func(t *testing.T) {
   129  		cmd := testenv.Command(t, testenv.GoToolPath(t), "vet", "-vettool="+vetPath(t), ".")
   130  		cmd.Env = append(os.Environ(), "GOWORK=off")
   131  		cmd.Dir = "testdata/rangeloop"
   132  		cmd.Stderr = new(strings.Builder) // all vet output goes to stderr
   133  		cmd.Run()
   134  		stderr := cmd.Stderr.(fmt.Stringer).String()
   135  
   136  		filename := filepath.FromSlash("testdata/rangeloop/rangeloop.go")
   137  
   138  		// Unlike the tests above, which runs vet in cmd/vet/, this one
   139  		// runs it in subdirectory, so the "full names" in the output
   140  		// are in fact short "./rangeloop.go".
   141  		// But we can't just pass "./rangeloop.go" as the "full name"
   142  		// argument to errorCheck as it does double duty as both a
   143  		// string that appears in the output, and as file name
   144  		// openable relative to the test directory, containing text
   145  		// expectations.
   146  		//
   147  		// So, we munge the file.
   148  		stderr = strings.ReplaceAll(stderr, filepath.FromSlash("./rangeloop.go"), filename)
   149  
   150  		if err := errorCheck(stderr, false, filename, filepath.Base(filename)); err != nil {
   151  			t.Errorf("error check failed: %s", err)
   152  			t.Log("vet stderr:\n", cmd.Stderr)
   153  		}
   154  	})
   155  }
   156  
   157  func cgoEnabled(t *testing.T) bool {
   158  	// Don't trust build.Default.CgoEnabled as it is false for
   159  	// cross-builds unless CGO_ENABLED is explicitly specified.
   160  	// That's fine for the builders, but causes commands like
   161  	// 'GOARCH=386 go test .' to fail.
   162  	// Instead, we ask the go command.
   163  	cmd := testenv.Command(t, testenv.GoToolPath(t), "list", "-f", "{{context.CgoEnabled}}")
   164  	out, _ := cmd.CombinedOutput()
   165  	return string(out) == "true\n"
   166  }
   167  
   168  func errchk(c *exec.Cmd, files []string, t *testing.T) {
   169  	output, err := c.CombinedOutput()
   170  	if _, ok := err.(*exec.ExitError); !ok {
   171  		t.Logf("vet output:\n%s", output)
   172  		t.Fatal(err)
   173  	}
   174  	fullshort := make([]string, 0, len(files)*2)
   175  	for _, f := range files {
   176  		fullshort = append(fullshort, f, filepath.Base(f))
   177  	}
   178  	err = errorCheck(string(output), false, fullshort...)
   179  	if err != nil {
   180  		t.Errorf("error check failed: %s", err)
   181  	}
   182  }
   183  
   184  // TestTags verifies that the -tags argument controls which files to check.
   185  func TestTags(t *testing.T) {
   186  	t.Parallel()
   187  	for tag, wantFile := range map[string]int{
   188  		"testtag":     1, // file1
   189  		"x testtag y": 1,
   190  		"othertag":    2,
   191  	} {
   192  		tag, wantFile := tag, wantFile
   193  		t.Run(tag, func(t *testing.T) {
   194  			t.Parallel()
   195  			t.Logf("-tags=%s", tag)
   196  			cmd := vetCmd(t, "-tags="+tag, "tagtest")
   197  			output, err := cmd.CombinedOutput()
   198  
   199  			want := fmt.Sprintf("file%d.go", wantFile)
   200  			dontwant := fmt.Sprintf("file%d.go", 3-wantFile)
   201  
   202  			// file1 has testtag and file2 has !testtag.
   203  			if !bytes.Contains(output, []byte(filepath.Join("tagtest", want))) {
   204  				t.Errorf("%s: %s was excluded, should be included", tag, want)
   205  			}
   206  			if bytes.Contains(output, []byte(filepath.Join("tagtest", dontwant))) {
   207  				t.Errorf("%s: %s was included, should be excluded", tag, dontwant)
   208  			}
   209  			if t.Failed() {
   210  				t.Logf("err=%s, output=<<%s>>", err, output)
   211  			}
   212  		})
   213  	}
   214  }
   215  
   216  // All declarations below were adapted from test/run.go.
   217  
   218  // errorCheck matches errors in outStr against comments in source files.
   219  // For each line of the source files which should generate an error,
   220  // there should be a comment of the form // ERROR "regexp".
   221  // If outStr has an error for a line which has no such comment,
   222  // this function will report an error.
   223  // Likewise if outStr does not have an error for a line which has a comment,
   224  // or if the error message does not match the <regexp>.
   225  // The <regexp> syntax is Perl but it's best to stick to egrep.
   226  //
   227  // Sources files are supplied as fullshort slice.
   228  // It consists of pairs: full path to source file and its base name.
   229  func errorCheck(outStr string, wantAuto bool, fullshort ...string) (err error) {
   230  	var errs []error
   231  	out := splitOutput(outStr, wantAuto)
   232  	// Cut directory name.
   233  	for i := range out {
   234  		for j := 0; j < len(fullshort); j += 2 {
   235  			full, short := fullshort[j], fullshort[j+1]
   236  			out[i] = strings.ReplaceAll(out[i], full, short)
   237  		}
   238  	}
   239  
   240  	var want []wantedError
   241  	for j := 0; j < len(fullshort); j += 2 {
   242  		full, short := fullshort[j], fullshort[j+1]
   243  		want = append(want, wantedErrors(full, short)...)
   244  	}
   245  	for _, we := range want {
   246  		var errmsgs []string
   247  		if we.auto {
   248  			errmsgs, out = partitionStrings("<autogenerated>", out)
   249  		} else {
   250  			errmsgs, out = partitionStrings(we.prefix, out)
   251  		}
   252  		if len(errmsgs) == 0 {
   253  			errs = append(errs, fmt.Errorf("%s:%d: missing error %q", we.file, we.lineNum, we.reStr))
   254  			continue
   255  		}
   256  		matched := false
   257  		n := len(out)
   258  		for _, errmsg := range errmsgs {
   259  			// Assume errmsg says "file:line: foo".
   260  			// Cut leading "file:line: " to avoid accidental matching of file name instead of message.
   261  			text := errmsg
   262  			if _, suffix, ok := strings.Cut(text, " "); ok {
   263  				text = suffix
   264  			}
   265  			if we.re.MatchString(text) {
   266  				matched = true
   267  			} else {
   268  				out = append(out, errmsg)
   269  			}
   270  		}
   271  		if !matched {
   272  			errs = append(errs, fmt.Errorf("%s:%d: no match for %#q in:\n\t%s", we.file, we.lineNum, we.reStr, strings.Join(out[n:], "\n\t")))
   273  			continue
   274  		}
   275  	}
   276  
   277  	if len(out) > 0 {
   278  		errs = append(errs, fmt.Errorf("Unmatched Errors:"))
   279  		for _, errLine := range out {
   280  			errs = append(errs, fmt.Errorf("%s", errLine))
   281  		}
   282  	}
   283  
   284  	if len(errs) == 0 {
   285  		return nil
   286  	}
   287  	if len(errs) == 1 {
   288  		return errs[0]
   289  	}
   290  	var buf strings.Builder
   291  	fmt.Fprintf(&buf, "\n")
   292  	for _, err := range errs {
   293  		fmt.Fprintf(&buf, "%s\n", err.Error())
   294  	}
   295  	return errors.New(buf.String())
   296  }
   297  
   298  func splitOutput(out string, wantAuto bool) []string {
   299  	// gc error messages continue onto additional lines with leading tabs.
   300  	// Split the output at the beginning of each line that doesn't begin with a tab.
   301  	// <autogenerated> lines are impossible to match so those are filtered out.
   302  	var res []string
   303  	for _, line := range strings.Split(out, "\n") {
   304  		line = strings.TrimSuffix(line, "\r") // normalize Windows output
   305  		if strings.HasPrefix(line, "\t") {
   306  			res[len(res)-1] += "\n" + line
   307  		} else if strings.HasPrefix(line, "go tool") || strings.HasPrefix(line, "#") || !wantAuto && strings.HasPrefix(line, "<autogenerated>") {
   308  			continue
   309  		} else if strings.TrimSpace(line) != "" {
   310  			res = append(res, line)
   311  		}
   312  	}
   313  	return res
   314  }
   315  
   316  // matchPrefix reports whether s starts with file name prefix followed by a :,
   317  // and possibly preceded by a directory name.
   318  func matchPrefix(s, prefix string) bool {
   319  	i := strings.Index(s, ":")
   320  	if i < 0 {
   321  		return false
   322  	}
   323  	j := strings.LastIndex(s[:i], "/")
   324  	s = s[j+1:]
   325  	if len(s) <= len(prefix) || s[:len(prefix)] != prefix {
   326  		return false
   327  	}
   328  	if s[len(prefix)] == ':' {
   329  		return true
   330  	}
   331  	return false
   332  }
   333  
   334  func partitionStrings(prefix string, strs []string) (matched, unmatched []string) {
   335  	for _, s := range strs {
   336  		if matchPrefix(s, prefix) {
   337  			matched = append(matched, s)
   338  		} else {
   339  			unmatched = append(unmatched, s)
   340  		}
   341  	}
   342  	return
   343  }
   344  
   345  type wantedError struct {
   346  	reStr   string
   347  	re      *regexp.Regexp
   348  	lineNum int
   349  	auto    bool // match <autogenerated> line
   350  	file    string
   351  	prefix  string
   352  }
   353  
   354  var (
   355  	errRx       = regexp.MustCompile(`// (?:GC_)?ERROR(NEXT)? (.*)`)
   356  	errAutoRx   = regexp.MustCompile(`// (?:GC_)?ERRORAUTO(NEXT)? (.*)`)
   357  	errQuotesRx = regexp.MustCompile(`"([^"]*)"`)
   358  	lineRx      = regexp.MustCompile(`LINE(([+-])(\d+))?`)
   359  )
   360  
   361  // wantedErrors parses expected errors from comments in a file.
   362  func wantedErrors(file, short string) (errs []wantedError) {
   363  	cache := make(map[string]*regexp.Regexp)
   364  
   365  	src, err := os.ReadFile(file)
   366  	if err != nil {
   367  		log.Fatal(err)
   368  	}
   369  	for i, line := range strings.Split(string(src), "\n") {
   370  		lineNum := i + 1
   371  		if strings.Contains(line, "////") {
   372  			// double comment disables ERROR
   373  			continue
   374  		}
   375  		var auto bool
   376  		m := errAutoRx.FindStringSubmatch(line)
   377  		if m != nil {
   378  			auto = true
   379  		} else {
   380  			m = errRx.FindStringSubmatch(line)
   381  		}
   382  		if m == nil {
   383  			continue
   384  		}
   385  		if m[1] == "NEXT" {
   386  			lineNum++
   387  		}
   388  		all := m[2]
   389  		mm := errQuotesRx.FindAllStringSubmatch(all, -1)
   390  		if mm == nil {
   391  			log.Fatalf("%s:%d: invalid errchk line: %s", file, lineNum, line)
   392  		}
   393  		for _, m := range mm {
   394  			replacedOnce := false
   395  			rx := lineRx.ReplaceAllStringFunc(m[1], func(m string) string {
   396  				if replacedOnce {
   397  					return m
   398  				}
   399  				replacedOnce = true
   400  				n := lineNum
   401  				if strings.HasPrefix(m, "LINE+") {
   402  					delta, _ := strconv.Atoi(m[5:])
   403  					n += delta
   404  				} else if strings.HasPrefix(m, "LINE-") {
   405  					delta, _ := strconv.Atoi(m[5:])
   406  					n -= delta
   407  				}
   408  				return fmt.Sprintf("%s:%d", short, n)
   409  			})
   410  			re := cache[rx]
   411  			if re == nil {
   412  				var err error
   413  				re, err = regexp.Compile(rx)
   414  				if err != nil {
   415  					log.Fatalf("%s:%d: invalid regexp \"%#q\" in ERROR line: %v", file, lineNum, rx, err)
   416  				}
   417  				cache[rx] = re
   418  			}
   419  			prefix := fmt.Sprintf("%s:%d", short, lineNum)
   420  			errs = append(errs, wantedError{
   421  				reStr:   rx,
   422  				re:      re,
   423  				prefix:  prefix,
   424  				auto:    auto,
   425  				lineNum: lineNum,
   426  				file:    short,
   427  			})
   428  		}
   429  	}
   430  
   431  	return
   432  }
   433  

View as plain text