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

Remove duplicate 'isStdLib' function #606

Merged
merged 6 commits into from
May 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions cmd/dep/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
fb "github.com/golang/dep/internal/feedback"
"github.com/golang/dep/internal/fs"
"github.com/golang/dep/internal/gps"
"github.com/golang/dep/internal/gps/paths"
"github.com/golang/dep/internal/gps/pkgtree"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -245,20 +246,6 @@ func contains(a []string, b string) bool {
return false
}

// isStdLib reports whether $GOROOT/src/path should be considered
// part of the standard distribution. For historical reasons we allow people to add
// their own code to $GOROOT instead of using $GOPATH, but we assume that
// code will start with a domain name (dot in the first element).
// This was loving taken from src/cmd/go/pkg.go in Go's code (isStandardImportPath).
func isStdLib(path string) bool {
i := strings.Index(path, "/")
if i < 0 {
i = len(path)
}
elem := path[:i]
return !strings.Contains(elem, ".")
}

// TODO solve failures can be really creative - we need to be similarly creative
// in handling them and informing the user appropriately
func handleAllTheFailuresOfTheWorld(err error) {
Expand Down Expand Up @@ -370,7 +357,7 @@ func getProjectData(ctx *dep.Ctx, pkgT pkgtree.PackageTree, cpr string, sm gps.S
return projectData{}, nil
}

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

// recurse
for _, rpkg := range reached.External {
if isStdLib(rpkg) {
if paths.IsStandardImportPath(rpkg) {
continue
}

Expand Down
19 changes: 0 additions & 19 deletions cmd/dep/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,6 @@ func TestContains(t *testing.T) {
}
}

func TestIsStdLib(t *testing.T) {
t.Parallel()

tests := map[string]bool{
"github.com/Sirupsen/logrus": false,
"encoding/json": true,
"golang.org/x/net/context": false,
"net/context": true,
".": false,
}

for p, e := range tests {
b := isStdLib(p)
if b != e {
t.Fatalf("%s: expected %t got %t", p, e, b)
}
}
}

func TestGetProjectPropertiesFromVersion(t *testing.T) {
t.Parallel()

Expand Down
5 changes: 3 additions & 2 deletions cmd/dep/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/golang/dep"
"github.com/golang/dep/internal/gps"
"github.com/golang/dep/internal/gps/paths"
"github.com/golang/dep/internal/gps/pkgtree"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -71,7 +72,7 @@ func (cmd *removeCommand) Run(ctx *dep.Ctx, args []string) error {
return errors.Errorf("remove takes no arguments when running with -unused")
}

reachlist := reachmap.FlattenOmitStdLib()
reachlist := reachmap.FlattenFn(paths.IsStandardImportPath)

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

otherroots := make(map[gps.ProjectRoot]bool)
for _, im := range reachlist {
if isStdLib(im) {
if paths.IsStandardImportPath(im) {
continue
}
pr, err := sm.DeduceProjectRoot(im)
Expand Down
7 changes: 4 additions & 3 deletions cmd/dep/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/golang/dep"
"github.com/golang/dep/internal/gps"
"github.com/golang/dep/internal/gps/paths"
"github.com/golang/dep/internal/gps/pkgtree"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -164,7 +165,7 @@ func (out *dotOutput) BasicHeader() {
ptree, _ := pkgtree.ListPackages(out.p.AbsRoot, string(out.p.ImportRoot))
prm, _ := ptree.ToReachMap(true, false, false, nil)

out.g.createNode(string(out.p.ImportRoot), "", prm.FlattenOmitStdLib())
out.g.createNode(string(out.p.ImportRoot), "", prm.FlattenFn(paths.IsStandardImportPath))
}

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

prm, _ := ptr.ToReachMap(true, false, false, nil)
bs.Children = prm.FlattenOmitStdLib()
bs.Children = prm.FlattenFn(paths.IsStandardImportPath)
}

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

external := rm.FlattenOmitStdLib()
external := rm.FlattenFn(paths.IsStandardImportPath)
roots := make(map[gps.ProjectRoot][]string, len(external))

type fail struct {
Expand Down
20 changes: 0 additions & 20 deletions internal/gps/internal/paths/paths.go

This file was deleted.

21 changes: 21 additions & 0 deletions internal/gps/paths/paths.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package paths

import "strings"

// IsStandardImportPath reports whether $GOROOT/src/path should be considered
// part of the standard distribution. For historical reasons we allow people to add
// their own code to $GOROOT instead of using $GOPATH, but we assume that
// code will start with a domain name (dot in the first element).
// This was loving taken from src/cmd/go/pkg.go in Go's code (isStandardImportPath).
func IsStandardImportPath(path string) bool {
i := strings.Index(path, "/")
if i < 0 {
i = len(path)
}

return !strings.Contains(path[:i], ".")
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestIsStandardImportPath(t *testing.T) {
{"github.com/anything", false},
{"github.com", false},
{"foo", true},
{".", false},
}

for _, f := range fix {
Expand Down
2 changes: 1 addition & 1 deletion internal/gps/pkgtree/pkgtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"testing"

"github.com/golang/dep/internal/fs"
"github.com/golang/dep/internal/gps/internal/paths"
"github.com/golang/dep/internal/gps/paths"
)

// PackageTree.ToReachMap() uses an easily separable algorithm, wmToReach(),
Expand Down
7 changes: 0 additions & 7 deletions internal/gps/pkgtree/reachmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ package pkgtree
import (
"sort"
"strings"

"github.com/golang/dep/internal/gps/internal/paths"
)

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

// FlattenOmitStdLib calls FlattenFn with a function to exclude standard library import paths.
func (rm ReachMap) FlattenOmitStdLib() []string {
return rm.FlattenFn(paths.IsStandardImportPath)
}

// Eliminate import paths with any elements having leading dots, leading
// underscores, or testdata. If these are internally reachable (which is
// a no-no, but possible), any external imports will have already been
Expand Down
2 changes: 1 addition & 1 deletion internal/gps/solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"strings"

"github.com/armon/go-radix"
"github.com/golang/dep/internal/gps/internal/paths"
"github.com/golang/dep/internal/gps/paths"
"github.com/golang/dep/internal/gps/pkgtree"
)

Expand Down