From 1a3567b43263a41c346ab6a51d580d9a8b835e44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Dachary?= Date: Fri, 10 Jun 2022 13:16:02 +0200 Subject: [PATCH] cmd: restore-repo --units is repeated It is not a coma separated value. Add tests and fix the documentation. --- cmd/main_test.go | 17 ++++++++++++++++ cmd/restore_repo.go | 18 +++++++++++++---- cmd/restore_repo_test.go | 42 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 cmd/main_test.go create mode 100644 cmd/restore_repo_test.go diff --git a/cmd/main_test.go b/cmd/main_test.go new file mode 100644 index 0000000000000..3db49513eb19d --- /dev/null +++ b/cmd/main_test.go @@ -0,0 +1,17 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package cmd + +import ( + "testing" + + "code.gitea.io/gitea/models/unittest" +) + +func TestMain(m *testing.M) { + unittest.MainTest(m, &unittest.TestOptions{ + GiteaRootPath: "..", + }) +} diff --git a/cmd/restore_repo.go b/cmd/restore_repo.go index f0b01e7984c7f..b1eba62c4dc82 100644 --- a/cmd/restore_repo.go +++ b/cmd/restore_repo.go @@ -15,6 +15,17 @@ import ( "github.com/urfave/cli" ) +var defaultUnits = cli.StringSlice{ + "wiki", + "issues", + "labels", + "releases", + "release_assets", + "milestones", + "pull_requests", + "comments", +} + // CmdRestoreRepository represents the available restore a repository sub-command. var CmdRestoreRepository = cli.Command{ Name: "restore-repo", @@ -37,11 +48,10 @@ var CmdRestoreRepository = cli.Command{ Value: "", Usage: "Restore destination repository name", }, - cli.StringFlag{ + cli.StringSliceFlag{ Name: "units", - Value: "", - Usage: `Which items will be restored, one or more units should be separated as comma. -wiki, issues, labels, releases, release_assets, milestones, pull_requests, comments are allowed. Empty means all units.`, + Value: &defaultUnits, + Usage: "Which items will be restored, can be repeated", }, cli.BoolFlag{ Name: "validation", diff --git a/cmd/restore_repo_test.go b/cmd/restore_repo_test.go new file mode 100644 index 0000000000000..e2ef8da00d1e6 --- /dev/null +++ b/cmd/restore_repo_test.go @@ -0,0 +1,42 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package cmd + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/urfave/cli" +) + +func TestRestoreRepoFlags(t *testing.T) { + app := cli.NewApp() + called := false + CmdRestoreRepository.Action = func(c *cli.Context) { + assert.EqualValues(t, []string{"issues", "labels"}, c.StringSlice("units")) + called = true + } + app.Commands = []cli.Command{ + CmdRestoreRepository, + } + err := app.Run([]string{"gitea", "restore-repo", "--units", "issues", "--units", "labels"}) + assert.True(t, called, "CmdRestoreRepository.Action") + assert.NoError(t, err) +} + +func TestRestoreRepoFlagsDefaults(t *testing.T) { + app := cli.NewApp() + called := false + CmdRestoreRepository.Action = func(c *cli.Context) { + assert.EqualValues(t, ([]string)(defaultUnits), c.StringSlice("units")) + called = true + } + app.Commands = []cli.Command{ + CmdRestoreRepository, + } + err := app.Run([]string{"gitea", "restore-repo"}) + assert.True(t, called, "CmdRestoreRepository.Action") + assert.NoError(t, err) +}