forked from golang/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an implementation of name= for go list. It will be used to implement goimports and godoc-like lookups by package name. Imported a copy of the semver package from the stdlib to do version comparison, and tweaked the gopathwalk API to include a hint about what kind of source directory is being traversed. Note that the tests, despite my best efforts, are not hermetic: go list insists on doing version lookups in situations where it seems to me like it shouldn't need to. I think this implementation is ready for serious use. The one thing I'm nervous about is that it currently does a substring match when looking for a package name, so if you look up a package named "a" you will get a huge number of results. This matches goimports' behavior but I don't know if it's suitable for general use. Change-Id: I2b7f823b74571fe30d3bd9c7dfafb4e6a40df5d3 Reviewed-on: https://go-review.googlesource.com/c/138878 Run-TryBot: Heschi Kreinick <[email protected]> Reviewed-by: Michael Matloob <[email protected]>
- Loading branch information
Showing
43 changed files
with
1,054 additions
and
68 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1107,6 +1107,106 @@ func TestContains_FallbackSticks(t *testing.T) { | |
} | ||
} | ||
|
||
func TestName(t *testing.T) { | ||
tmp, cleanup := makeTree(t, map[string]string{ | ||
"src/a/needle/needle.go": `package needle; import "c"`, | ||
"src/b/needle/needle.go": `package needle;`, | ||
"src/c/c.go": `package c;`, | ||
"src/irrelevant/irrelevant.go": `package irrelevant;`, | ||
}) | ||
defer cleanup() | ||
|
||
cfg := &packages.Config{ | ||
Mode: packages.LoadImports, | ||
Dir: tmp, | ||
Env: append(os.Environ(), "GOPATH="+tmp, "GO111MODULE=off"), | ||
} | ||
initial, err := packages.Load(cfg, "name=needle") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
graph, _ := importGraph(initial) | ||
wantGraph := ` | ||
* a/needle | ||
* b/needle | ||
c | ||
a/needle -> c | ||
`[1:] | ||
if graph != wantGraph { | ||
t.Errorf("wrong import graph: got <<%s>>, want <<%s>>", graph, wantGraph) | ||
} | ||
} | ||
|
||
func TestName_Modules(t *testing.T) { | ||
tmp, cleanup := makeTree(t, map[string]string{ | ||
"src/localmod/go.mod": `module test`, | ||
"src/localmod/pkg/pkg.go": `package pkg;`, | ||
}) | ||
defer cleanup() | ||
|
||
wd, err := os.Getwd() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
// testdata/TestNamed_Modules contains: | ||
// - pkg/mod/github.com/heschik/[email protected]/pkg | ||
// - pkg/mod/github.com/heschik/tools-testrepo/[email protected]/pkg | ||
// - src/b/pkg | ||
cfg := &packages.Config{ | ||
Mode: packages.LoadImports, | ||
Dir: filepath.Join(tmp, "src/localmod"), | ||
Env: append(os.Environ(), "GOPATH="+wd+"/testdata/TestName_Modules", "GO111MODULE=on"), | ||
} | ||
|
||
initial, err := packages.Load(cfg, "name=pkg") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
graph, _ := importGraph(initial) | ||
wantGraph := ` | ||
* github.com/heschik/tools-testrepo/pkg | ||
* github.com/heschik/tools-testrepo/v2/pkg | ||
* test/pkg | ||
`[1:] | ||
if graph != wantGraph { | ||
t.Errorf("wrong import graph: got <<%s>>, want <<%s>>", graph, wantGraph) | ||
} | ||
} | ||
|
||
func TestName_ModulesDedup(t *testing.T) { | ||
tmp, cleanup := makeTree(t, map[string]string{ | ||
"src/localmod/go.mod": `module test`, | ||
}) | ||
defer cleanup() | ||
|
||
wd, err := os.Getwd() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
// testdata/TestNamed_ModulesDedup contains: | ||
// - pkg/mod/github.com/heschik/tools-testrepo/[email protected]/pkg/pkg.go | ||
// - pkg/mod/github.com/heschik/tools-testrepo/[email protected]/pkg/pkg.go | ||
// - pkg/mod/github.com/heschik/[email protected]/pkg/pkg.go | ||
// but, inexplicably, not v2.0.0. Nobody knows why. | ||
cfg := &packages.Config{ | ||
Mode: packages.LoadImports, | ||
Dir: filepath.Join(tmp, "src/localmod"), | ||
Env: append(os.Environ(), "GOPATH="+wd+"/testdata/TestName_ModulesDedup", "GO111MODULE=on"), | ||
} | ||
initial, err := packages.Load(cfg, "name=pkg") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
for _, pkg := range initial { | ||
if strings.Contains(pkg.PkgPath, "v2") { | ||
if strings.Contains(pkg.GoFiles[0], "v2.0.2") { | ||
return | ||
} | ||
} | ||
} | ||
t.Errorf("didn't find v2.0.2 of pkg in Load results: %v", initial) | ||
} | ||
|
||
func TestJSON(t *testing.T) { | ||
//TODO: add in some errors | ||
tmp, cleanup := makeTree(t, map[string]string{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Test data directories here were created by running go commands with GOPATH set as such: | ||
GOPATH=......./testdata/TestNamed_ModulesDedup go get github.com/heschik/tools-testrepo/[email protected] | ||
and then removing the vcs cache directories, which appear to be unnecessary. |
1 change: 1 addition & 0 deletions
1
...estdata/TestName_Modules/pkg/mod/cache/download/github.com/heschik/tools-testrepo/@v/list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
v1.0.0 |
1 change: 1 addition & 0 deletions
1
.../TestName_Modules/pkg/mod/cache/download/github.com/heschik/tools-testrepo/@v/v1.0.0.info
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"Version":"v1.0.0","Time":"2018-09-28T22:09:08Z"} |
1 change: 1 addition & 0 deletions
1
...a/TestName_Modules/pkg/mod/cache/download/github.com/heschik/tools-testrepo/@v/v1.0.0.mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module github.com/heschik/tools-testrepo |
Binary file added
BIN
+467 Bytes
...a/TestName_Modules/pkg/mod/cache/download/github.com/heschik/tools-testrepo/@v/v1.0.0.zip
Binary file not shown.
1 change: 1 addition & 0 deletions
1
...stName_Modules/pkg/mod/cache/download/github.com/heschik/tools-testrepo/@v/v1.0.0.ziphash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
h1:D2qc+R2eCTCyoT8WAYoExXhPBThJWmlYSfB4coWbfBE= |
1 change: 1 addition & 0 deletions
1
...data/TestName_Modules/pkg/mod/cache/download/github.com/heschik/tools-testrepo/v2/@v/list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
v2.0.0 |
1 change: 1 addition & 0 deletions
1
...stName_Modules/pkg/mod/cache/download/github.com/heschik/tools-testrepo/v2/@v/v2.0.0.info
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"Version":"v2.0.0","Time":"2018-09-28T22:12:08Z"} |
1 change: 1 addition & 0 deletions
1
...estName_Modules/pkg/mod/cache/download/github.com/heschik/tools-testrepo/v2/@v/v2.0.0.mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module github.com/heschik/tools-testrepo/v2 |
Binary file added
BIN
+482 Bytes
...estName_Modules/pkg/mod/cache/download/github.com/heschik/tools-testrepo/v2/@v/v2.0.0.zip
Binary file not shown.
1 change: 1 addition & 0 deletions
1
...ame_Modules/pkg/mod/cache/download/github.com/heschik/tools-testrepo/v2/@v/v2.0.0.ziphash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
h1:Ll4Bx8ZD8zg8lD4idX7CAhx/jh16o9dWC2m9SnT1qu0= |
1 change: 1 addition & 0 deletions
1
...ages/testdata/TestName_Modules/pkg/mod/github.com/heschik/tools-testrepo/[email protected]/go.mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module github.com/heschik/tools-testrepo/v2 |
1 change: 1 addition & 0 deletions
1
.../testdata/TestName_Modules/pkg/mod/github.com/heschik/tools-testrepo/[email protected]/pkg/pkg.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package pkg |
1 change: 1 addition & 0 deletions
1
...ackages/testdata/TestName_Modules/pkg/mod/github.com/heschik/[email protected]/go.mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module github.com/heschik/tools-testrepo |
1 change: 1 addition & 0 deletions
1
...ges/testdata/TestName_Modules/pkg/mod/github.com/heschik/[email protected]/pkg/pkg.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package pkg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package pkg |
1 change: 1 addition & 0 deletions
1
...ta/TestName_ModulesDedup/pkg/mod/cache/download/github.com/heschik/tools-testrepo/@v/list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
v1.0.0 |
1 change: 1 addition & 0 deletions
1
...Name_ModulesDedup/pkg/mod/cache/download/github.com/heschik/tools-testrepo/@v/v1.0.0.info
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"Version":"v1.0.0","Time":"2018-09-28T22:09:08Z"} |
1 change: 1 addition & 0 deletions
1
...tName_ModulesDedup/pkg/mod/cache/download/github.com/heschik/tools-testrepo/@v/v1.0.0.mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module github.com/heschik/tools-testrepo |
Binary file added
BIN
+467 Bytes
...tName_ModulesDedup/pkg/mod/cache/download/github.com/heschik/tools-testrepo/@v/v1.0.0.zip
Binary file not shown.
1 change: 1 addition & 0 deletions
1
...e_ModulesDedup/pkg/mod/cache/download/github.com/heschik/tools-testrepo/@v/v1.0.0.ziphash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
h1:D2qc+R2eCTCyoT8WAYoExXhPBThJWmlYSfB4coWbfBE= |
2 changes: 2 additions & 0 deletions
2
...TestName_ModulesDedup/pkg/mod/cache/download/github.com/heschik/tools-testrepo/v2/@v/list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
v2.0.1 | ||
v2.0.2 |
1 change: 1 addition & 0 deletions
1
...e_ModulesDedup/pkg/mod/cache/download/github.com/heschik/tools-testrepo/v2/@v/v2.0.1.info
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"Version":"v2.0.1","Time":"2018-09-28T22:12:08Z"} |
1 change: 1 addition & 0 deletions
1
...me_ModulesDedup/pkg/mod/cache/download/github.com/heschik/tools-testrepo/v2/@v/v2.0.1.mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module github.com/heschik/tools-testrepo/v2 |
Binary file added
BIN
+482 Bytes
...me_ModulesDedup/pkg/mod/cache/download/github.com/heschik/tools-testrepo/v2/@v/v2.0.1.zip
Binary file not shown.
1 change: 1 addition & 0 deletions
1
...odulesDedup/pkg/mod/cache/download/github.com/heschik/tools-testrepo/v2/@v/v2.0.1.ziphash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
h1:efPBVdJ45IMcA/KXBOWyOZLo1TETKCXvzrZgfY+gqZk= |
1 change: 1 addition & 0 deletions
1
...e_ModulesDedup/pkg/mod/cache/download/github.com/heschik/tools-testrepo/v2/@v/v2.0.2.info
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"Version":"v2.0.2","Time":"2018-09-28T22:12:08Z"} |
1 change: 1 addition & 0 deletions
1
...me_ModulesDedup/pkg/mod/cache/download/github.com/heschik/tools-testrepo/v2/@v/v2.0.2.mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module github.com/heschik/tools-testrepo/v2 |
Binary file added
BIN
+482 Bytes
...me_ModulesDedup/pkg/mod/cache/download/github.com/heschik/tools-testrepo/v2/@v/v2.0.2.zip
Binary file not shown.
1 change: 1 addition & 0 deletions
1
...odulesDedup/pkg/mod/cache/download/github.com/heschik/tools-testrepo/v2/@v/v2.0.2.ziphash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
h1:vUnR/JOkfEQt/wvMqbT9G2gODHVgVD1saTJ8x2ngAck= |
1 change: 1 addition & 0 deletions
1
...testdata/TestName_ModulesDedup/pkg/mod/github.com/heschik/tools-testrepo/[email protected]/go.mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module github.com/heschik/tools-testrepo/v2 |
1 change: 1 addition & 0 deletions
1
...data/TestName_ModulesDedup/pkg/mod/github.com/heschik/tools-testrepo/[email protected]/pkg/pkg.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package pkg |
1 change: 1 addition & 0 deletions
1
...testdata/TestName_ModulesDedup/pkg/mod/github.com/heschik/tools-testrepo/[email protected]/go.mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module github.com/heschik/tools-testrepo/v2 |
1 change: 1 addition & 0 deletions
1
...data/TestName_ModulesDedup/pkg/mod/github.com/heschik/tools-testrepo/[email protected]/pkg/pkg.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package pkg |
1 change: 1 addition & 0 deletions
1
...es/testdata/TestName_ModulesDedup/pkg/mod/github.com/heschik/[email protected]/go.mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module github.com/heschik/tools-testrepo |
1 change: 1 addition & 0 deletions
1
...estdata/TestName_ModulesDedup/pkg/mod/github.com/heschik/[email protected]/pkg/pkg.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package pkg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.