Add tests for path preparation
This commit is contained in:
parent
629fc20b44
commit
155383b1c7
2 changed files with 50 additions and 18 deletions
|
@ -74,19 +74,26 @@ func splitPath(p string) []string {
|
||||||
return append(s, f)
|
return append(s, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
func cleanupPathList(pathComponentsList [][]string) [][]string {
|
func preparePathList(pathComponentsList []string) [][]string {
|
||||||
// Build a set of paths for quick lookup
|
|
||||||
pathSet := make(map[string]struct{})
|
pathSet := make(map[string]struct{})
|
||||||
|
|
||||||
for _, splittedPath := range pathComponentsList {
|
for _, p := range pathComponentsList {
|
||||||
|
p = filepath.Clean(p)
|
||||||
|
splittedPath := splitPath(p)
|
||||||
|
if len(splittedPath) == 1 && splittedPath[0] == "" {
|
||||||
|
return [][]string{{""}}
|
||||||
|
}
|
||||||
|
|
||||||
pathStr := path.Join(splittedPath...)
|
pathStr := path.Join(splittedPath...)
|
||||||
pathSet[pathStr] = struct{}{}
|
pathSet[pathStr] = struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter out subpaths that are covered by parent directories
|
// Use a set to store the filtered paths
|
||||||
filteredList := [][]string{}
|
filteredSet := make(map[string][]string)
|
||||||
|
|
||||||
|
for _, p := range pathComponentsList {
|
||||||
|
splittedPath := splitPath(path.Clean(p))
|
||||||
|
|
||||||
for _, splittedPath := range pathComponentsList {
|
|
||||||
isCovered := false
|
isCovered := false
|
||||||
// Check if any prefix of the path exists in the pathSet
|
// Check if any prefix of the path exists in the pathSet
|
||||||
for i := len(splittedPath) - 1; i >= 1; i-- {
|
for i := len(splittedPath) - 1; i >= 1; i-- {
|
||||||
|
@ -97,10 +104,17 @@ func cleanupPathList(pathComponentsList [][]string) [][]string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !isCovered {
|
if !isCovered {
|
||||||
filteredList = append(filteredList, splittedPath)
|
pathStr := path.Join(splittedPath...)
|
||||||
|
filteredSet[pathStr] = splittedPath
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convert the values of the set to a list
|
||||||
|
filteredList := make([][]string, 0, len(filteredSet))
|
||||||
|
for _, splittedPath := range filteredSet {
|
||||||
|
filteredList = append(filteredList, splittedPath)
|
||||||
|
}
|
||||||
|
|
||||||
return filteredList
|
return filteredList
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -263,17 +277,7 @@ func runDump(ctx context.Context, opts DumpOptions, gopts GlobalOptions, args []
|
||||||
d := dump.New(opts.Archive, repo, outputFileWriter)
|
d := dump.New(opts.Archive, repo, outputFileWriter)
|
||||||
defer d.Close()
|
defer d.Close()
|
||||||
|
|
||||||
var splittedPathList [][]string
|
splittedPathList := preparePathList(args[1:])
|
||||||
|
|
||||||
for i := 1; i < len(args); i++ {
|
|
||||||
pathToPrint := args[i]
|
|
||||||
debug.Log("dump file %q from %q", pathToPrint, snapshotIDString)
|
|
||||||
|
|
||||||
splittedPath := splitPath(path.Clean(pathToPrint))
|
|
||||||
splittedPathList = append(splittedPathList, splittedPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
splittedPathList = cleanupPathList(splittedPathList)
|
|
||||||
|
|
||||||
err = printFromTree(ctx, tree, repo, "/", splittedPathList, d, canWriteArchiveFunc)
|
err = printFromTree(ctx, tree, repo, "/", splittedPathList, d, canWriteArchiveFunc)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -25,3 +25,31 @@ func TestDumpSplitPath(t *testing.T) {
|
||||||
rtest.Equals(t, path.result, parts)
|
rtest.Equals(t, path.result, parts)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDumpPreparePathList(t *testing.T) {
|
||||||
|
testPaths := []struct {
|
||||||
|
paths []string
|
||||||
|
result [][]string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
[]string{"test", "test/dir", "test/dir/sub"},
|
||||||
|
[][]string{{"test"}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
[]string{"/", "man", "doc", "doc/icons"},
|
||||||
|
[][]string{{""}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
[]string{"doc"},
|
||||||
|
[][]string{{"doc"}},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
[]string{"man/", "doc", "doc/icons"},
|
||||||
|
[][]string{{"man"}, {"doc"}},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, path := range testPaths {
|
||||||
|
parts := preparePathList(path.paths)
|
||||||
|
rtest.Equals(t, path.result, parts)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue