restic/cmd/restic/cmd_ls_integration_test.go

48 lines
1.2 KiB
Go
Raw Permalink Normal View History

package main
import (
"context"
2024-01-21 15:56:07 +01:00
"encoding/json"
"path/filepath"
"strings"
"testing"
rtest "github.com/restic/restic/internal/test"
)
2024-01-21 15:56:07 +01:00
func testRunLsWithOpts(t testing.TB, gopts GlobalOptions, opts LsOptions, args []string) []byte {
buf, err := withCaptureStdout(func() error {
gopts.Quiet = true
2024-01-21 15:56:07 +01:00
return runLs(context.TODO(), opts, gopts, args)
})
rtest.OK(t, err)
2024-01-21 15:56:07 +01: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 15:58:49 +01:00
func assertIsValidJSON(t *testing.T, data []byte) {
2024-01-21 15:56:07 +01:00
// Sanity check: output must be valid JSON.
var v interface{}
err := json.Unmarshal(data, &v)
rtest.OK(t, err)
}
func TestRunLsNcdu(t *testing.T) {
env, cleanup := withTestEnvironment(t)
defer cleanup()
testRunInit(t, env.gopts)
opts := BackupOptions{}
testRunBackup(t, filepath.Dir(env.testdata), []string{"testdata"}, opts, env.gopts)
ncdu := testRunLsWithOpts(t, env.gopts, LsOptions{Ncdu: true}, []string{"latest"})
2024-01-21 15:58:49 +01:00
assertIsValidJSON(t, ncdu)
2024-01-21 15:56:07 +01:00
ncdu = testRunLsWithOpts(t, env.gopts, LsOptions{Ncdu: true}, []string{"latest", "/testdata"})
2024-01-21 15:58:49 +01:00
assertIsValidJSON(t, ncdu)
}