Skip to content
This repository was archived by the owner on Sep 9, 2020. It is now read-only.

Commit fe4b78c

Browse files
authored
Merge pull request #606 from jmank88/paths_pkg
Remove duplicate 'isStdLib' function
2 parents a90bfbb + fe7a376 commit fe4b78c

File tree

10 files changed

+34
-69
lines changed

10 files changed

+34
-69
lines changed

cmd/dep/init.go

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
fb "github.com/golang/dep/internal/feedback"
1818
"github.com/golang/dep/internal/fs"
1919
"github.com/golang/dep/internal/gps"
20+
"github.com/golang/dep/internal/gps/paths"
2021
"github.com/golang/dep/internal/gps/pkgtree"
2122
"github.com/pkg/errors"
2223
)
@@ -245,20 +246,6 @@ func contains(a []string, b string) bool {
245246
return false
246247
}
247248

248-
// isStdLib reports whether $GOROOT/src/path should be considered
249-
// part of the standard distribution. For historical reasons we allow people to add
250-
// their own code to $GOROOT instead of using $GOPATH, but we assume that
251-
// code will start with a domain name (dot in the first element).
252-
// This was loving taken from src/cmd/go/pkg.go in Go's code (isStandardImportPath).
253-
func isStdLib(path string) bool {
254-
i := strings.Index(path, "/")
255-
if i < 0 {
256-
i = len(path)
257-
}
258-
elem := path[:i]
259-
return !strings.Contains(elem, ".")
260-
}
261-
262249
// TODO solve failures can be really creative - we need to be similarly creative
263250
// in handling them and informing the user appropriately
264251
func handleAllTheFailuresOfTheWorld(err error) {
@@ -370,7 +357,7 @@ func getProjectData(ctx *dep.Ctx, pkgT pkgtree.PackageTree, cpr string, sm gps.S
370357
return projectData{}, nil
371358
}
372359

373-
for _, ip := range rm.FlattenOmitStdLib() {
360+
for _, ip := range rm.FlattenFn(paths.IsStandardImportPath) {
374361
pr, err := sm.DeduceProjectRoot(ip)
375362
if err != nil {
376363
return projectData{}, errors.Wrap(err, "sm.DeduceProjectRoot") // TODO: Skip and report ?
@@ -506,7 +493,7 @@ func getProjectData(ctx *dep.Ctx, pkgT pkgtree.PackageTree, cpr string, sm gps.S
506493

507494
// recurse
508495
for _, rpkg := range reached.External {
509-
if isStdLib(rpkg) {
496+
if paths.IsStandardImportPath(rpkg) {
510497
continue
511498
}
512499

cmd/dep/init_test.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,6 @@ func TestContains(t *testing.T) {
2424
}
2525
}
2626

27-
func TestIsStdLib(t *testing.T) {
28-
t.Parallel()
29-
30-
tests := map[string]bool{
31-
"github.com/Sirupsen/logrus": false,
32-
"encoding/json": true,
33-
"golang.org/x/net/context": false,
34-
"net/context": true,
35-
".": false,
36-
}
37-
38-
for p, e := range tests {
39-
b := isStdLib(p)
40-
if b != e {
41-
t.Fatalf("%s: expected %t got %t", p, e, b)
42-
}
43-
}
44-
}
45-
4627
func TestGetProjectPropertiesFromVersion(t *testing.T) {
4728
t.Parallel()
4829

cmd/dep/remove.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/golang/dep"
1212
"github.com/golang/dep/internal/gps"
13+
"github.com/golang/dep/internal/gps/paths"
1314
"github.com/golang/dep/internal/gps/pkgtree"
1415
"github.com/pkg/errors"
1516
)
@@ -71,7 +72,7 @@ func (cmd *removeCommand) Run(ctx *dep.Ctx, args []string) error {
7172
return errors.Errorf("remove takes no arguments when running with -unused")
7273
}
7374

74-
reachlist := reachmap.FlattenOmitStdLib()
75+
reachlist := reachmap.FlattenFn(paths.IsStandardImportPath)
7576

7677
// warm the cache in parallel, in case any paths require go get metadata
7778
// discovery
@@ -81,7 +82,7 @@ func (cmd *removeCommand) Run(ctx *dep.Ctx, args []string) error {
8182

8283
otherroots := make(map[gps.ProjectRoot]bool)
8384
for _, im := range reachlist {
84-
if isStdLib(im) {
85+
if paths.IsStandardImportPath(im) {
8586
continue
8687
}
8788
pr, err := sm.DeduceProjectRoot(im)

cmd/dep/status.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/golang/dep"
1717
"github.com/golang/dep/internal/gps"
18+
"github.com/golang/dep/internal/gps/paths"
1819
"github.com/golang/dep/internal/gps/pkgtree"
1920
"github.com/pkg/errors"
2021
)
@@ -164,7 +165,7 @@ func (out *dotOutput) BasicHeader() {
164165
ptree, _ := pkgtree.ListPackages(out.p.AbsRoot, string(out.p.ImportRoot))
165166
prm, _ := ptree.ToReachMap(true, false, false, nil)
166167

167-
out.g.createNode(string(out.p.ImportRoot), "", prm.FlattenOmitStdLib())
168+
out.g.createNode(string(out.p.ImportRoot), "", prm.FlattenFn(paths.IsStandardImportPath))
168169
}
169170

170171
func (out *dotOutput) BasicFooter() {
@@ -301,7 +302,7 @@ func runStatusAll(loggers *dep.Loggers, out outputter, p *dep.Project, sm gps.So
301302
}
302303

303304
prm, _ := ptr.ToReachMap(true, false, false, nil)
304-
bs.Children = prm.FlattenOmitStdLib()
305+
bs.Children = prm.FlattenFn(paths.IsStandardImportPath)
305306
}
306307

307308
// Split apart the version from the lock into its constituent parts
@@ -370,7 +371,7 @@ func runStatusAll(loggers *dep.Loggers, out outputter, p *dep.Project, sm gps.So
370371
// lock.
371372
rm, _ := ptree.ToReachMap(true, true, false, nil)
372373

373-
external := rm.FlattenOmitStdLib()
374+
external := rm.FlattenFn(paths.IsStandardImportPath)
374375
roots := make(map[gps.ProjectRoot][]string, len(external))
375376

376377
type fail struct {

internal/gps/internal/paths/paths.go

Lines changed: 0 additions & 20 deletions
This file was deleted.

internal/gps/paths/paths.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package paths
6+
7+
import "strings"
8+
9+
// IsStandardImportPath reports whether $GOROOT/src/path should be considered
10+
// part of the standard distribution. For historical reasons we allow people to add
11+
// their own code to $GOROOT instead of using $GOPATH, but we assume that
12+
// code will start with a domain name (dot in the first element).
13+
// This was loving taken from src/cmd/go/pkg.go in Go's code (isStandardImportPath).
14+
func IsStandardImportPath(path string) bool {
15+
i := strings.Index(path, "/")
16+
if i < 0 {
17+
i = len(path)
18+
}
19+
20+
return !strings.Contains(path[:i], ".")
21+
}

internal/gps/internal/paths/paths_test.go renamed to internal/gps/paths/paths_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ func TestIsStandardImportPath(t *testing.T) {
1616
{"github.com/anything", false},
1717
{"github.com", false},
1818
{"foo", true},
19+
{".", false},
1920
}
2021

2122
for _, f := range fix {

internal/gps/pkgtree/pkgtree_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"testing"
1919

2020
"github.com/golang/dep/internal/fs"
21-
"github.com/golang/dep/internal/gps/internal/paths"
21+
"github.com/golang/dep/internal/gps/paths"
2222
)
2323

2424
// PackageTree.ToReachMap() uses an easily separable algorithm, wmToReach(),

internal/gps/pkgtree/reachmap.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ package pkgtree
77
import (
88
"sort"
99
"strings"
10-
11-
"github.com/golang/dep/internal/gps/internal/paths"
1210
)
1311

1412
// ReachMap maps a set of import paths (keys) to the sets of transitively
@@ -20,11 +18,6 @@ type ReachMap map[string]struct {
2018
Internal, External []string
2119
}
2220

23-
// FlattenOmitStdLib calls FlattenFn with a function to exclude standard library import paths.
24-
func (rm ReachMap) FlattenOmitStdLib() []string {
25-
return rm.FlattenFn(paths.IsStandardImportPath)
26-
}
27-
2821
// Eliminate import paths with any elements having leading dots, leading
2922
// underscores, or testdata. If these are internally reachable (which is
3023
// a no-no, but possible), any external imports will have already been

internal/gps/solver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"strings"
1313

1414
"github.com/armon/go-radix"
15-
"github.com/golang/dep/internal/gps/internal/paths"
15+
"github.com/golang/dep/internal/gps/paths"
1616
"github.com/golang/dep/internal/gps/pkgtree"
1717
)
1818

0 commit comments

Comments
 (0)