Skip to content

Commit 77e3bb0

Browse files
committed
go/packages/packagestest: add package use example
The purpose and value of packagestest is easy to understand. However, the current API may not be easy to get started with. It includes identifiers such as Exporter, Exported, Export, whose names may not make it very clear in what order they are to be used, and whether the Exporter interfaces needs to be implemented by the caller. There are fairly common patterns of usage spread out across various packages that use packagestest. Add an example of basic usage to the documentation of this package that connects all of the pieces together, so that users don't have to look for it elsewhere. I would've preferred to add the example as example code¹, but it doesn't seem viable to write example code for test helper packages. There isn't a way to get a functional *testing.T in a func Example. ¹ https://golang.org/pkg/testing/#hdr-Examples Updates golang/go#33655 Change-Id: I0b15ff7974be25a71dfd4b68f470441ce7331d18 Reviewed-on: https://go-review.googlesource.com/c/tools/+/196980 Reviewed-by: Michael Matloob <[email protected]>
1 parent b2a5ed3 commit 77e3bb0

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

go/packages/packagestest/export.go

+53
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,59 @@ Package packagestest creates temporary projects on disk for testing go tools on.
88
By changing the exporter used, you can create projects for multiple build
99
systems from the same description, and run the same tests on them in many
1010
cases.
11+
12+
Example
13+
14+
As an example of packagestest use, consider the following test that runs
15+
the 'go list' command on the specified modules:
16+
17+
// TestGoList exercises the 'go list' command in module mode and in GOPATH mode.
18+
func TestGoList(t *testing.T) { packagestest.TestAll(t, testGoList) }
19+
func testGoList(t *testing.T, x packagestest.Exporter) {
20+
e := packagestest.Export(t, x, []packagestest.Module{
21+
{
22+
Name: "gopher.example/repoa",
23+
Files: map[string]interface{}{
24+
"a/a.go": "package a",
25+
},
26+
},
27+
{
28+
Name: "gopher.example/repob",
29+
Files: map[string]interface{}{
30+
"b/b.go": "package b",
31+
},
32+
},
33+
})
34+
defer e.Cleanup()
35+
36+
cmd := exec.Command("go", "list", "gopher.example/...")
37+
cmd.Dir = e.Config.Dir
38+
cmd.Env = e.Config.Env
39+
out, err := cmd.Output()
40+
if err != nil {
41+
t.Fatal(err)
42+
}
43+
t.Logf("'go list gopher.example/...' with %s mode layout:\n%s", x.Name(), out)
44+
}
45+
46+
TestGoList uses TestAll to exercise the 'go list' command with all
47+
exporters known to packagestest. Currently, packagestest includes
48+
exporters that produce module mode layouts and GOPATH mode layouts.
49+
Running the test with verbose output will print:
50+
51+
=== RUN TestGoList
52+
=== RUN TestGoList/GOPATH
53+
=== RUN TestGoList/Modules
54+
--- PASS: TestGoList (0.21s)
55+
--- PASS: TestGoList/GOPATH (0.03s)
56+
main_test.go:36: 'go list gopher.example/...' with GOPATH mode layout:
57+
gopher.example/repoa/a
58+
gopher.example/repob/b
59+
--- PASS: TestGoList/Modules (0.18s)
60+
main_test.go:36: 'go list gopher.example/...' with Modules mode layout:
61+
gopher.example/repoa/a
62+
gopher.example/repob/b
63+
1164
*/
1265
package packagestest
1366

0 commit comments

Comments
 (0)