mirror of
https://github.com/golang/go
synced 2024-11-06 04:16:11 -07:00
6d582d504c
In the common case that a file has imports, we're going to diff just the import block. That means that ApplyFixes doesn't need to produce the whole formatted file, which is a huge speedup. We will do more work twice for files with no imports, but those are presumably pretty short. Hat tip to Muir for pointing towards this in https://go-review.googlesource.com/c/tools/+/209579/2/internal/imports/imports.go#87 even if I didn't catch it. Updates golang/go#36001. Change-Id: Ibbeb4d88c6505eac26a36994de514813606c8c79 Reviewed-on: https://go-review.googlesource.com/c/tools/+/210200 Run-TryBot: Heschi Kreinick <heschi@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
83 lines
1.5 KiB
Go
83 lines
1.5 KiB
Go
package imports
|
|
|
|
import (
|
|
"go/build"
|
|
"os"
|
|
"testing"
|
|
|
|
"golang.org/x/tools/internal/testenv"
|
|
)
|
|
|
|
func TestMain(m *testing.M) {
|
|
testenv.ExitIfSmallMachine()
|
|
os.Exit(m.Run())
|
|
}
|
|
|
|
// TestNilOpts tests that process does not crash with nil opts.
|
|
func TestNilOpts(t *testing.T) {
|
|
var testOpts = []struct {
|
|
name string
|
|
opt *Options
|
|
}{
|
|
{
|
|
name: "nil",
|
|
opt: nil,
|
|
},
|
|
{
|
|
name: "nil env",
|
|
opt: &Options{Comments: true, TabIndent: true, TabWidth: 8},
|
|
},
|
|
{
|
|
name: "default",
|
|
opt: &Options{
|
|
Env: &ProcessEnv{
|
|
GOPATH: build.Default.GOPATH,
|
|
GOROOT: build.Default.GOROOT,
|
|
},
|
|
Comments: true,
|
|
TabIndent: true,
|
|
TabWidth: 8,
|
|
},
|
|
},
|
|
}
|
|
|
|
input := `package p
|
|
|
|
func _() {
|
|
fmt.Println()
|
|
}
|
|
`
|
|
want := `package p
|
|
|
|
import "fmt"
|
|
|
|
func _() {
|
|
fmt.Println()
|
|
}
|
|
`
|
|
for _, test := range testOpts {
|
|
// Test Process
|
|
got, err := Process("", []byte(input), test.opt)
|
|
if err != nil {
|
|
t.Errorf("%s: %s", test.name, err.Error())
|
|
}
|
|
if string(got) != want {
|
|
t.Errorf("%s: Process: Got:\n%s\nWant:\n%s\n", test.name, string(got), want)
|
|
}
|
|
|
|
// Test FixImports and ApplyFixes
|
|
fixes, err := FixImports("", []byte(input), test.opt)
|
|
if err != nil {
|
|
t.Errorf("%s: %s", test.name, err.Error())
|
|
}
|
|
|
|
got, err = ApplyFixes(fixes, "", []byte(input), test.opt, 0)
|
|
if err != nil {
|
|
t.Errorf("%s: %s", test.name, err.Error())
|
|
}
|
|
if string(got) != want {
|
|
t.Errorf("%s: ApplyFix: Got:\n%s\nWant:\n%s\n", test.name, string(got), want)
|
|
}
|
|
}
|
|
}
|