mirror of
https://github.com/golang/go
synced 2024-11-18 08:44:43 -07:00
cmd/goimports, imports: add -local flag
This allows the caller to indicate they want certain import paths to sort into another group after 3rd-party imports when added by goimports. For example, running 'goimports -local example.com/' might produce import ( "database/sql" "io" "strconv" "golang.org/x/net/context" "example.com/foo/bar" "example.com/foo/baz" ) Resolves golang/go#12420 Change-Id: If6d88599f6cca2f102313bce95ba6ac46ffec1fe Reviewed-on: https://go-review.googlesource.com/25145 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
f3284303b6
commit
ed69e84b15
@ -49,6 +49,7 @@ var (
|
||||
|
||||
func init() {
|
||||
flag.BoolVar(&options.AllErrors, "e", false, "report all errors (not just the first 10 on different lines)")
|
||||
flag.StringVar(&imports.LocalPrefix, "local", "", "put imports beginning with this string after 3rd-party packages")
|
||||
}
|
||||
|
||||
func report(err error) {
|
||||
|
@ -32,9 +32,19 @@ var (
|
||||
testMu sync.RWMutex // guards globals reset by tests; used only if inTests
|
||||
)
|
||||
|
||||
// If set, LocalPrefix instructs Process to sort import paths with the given
|
||||
// prefix into another group after 3rd-party packages.
|
||||
var LocalPrefix string
|
||||
|
||||
// importToGroup is a list of functions which map from an import path to
|
||||
// a group number.
|
||||
var importToGroup = []func(importPath string) (num int, ok bool){
|
||||
func(importPath string) (num int, ok bool) {
|
||||
if LocalPrefix != "" && strings.HasPrefix(importPath, LocalPrefix) {
|
||||
return 3, true
|
||||
}
|
||||
return
|
||||
},
|
||||
func(importPath string) (num int, ok bool) {
|
||||
if strings.HasPrefix(importPath, "appengine") {
|
||||
return 2, true
|
||||
|
@ -1257,6 +1257,38 @@ const Y = bar.X
|
||||
})
|
||||
}
|
||||
|
||||
// Tests that the LocalPrefix option causes imports
|
||||
// to be added into a later group (num=3).
|
||||
func TestLocalPrefix(t *testing.T) {
|
||||
defer func(s string) { LocalPrefix = s }(LocalPrefix)
|
||||
LocalPrefix = "foo/"
|
||||
|
||||
testConfig{
|
||||
gopathFiles: map[string]string{
|
||||
"foo/bar/bar.go": "package bar \n const X = 1",
|
||||
},
|
||||
}.test(t, func(t *goimportTest) {
|
||||
buf, err := Process(t.gopath+"/src/test/t.go", []byte("package main \n const Y = bar.X \n const _ = runtime.GOOS"), &Options{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
const want = `package main
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
|
||||
"foo/bar"
|
||||
)
|
||||
|
||||
const Y = bar.X
|
||||
const _ = runtime.GOOS
|
||||
`
|
||||
if string(buf) != want {
|
||||
t.Errorf("Got:\n%s\nWant:\n%s", buf, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Tests that running goimport on files in GOROOT (for people hacking
|
||||
// on Go itself) don't cause the GOPATH to be scanned (which might be
|
||||
// much bigger).
|
||||
|
Loading…
Reference in New Issue
Block a user