From 12a1b64961cb2c663a20fd5bf67667ec70603371 Mon Sep 17 00:00:00 2001 From: Ibrahim AshShohail Date: Fri, 29 Sep 2017 12:50:58 +0300 Subject: [PATCH] internal/gps: update prune functions to use in-memory filesystemState This commit update prune functions to use filesystemState instead of repeated walks over the filesyste. strip_vendor functions still walk the filesystem and will be updated in a later commit. Signed-off-by: Ibrahim AshShohail --- hack/lint.bash | 2 +- internal/gps/prune.go | 254 ++++++++--------- internal/gps/prune_test.go | 280 ++++++++++++------- internal/gps/strip_vendor_nonwindows_test.go | 72 ++--- internal/gps/strip_vendor_test.go | 31 +- internal/gps/strip_vendor_windows_test.go | 74 ++--- 6 files changed, 384 insertions(+), 329 deletions(-) diff --git a/hack/lint.bash b/hack/lint.bash index 2dc1d6e8ad..aaa25da1a5 100755 --- a/hack/lint.bash +++ b/hack/lint.bash @@ -9,4 +9,4 @@ set -e PKGS=$(go list ./... | grep -vF /vendor/) go vet $PKGS golint $PKGS -megacheck -unused.exported -ignore "github.com/golang/dep/internal/test/test.go:U1000 github.com/golang/dep/internal/gps/prune.go:U1000" $PKGS +megacheck -unused.exported -ignore "github.com/golang/dep/internal/test/test.go:U1000" $PKGS diff --git a/internal/gps/prune.go b/internal/gps/prune.go index b67e01b1ff..4906f73d2c 100644 --- a/internal/gps/prune.go +++ b/internal/gps/prune.go @@ -18,7 +18,7 @@ type PruneOptions uint8 const ( // PruneNestedVendorDirs indicates if nested vendor directories should be pruned. - PruneNestedVendorDirs = 1 << iota + PruneNestedVendorDirs PruneOptions = 1 << iota // PruneUnusedPackages indicates if unused Go packages should be pruned. PruneUnusedPackages // PruneNonGoFiles indicates if non-Go files should be pruned. @@ -57,12 +57,13 @@ var ( // on the PruneOptions passed. // // A Lock must be passed if PruneUnusedPackages is toggled on. -func Prune(baseDir string, options PruneOptions, l Lock, logger *log.Logger) error { +func Prune(baseDir string, l Lock, options PruneOptions, logger *log.Logger) error { // TODO(ibrasho) allow passing specific options per project + for _, lp := range l.Projects() { projectDir := filepath.Join(baseDir, string(lp.Ident().ProjectRoot)) - err := PruneProject(projectDir, lp, options, logger) - if err != nil { + + if err := PruneProject(projectDir, lp, options, logger); err != nil { return err } } @@ -73,28 +74,31 @@ func Prune(baseDir string, options PruneOptions, l Lock, logger *log.Logger) err // PruneProject remove excess files according to the options passed, from // the lp directory in baseDir. func PruneProject(baseDir string, lp LockedProject, options PruneOptions, logger *log.Logger) error { - projectDir := filepath.Join(baseDir, string(lp.Ident().ProjectRoot)) + fs, err := deriveFilesystemState(baseDir) + if err != nil { + return errors.Wrap(err, "could not derive filesystem state") + } if (options & PruneNestedVendorDirs) != 0 { - if err := pruneNestedVendorDirs(projectDir); err != nil { + if err := pruneNestedVendorDirs(baseDir); err != nil { return err } } if (options & PruneUnusedPackages) != 0 { - if err := pruneUnusedPackages(lp, projectDir, logger); err != nil { + if err := pruneUnusedPackages(lp, fs, logger); err != nil { return errors.Wrap(err, "failed to prune unused packages") } } if (options & PruneNonGoFiles) != 0 { - if err := pruneNonGoFiles(projectDir, logger); err != nil { + if err := pruneNonGoFiles(fs, logger); err != nil { return errors.Wrap(err, "failed to prune non-Go files") } } if (options & PruneGoTestFiles) != 0 { - if err := pruneGoTestFiles(projectDir, logger); err != nil { + if err := pruneGoTestFiles(fs, logger); err != nil { return errors.Wrap(err, "failed to prune Go test files") } } @@ -107,152 +111,141 @@ func pruneNestedVendorDirs(baseDir string) error { return filepath.Walk(baseDir, stripVendor) } -// pruneUnusedPackages deletes unimported packages found within baseDir. +// pruneVendorDirs deletes all nested vendor directories within baseDir. +// func pruneVendorDirs(fs filesystemState, logger *log.Logger) error { +// toDelete := collectNestedVendorDirs(fs) + +// for _, path := range toDelete { +// logger.Printf(" * %s", path) + +// if err := os.Remove(path); err != nil && !os.IsNotExist(err) { +// return err +// } +// } + +// return nil +// } + +// func collectNestedVendorDirs(fs filesystemState) []string { +// toDelete := make([]string, 0, len(fs.dirs)/4) + +// for _, dir := range fs.dirs { +// if filepath.Base(dir) +// toDelete = append(toDelete, dir) +// } + +// for _, link := range fs.links { +// toDelete = append(toDelete) +// } + +// return files +// } + +// pruneUnusedPackages deletes unimported packages found in fs. // Determining whether packages are imported or not is based on the passed LockedProject. -func pruneUnusedPackages(lp LockedProject, projectDir string, logger *log.Logger) error { +func pruneUnusedPackages(lp LockedProject, fs filesystemState, logger *log.Logger) error { pr := string(lp.Ident().ProjectRoot) - logger.Printf("Calculating unused packages in %s to prune.\n", pr) + unusedPackages := calculateUnusedPackages(lp, fs) - unusedPackages, err := calculateUnusedPackages(lp, projectDir) - if err != nil { - return errors.Wrapf(err, "could not calculate unused packages in %s", pr) - } + logger.Printf("Found %d unused packages in %s:\n", len(unusedPackages), pr) - logger.Printf("Found the following unused packages in %s:\n", pr) for pkg := range unusedPackages { logger.Printf(" * %s\n", filepath.Join(pr, pkg)) } - unusedPackagesFiles, err := collectUnusedPackagesFiles(projectDir, unusedPackages) - if err != nil { - return errors.Wrapf(err, "could not collect unused packages' files in %s", pr) - } + toDelete := collectUnusedPackagesFiles(fs, unusedPackages) + + logger.Printf("Deleting %d files in unused packages:", len(toDelete)) - if err := deleteFiles(unusedPackagesFiles); err != nil { - return errors.Wrapf(err, "") + for _, path := range toDelete { + logger.Printf(" * %s\n", path) + + if err := os.Remove(path); err != nil && !os.IsNotExist(err) { + return err + } } return nil } // calculateUnusedPackages generates a list of unused packages in lp. -func calculateUnusedPackages(lp LockedProject, projectDir string) (map[string]struct{}, error) { - // TODO(ibrasho): optimize this... +func calculateUnusedPackages(lp LockedProject, fs filesystemState) map[string]struct{} { unused := make(map[string]struct{}) imported := make(map[string]struct{}) + for _, pkg := range lp.Packages() { imported[pkg] = struct{}{} } - err := filepath.Walk(projectDir, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - // Ignore anything that's not a directory. - if !info.IsDir() { - return nil - } + // Add the root package if it's not imported. + if _, ok := imported["."]; !ok { + unused["."] = struct{}{} + } - pkg, err := filepath.Rel(projectDir, path) - if err != nil { - return errors.Wrap(err, "unexpected error while calculating unused packages") - } + for _, dirPath := range fs.dirs { + pkg := filepath.ToSlash(dirPath) - pkg = filepath.ToSlash(pkg) if _, ok := imported[pkg]; !ok { unused[pkg] = struct{}{} } + } - return nil - }) - - return unused, err + return unused } -// collectUnusedPackagesFiles returns a slice of all files in the unused packages in projectDir. -func collectUnusedPackagesFiles(projectDir string, unusedPackages map[string]struct{}) ([]string, error) { +// collectUnusedPackagesFiles returns a slice of all files in the unused packages based on fs. +func collectUnusedPackagesFiles(fs filesystemState, unusedPackages map[string]struct{}) []string { // TODO(ibrasho): is this useful? files := make([]string, 0, len(unusedPackages)) - err := filepath.Walk(projectDir, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err + for _, path := range fs.files { + // Keep perserved files. + if isPreservedFile(filepath.Base(path)) { + continue } - // Ignore directories. - if info.IsDir() { - return nil - } + pkg := filepath.ToSlash(filepath.Dir(path)) - // Ignore perserved files. - if isPreservedFile(info.Name()) { - return nil - } - - pkg, err := filepath.Rel(projectDir, filepath.Dir(path)) - if err != nil { - return errors.Wrap(err, "unexpected error while calculating unused packages") - } - - pkg = filepath.ToSlash(pkg) if _, ok := unusedPackages[pkg]; ok { - files = append(files, path) + files = append(files, filepath.Join(fs.root, path)) } + } - return nil - }) - - return files, err + return files } -// pruneNonGoFiles delete all non-Go files existing within baseDir. +// pruneNonGoFiles delete all non-Go files existing in fs. // Files with names that are prefixed by any entry in preservedNonGoFiles // are not deleted. -func pruneNonGoFiles(baseDir string, logger *log.Logger) error { - files, err := collectNonGoFiles(baseDir, logger) - if err != nil { - return errors.Wrap(err, "could not collect non-Go files") - } - - if err := deleteFiles(files); err != nil { - return errors.Wrap(err, "could not prune Go test files") - } - - return nil -} - -// collectNonGoFiles returns a slice containing all non-Go files in baseDir. -// Files meeting the checks in isPreservedFile are not returned. -func collectNonGoFiles(baseDir string, logger *log.Logger) ([]string, error) { - files := make([]string, 0) - - err := filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err +func pruneNonGoFiles(fs filesystemState, logger *log.Logger) error { + // TODO(ibrasho) detemine a sane capacity + toDelete := make([]string, 0, len(fs.files)/4) + + for _, path := range fs.files { + // Ignore Go files. + if strings.HasSuffix(path, ".go") { + continue } - // Ignore directories. - if info.IsDir() { - return nil + // Ignore perserved files. + if isPreservedFile(filepath.Base(path)) { + continue } - // Ignore all Go files. - if strings.HasSuffix(info.Name(), ".go") { - return nil - } + toDelete = append(toDelete, filepath.Join(fs.root, path)) + } - // Ignore perserved files. - if isPreservedFile(info.Name()) { - return nil - } + logger.Printf("Deleting %d non-Go files:\n", len(toDelete)) - files = append(files, path) + for _, path := range toDelete { + logger.Printf(" * %s\n", path) - return nil - }) + if err := os.Remove(path); err != nil && !os.IsNotExist(err) { + return err + } + } - return files, err + return nil } // isPreservedFile checks if the file name indicates that the file should be @@ -275,51 +268,26 @@ func isPreservedFile(name string) bool { return false } -// pruneGoTestFiles deletes all Go test files (*_test.go) within baseDir. -func pruneGoTestFiles(baseDir string, logger *log.Logger) error { - files, err := collectGoTestFiles(baseDir) - if err != nil { - return errors.Wrap(err, "could not collect Go test files") - } - - if err := deleteFiles(files); err != nil { - return errors.Wrap(err, "could not prune Go test files") - } - - return nil -} - -// collectGoTestFiles returns a slice contains all Go test files (any files -// prefixed with _test.go) in baseDir. -func collectGoTestFiles(baseDir string) ([]string, error) { - files := make([]string, 0) +// pruneGoTestFiles deletes all Go test files (*_test.go) in fs. +func pruneGoTestFiles(fs filesystemState, logger *log.Logger) error { + // TODO(ibrasho) detemine a sane capacity + toDelete := make([]string, 0, len(fs.files)/2) - err := filepath.Walk(baseDir, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - // Ignore directories. - if info.IsDir() { - return nil - } - - // Ignore any files that is not a Go test file. - if strings.HasSuffix(info.Name(), "_test.go") { - files = append(files, path) + for _, path := range fs.files { + if strings.HasSuffix(path, "_test.go") { + toDelete = append(toDelete, filepath.Join(fs.root, path)) } + } - return nil - }) + logger.Printf("Deleting %d Go test files:\n", len(toDelete)) - return files, err -} + for _, path := range toDelete { + logger.Printf(" * %s\n", path) -func deleteFiles(paths []string) error { - for _, path := range paths { - if err := os.Remove(path); err != nil { + if err := os.Remove(path); err != nil && !os.IsNotExist(err) { return err } } + return nil } diff --git a/internal/gps/prune_test.go b/internal/gps/prune_test.go index de7791cd77..50c814469c 100644 --- a/internal/gps/prune_test.go +++ b/internal/gps/prune_test.go @@ -12,13 +12,78 @@ import ( "github.com/golang/dep/internal/test" ) +func TestPrune(t *testing.T) { + h := test.NewHelper(t) + defer h.Cleanup() + + pr := "github.com/project/repository" + + h.TempDir(".") + h.TempDir(pr) + + baseDir := h.Path(".") + + l := SimpleLock{ + LockedProject{ + pi: ProjectIdentifier{ + ProjectRoot: ProjectRoot(pr), + }, + pkgs: []string{}, + }, + } + options := PruneNestedVendorDirs | PruneNonGoFiles | PruneGoTestFiles | PruneUnusedPackages + logger := log.New(ioutil.Discard, "", 0) + + err := Prune(baseDir, l, options, logger) + if err != nil { + t.Fatal(err) + } +} + +func TestPruneProject(t *testing.T) { + h := test.NewHelper(t) + defer h.Cleanup() + + pr := "github.com/project/repository" + h.TempDir(pr) + + baseDir := h.Path(".") + lp := LockedProject{ + pi: ProjectIdentifier{ + ProjectRoot: ProjectRoot(pr), + }, + pkgs: []string{}, + } + + options := PruneNestedVendorDirs | PruneNonGoFiles | PruneGoTestFiles | PruneUnusedPackages + logger := log.New(ioutil.Discard, "", 0) + + err := PruneProject(baseDir, lp, options, logger) + if err != nil { + t.Fatal(err) + } +} + +// func TestPruneNestedVendorDirs(t *testing.T) { +// h := test.NewHelper(t) +// defer h.Cleanup() + +// h.TempDir(".") +// baseDir := h.Path(".") + +// err := pruneNestedVendorDirs(baseDir) +// if err != nil { +// t.Fatal(err) +// } +// } + func TestPruneUnusedPackages(t *testing.T) { h := test.NewHelper(t) defer h.Cleanup() h.TempDir(".") - pr := "github.com/test/project" + pr := "github.com/sample/repository" pi := ProjectIdentifier{ProjectRoot: ProjectRoot(pr)} testcases := []struct { @@ -30,18 +95,20 @@ func TestPruneUnusedPackages(t *testing.T) { { "one-package", LockedProject{ - pi: pi, - pkgs: []string{"."}, + pi: pi, + pkgs: []string{ + ".", + }, }, fsTestCase{ before: filesystemState{ - files: []fsPath{ - {"main.go"}, + files: []string{ + "main.go", }, }, after: filesystemState{ - files: []fsPath{ - {"main.go"}, + files: []string{ + "main.go", }, }, }, @@ -50,25 +117,27 @@ func TestPruneUnusedPackages(t *testing.T) { { "nested-package", LockedProject{ - pi: pi, - pkgs: []string{"pkg"}, + pi: pi, + pkgs: []string{ + "pkg", + }, }, fsTestCase{ before: filesystemState{ - dirs: []fsPath{ - {"pkg"}, + dirs: []string{ + "pkg", }, - files: []fsPath{ - {"main.go"}, - {"pkg", "main.go"}, + files: []string{ + "main.go", + "pkg/main.go", }, }, after: filesystemState{ - dirs: []fsPath{ - {"pkg"}, + dirs: []string{ + "pkg", }, - files: []fsPath{ - {"pkg", "main.go"}, + files: []string{ + "pkg/main.go", }, }, }, @@ -77,36 +146,39 @@ func TestPruneUnusedPackages(t *testing.T) { { "complex-project", LockedProject{ - pi: pi, - pkgs: []string{"pkg", "pkg/nestedpkg/otherpkg"}, + pi: pi, + pkgs: []string{ + "pkg", + "pkg/nestedpkg/otherpkg", + }, }, fsTestCase{ before: filesystemState{ - dirs: []fsPath{ - {"pkg"}, - {"pkg", "nestedpkg"}, - {"pkg", "nestedpkg", "otherpkg"}, + dirs: []string{ + "pkg", + "pkg/nestedpkg", + "pkg/nestedpkg/otherpkg", }, - files: []fsPath{ - {"main.go"}, - {"COPYING"}, - {"pkg", "main.go"}, - {"pkg", "nestedpkg", "main.go"}, - {"pkg", "nestedpkg", "PATENT.md"}, - {"pkg", "nestedpkg", "otherpkg", "main.go"}, + files: []string{ + "main.go", + "COPYING", + "pkg/main.go", + "pkg/nestedpkg/main.go", + "pkg/nestedpkg/PATENT.md", + "pkg/nestedpkg/otherpkg/main.go", }, }, after: filesystemState{ - dirs: []fsPath{ - {"pkg"}, - {"pkg", "nestedpkg"}, - {"pkg", "nestedpkg", "otherpkg"}, + dirs: []string{ + "pkg", + "pkg/nestedpkg", + "pkg/nestedpkg/otherpkg", }, - files: []fsPath{ - {"COPYING"}, - {"pkg", "main.go"}, - {"pkg", "nestedpkg", "PATENT.md"}, - {"pkg", "nestedpkg", "otherpkg", "main.go"}, + files: []string{ + "COPYING", + "pkg/main.go", + "pkg/nestedpkg/PATENT.md", + "pkg/nestedpkg/otherpkg/main.go", }, }, }, @@ -119,20 +191,24 @@ func TestPruneUnusedPackages(t *testing.T) { for _, tc := range testcases { t.Run(tc.name, func(t *testing.T) { h.TempDir(pr) - projectDir := h.Path(pr) - tc.fs.before.root = projectDir - tc.fs.after.root = projectDir + baseDir := h.Path(pr) + tc.fs.before.root = baseDir + tc.fs.after.root = baseDir + tc.fs.setup(t) - tc.fs.before.setup(t) + fs, err := deriveFilesystemState(baseDir) + if err != nil { + t.Fatal(err) + } - err := pruneUnusedPackages(tc.lp, projectDir, logger) + err = pruneUnusedPackages(tc.lp, fs, logger) if tc.err && err == nil { - t.Errorf("expected an error, got nil") + t.Fatalf("expected an error, got nil") } else if !tc.err && err != nil { - t.Errorf("unexpected error: %s", err) + t.Fatalf("unexpected error: %s", err) } - tc.fs.after.assert(t) + tc.fs.assert(t) }) } } @@ -152,8 +228,8 @@ func TestPruneNonGoFiles(t *testing.T) { "one-file", fsTestCase{ before: filesystemState{ - files: []fsPath{ - {"README.md"}, + files: []string{ + "README.md", }, }, after: filesystemState{}, @@ -164,16 +240,16 @@ func TestPruneNonGoFiles(t *testing.T) { "multiple-files", fsTestCase{ before: filesystemState{ - files: []fsPath{ - {"main.go"}, - {"main_test.go"}, - {"README"}, + files: []string{ + "main.go", + "main_test.go", + "README", }, }, after: filesystemState{ - files: []fsPath{ - {"main.go"}, - {"main_test.go"}, + files: []string{ + "main.go", + "main_test.go", }, }, }, @@ -183,22 +259,22 @@ func TestPruneNonGoFiles(t *testing.T) { "mixed-files", fsTestCase{ before: filesystemState{ - dirs: []fsPath{ - {"dir"}, + dirs: []string{ + "dir", }, - files: []fsPath{ - {"dir", "main.go"}, - {"dir", "main_test.go"}, - {"dir", "db.sqlite"}, + files: []string{ + "dir/main.go", + "dir/main_test.go", + "dir/db.sqlite", }, }, after: filesystemState{ - dirs: []fsPath{ - {"dir"}, + dirs: []string{ + "dir", }, - files: []fsPath{ - {"dir", "main.go"}, - {"dir", "main_test.go"}, + files: []string{ + "dir/main.go", + "dir/main_test.go", }, }, }, @@ -215,16 +291,21 @@ func TestPruneNonGoFiles(t *testing.T) { tc.fs.before.root = baseDir tc.fs.after.root = baseDir - tc.fs.before.setup(t) + tc.fs.setup(t) - err := pruneNonGoFiles(baseDir, logger) + fs, err := deriveFilesystemState(baseDir) + if err != nil { + t.Fatal(err) + } + + err = pruneNonGoFiles(fs, logger) if tc.err && err == nil { t.Errorf("expected an error, got nil") } else if !tc.err && err != nil { t.Errorf("unexpected error: %s", err) } - tc.fs.after.assert(t) + tc.fs.assert(t) }) } } @@ -244,8 +325,8 @@ func TestPruneGoTestFiles(t *testing.T) { "one-test-file", fsTestCase{ before: filesystemState{ - files: []fsPath{ - {"main_test.go"}, + files: []string{ + "main_test.go", }, }, after: filesystemState{}, @@ -256,17 +337,17 @@ func TestPruneGoTestFiles(t *testing.T) { "multiple-files", fsTestCase{ before: filesystemState{ - dirs: []fsPath{ - {"dir"}, + dirs: []string{ + "dir", }, - files: []fsPath{ - {"dir", "main_test.go"}, - {"dir", "main2_test.go"}, + files: []string{ + "dir/main_test.go", + "dir/main2_test.go", }, }, after: filesystemState{ - dirs: []fsPath{ - {"dir"}, + dirs: []string{ + "dir", }, }, }, @@ -276,23 +357,23 @@ func TestPruneGoTestFiles(t *testing.T) { "mixed-files", fsTestCase{ before: filesystemState{ - dirs: []fsPath{ - {"dir"}, + dirs: []string{ + "dir", }, - files: []fsPath{ - {"dir", "main.go"}, - {"dir", "main2.go"}, - {"dir", "main_test.go"}, - {"dir", "main2_test.go"}, + files: []string{ + "dir/main.go", + "dir/main2.go", + "dir/main_test.go", + "dir/main2_test.go", }, }, after: filesystemState{ - dirs: []fsPath{ - {"dir"}, + dirs: []string{ + "dir", }, - files: []fsPath{ - {"dir", "main.go"}, - {"dir", "main2.go"}, + files: []string{ + "dir/main.go", + "dir/main2.go", }, }, }, @@ -309,16 +390,21 @@ func TestPruneGoTestFiles(t *testing.T) { tc.fs.before.root = baseDir tc.fs.after.root = baseDir - tc.fs.before.setup(t) + tc.fs.setup(t) + + fs, err := deriveFilesystemState(baseDir) + if err != nil { + t.Fatal(err) + } - err := pruneGoTestFiles(baseDir, logger) + err = pruneGoTestFiles(fs, logger) if tc.err && err == nil { - t.Errorf("expected an error, got nil") + t.Fatalf("expected an error, got nil") } else if !tc.err && err != nil { - t.Errorf("unexpected error: %s", err) + t.Fatalf("unexpected error: %s", err) } - tc.fs.after.assert(t) + tc.fs.assert(t) }) } } diff --git a/internal/gps/strip_vendor_nonwindows_test.go b/internal/gps/strip_vendor_nonwindows_test.go index a8eb9ae928..bb00d5c4ce 100644 --- a/internal/gps/strip_vendor_nonwindows_test.go +++ b/internal/gps/strip_vendor_nonwindows_test.go @@ -11,46 +11,46 @@ import "testing" func TestStripVendorSymlinks(t *testing.T) { t.Run("vendor symlink", stripVendorTestCase(fsTestCase{ before: filesystemState{ - dirs: []fsPath{ - {"package"}, - {"package", "_vendor"}, + dirs: []string{ + "package", + "package/_vendor", }, links: []fsLink{ { - path: fsPath{"package", "vendor"}, + path: "package/vendor", to: "_vendor", }, }, }, after: filesystemState{ - dirs: []fsPath{ - {"package"}, - {"package", "_vendor"}, + dirs: []string{ + "package", + "package/_vendor", }, }, })) t.Run("nonvendor symlink", stripVendorTestCase(fsTestCase{ before: filesystemState{ - dirs: []fsPath{ - {"package"}, - {"package", "_vendor"}, + dirs: []string{ + "package", + "package/_vendor", }, links: []fsLink{ { - path: fsPath{"package", "link"}, + path: "package/link", to: "_vendor", }, }, }, after: filesystemState{ - dirs: []fsPath{ - {"package"}, - {"package", "_vendor"}, + dirs: []string{ + "package", + "package/_vendor", }, links: []fsLink{ { - path: fsPath{"package", "link"}, + path: "package/link", to: "_vendor", }, }, @@ -59,23 +59,23 @@ func TestStripVendorSymlinks(t *testing.T) { t.Run("vendor symlink to file", stripVendorTestCase(fsTestCase{ before: filesystemState{ - files: []fsPath{ - {"file"}, + files: []string{ + "file", }, links: []fsLink{ { - path: fsPath{"vendor"}, + path: "vendor", to: "file", }, }, }, after: filesystemState{ - files: []fsPath{ - {"file"}, + files: []string{ + "file", }, links: []fsLink{ { - path: fsPath{"vendor"}, + path: "vendor", to: "file", }, }, @@ -84,27 +84,27 @@ func TestStripVendorSymlinks(t *testing.T) { t.Run("chained symlinks", stripVendorTestCase(fsTestCase{ before: filesystemState{ - dirs: []fsPath{ - {"_vendor"}, + dirs: []string{ + "_vendor", }, links: []fsLink{ { - path: fsPath{"vendor"}, + path: "vendor", to: "vendor2", }, { - path: fsPath{"vendor2"}, + path: "vendor2", to: "_vendor", }, }, }, after: filesystemState{ - dirs: []fsPath{ - {"_vendor"}, + dirs: []string{ + "_vendor", }, links: []fsLink{ { - path: fsPath{"vendor2"}, + path: "vendor2", to: "_vendor", }, }, @@ -113,31 +113,31 @@ func TestStripVendorSymlinks(t *testing.T) { t.Run("circular symlinks", stripVendorTestCase(fsTestCase{ before: filesystemState{ - dirs: []fsPath{ - {"package"}, + dirs: []string{ + "package", }, links: []fsLink{ { - path: fsPath{"package", "link1"}, + path: "package/link1", to: "link2", }, { - path: fsPath{"package", "link2"}, + path: "package/link2", to: "link1", }, }, }, after: filesystemState{ - dirs: []fsPath{ - {"package"}, + dirs: []string{ + "package", }, links: []fsLink{ { - path: fsPath{"package", "link1"}, + path: "package/link1", to: "link2", }, { - path: fsPath{"package", "link2"}, + path: "package/link2", to: "link1", }, }, diff --git a/internal/gps/strip_vendor_test.go b/internal/gps/strip_vendor_test.go index 18a722cd9c..4dba601715 100644 --- a/internal/gps/strip_vendor_test.go +++ b/internal/gps/strip_vendor_test.go @@ -22,49 +22,50 @@ func stripVendorTestCase(tc fsTestCase) func(*testing.T) { t.Errorf("os.RemoveAll(%q) err=%q", tempDir, err) } }() + tc.before.root = tempDir tc.after.root = tempDir - tc.before.setup(t) + tc.setup(t) if err := filepath.Walk(tempDir, stripVendor); err != nil { t.Errorf("filepath.Walk err=%q", err) } - tc.after.assert(t) + tc.assert(t) } } func TestStripVendorDirectory(t *testing.T) { t.Run("vendor directory", stripVendorTestCase(fsTestCase{ before: filesystemState{ - dirs: []fsPath{ - {"package"}, - {"package", "vendor"}, + dirs: []string{ + "package", + "package/vendor", }, }, after: filesystemState{ - dirs: []fsPath{ - {"package"}, + dirs: []string{ + "package", }, }, })) t.Run("vendor file", stripVendorTestCase(fsTestCase{ before: filesystemState{ - dirs: []fsPath{ - {"package"}, + dirs: []string{ + "package", }, - files: []fsPath{ - {"package", "vendor"}, + files: []string{ + "package/vendor", }, }, after: filesystemState{ - dirs: []fsPath{ - {"package"}, + dirs: []string{ + "package", }, - files: []fsPath{ - {"package", "vendor"}, + files: []string{ + "package/vendor", }, }, })) diff --git a/internal/gps/strip_vendor_windows_test.go b/internal/gps/strip_vendor_windows_test.go index 7569a48741..c1948c1a0e 100644 --- a/internal/gps/strip_vendor_windows_test.go +++ b/internal/gps/strip_vendor_windows_test.go @@ -13,25 +13,25 @@ func TestStripVendorSymlinks(t *testing.T) { // they're too hard to distinguish from junctions. t.Run("vendor symlink", stripVendorTestCase(fsTestCase{ before: filesystemState{ - dirs: []fsPath{ - {"package"}, - {"package", "_vendor"}, + dirs: []string{ + "package", + "package/_vendor", }, links: []fsLink{ { - path: fsPath{"package", "vendor"}, + path: "package/vendor", to: "_vendor", }, }, }, after: filesystemState{ - dirs: []fsPath{ - {"package"}, - {"package", "_vendor"}, + dirs: []string{ + "package", + "package/_vendor", }, links: []fsLink{ { - path: fsPath{"package", "vendor"}, + path: "package/vendor", to: "_vendor", }, }, @@ -40,25 +40,25 @@ func TestStripVendorSymlinks(t *testing.T) { t.Run("nonvendor symlink", stripVendorTestCase(fsTestCase{ before: filesystemState{ - dirs: []fsPath{ - {"package"}, - {"package", "_vendor"}, + dirs: []string{ + "package", + "package/_vendor", }, links: []fsLink{ { - path: fsPath{"package", "link"}, + path: "package/link", to: "_vendor", }, }, }, after: filesystemState{ - dirs: []fsPath{ - {"package"}, - {"package", "_vendor"}, + dirs: []string{ + "package", + "package/_vendor", }, links: []fsLink{ { - path: fsPath{"package", "link"}, + path: "package/link", to: "_vendor", }, }, @@ -67,23 +67,23 @@ func TestStripVendorSymlinks(t *testing.T) { t.Run("vendor symlink to file", stripVendorTestCase(fsTestCase{ before: filesystemState{ - files: []fsPath{ - {"file"}, + files: []string{ + "file", }, links: []fsLink{ { - path: fsPath{"vendor"}, + path: "vendor", to: "file", }, }, }, after: filesystemState{ - files: []fsPath{ - {"file"}, + files: []string{ + "file", }, links: []fsLink{ { - path: fsPath{"vendor"}, + path: "vendor", to: "file", }, }, @@ -96,27 +96,27 @@ func TestStripVendorSymlinks(t *testing.T) { // symlink, because the first symlink doesn't appear to Go to be a // directory. before: filesystemState{ - dirs: []fsPath{ - {"_vendor"}, + dirs: []string{ + "_vendor", }, links: []fsLink{ { - path: fsPath{"vendor"}, + path: "vendor", to: "vendor2", }, { - path: fsPath{"vendor2"}, + path: "vendor2", to: "_vendor", }, }, }, after: filesystemState{ - dirs: []fsPath{ - {"_vendor"}, + dirs: []string{ + "_vendor", }, links: []fsLink{ { - path: fsPath{"vendor2"}, + path: "vendor2", to: "_vendor", }, }, @@ -125,31 +125,31 @@ func TestStripVendorSymlinks(t *testing.T) { t.Run("circular symlinks", stripVendorTestCase(fsTestCase{ before: filesystemState{ - dirs: []fsPath{ - {"package"}, + dirs: []string{ + "package", }, links: []fsLink{ { - path: fsPath{"package", "link1"}, + path: "package/link1", to: "link2", }, { - path: fsPath{"package", "link2"}, + path: "package/link2", to: "link1", }, }, }, after: filesystemState{ - dirs: []fsPath{ - {"package"}, + dirs: []string{ + "package", }, links: []fsLink{ { - path: fsPath{"package", "link1"}, + path: "package/link1", to: "link2", }, { - path: fsPath{"package", "link2"}, + path: "package/link2", to: "link1", }, },