From 6fd33b58854b7ea941143965886047b6ba852b9c Mon Sep 17 00:00:00 2001 From: sam boyer Date: Thu, 26 Jan 2017 00:23:43 -0500 Subject: [PATCH] Ensure writing deptree works for bzr, hg The main fix here is avoiding creating an empty directory for the destination, as copyDir() doesn't like that. Instead, we create only up to the parent dir. The other bit is ensuring the source repos exist in the cache before attempting to export them, for both bzr and hg. Addresses golang/dep#144 --- result.go | 7 ++++--- result_test.go | 42 ++++++++++++++++++++++++++++++++++++------ source.go | 3 +++ 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/result.go b/result.go index e38f08d..d46b6a6 100644 --- a/result.go +++ b/result.go @@ -3,7 +3,6 @@ package gps import ( "fmt" "os" - "path" "path/filepath" ) @@ -46,9 +45,11 @@ func WriteDepTree(basedir string, l Lock, sm SourceManager, sv bool) error { // TODO(sdboyer) parallelize for _, p := range l.Projects() { - to := path.Join(basedir, string(p.Ident().ProjectRoot)) + to := filepath.FromSlash(filepath.Join(basedir, string(p.Ident().ProjectRoot))) - err := os.MkdirAll(to, 0777) + // Only make the parent dir, as some source implementations will balk on + // trying to write to an empty but existing dir. + err := os.MkdirAll(filepath.Dir(to), 0777) if err != nil { return err } diff --git a/result_test.go b/result_test.go index d0fd972..ee6ab35 100644 --- a/result_test.go +++ b/result_test.go @@ -1,8 +1,10 @@ package gps import ( + "io/ioutil" "os" "path" + "path/filepath" "testing" ) @@ -43,26 +45,54 @@ func TestWriteDepTree(t *testing.T) { t.Skip("Skipping dep tree writing test in short mode") } - r := basicResult + tmp, err := ioutil.TempDir("", "writetree") + if err != nil { + t.Errorf("Failed to create temp dir: %s", err) + t.FailNow() + } + defer os.RemoveAll(tmp) - tmp := path.Join(os.TempDir(), "vsolvtest") - os.RemoveAll(tmp) + r := solution{ + att: 1, + p: []LockedProject{ + pa2lp(atom{ + id: pi("github.com/sdboyer/testrepo"), + v: NewBranch("master").Is(Revision("4d59fb584b15a94d7401e356d2875c472d76ef45")), + }, nil), + pa2lp(atom{ + id: pi("launchpad.net/govcstestbzrrepo"), + v: NewVersion("1.0.0").Is(Revision("matt@mattfarina.com-20150731135137-pbphasfppmygpl68")), + }, nil), + pa2lp(atom{ + id: pi("bitbucket.org/sdboyer/withbm"), + v: NewVersion("v1.0.0").Is(Revision("aa110802a0c64195d0a6c375c9f66668827c90b4")), + }, nil), + }, + } sm, clean := mkNaiveSM(t) defer clean() // nil lock/result should err immediately - err := WriteDepTree(path.Join(tmp, "export"), nil, sm, true) + err = WriteDepTree(tmp, nil, sm, true) if err == nil { t.Errorf("Should error if nil lock is passed to WriteDepTree") } - err = WriteDepTree(path.Join(tmp, "export"), r, sm, true) + err = WriteDepTree(tmp, r, sm, true) if err != nil { t.Errorf("Unexpected error while creating vendor tree: %s", err) } - // TODO(sdboyer) add more checks + if _, err = os.Stat(filepath.Join(tmp, "github.com", "sdboyer", "testrepo")); err != nil { + t.Errorf("Directory for github.com/sdboyer/testrepo does not exist") + } + if _, err = os.Stat(filepath.Join(tmp, "launchpad.net", "govcstestbzrrepo")); err != nil { + t.Errorf("Directory for launchpad.net/govcstestbzrrepo does not exist") + } + if _, err = os.Stat(filepath.Join(tmp, "bitbucket.org", "sdboyer", "withbm")); err != nil { + t.Errorf("Directory for bitbucket.org/sdboyer/withbm does not exist") + } } func BenchmarkCreateVendorTree(b *testing.B) { diff --git a/source.go b/source.go index 66636f4..18fb667 100644 --- a/source.go +++ b/source.go @@ -432,5 +432,8 @@ func (bs *baseVCSSource) toRevOrErr(v Version) (r Revision, err error) { } func (bs *baseVCSSource) exportVersionTo(v Version, to string) error { + if err := bs.ensureCacheExistence(); err != nil { + return err + } return bs.crepo.exportVersionTo(v, to) }