2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-22 12:55:18 +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)
}
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 {

View File

@ -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)

View File

@ -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())
}