2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-26 14:56:29 +00:00

ls: fix handling of toplevel directories in ncdu output

This commit is contained in:
Michael Eischer 2024-07-12 21:22:37 +02:00
parent 375c572c4f
commit 9f66065237
3 changed files with 10 additions and 8 deletions

View File

@ -178,7 +178,7 @@ func (p *ncduLsPrinter) Snapshot(sn *restic.Snapshot) {
Warnf("JSON encode failed: %v\n", err) Warnf("JSON encode failed: %v\n", err)
} }
p.depth++ 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) { func lsNcduNode(_ string, node *restic.Node) ([]byte, error) {
@ -246,7 +246,7 @@ func (p *ncduLsPrinter) LeaveDir(_ string) {
} }
func (p *ncduLsPrinter) Close() { func (p *ncduLsPrinter) Close() {
fmt.Fprint(p.out, "\n]\n") fmt.Fprint(p.out, "\n]\n]\n")
} }
type textLsPrinter struct { type textLsPrinter struct {

View File

@ -3,7 +3,6 @@ package main
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"path/filepath"
"strings" "strings"
"testing" "testing"
@ -26,9 +25,10 @@ func testRunLs(t testing.TB, gopts GlobalOptions, snapshotID string) []string {
func assertIsValidJSON(t *testing.T, data []byte) { func assertIsValidJSON(t *testing.T, data []byte) {
// Sanity check: output must be valid JSON. // Sanity check: output must be valid JSON.
var v interface{} var v []any
err := json.Unmarshal(data, &v) err := json.Unmarshal(data, &v)
rtest.OK(t, err) 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) { func TestRunLsNcdu(t *testing.T) {
@ -37,12 +37,13 @@ func TestRunLsNcdu(t *testing.T) {
testSetupBackupData(t, env) testSetupBackupData(t, env)
opts := BackupOptions{} 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{ for _, paths := range [][]string{
{"latest"}, {"latest"},
{"latest", "/testdata"}, {"latest", "/0"},
{"latest", "/testdata/0", "/testdata/0/tests"}, {"latest", "/0", "/0/9"},
} { } {
ncdu := testRunLsWithOpts(t, env.gopts, LsOptions{Ncdu: true}, paths) ncdu := testRunLsWithOpts(t, env.gopts, LsOptions{Ncdu: true}, paths)
assertIsValidJSON(t, ncdu) assertIsValidJSON(t, ncdu)

View File

@ -149,11 +149,12 @@ func TestLsNcdu(t *testing.T) {
printer.LeaveDir("/directory") printer.LeaveDir("/directory")
printer.Close() 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":"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} {"name":"data","asize":42,"dsize":512,"dev":0,"ino":0,"nlink":0,"notreg":false,"uid":0,"gid":0,"mode":0,"mtime":-62135596800}
] ]
] ]
]
`, buf.String()) `, buf.String())
} }