Skip to content
This repository has been archived by the owner on Feb 3, 2018. It is now read-only.

Commit

Permalink
Add relative imports tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sdboyer committed Jan 19, 2017
1 parent 7522a85 commit 8281691
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 11 deletions.
9 changes: 9 additions & 0 deletions _testdata/src/relimport/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package relimport

import (
"sort"
)

var (
A = sort.Strings
)
10 changes: 10 additions & 0 deletions _testdata/src/relimport/dot/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dot

import (
"."
"sort"
)

var (
A = sort.Strings
)
9 changes: 9 additions & 0 deletions _testdata/src/relimport/dotdot/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dotdot

import (
relimport ".."
)

var (
A = relimport.A
)
9 changes: 9 additions & 0 deletions _testdata/src/relimport/dotdotslash/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dotslash

import (
"../github.com/sdboyer/gps"
)

var (
A = gps.Solver
)
9 changes: 9 additions & 0 deletions _testdata/src/relimport/dotslash/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dotslash

import (
"./simple"
)

var (
A = simple.A
)
20 changes: 9 additions & 11 deletions analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func init() {
// Stored as a var so that tests can swap it out. Ugh globals, ugh.
var isStdLib = doIsStdLib

// This was loving taken from src/cmd/go/pkg.go in Go's code (isStandardImportPath).
// This was lovingly lifted from src/cmd/go/pkg.go in Go's code
// (isStandardImportPath).
func doIsStdLib(path string) bool {
i := strings.Index(path, "/")
if i < 0 {
Expand Down Expand Up @@ -150,8 +151,8 @@ func ListPackages(fileRoot, importRoot string) (PackageTree, error) {
}

// Compute the import path. Run the result through ToSlash(), so that
// windows paths are normalized to slashes, as import paths are expected
// to be.
// windows file paths are normalized to slashes, as is expected of
// import paths.
ip := filepath.ToSlash(filepath.Join(importRoot, strings.TrimPrefix(path, fileRoot)))

// Find all the imports, across all os/arch combos
Expand Down Expand Up @@ -253,22 +254,18 @@ func ListPackages(fileRoot, importRoot string) (PackageTree, error) {
// Do allow the single-dot, at least for now
case imp == "..":
lim = append(lim, imp)
// ignore stdlib done this way, b/c that's what the go tooling does
case strings.HasPrefix(imp, "./"):
if isStdLib(imp[2:]) {
lim = append(lim, imp)
}
lim = append(lim, imp)
case strings.HasPrefix(imp, "../"):
if isStdLib(imp[3:]) {
lim = append(lim, imp)
}
lim = append(lim, imp)
}
}

if len(lim) > 0 {
ptree.Packages[ip] = PackageOrErr{
Err: &LocalImportsError{
Dir: ip,
Dir: path,
ImportPath: ip,
LocalImports: lim,
},
}
Expand All @@ -293,6 +290,7 @@ func ListPackages(fileRoot, importRoot string) (PackageTree, error) {
//
// TODO(sdboyer) add a Files property once we're doing our own per-file parsing
type LocalImportsError struct {
ImportPath string
Dir string
LocalImports []string
}
Expand Down
57 changes: 57 additions & 0 deletions analysis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,63 @@ func TestListPackages(t *testing.T) {
},
},
},
"relative imports": {
fileRoot: j("relimport"),
importRoot: "relimport",
out: PackageTree{
ImportRoot: "relimport",
Packages: map[string]PackageOrErr{
"relimport": {
P: Package{
ImportPath: "relimport",
CommentPath: "",
Name: "relimport",
Imports: []string{
"sort",
},
},
},
"relimport/dot": {
P: Package{
ImportPath: "relimport/dot",
CommentPath: "",
Name: "dot",
Imports: []string{
".",
"sort",
},
},
},
"relimport/dotdot": {
Err: &LocalImportsError{
Dir: j("relimport/dotdot"),
ImportPath: "relimport/dotdot",
LocalImports: []string{
"..",
},
},
},
"relimport/dotslash": {
Err: &LocalImportsError{
Dir: j("relimport/dotslash"),
ImportPath: "relimport/dotslash",
LocalImports: []string{
"./simple",
},
},
},
"relimport/dotdotslash": {
Err: &LocalImportsError{
Dir: j("relimport/dotdotslash"),
ImportPath: "relimport/dotdotslash",
LocalImports: []string{
"../github.com/sdboyer/gps",
},
},
},
},
},
},
// This case mostly exists for the PackageTree methods, but it does
// cover a bit of range
"varied": {
Expand Down

0 comments on commit 8281691

Please sign in to comment.