Source file src/go/build/vendor_test.go

     1  // Copyright 2024 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 build
     6  
     7  import (
     8  	"internal/testenv"
     9  	"runtime"
    10  	"strings"
    11  	"testing"
    12  )
    13  
    14  // Prefixes for packages that can be vendored into the go repo.
    15  // The prefixes are component-wise; for example, "golang.org/x"
    16  // matches "golang.org/x/build" but not "golang.org/xyz".
    17  //
    18  // DO NOT ADD TO THIS LIST TO FIX BUILDS.
    19  // Vendoring a new package requires prior discussion.
    20  var allowedPackagePrefixes = []string{
    21  	"golang.org/x",
    22  	"github.com/google/pprof",
    23  	"github.com/ianlancetaylor/demangle",
    24  	"rsc.io/markdown",
    25  }
    26  
    27  // Verify that the vendor directories contain only packages matching the list above.
    28  func TestVendorPackages(t *testing.T) {
    29  	_, thisFile, _, _ := runtime.Caller(0)
    30  	goBin := testenv.GoToolPath(t)
    31  	listCmd := testenv.Command(t, goBin, "list", "std", "cmd")
    32  	out, err := listCmd.Output()
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	for _, fullPkg := range strings.Split(string(out), "\n") {
    37  		pkg, found := strings.CutPrefix(fullPkg, "vendor/")
    38  		if !found {
    39  			_, pkg, found = strings.Cut(fullPkg, "/vendor/")
    40  			if !found {
    41  				continue
    42  			}
    43  		}
    44  		if !isAllowed(pkg) {
    45  			t.Errorf(`
    46  		Package %q should not be vendored into this repo.
    47  		After getting approval from the Go team, add it to allowedPackagePrefixes
    48  		in %s.`,
    49  				pkg, thisFile)
    50  		}
    51  	}
    52  }
    53  
    54  func isAllowed(pkg string) bool {
    55  	for _, pre := range allowedPackagePrefixes {
    56  		if pkg == pre || strings.HasPrefix(pkg, pre+"/") {
    57  			return true
    58  		}
    59  	}
    60  	return false
    61  }
    62  
    63  func TestIsAllowed(t *testing.T) {
    64  	for _, test := range []struct {
    65  		in   string
    66  		want bool
    67  	}{
    68  		{"evil.com/bad", false},
    69  		{"golang.org/x/build", true},
    70  		{"rsc.io/markdown", true},
    71  		{"rsc.io/markdowntonabbey", false},
    72  		{"rsc.io/markdown/sub", true},
    73  	} {
    74  		got := isAllowed(test.in)
    75  		if got != test.want {
    76  			t.Errorf("%q: got %t, want %t", test.in, got, test.want)
    77  		}
    78  	}
    79  }
    80  

View as plain text