From 2e15dc7cd66acafcfca99b361dcddfb9e25286a9 Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 31 Jan 2024 12:15:38 -0500 Subject: [PATCH] gopls/internal/test/marker: reject "module testdata" Updates golang/go#65406 Change-Id: I1ce4e4d94f118c4b51dc52d624d90e2f7e05f048 Reviewed-on: https://go-review.googlesource.com/c/tools/+/559457 Reviewed-by: Robert Findley LUCI-TryBot-Result: Go LUCI --- gopls/internal/test/marker/marker_test.go | 9 +++++++++ .../testdata/codeaction/extract-variadic-63287.txt | 2 +- .../test/marker/testdata/codeaction/inline.txt | 2 +- .../test/marker/testdata/codeaction/issue64558.txt | 2 +- .../internal/test/marker/testdata/hover/methods.txt | 10 +++++----- .../internal/test/marker/testdata/rename/doclink.txt | 8 ++++---- .../test/marker/testdata/signature/issue63804.txt | 2 +- .../test/marker/testdata/suggestedfix/issue65024.txt | 12 ++++++------ 8 files changed, 28 insertions(+), 19 deletions(-) diff --git a/gopls/internal/test/marker/marker_test.go b/gopls/internal/test/marker/marker_test.go index 5de4408f141..18c97000762 100644 --- a/gopls/internal/test/marker/marker_test.go +++ b/gopls/internal/test/marker/marker_test.go @@ -698,6 +698,15 @@ func loadMarkerTest(name string, content []byte) (*markerTest, error) { return nil, fmt.Errorf("%s:%d: unwanted space before marker (// @)", file.Name, line) } + // The 'go list' command doesn't work correct with modules named + // testdata", so don't allow it as a module name (golang/go#65406). + // (Otherwise files within it will end up in an ad hoc + // package, "command-line-arguments/$TMPDIR/...".) + if filepath.Base(file.Name) == "go.mod" && + bytes.Contains(file.Data, []byte("module testdata")) { + return nil, fmt.Errorf("'testdata' is not a valid module name") + } + test.notes = append(test.notes, notes...) test.files[file.Name] = file.Data } diff --git a/gopls/internal/test/marker/testdata/codeaction/extract-variadic-63287.txt b/gopls/internal/test/marker/testdata/codeaction/extract-variadic-63287.txt index 756a88a4f78..d5dbe931226 100644 --- a/gopls/internal/test/marker/testdata/codeaction/extract-variadic-63287.txt +++ b/gopls/internal/test/marker/testdata/codeaction/extract-variadic-63287.txt @@ -3,7 +3,7 @@ It is a regression test for bug #63287 in which the final paramater's "..." would go missing. -- go.mod -- -module testdata +module example.com go 1.18 -- a/a.go -- diff --git a/gopls/internal/test/marker/testdata/codeaction/inline.txt b/gopls/internal/test/marker/testdata/codeaction/inline.txt index 813a69ce09c..339a05b0c41 100644 --- a/gopls/internal/test/marker/testdata/codeaction/inline.txt +++ b/gopls/internal/test/marker/testdata/codeaction/inline.txt @@ -1,7 +1,7 @@ This is a minimal test of the refactor.inline code action. -- go.mod -- -module testdata/codeaction +module example.com/codeaction go 1.18 -- a/a.go -- diff --git a/gopls/internal/test/marker/testdata/codeaction/issue64558.txt b/gopls/internal/test/marker/testdata/codeaction/issue64558.txt index 71d966159f8..59aaffba371 100644 --- a/gopls/internal/test/marker/testdata/codeaction/issue64558.txt +++ b/gopls/internal/test/marker/testdata/codeaction/issue64558.txt @@ -1,7 +1,7 @@ Test of an inlining failure due to an ill-typed input program (#64558). -- go.mod -- -module testdata +module example.com go 1.18 -- a/a.go -- diff --git a/gopls/internal/test/marker/testdata/hover/methods.txt b/gopls/internal/test/marker/testdata/hover/methods.txt index 101a719795d..8af22494f75 100644 --- a/gopls/internal/test/marker/testdata/hover/methods.txt +++ b/gopls/internal/test/marker/testdata/hover/methods.txt @@ -9,7 +9,7 @@ Observe that: - only accessible methods are shown. -- go.mod -- -module testdata +module example.com -- lib/lib.go -- package lib @@ -31,7 +31,7 @@ func (s *S) pb() {} -- a/a.go -- package a -import "testdata/lib" +import "example.com/lib" var _ lib.I //@hover("I", "I", I) var _ lib.J //@hover("J", "J", J) @@ -50,13 +50,13 @@ type I interface { func (lib.J) C() ``` -[`lib.I` on pkg.go.dev](https://pkg.go.dev/testdata/lib#I) +[`lib.I` on pkg.go.dev](https://pkg.go.dev/example.com/lib#I) -- @J -- ```go type J interface{ C() } ``` -[`lib.J` on pkg.go.dev](https://pkg.go.dev/testdata/lib#J) +[`lib.J` on pkg.go.dev](https://pkg.go.dev/example.com/lib#J) -- @S -- ```go type S struct{ I } @@ -68,4 +68,4 @@ func (lib.J) C() func (s *lib.S) PA() ``` -[`lib.S` on pkg.go.dev](https://pkg.go.dev/testdata/lib#S) +[`lib.S` on pkg.go.dev](https://pkg.go.dev/example.com/lib#S) diff --git a/gopls/internal/test/marker/testdata/rename/doclink.txt b/gopls/internal/test/marker/testdata/rename/doclink.txt index 1461f6f13b3..d4e9f96891e 100644 --- a/gopls/internal/test/marker/testdata/rename/doclink.txt +++ b/gopls/internal/test/marker/testdata/rename/doclink.txt @@ -1,7 +1,7 @@ This test checks that doc links are also handled correctly (golang/go#64495). -- go.mod -- -module testdata +module example.com go 1.21 @@ -35,7 +35,7 @@ func (E[T]) Foo() {} //@rename("Foo", "Bar", EFooToEBar) -- b/b.go -- package b -import aa "testdata/a" //@rename("aa", "a", pkgRename) +import aa "example.com/a" //@rename("aa", "a", pkgRename) // FooBar just for test [aa.Foo] [aa.A] [aa.B] [aa.C] [aa.C.F] [aa.C.PF] // reference pointer type [*aa.D] @@ -124,8 +124,8 @@ func FooBar() { +// FooBar just for test [aa.Foo] [aa.A] [aa.B] [aa.C] [aa.C.F] [aa.C.PFF] -- @pkgRename/b/b.go -- @@ -3 +3 @@ --import aa "testdata/a" //@rename("aa", "a", pkgRename) -+import "testdata/a" //@rename("aa", "a", pkgRename) +-import aa "example.com/a" //@rename("aa", "a", pkgRename) ++import "example.com/a" //@rename("aa", "a", pkgRename) @@ -5,3 +5,3 @@ -// FooBar just for test [aa.Foo] [aa.A] [aa.B] [aa.C] [aa.C.F] [aa.C.PF] -// reference pointer type [*aa.D] diff --git a/gopls/internal/test/marker/testdata/signature/issue63804.txt b/gopls/internal/test/marker/testdata/signature/issue63804.txt index 4ed952956f9..b65183391ef 100644 --- a/gopls/internal/test/marker/testdata/signature/issue63804.txt +++ b/gopls/internal/test/marker/testdata/signature/issue63804.txt @@ -4,7 +4,7 @@ the server's Signature method never returns an actual error, so the best we can assert is that there is no result. -- go.mod -- -module testdata +module example.com go 1.18 -- a/a.go -- diff --git a/gopls/internal/test/marker/testdata/suggestedfix/issue65024.txt b/gopls/internal/test/marker/testdata/suggestedfix/issue65024.txt index d3cdb4553ef..afdfce9f1cc 100644 --- a/gopls/internal/test/marker/testdata/suggestedfix/issue65024.txt +++ b/gopls/internal/test/marker/testdata/suggestedfix/issue65024.txt @@ -1,11 +1,11 @@ -Regression test for #65024, "incorrect package qualification when +Regression example.com for #65024, "incorrect package qualification when stubbing method in v2 module". The second test (a-a) ensures that we don't use path-based heuristics to guess the PkgName of an import. -- a/v2/go.mod -- -module testdata/a/v2 +module example.com/a/v2 go 1.18 -- a/v2/a.go -- @@ -18,7 +18,7 @@ type T struct {} -- a/v2/b/b.go -- package b -import "testdata/a/v2" +import "example.com/a/v2" type B struct{} @@ -40,7 +40,7 @@ var _ a.I = &B{} //@ suggestedfix("&B{}", re"does not implement", out) // It won't do to use it as an identifier. // The correct name is the one in the package decl, // which in this case is not what the path heuristic would guess. -module testdata/a-a/v2 +module example.com/a-a/v2 go 1.18 -- a-a/v2/a.go -- @@ -61,12 +61,12 @@ var _ I = &B{} //@ suggestedfix("&B{}", re"does not implement", out2) -- a-a/v2/b/import-a-I.go -- package b -import "testdata/a-a/v2" +import "example.com/a-a/v2" type I = a.I -- @out2/a-a/v2/b/b.go -- @@ -3 +3,2 @@ -+import a "testdata/a-a/v2" ++import a "example.com/a-a/v2" + @@ -7 +9,5 @@ +// F implements a.I.