1
0
mirror of https://github.com/golang/go synced 2024-10-01 10:28:31 -06:00
go/internal/lsp/cmd/test/suggested_fix.go
Kalman Bekesi 1e24073be8 tools/gopls: add command line support for suggestedfix
This adds support for calling suggestedfix from the gopls command line, e.g.

$ gopls suggestedfix ~/tmp/foo/main.go

Optional arguments are:
-w, which writes the changes back to the original file; and
-d, which prints a unified diff to stdout
With no arguments, the changed files are printed to stdout.

Wasn't sure if the command should be `suggestedfix` or just `fix` or `quickfix`?
Also this applies all changes to a file, does not allow for selective fixes.

Updates golang/go#32875

Change-Id: I8b75f9824be82974f6edb7c03383b4d56116943c
GitHub-Last-Rev: 070fcda33ac3494bfe8f19c2cd78c089c713ed98
GitHub-Pull-Request: golang/tools#174
Reviewed-on: https://go-review.googlesource.com/c/tools/+/202480
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-10-30 23:29:56 +00:00

30 lines
844 B
Go

// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cmdtest
import (
"testing"
"golang.org/x/tools/internal/lsp/cmd"
"golang.org/x/tools/internal/span"
"golang.org/x/tools/internal/tool"
)
func (r *runner) SuggestedFix(t *testing.T, spn span.Span) {
uri := spn.URI()
filename := uri.Filename()
args := []string{"fix", "-a", filename}
app := cmd.New("gopls-test", r.data.Config.Dir, r.data.Exported.Config.Env, r.options)
got := CaptureStdOut(t, func() {
_ = tool.Run(r.ctx, app, args)
})
want := string(r.data.Golden("suggestedfix", filename, func() ([]byte, error) {
return []byte(got), nil
}))
if want != got {
t.Errorf("suggested fixes failed for %s, expected:\n%v\ngot:\n%v", filename, want, got)
}
}