From 9f66065237be7a80eb925e43ba9c613f7b3550ea Mon Sep 17 00:00:00 2001 From: Michael Eischer Date: Fri, 12 Jul 2024 21:22:37 +0200 Subject: [PATCH] ls: fix handling of toplevel directories in ncdu output --- cmd/restic/cmd_ls.go | 4 ++-- cmd/restic/cmd_ls_integration_test.go | 11 ++++++----- cmd/restic/cmd_ls_test.go | 3 ++- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/cmd/restic/cmd_ls.go b/cmd/restic/cmd_ls.go index d9a3b0fb8..13bc50406 100644 --- a/cmd/restic/cmd_ls.go +++ b/cmd/restic/cmd_ls.go @@ -178,7 +178,7 @@ func (p *ncduLsPrinter) Snapshot(sn *restic.Snapshot) { Warnf("JSON encode failed: %v\n", err) } p.depth++ - fmt.Fprintf(p.out, "[%d, %d, %s", NcduMajorVer, NcduMinorVer, string(snapshotBytes)) + fmt.Fprintf(p.out, "[%d, %d, %s, [{\"name\":\"\"}", NcduMajorVer, NcduMinorVer, string(snapshotBytes)) } func lsNcduNode(_ string, node *restic.Node) ([]byte, error) { @@ -246,7 +246,7 @@ func (p *ncduLsPrinter) LeaveDir(_ string) { } func (p *ncduLsPrinter) Close() { - fmt.Fprint(p.out, "\n]\n") + fmt.Fprint(p.out, "\n]\n]\n") } type textLsPrinter struct { diff --git a/cmd/restic/cmd_ls_integration_test.go b/cmd/restic/cmd_ls_integration_test.go index 2b742d1b2..f5655bdff 100644 --- a/cmd/restic/cmd_ls_integration_test.go +++ b/cmd/restic/cmd_ls_integration_test.go @@ -3,7 +3,6 @@ package main import ( "context" "encoding/json" - "path/filepath" "strings" "testing" @@ -26,9 +25,10 @@ func testRunLs(t testing.TB, gopts GlobalOptions, snapshotID string) []string { func assertIsValidJSON(t *testing.T, data []byte) { // Sanity check: output must be valid JSON. - var v interface{} + var v []any err := json.Unmarshal(data, &v) rtest.OK(t, err) + rtest.Assert(t, len(v) == 4, "invalid ncdu output, expected 4 array elements, got %v", len(v)) } func TestRunLsNcdu(t *testing.T) { @@ -37,12 +37,13 @@ func TestRunLsNcdu(t *testing.T) { testSetupBackupData(t, env) opts := BackupOptions{} - testRunBackup(t, filepath.Dir(env.testdata), []string{"testdata"}, opts, env.gopts) + // backup such that there are multiple toplevel elements + testRunBackup(t, env.testdata+"/0", []string{"."}, opts, env.gopts) for _, paths := range [][]string{ {"latest"}, - {"latest", "/testdata"}, - {"latest", "/testdata/0", "/testdata/0/tests"}, + {"latest", "/0"}, + {"latest", "/0", "/0/9"}, } { ncdu := testRunLsWithOpts(t, env.gopts, LsOptions{Ncdu: true}, paths) assertIsValidJSON(t, ncdu) diff --git a/cmd/restic/cmd_ls_test.go b/cmd/restic/cmd_ls_test.go index 194975053..8523f702f 100644 --- a/cmd/restic/cmd_ls_test.go +++ b/cmd/restic/cmd_ls_test.go @@ -149,11 +149,12 @@ func TestLsNcdu(t *testing.T) { printer.LeaveDir("/directory") printer.Close() - rtest.Equals(t, `[1, 2, {"time":"0001-01-01T00:00:00Z","tree":null,"paths":["/example"],"hostname":"host"}, + rtest.Equals(t, `[1, 2, {"time":"0001-01-01T00:00:00Z","tree":null,"paths":["/example"],"hostname":"host"}, [{"name":""}, [ {"name":"directory","asize":0,"dsize":0,"dev":0,"ino":0,"nlink":0,"notreg":false,"uid":0,"gid":0,"mode":0,"mtime":-62135596800}, {"name":"data","asize":42,"dsize":512,"dev":0,"ino":0,"nlink":0,"notreg":false,"uid":0,"gid":0,"mode":0,"mtime":-62135596800} ] ] +] `, buf.String()) }