Skip to content

Commit 4356cea

Browse files
committed
internal/tests: add a dependency test for cmd/pkgsite
Ensuring that it keeps its limited set of dependencies. The test is rather slow: it takes about 5 seconds for me. I tried parallelizing it and only got it down to about 3 seconds so I figured it wasn't worth it. For golang/go#61399 Change-Id: I34b75a1f77480e02368ddb7d552e89ab106bf28e Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/551515 LUCI-TryBot-Result: Go LUCI <[email protected]> kokoro-CI: kokoro <[email protected]> Reviewed-by: Jonathan Amsterdam <[email protected]>
1 parent c63424d commit 4356cea

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
// deps_test tests dependencies of cmd/pkgsite are kept to a limited set.
6+
package deps_test
7+
8+
import (
9+
"os/exec"
10+
"strings"
11+
"testing"
12+
)
13+
14+
// non-test packages are allowed to depend on licensecheck and safehtml, x/ repos, and markdown.
15+
var allowedModDeps = map[string]bool{
16+
"github.com/google/licensecheck": true,
17+
"github.com/google/safehtml": true,
18+
"golang.org/x/mod": true,
19+
"golang.org/x/net": true,
20+
"golang.org/x/pkgsite": true,
21+
"golang.org/x/sync": true,
22+
"golang.org/x/text": true,
23+
"golang.org/x/tools": true,
24+
"rsc.io/markdown": true,
25+
}
26+
27+
// test packages are also allowed to depend on go-cmp
28+
var additionalAllowedTestModDeps = map[string]bool{
29+
"github.com/google/go-cmp": true,
30+
}
31+
32+
func TestCmdPkgsiteDeps(t *testing.T) {
33+
// First, list all dependencies of pkgsite.
34+
out, err := exec.Command("go", "list", "-deps", "golang.org/x/pkgsite/cmd/pkgsite").Output()
35+
if err != nil {
36+
t.Fatal("running go list: ", err)
37+
}
38+
pkgs := strings.Fields(string(out))
39+
for _, pkg := range pkgs {
40+
// Filter to only the dependencies that are in the pkgsite module.
41+
if !strings.HasPrefix(pkg, "golang.org/x/pkgsite") {
42+
continue
43+
}
44+
45+
// Get the test module deps and check them against allowedTestModDeps.
46+
out, err := exec.Command("go", "list", "-deps", "-test", "-f", "{{if .Module}}{{.Module.Path}}{{end}}", pkg).Output()
47+
if err != nil {
48+
t.Fatal(err)
49+
}
50+
testmodules := strings.Fields(string(out))
51+
for _, m := range testmodules {
52+
if !(allowedModDeps[m] || additionalAllowedTestModDeps[m]) {
53+
t.Fatalf("disallowed test module dependency %q found through %q", m, pkg)
54+
}
55+
}
56+
57+
// Get the module deps and check them against allowedModDeps
58+
out, err = exec.Command("go", "list", "-deps", "-f", "{{if .Module}}{{.Module.Path}}{{end}}", pkg).Output()
59+
if err != nil {
60+
t.Fatal(err)
61+
}
62+
modules := strings.Fields(string(out))
63+
for _, m := range modules {
64+
if !allowedModDeps[m] {
65+
t.Fatalf("disallowed module dependency %q found through %q", m, pkg)
66+
}
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)