Skip to content

Commit

Permalink
Merge pull request sdboyer/gps#189 from narqo/extract-package-tree
Browse files Browse the repository at this point in the history
Extract PackageTree and stuff into subpackage
  • Loading branch information
sdboyer authored Mar 29, 2017
2 parents 3c68118 + befdcc4 commit e8e777c
Show file tree
Hide file tree
Showing 24 changed files with 531 additions and 474 deletions.
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ build_script:
- C:\gopath\bin\glide install

test_script:
- go test
- go test . ./internal/... ./pkgtree/...
- go build example.go

deploy: off
6 changes: 4 additions & 2 deletions bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"path/filepath"
"sync/atomic"

"github.com/sdboyer/gps/pkgtree"
)

// sourceBridges provide an adapter to SourceManagers that tailor operations
Expand Down Expand Up @@ -43,7 +45,7 @@ type bridge struct {

// Simple, local cache of the root's PackageTree
crp *struct {
ptree PackageTree
ptree pkgtree.PackageTree
err error
}

Expand Down Expand Up @@ -280,7 +282,7 @@ func (b *bridge) vtu(id ProjectIdentifier, v Version) versionTypeUnion {
//
// The root project is handled separately, as the source manager isn't
// responsible for that code.
func (b *bridge) ListPackages(id ProjectIdentifier, v Version) (PackageTree, error) {
func (b *bridge) ListPackages(id ProjectIdentifier, v Version) (pkgtree.PackageTree, error) {
if b.s.rd.isRoot(id.ProjectRoot) {
return b.s.rd.rpt, nil
}
Expand Down
7 changes: 6 additions & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ test:
pre:
- go vet
override:
- cd $RD && go test -v -coverprofile=coverage.txt -covermode=atomic
- |
cd $RD && \
echo 'mode: atomic' > coverage.txt && \
go list ./... | grep -v "/vendor/" | \
xargs -n1 -I% sh -c 'go test -covermode=atomic -coverprofile=coverage.out -v % ; tail -n +2 coverage.out >> coverage.txt' && \
rm coverage.out
- cd $RD && go build example.go
- cd $RD && bash <(curl -s https://codecov.io/bash)
24 changes: 0 additions & 24 deletions deduce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,27 +642,3 @@ func ufmt(u *url.URL) string {
return fmt.Sprintf("host=%q, path=%q, opaque=%q, scheme=%q, user=%#v, pass=%#v, rawpath=%q, rawq=%q, frag=%q",
u.Host, u.Path, u.Opaque, u.Scheme, user, pass, u.RawPath, u.RawQuery, u.Fragment)
}

func TestIsStdLib(t *testing.T) {
fix := []struct {
ip string
is bool
}{
{"appengine", true},
{"net/http", true},
{"github.com/anything", false},
{"foo", true},
}

for _, f := range fix {
r := doIsStdLib(f.ip)
if r != f.is {
if r {
t.Errorf("%s was marked stdlib but should not have been", f.ip)
} else {
t.Errorf("%s was not marked stdlib but should have been", f.ip)

}
}
}
}
3 changes: 2 additions & 1 deletion example.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/sdboyer/gps"
"github.com/sdboyer/gps/pkgtree"
)

// This is probably the simplest possible implementation of gps. It does the
Expand All @@ -35,7 +36,7 @@ func main() {
TraceLogger: log.New(os.Stdout, "", 0),
}
// Perform static analysis on the current project to find all of its imports.
params.RootPackageTree, _ = gps.ListPackages(root, importroot)
params.RootPackageTree, _ = pkgtree.ListPackages(root, importroot)

// Set up a SourceManager. This manages interaction with sources (repositories).
tempdir, _ := ioutil.TempDir("", "gps-repocache")
Expand Down
6 changes: 4 additions & 2 deletions hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"crypto/sha256"
"io"
"sort"
"strings"
"strconv"
"strings"

"github.com/sdboyer/gps/pkgtree"
)

// string headers used to demarcate sections in hash input creation
Expand Down Expand Up @@ -128,7 +130,7 @@ func HashingInputsAsString(s Solver) string {
return (*bytes.Buffer)(buf).String()
}

type sortPackageOrErr []PackageOrErr
type sortPackageOrErr []pkgtree.PackageOrErr

func (s sortPackageOrErr) Len() int { return len(s) }
func (s sortPackageOrErr) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
Expand Down
38 changes: 22 additions & 16 deletions util.go → internal/fs/fs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gps
package fs

import (
"fmt"
"errors"
"io"
"io/ioutil"
"os"
Expand All @@ -10,10 +10,10 @@ import (
"syscall"
)

// renameWithFallback attempts to rename a file or directory, but falls back to
// RenameWithFallback attempts to rename a file or directory, but falls back to
// copying in the event of a cross-link device error. If the fallback copy
// succeeds, src is still removed, emulating normal rename behavior.
func renameWithFallback(src, dest string) error {
func RenameWithFallback(src, dest string) error {
fi, err := os.Stat(src)
if err != nil {
return err
Expand All @@ -36,9 +36,9 @@ func renameWithFallback(src, dest string) error {
var cerr error
if terr.Err == syscall.EXDEV {
if fi.IsDir() {
cerr = copyDir(src, dest)
cerr = CopyDir(src, dest)
} else {
cerr = copyFile(src, dest)
cerr = CopyFile(src, dest)
}
} else if runtime.GOOS == "windows" {
// In windows it can drop down to an operating system call that
Expand All @@ -49,9 +49,9 @@ func renameWithFallback(src, dest string) error {
// See https://msdn.microsoft.com/en-us/library/cc231199.aspx
if ok && noerr == 0x11 {
if fi.IsDir() {
cerr = copyDir(src, dest)
cerr = CopyDir(src, dest)
} else {
cerr = copyFile(src, dest)
cerr = CopyFile(src, dest)
}
}
} else {
Expand All @@ -65,10 +65,15 @@ func renameWithFallback(src, dest string) error {
return os.RemoveAll(src)
}

// copyDir recursively copies a directory tree, attempting to preserve permissions.
var (
errSrcNotDir = errors.New("source is not a directory")
errDestExist = errors.New("destination already exists")
)

// CopyDir recursively copies a directory tree, attempting to preserve permissions.
// Source directory must exist, destination directory must *not* exist.
// Symlinks are ignored and skipped.
func copyDir(src string, dst string) (err error) {
func CopyDir(src string, dst string) (err error) {
src = filepath.Clean(src)
dst = filepath.Clean(dst)

Expand All @@ -77,15 +82,15 @@ func copyDir(src string, dst string) (err error) {
return err
}
if !si.IsDir() {
return fmt.Errorf("source is not a directory")
return errSrcNotDir
}

_, err = os.Stat(dst)
if err != nil && !os.IsNotExist(err) {
return
}
if err == nil {
return fmt.Errorf("destination already exists")
return errDestExist
}

err = os.MkdirAll(dst, si.Mode())
Expand All @@ -103,14 +108,14 @@ func copyDir(src string, dst string) (err error) {
dstPath := filepath.Join(dst, entry.Name())

if entry.IsDir() {
err = copyDir(srcPath, dstPath)
err = CopyDir(srcPath, dstPath)
if err != nil {
return
}
} else {
// This will include symlinks, which is what we want in all cases
// where gps is copying things.
err = copyFile(srcPath, dstPath)
err = CopyFile(srcPath, dstPath)
if err != nil {
return
}
Expand All @@ -120,12 +125,12 @@ func copyDir(src string, dst string) (err error) {
return
}

// copyFile copies the contents of the file named src to the file named
// CopyFile copies the contents of the file named src to the file named
// by dst. The file will be created if it does not already exist. If the
// destination file exists, all it's contents will be replaced by the contents
// of the source file. The file mode will be copied from the source and
// the copied data is synced/flushed to stable storage.
func copyFile(src, dst string) (err error) {
func CopyFile(src, dst string) (err error) {
in, err := os.Open(src)
if err != nil {
return
Expand Down Expand Up @@ -163,3 +168,4 @@ func copyFile(src, dst string) (err error) {

return
}

17 changes: 3 additions & 14 deletions util_test.go → internal/fs/fs_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package gps
package fs

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"testing"
)
Expand Down Expand Up @@ -47,7 +46,7 @@ func TestCopyDir(t *testing.T) {
srcf.Close()

destdir := filepath.Join(dir, "dest")
if err := copyDir(srcdir, destdir); err != nil {
if err := CopyDir(srcdir, destdir); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -103,7 +102,7 @@ func TestCopyFile(t *testing.T) {
srcf.Close()

destf := filepath.Join(dir, "destf")
if err := copyFile(srcf.Name(), destf); err != nil {
if err := CopyFile(srcf.Name(), destf); err != nil {
t.Fatal(err)
}

Expand All @@ -130,13 +129,3 @@ func TestCopyFile(t *testing.T) {
t.Fatalf("expected %s: %#v\n to be the same mode as %s: %#v", srcf.Name(), srcinfo.Mode(), destf, destinfo.Mode())
}
}

// Fail a test if the specified binaries aren't installed.
func requiresBins(t *testing.T, bins ...string) {
for _, b := range bins {
_, err := exec.LookPath(b)
if err != nil {
t.Fatalf("%s is not installed", b)
}
}
}
19 changes: 19 additions & 0 deletions internal/internal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Package internal provides support for gps own packages.
package internal

import "strings"

// IsStdLib is a reference to internal implementation.
// It is stored as a var so that tests can swap it out. Ugh globals, ugh.
var IsStdLib = doIsStdLib

// 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 {
i = len(path)
}

return !strings.Contains(path[:i], ".")
}
28 changes: 28 additions & 0 deletions internal/internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package internal

import "testing"

func TestIsStdLib(t *testing.T) {
fix := []struct {
ip string
is bool
}{
{"appengine", true},
{"net/http", true},
{"github.com/anything", false},
{"foo", true},
}

for _, f := range fix {
r := doIsStdLib(f.ip)
if r != f.is {
if r {
t.Errorf("%s was marked stdlib but should not have been", f.ip)
} else {
t.Errorf("%s was not marked stdlib but should have been", f.ip)

}
}
}
}

Loading

0 comments on commit e8e777c

Please sign in to comment.