From 0584d8c5055fd1f0527240abf35626b54778c8e6 Mon Sep 17 00:00:00 2001 From: Ibrahim AshShohail Date: Wed, 21 Jun 2017 03:06:13 +0300 Subject: [PATCH 1/2] dep: add TestCalculatePrune Add a test for dep.calculatePrune Signed-off-by: Ibrahim AshShohail --- txn_writer_test.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/txn_writer_test.go b/txn_writer_test.go index c0f0b9187a..a24cd0cadd 100644 --- a/txn_writer_test.go +++ b/txn_writer_test.go @@ -8,9 +8,12 @@ import ( "io/ioutil" "os" "path/filepath" + "sort" "strings" "testing" + "reflect" + "github.com/golang/dep/internal/test" "github.com/pkg/errors" ) @@ -567,3 +570,37 @@ func TestSafeWriter_VendorDotGitPreservedWithForceVendor(t *testing.T) { t.Fatal(err) } } + +func TestCalculatePrune(t *testing.T) { + h := test.NewHelper(t) + defer h.Cleanup() + + vendorDir := "vendor" + h.TempDir(vendorDir) + h.TempDir(filepath.Join(vendorDir, "github.com", "prune", "package")) + h.TempDir(filepath.Join(vendorDir, "github.com", "keep", "package")) + + toKeep := []string{ + "github.com/keep/package", + } + + got, err := calculatePrune(h.Path(vendorDir), toKeep, nil) + if err != nil { + t.Fatal(err) + } + + sort.Sort(byLen(got)) + + if len(got) != 2 { + t.Fatalf("expected 2 directories, got %v", len(got)) + } + + want := []string{ + h.Path(filepath.Join(vendorDir, "github.com", "prune", "package")), + h.Path(filepath.Join(vendorDir, "github.com", "prune")), + } + + if !reflect.DeepEqual(want, got) { + t.Fatalf("expect %s, got %s", want, got) + } +} From 30ba901b2cf230307ac60b42f51b88099d5706ab Mon Sep 17 00:00:00 2001 From: Ibrahim AshShohail Date: Wed, 21 Jun 2017 03:19:36 +0300 Subject: [PATCH 2/2] dep: update calculatePrune to not assume "/" as separtor dep.calculatePrune assumes "/" is the file separtor. This change fixes an issue caused by that on Windows. Fixes #775 Signed-off-by: Ibrahim AshShohail --- txn_writer.go | 3 ++- txn_writer_test.go | 18 ++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/txn_writer.go b/txn_writer.go index 97d38fe823..43420dc52b 100644 --- a/txn_writer.go +++ b/txn_writer.go @@ -530,6 +530,7 @@ fail: return failerr } +// calculatePrune returns the paths of the packages to be deleted from vendorDir. func calculatePrune(vendorDir string, keep []string, logger *log.Logger) ([]string, error) { if logger != nil { logger.Println("Calculating prune. Checking the following packages:") @@ -547,7 +548,7 @@ func calculatePrune(vendorDir string, keep []string, logger *log.Logger) ([]stri return nil } - name := strings.TrimPrefix(path, vendorDir+"/") + name := strings.TrimPrefix(path, vendorDir+string(filepath.Separator)) if logger != nil { logger.Printf(" %s", name) } diff --git a/txn_writer_test.go b/txn_writer_test.go index a24cd0cadd..53abb33695 100644 --- a/txn_writer_test.go +++ b/txn_writer_test.go @@ -577,11 +577,12 @@ func TestCalculatePrune(t *testing.T) { vendorDir := "vendor" h.TempDir(vendorDir) - h.TempDir(filepath.Join(vendorDir, "github.com", "prune", "package")) - h.TempDir(filepath.Join(vendorDir, "github.com", "keep", "package")) + h.TempDir(filepath.Join(vendorDir, "github.com/keep/pkg/sub")) + h.TempDir(filepath.Join(vendorDir, "github.com/prune/pkg/sub")) toKeep := []string{ - "github.com/keep/package", + filepath.FromSlash("github.com/keep/pkg"), + filepath.FromSlash("github.com/keep/pkg/sub"), } got, err := calculatePrune(h.Path(vendorDir), toKeep, nil) @@ -591,16 +592,13 @@ func TestCalculatePrune(t *testing.T) { sort.Sort(byLen(got)) - if len(got) != 2 { - t.Fatalf("expected 2 directories, got %v", len(got)) - } - want := []string{ - h.Path(filepath.Join(vendorDir, "github.com", "prune", "package")), - h.Path(filepath.Join(vendorDir, "github.com", "prune")), + h.Path(filepath.Join(vendorDir, "github.com/prune/pkg/sub")), + h.Path(filepath.Join(vendorDir, "github.com/prune/pkg")), + h.Path(filepath.Join(vendorDir, "github.com/prune")), } if !reflect.DeepEqual(want, got) { - t.Fatalf("expect %s, got %s", want, got) + t.Fatalf("calculated prune paths are not as expected.\n(WNT) %s\n(GOT) %s", want, got) } }