From 155383b1c73a1379e9a5f1ae3447eae7b13de0ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Phillipp=20R=C3=B6ll?= Date: Sun, 15 Sep 2024 18:51:08 +0200 Subject: [PATCH] Add tests for path preparation --- cmd/restic/cmd_dump.go | 40 ++++++++++++++++++++----------------- cmd/restic/cmd_dump_test.go | 28 ++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 18 deletions(-) diff --git a/cmd/restic/cmd_dump.go b/cmd/restic/cmd_dump.go index ec7a4d4d3..88f3886e4 100644 --- a/cmd/restic/cmd_dump.go +++ b/cmd/restic/cmd_dump.go @@ -74,19 +74,26 @@ func splitPath(p string) []string { return append(s, f) } -func cleanupPathList(pathComponentsList [][]string) [][]string { - // Build a set of paths for quick lookup +func preparePathList(pathComponentsList []string) [][]string { 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...) pathSet[pathStr] = struct{}{} } - // Filter out subpaths that are covered by parent directories - filteredList := [][]string{} + // Use a set to store the filtered paths + filteredSet := make(map[string][]string) + + for _, p := range pathComponentsList { + splittedPath := splitPath(path.Clean(p)) - for _, splittedPath := range pathComponentsList { isCovered := false // Check if any prefix of the path exists in the pathSet for i := len(splittedPath) - 1; i >= 1; i-- { @@ -97,10 +104,17 @@ func cleanupPathList(pathComponentsList [][]string) [][]string { } } 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 } @@ -263,17 +277,7 @@ func runDump(ctx context.Context, opts DumpOptions, gopts GlobalOptions, args [] d := dump.New(opts.Archive, repo, outputFileWriter) defer d.Close() - var splittedPathList [][]string - - 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) + splittedPathList := preparePathList(args[1:]) err = printFromTree(ctx, tree, repo, "/", splittedPathList, d, canWriteArchiveFunc) if err != nil { diff --git a/cmd/restic/cmd_dump_test.go b/cmd/restic/cmd_dump_test.go index aa43117ee..1e6a46922 100644 --- a/cmd/restic/cmd_dump_test.go +++ b/cmd/restic/cmd_dump_test.go @@ -25,3 +25,31 @@ func TestDumpSplitPath(t *testing.T) { 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) + } +}