From d9e22c2df171d7e1e58039245d697ec6a2086640 Mon Sep 17 00:00:00 2001 From: David Brown Date: Fri, 22 Feb 2019 21:52:30 -0700 Subject: [PATCH] Add test for `--json` support for `forget` command This adds a test of the json output of the forget command, by running it once, asking it to keep one snapshot, and verifying that the output has the right number of snapshots listed in the Keep and Remove fields of the result. --- cmd/restic/integration_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/cmd/restic/integration_test.go b/cmd/restic/integration_test.go index e47000d34..612685f53 100644 --- a/cmd/restic/integration_test.go +++ b/cmd/restic/integration_test.go @@ -219,6 +219,35 @@ func testRunForget(t testing.TB, gopts GlobalOptions, args ...string) { rtest.OK(t, runForget(opts, gopts, args)) } +func testRunForgetJSON(t testing.TB, gopts GlobalOptions, args ...string) { + buf := bytes.NewBuffer(nil) + oldJSON := gopts.JSON + gopts.stdout = buf + gopts.JSON = true + defer func() { + gopts.stdout = os.Stdout + gopts.JSON = oldJSON + }() + + opts := ForgetOptions{ + DryRun: true, + Last: 1, + } + + rtest.OK(t, runForget(opts, gopts, args)) + + var forgets []*ForgetGroup + rtest.OK(t, json.Unmarshal(buf.Bytes(), &forgets)) + + rtest.Assert(t, len(forgets) == 1, + "Expected 1 snapshot group, got %v", len(forgets)) + rtest.Assert(t, len(forgets[0].Keep) == 1, + "Expected 1 snapshot to be kept, got %v", len(forgets[0].Keep)) + rtest.Assert(t, len(forgets[0].Remove) == 2, + "Expected 2 snapshots to be removed, got %v", len(forgets[0].Remove)) + return +} + func testRunPrune(t testing.TB, gopts GlobalOptions) { rtest.OK(t, runPrune(gopts)) } @@ -1051,6 +1080,7 @@ func TestPrune(t *testing.T) { rtest.Assert(t, len(snapshotIDs) == 3, "expected 3 snapshot, got %v", snapshotIDs) + testRunForgetJSON(t, env.gopts) testRunForget(t, env.gopts, firstSnapshot[0].String()) testRunPrune(t, env.gopts) testRunCheck(t, env.gopts)