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

dep: update calculatePrune to not assume "/" as file separator #780

Merged
merged 2 commits into from
Jun 25, 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
3 changes: 2 additions & 1 deletion txn_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:")
Expand All @@ -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)
}
Expand Down
35 changes: 35 additions & 0 deletions txn_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import (
"io/ioutil"
"os"
"path/filepath"
"sort"
"strings"
"testing"

"reflect"

"github.com/golang/dep/internal/test"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -567,3 +570,35 @@ 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/keep/pkg/sub"))
h.TempDir(filepath.Join(vendorDir, "github.com/prune/pkg/sub"))

toKeep := []string{
filepath.FromSlash("github.com/keep/pkg"),
filepath.FromSlash("github.com/keep/pkg/sub"),
}

got, err := calculatePrune(h.Path(vendorDir), toKeep, nil)
if err != nil {
t.Fatal(err)
}

sort.Sort(byLen(got))

want := []string{
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("calculated prune paths are not as expected.\n(WNT) %s\n(GOT) %s", want, got)
}
}