Skip to content

Commit

Permalink
gopls/internal/regtest: eliminate DiagnosticsAtRegexpWithMessage
Browse files Browse the repository at this point in the history
Replace with assertions on filtered diagnostics.

Updates golang/go#39384

Change-Id: If88e29a8241008dd778fbfabe37d840b48f17691
Reviewed-on: https://go-review.googlesource.com/c/tools/+/461938
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
  • Loading branch information
findleyr committed Jan 13, 2023
1 parent 5d65394 commit bd48b9a
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 59 deletions.
14 changes: 3 additions & 11 deletions gopls/internal/lsp/regtest/expectation.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,9 +829,9 @@ func AtPosition(name string, line, character uint32) DiagnosticFilter {
}
}

// WithMessageContaining filters to diagnostics whose message contains the
// given substring.
func WithMessageContaining(substring string) DiagnosticFilter {
// WithMessage filters to diagnostics whose message contains the given
// substring.
func WithMessage(substring string) DiagnosticFilter {
return DiagnosticFilter{
desc: fmt.Sprintf("with message containing %q", substring),
check: func(_ string, d protocol.Diagnostic) bool {
Expand All @@ -850,11 +850,3 @@ func (e *Env) DiagnosticAtRegexp(name, re string) DiagnosticExpectation {
pos := e.RegexpSearch(name, re)
return DiagnosticExpectation{path: name, pos: &pos, re: re, present: true}
}

// DiagnosticAtRegexpWithMessage is like DiagnosticAtRegexp, but it also
// checks for the content of the diagnostic message,
func (e *Env) DiagnosticAtRegexpWithMessage(name, re, msg string) DiagnosticExpectation {
e.T.Helper()
pos := e.RegexpSearch(name, re)
return DiagnosticExpectation{path: name, pos: &pos, re: re, present: true, message: msg}
}
8 changes: 4 additions & 4 deletions gopls/internal/regtest/codelens/codelens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ require golang.org/x/hello v1.2.3
env.ExecuteCodeLensCommand("a/go.mod", command.CheckUpgrades, nil)
d := &protocol.PublishDiagnosticsParams{}
env.OnceMet(
env.DiagnosticAtRegexpWithMessage("a/go.mod", `require`, "can be upgraded"),
Diagnostics(env.AtRegexp("a/go.mod", `require`), WithMessage("can be upgraded")),
ReadDiagnostics("a/go.mod", d),
// We do not want there to be a diagnostic for b/go.mod,
// but there may be some subtlety in timing here, where this
Expand All @@ -219,7 +219,7 @@ require golang.org/x/hello v1.2.3
)
// Check for upgrades in b/go.mod and then clear them.
env.ExecuteCodeLensCommand("b/go.mod", command.CheckUpgrades, nil)
env.Await(env.DiagnosticAtRegexpWithMessage("b/go.mod", `require`, "can be upgraded"))
env.Await(Diagnostics(env.AtRegexp("b/go.mod", `require`), WithMessage("can be upgraded")))
env.ExecuteCodeLensCommand("b/go.mod", command.ResetGoModDiagnostics, nil)
env.Await(NoDiagnostics(ForFile("b/go.mod")))

Expand Down Expand Up @@ -319,14 +319,14 @@ func Foo() {
// Open the file. We have a nonexistant symbol that will break cgo processing.
env.OpenFile("cgo.go")
env.AfterChange(
env.DiagnosticAtRegexpWithMessage("cgo.go", ``, "go list failed to return CompiledGoFiles"),
Diagnostics(env.AtRegexp("cgo.go", ``), WithMessage("go list failed to return CompiledGoFiles")),
)

// Fix the C function name. We haven't regenerated cgo, so nothing should be fixed.
env.RegexpReplace("cgo.go", `int fortythree`, "int fortytwo")
env.SaveBuffer("cgo.go")
env.AfterChange(
env.DiagnosticAtRegexpWithMessage("cgo.go", ``, "go list failed to return CompiledGoFiles"),
Diagnostics(env.AtRegexp("cgo.go", ``), WithMessage("go list failed to return CompiledGoFiles")),
)

// Regenerate cgo, fixing the diagnostic.
Expand Down
47 changes: 25 additions & 22 deletions gopls/internal/regtest/diagnostics/diagnostics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,8 @@ func main() {
).Run(t, collision, func(t *testing.T, env *Env) {
env.OpenFile("x/x.go")
env.AfterChange(
env.DiagnosticAtRegexpWithMessage("x/x.go", `^`, "found packages main (main.go) and x (x.go)"),
env.DiagnosticAtRegexpWithMessage("x/main.go", `^`, "found packages main (main.go) and x (x.go)"),
Diagnostics(env.AtRegexp("x/x.go", `^`), WithMessage("found packages main (main.go) and x (x.go)")),
Diagnostics(env.AtRegexp("x/main.go", `^`), WithMessage("found packages main (main.go) and x (x.go)")),
)

// We don't recover cleanly from the errors without good overlay support.
Expand Down Expand Up @@ -662,7 +662,7 @@ func main() {
// Expect a diagnostic and fix to remove the dependency in the go.mod.
env.AfterChange(
NoDiagnostics(ForFile("main.go")),
env.DiagnosticAtRegexpWithMessage("go.mod", "require github.com/ardanlabs/conf", "not used in this module"),
Diagnostics(env.AtRegexp("go.mod", "require github.com/ardanlabs/conf"), WithMessage("not used in this module")),
ReadDiagnostics("go.mod", &d),
)
env.ApplyQuickFixes("go.mod", d.Diagnostics)
Expand Down Expand Up @@ -702,7 +702,7 @@ func main() {
env.SaveBuffer("main.go")
var d protocol.PublishDiagnosticsParams
env.AfterChange(
env.DiagnosticAtRegexpWithMessage("main.go", `"github.com/ardanlabs/conf"`, "no required module"),
Diagnostics(env.AtRegexp("main.go", `"github.com/ardanlabs/conf"`), WithMessage("no required module")),
ReadDiagnostics("main.go", &d),
)
env.ApplyQuickFixes("main.go", d.Diagnostics)
Expand Down Expand Up @@ -1136,7 +1136,7 @@ func main() {}
Run(t, pkgDefault, func(t *testing.T, env *Env) {
env.OpenFile("main.go")
env.Await(
env.DiagnosticAtRegexpWithMessage("main.go", "default", "expected 'IDENT'"),
Diagnostics(env.AtRegexp("main.go", "default"), WithMessage("expected 'IDENT'")),
)
})
}
Expand Down Expand Up @@ -1205,7 +1205,7 @@ func main() {
env.OpenFile("main.go")
var d protocol.PublishDiagnosticsParams
env.AfterChange(
env.DiagnosticAtRegexpWithMessage("main.go", `t{"msg"}`, "redundant type"),
Diagnostics(env.AtRegexp("main.go", `t{"msg"}`), WithMessage("redundant type")),
ReadDiagnostics("main.go", &d),
)
if tags := d.Diagnostics[0].Tags; len(tags) == 0 || tags[0] != protocol.Unnecessary {
Expand Down Expand Up @@ -1369,7 +1369,7 @@ func main() {
Run(t, mod, func(t *testing.T, env *Env) {
env.OnceMet(
InitialWorkspaceLoad,
NoDiagnostics(WithMessageContaining("illegal character U+0023 '#'")),
NoDiagnostics(WithMessage("illegal character U+0023 '#'")),
)
})
}
Expand Down Expand Up @@ -1539,9 +1539,9 @@ import _ "mod.com/triple/a"
`
Run(t, mod, func(t *testing.T, env *Env) {
env.Await(
env.DiagnosticAtRegexpWithMessage("self/self.go", `_ "mod.com/self"`, "import cycle not allowed"),
env.DiagnosticAtRegexpWithMessage("double/a/a.go", `_ "mod.com/double/b"`, "import cycle not allowed"),
env.DiagnosticAtRegexpWithMessage("triple/a/a.go", `_ "mod.com/triple/b"`, "import cycle not allowed"),
Diagnostics(env.AtRegexp("self/self.go", `_ "mod.com/self"`), WithMessage("import cycle not allowed")),
Diagnostics(env.AtRegexp("double/a/a.go", `_ "mod.com/double/b"`), WithMessage("import cycle not allowed")),
Diagnostics(env.AtRegexp("triple/a/a.go", `_ "mod.com/triple/b"`), WithMessage("import cycle not allowed")),
)
})
}
Expand Down Expand Up @@ -1578,8 +1578,8 @@ const B = a.B
//
// TODO(golang/go#52904): we should get *both* of these errors.
AnyOf(
env.DiagnosticAtRegexpWithMessage("a/a.go", `"mod.test/b"`, "import cycle"),
env.DiagnosticAtRegexpWithMessage("b/b.go", `"mod.test/a"`, "import cycle"),
Diagnostics(env.AtRegexp("a/a.go", `"mod.test/b"`), WithMessage("import cycle")),
Diagnostics(env.AtRegexp("b/b.go", `"mod.test/a"`), WithMessage("import cycle")),
),
)
env.RegexpReplace("b/b.go", `const B = a\.B`, "")
Expand Down Expand Up @@ -1607,7 +1607,7 @@ import (
t.Run("module", func(t *testing.T) {
Run(t, mod, func(t *testing.T, env *Env) {
env.Await(
env.DiagnosticAtRegexpWithMessage("main.go", `"nosuchpkg"`, `could not import nosuchpkg (no required module provides package "nosuchpkg"`),
Diagnostics(env.AtRegexp("main.go", `"nosuchpkg"`), WithMessage(`could not import nosuchpkg (no required module provides package "nosuchpkg"`)),
)
})
})
Expand All @@ -1618,7 +1618,7 @@ import (
Modes(Default),
).Run(t, mod, func(t *testing.T, env *Env) {
env.Await(
env.DiagnosticAtRegexpWithMessage("main.go", `"nosuchpkg"`, `cannot find package "nosuchpkg" in any of`),
Diagnostics(env.AtRegexp("main.go", `"nosuchpkg"`), WithMessage(`cannot find package "nosuchpkg" in any of`)),
)
})
})
Expand Down Expand Up @@ -1676,7 +1676,7 @@ func helloHelper() {}
env.OpenFile("nested/hello/hello.go")
env.AfterChange(
env.DiagnosticAtRegexp("nested/hello/hello.go", "helloHelper"),
env.DiagnosticAtRegexpWithMessage("nested/hello/hello.go", "package hello", "nested module"),
Diagnostics(env.AtRegexp("nested/hello/hello.go", "package hello"), WithMessage("nested module")),
OutstandingWork(lsp.WorkspaceLoadFailure, "nested module"),
)
})
Expand Down Expand Up @@ -1753,7 +1753,7 @@ package main
env.SetBufferContent("other.go", "package main\n\nasdf")
// The new diagnostic in other.go should not suppress diagnostics in main.go.
env.AfterChange(
env.DiagnosticAtRegexpWithMessage("other.go", "asdf", "expected declaration"),
Diagnostics(env.AtRegexp("other.go", "asdf"), WithMessage("expected declaration")),
env.DiagnosticAtRegexp("main.go", "asdf"),
)
})
Expand Down Expand Up @@ -1815,7 +1815,7 @@ const C = 0b10
Run(t, files, func(t *testing.T, env *Env) {
env.OnceMet(
InitialWorkspaceLoad,
env.DiagnosticAtRegexpWithMessage("main.go", `0b10`, "go1.13 or later"),
Diagnostics(env.AtRegexp("main.go", `0b10`), WithMessage("go1.13 or later")),
)
env.WriteWorkspaceFile("go.mod", "module mod.com \n\ngo 1.13\n")
env.AfterChange(
Expand Down Expand Up @@ -1868,7 +1868,7 @@ func F[T any](_ T) {
var d protocol.PublishDiagnosticsParams
env.OnceMet(
InitialWorkspaceLoad,
env.DiagnosticAtRegexpWithMessage("main.go", `T any`, "type parameter"),
Diagnostics(env.AtRegexp("main.go", `T any`), WithMessage("type parameter")),
ReadDiagnostics("main.go", &d),
)

Expand Down Expand Up @@ -1902,7 +1902,7 @@ func F[T any](_ T) {
// We should have a diagnostic because generics are not supported at 1.16.
env.OnceMet(
InitialWorkspaceLoad,
env.DiagnosticAtRegexpWithMessage("main.go", `T any`, "type parameter"),
Diagnostics(env.AtRegexp("main.go", `T any`), WithMessage("type parameter")),
ReadDiagnostics("main.go", &d),
)

Expand Down Expand Up @@ -1955,8 +1955,11 @@ func MyPrintf(format string, args ...interface{}) {
Run(t, src, func(t *testing.T, env *Env) {
env.OpenFile("a/a.go")
env.AfterChange(
env.DiagnosticAtRegexpWithMessage("a/a.go", "new.*Printf",
"format %d has arg \"s\" of wrong type string"))
Diagnostics(
env.AtRegexp("a/a.go", "new.*Printf"),
WithMessage("format %d has arg \"s\" of wrong type string"),
),
)
})
}

Expand Down Expand Up @@ -2005,7 +2008,7 @@ var _ = 1 / "" // type error
var diags protocol.PublishDiagnosticsParams
env.OpenFile("a/a.go")
env.AfterChange(
env.DiagnosticAtRegexpWithMessage("a/a.go", "mu2 := (mu)", "assignment copies lock value"),
Diagnostics(env.AtRegexp("a/a.go", "mu2 := (mu)"), WithMessage("assignment copies lock value")),
ReadDiagnostics("a/a.go", &diags))

// Assert that there were no other diagnostics.
Expand Down
7 changes: 6 additions & 1 deletion gopls/internal/regtest/misc/embed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ var foo string
`
Run(t, files, func(t *testing.T, env *Env) {
env.OpenFile("x.go")
env.AfterChange(env.DiagnosticAtRegexpWithMessage("x.go", `NONEXISTENT`, "no matching files found"))
env.AfterChange(
Diagnostics(
env.AtRegexp("x.go", `NONEXISTENT`),
WithMessage("no matching files found"),
),
)
env.RegexpReplace("x.go", `NONEXISTENT`, "x.go")
env.AfterChange(NoDiagnostics(ForFile("x.go")))
})
Expand Down
4 changes: 2 additions & 2 deletions gopls/internal/regtest/misc/failures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ const a = 2
Run(t, badPackageDup, func(t *testing.T, env *Env) {
env.OpenFile("b.go")
env.Await(
env.DiagnosticAtRegexpWithMessage("b.go", `a = 2`, "a redeclared"),
env.DiagnosticAtRegexpWithMessage("a.go", `a = 1`, "other declaration"),
Diagnostics(env.AtRegexp("b.go", `a = 2`), WithMessage("a redeclared")),
Diagnostics(env.AtRegexp("a.go", `a = 1`), WithMessage("other declaration")),
)

// Fix the error by editing the const name in b.go to `b`.
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/regtest/misc/fix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func Foo() error {
var d protocol.PublishDiagnosticsParams
env.AfterChange(
// The error message here changed in 1.18; "return values" covers both forms.
env.DiagnosticAtRegexpWithMessage("main.go", `return`, "return values"),
Diagnostics(env.AtRegexp("main.go", `return`), WithMessage("return values")),
ReadDiagnostics("main.go", &d),
)
codeActions := env.CodeAction("main.go", d.Diagnostics)
Expand Down
4 changes: 2 additions & 2 deletions gopls/internal/regtest/misc/vendor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ func _() {
d := &protocol.PublishDiagnosticsParams{}
env.OnceMet(
InitialWorkspaceLoad,
env.DiagnosticAtRegexpWithMessage("go.mod", "module mod.com", "Inconsistent vendoring"),
Diagnostics(env.AtRegexp("go.mod", "module mod.com"), WithMessage("Inconsistent vendoring")),
ReadDiagnostics("go.mod", d),
)
env.ApplyQuickFixes("go.mod", d.Diagnostics)

env.AfterChange(
env.DiagnosticAtRegexpWithMessage("a/a1.go", `q int`, "not used"),
Diagnostics(env.AtRegexp("a/a1.go", `q int`), WithMessage("not used")),
)
})
}
19 changes: 11 additions & 8 deletions gopls/internal/regtest/modfile/modfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,8 @@ var _ = blah.Name
env.AfterChange(
// We would like for the error to appear in the v2 module, but
// as of writing non-workspace packages are not diagnosed.
env.DiagnosticAtRegexpWithMessage("a/main.go", `"example.com/blah/v2"`, "cannot find module providing"),
env.DiagnosticAtRegexpWithMessage("a/go.mod", `require example.com/blah/v2`, "cannot find module providing"),
Diagnostics(env.AtRegexp("a/main.go", `"example.com/blah/v2"`), WithMessage("cannot find module providing")),
Diagnostics(env.AtRegexp("a/go.mod", `require example.com/blah/v2`), WithMessage("cannot find module providing")),
ReadDiagnostics("a/go.mod", &modDiags),
)
env.ApplyQuickFixes("a/go.mod", modDiags.Diagnostics)
Expand Down Expand Up @@ -604,7 +604,7 @@ func main() {
d := protocol.PublishDiagnosticsParams{}
env.AfterChange(
// Make sure the diagnostic mentions the new version -- the old diagnostic is in the same place.
env.DiagnosticAtRegexpWithMessage("a/go.mod", "example.com v1.2.3", "example.com@v1.2.3"),
Diagnostics(env.AtRegexp("a/go.mod", "example.com v1.2.3"), WithMessage("example.com@v1.2.3")),
ReadDiagnostics("a/go.mod", &d),
)
qfs := env.GetQuickFixes("a/go.mod", d.Diagnostics)
Expand Down Expand Up @@ -793,7 +793,7 @@ func main() {
ProxyFiles(workspaceProxy),
).Run(t, mod, func(t *testing.T, env *Env) {
env.Await(
env.DiagnosticAtRegexpWithMessage("a/go.mod", "example.com v1.2.3", "is not used"),
Diagnostics(env.AtRegexp("a/go.mod", "example.com v1.2.3"), WithMessage("is not used")),
)
})
}
Expand Down Expand Up @@ -875,7 +875,7 @@ func main() {
env.AfterChange(
Diagnostics(
env.AtRegexp("go.mod", `example.com v1.2.3`),
WithMessageContaining("go.sum is out of sync"),
WithMessage("go.sum is out of sync"),
),
ReadDiagnostics("go.mod", d),
)
Expand Down Expand Up @@ -1078,7 +1078,7 @@ func main() {
env.AfterChange(
Diagnostics(
env.AtRegexp("go.mod", `example.com`),
WithMessageContaining("go.sum is out of sync"),
WithMessage("go.sum is out of sync"),
),
ReadDiagnostics("go.mod", params),
)
Expand Down Expand Up @@ -1144,7 +1144,10 @@ func main() {
env.OpenFile("main.go")
d := &protocol.PublishDiagnosticsParams{}
env.Await(
env.DiagnosticAtRegexpWithMessage("main.go", `"example.com/blah"`, `could not import example.com/blah (no required module provides package "example.com/blah")`),
Diagnostics(
env.AtRegexp("main.go", `"example.com/blah"`),
WithMessage(`could not import example.com/blah (no required module provides package "example.com/blah")`),
),
ReadDiagnostics("main.go", d),
)
env.ApplyQuickFixes("main.go", d.Diagnostics)
Expand All @@ -1167,7 +1170,7 @@ package main
Run(t, files, func(t *testing.T, env *Env) {
env.OnceMet(
InitialWorkspaceLoad,
env.DiagnosticAtRegexpWithMessage("go.mod", `go foo`, "invalid go version"),
Diagnostics(env.AtRegexp("go.mod", `go foo`), WithMessage("invalid go version")),
)
env.WriteWorkspaceFile("go.mod", "module mod.com \n\ngo 1.12\n")
env.AfterChange(NoDiagnostics(ForFile("go.mod")))
Expand Down
2 changes: 1 addition & 1 deletion gopls/internal/regtest/workspace/fromenv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ use (
// included in the workspace.
env.OpenFile("work/a/a.go")
env.AfterChange(
env.DiagnosticAtRegexpWithMessage("work/b/b.go", "x := 1", "not used"),
Diagnostics(env.AtRegexp("work/b/b.go", "x := 1"), WithMessage("not used")),
)
})
}
2 changes: 1 addition & 1 deletion gopls/internal/regtest/workspace/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func main() {}
// packages for bar.go
env.RegexpReplace("bar.go", "ignore", "excluded")
env.AfterChange(
env.DiagnosticAtRegexpWithMessage("bar.go", "package (main)", "No packages"),
Diagnostics(env.AtRegexp("bar.go", "package (main)"), WithMessage("No packages")),
)
})
}
Expand Down
Loading

0 comments on commit bd48b9a

Please sign in to comment.