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

Commit

Permalink
extract PackageTree to pkgtree package
Browse files Browse the repository at this point in the history
fix tests
  • Loading branch information
Vladimir Varankin committed Mar 14, 2017
1 parent ff81dcf commit f3a4a56
Show file tree
Hide file tree
Showing 21 changed files with 1,355 additions and 1,286 deletions.
938 changes: 0 additions & 938 deletions analysis.go

Large diffs are not rendered by default.

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
24 changes: 0 additions & 24 deletions deduce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,27 +626,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
38 changes: 22 additions & 16 deletions util.go → 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 → 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)
}
}
}
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
Loading

0 comments on commit f3a4a56

Please sign in to comment.