2023-05-06 08:37:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-01-21 14:56:07 +00:00
|
|
|
"encoding/json"
|
2023-05-06 08:37:17 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
rtest "github.com/restic/restic/internal/test"
|
|
|
|
)
|
|
|
|
|
2024-01-21 14:56:07 +00:00
|
|
|
func testRunLsWithOpts(t testing.TB, gopts GlobalOptions, opts LsOptions, args []string) []byte {
|
2023-05-07 20:06:39 +00:00
|
|
|
buf, err := withCaptureStdout(func() error {
|
2023-05-07 20:07:47 +00:00
|
|
|
gopts.Quiet = true
|
2024-01-21 14:56:07 +00:00
|
|
|
return runLs(context.TODO(), opts, gopts, args)
|
2023-05-07 20:06:39 +00:00
|
|
|
})
|
|
|
|
rtest.OK(t, err)
|
2024-01-21 14:56:07 +00:00
|
|
|
return buf.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRunLs(t testing.TB, gopts GlobalOptions, snapshotID string) []string {
|
|
|
|
out := testRunLsWithOpts(t, gopts, LsOptions{}, []string{snapshotID})
|
|
|
|
return strings.Split(string(out), "\n")
|
|
|
|
}
|
|
|
|
|
2024-01-21 14:58:49 +00:00
|
|
|
func assertIsValidJSON(t *testing.T, data []byte) {
|
2024-01-21 14:56:07 +00:00
|
|
|
// Sanity check: output must be valid JSON.
|
2024-07-12 19:22:37 +00:00
|
|
|
var v []any
|
2024-01-21 14:56:07 +00:00
|
|
|
err := json.Unmarshal(data, &v)
|
|
|
|
rtest.OK(t, err)
|
2024-07-12 19:22:37 +00:00
|
|
|
rtest.Assert(t, len(v) == 4, "invalid ncdu output, expected 4 array elements, got %v", len(v))
|
2024-01-21 14:56:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRunLsNcdu(t *testing.T) {
|
|
|
|
env, cleanup := withTestEnvironment(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
2024-07-07 11:28:56 +00:00
|
|
|
testSetupBackupData(t, env)
|
2024-01-21 14:56:07 +00:00
|
|
|
opts := BackupOptions{}
|
2024-07-12 19:22:37 +00:00
|
|
|
// backup such that there are multiple toplevel elements
|
|
|
|
testRunBackup(t, env.testdata+"/0", []string{"."}, opts, env.gopts)
|
2024-01-21 14:56:07 +00:00
|
|
|
|
2024-07-07 11:28:56 +00:00
|
|
|
for _, paths := range [][]string{
|
|
|
|
{"latest"},
|
2024-07-12 19:22:37 +00:00
|
|
|
{"latest", "/0"},
|
|
|
|
{"latest", "/0", "/0/9"},
|
2024-07-07 11:28:56 +00:00
|
|
|
} {
|
|
|
|
ncdu := testRunLsWithOpts(t, env.gopts, LsOptions{Ncdu: true}, paths)
|
|
|
|
assertIsValidJSON(t, ncdu)
|
|
|
|
}
|
2023-05-06 08:37:17 +00:00
|
|
|
}
|