From a9c5a993c89a4ff549fe9fd4b490eaee5cce52a7 Mon Sep 17 00:00:00 2001 From: NickIAm Date: Fri, 4 Aug 2023 03:36:10 +1000 Subject: [PATCH] fix(cli): prevent duplicate snapshots when --all and a source path are given (#3067) Adds a check for snapshot create when --all and a source path are given simultaneously. Returns an error in this case since it would otherwise create a duplicate snapshot for the specified source path. --- Co-authored-by: Julio Lopez <1953782+julio-lopez@users.noreply.github.com> --- cli/command_snapshot_create.go | 6 +++++- tests/end_to_end_test/snapshot_create_test.go | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/cli/command_snapshot_create.go b/cli/command_snapshot_create.go index 96f7b25d5..395a250a4 100644 --- a/cli/command_snapshot_create.go +++ b/cli/command_snapshot_create.go @@ -55,7 +55,7 @@ func (c *commandSnapshotCreate) setup(svc appServices, parent commandParent) { cmd := parent.Command("create", "Creates a snapshot of local directory or file.") cmd.Arg("source", "Files or directories to create snapshot(s) of.").StringsVar(&c.snapshotCreateSources) - cmd.Flag("all", "Create snapshots for files or directories previously backed up by this user on this computer").BoolVar(&c.snapshotCreateAll) + cmd.Flag("all", "Create snapshots for files or directories previously backed up by this user on this computer. Cannot be used when a source path argument is also specified.").BoolVar(&c.snapshotCreateAll) cmd.Flag("upload-limit-mb", "Stop the backup process after the specified amount of data (in MB) has been uploaded.").PlaceHolder("MB").Default("0").Int64Var(&c.snapshotCreateCheckpointUploadLimitMB) cmd.Flag("checkpoint-interval", "Interval between periodic checkpoints (must be <= 45 minutes).").Hidden().DurationVar(&c.snapshotCreateCheckpointInterval) cmd.Flag("description", "Free-form snapshot description.").StringVar(&c.snapshotCreateDescription) @@ -89,6 +89,10 @@ func (c *commandSnapshotCreate) setup(svc appServices, parent commandParent) { func (c *commandSnapshotCreate) run(ctx context.Context, rep repo.RepositoryWriter) error { sources := c.snapshotCreateSources + if c.snapshotCreateAll && len(sources) > 0 { + return errors.New("cannot use --all when a source path argument is specified") + } + if err := maybeAutoUpgradeRepository(ctx, rep); err != nil { return errors.Wrap(err, "error upgrading repository") } diff --git a/tests/end_to_end_test/snapshot_create_test.go b/tests/end_to_end_test/snapshot_create_test.go index a8c4539dd..2ec4a2376 100644 --- a/tests/end_to_end_test/snapshot_create_test.go +++ b/tests/end_to_end_test/snapshot_create_test.go @@ -783,3 +783,19 @@ func TestSnapshotCreateAllSnapshotPath(t *testing.T) { require.Equal(t, "/foo/bar", si[2].Path) } } + +func TestSnapshotCreateWithAllAndPath(t *testing.T) { + t.Parallel() + + runner := testenv.NewInProcRunner(t) + e := testenv.NewCLITest(t, testenv.RepoFormatNotImportant, runner) + + defer e.RunAndExpectSuccess(t, "repo", "disconnect") + + e.RunAndExpectSuccess(t, "repo", "create", "filesystem", "--path", e.RepoDir) + + // creating a snapshot with a directory and --all should fail + e.RunAndExpectSuccess(t, "snapshot", "create", sharedTestDataDir1) + e.RunAndExpectSuccess(t, "snapshot", "create", sharedTestDataDir2) + e.RunAndExpectFailure(t, "snapshot", "create", sharedTestDataDir1, "--all") +}