added function tests

This commit is contained in:
Parth 2021-10-08 21:02:40 -04:00
parent 3001afda77
commit c894587761
2 changed files with 63 additions and 33 deletions

View file

@ -17,10 +17,11 @@ import (
)
type RenameOptions struct {
HttpClient func() (*http.Client, error)
IO *iostreams.IOStreams
Config func() (config.Config, error)
RepoName []string
HttpClient func() (*http.Client, error)
IO *iostreams.IOStreams
Config func() (config.Config, error)
oldRepoName string
newRepoName string
}
type renameRepo struct {
@ -43,7 +44,8 @@ func NewCmdRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Co
Long: "Rename a GitHub repository",
Args: cmdutil.ExactArgs(2, "cannot rename: repository argument required"),
RunE: func(cmd *cobra.Command, args []string) error {
opts.RepoName = append(opts.RepoName, args[0], args[1])
opts.oldRepoName = args[0]
opts.newRepoName = args[1]
if runf != nil {
return runf(opts)
}
@ -66,11 +68,11 @@ func renameRun(opts *RenameOptions) error {
return err
}
oldRepoName := opts.RepoName[0]
oldRepoName := opts.oldRepoName
if !strings.Contains(oldRepoName, "/") {
oldRepoName = currentUser + "/" + oldRepoName
}
newRepoName := opts.RepoName[1]
newRepoName := opts.newRepoName
repo, err := ghrepo.FromFullName(oldRepoName)
if err != nil {

View file

@ -2,39 +2,67 @@ package rename
import (
"testing"
"github.com/cli/cli/v2/pkg/cmdutil"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/google/shlex"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewCmdRename(t * testing.T) {
tests := []struct {
name string
tty bool
input string
output RenameOptions
wantErr bool
errMsg string
func TestNewCmdRename(t *testing.T) {
testCases := []struct {
name string
args string
wantOpts RenameOptions
wantErr string
}{
{
name: "no argument",
tty: true,
input: "",
output: RenameOptions{},
},
{
name: "argument",
tty: true,
input: "cli/cli",
output: RenameOptions{
DestArg: "cli comand-line-interface",
},
name: "no arguments",
args: "",
wantErr: "cannot rename: repository argument required",
},
{
name: "incorrect argument",
tty: true,
input: "",
output: RenameOptions{
DestArg: "cli ",
name: "correct argument",
args: "OWNER/REPO REPOS",
wantOpts: RenameOptions{
oldRepoName: "OWNER/REPO",
newRepoName: "REPOS",
},
},
}
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
io, stdin, stdout, stderr := iostreams.Test()
fac := &cmdutil.Factory{IOStreams: io}
var opts *RenameOptions
cmd := NewCmdRename(fac, func(co *RenameOptions) error {
opts = co
return nil
})
argv, err := shlex.Split(tt.args)
require.NoError(t, err)
cmd.SetArgs(argv)
cmd.SetIn(stdin)
cmd.SetOut(stdout)
cmd.SetErr(stderr)
_, err = cmd.ExecuteC()
if tt.wantErr != "" {
assert.EqualError(t, err, tt.wantErr)
return
} else {
assert.NoError(t, err)
}
assert.Equal(t, "", stdout.String())
assert.Equal(t, "", stderr.String())
assert.Equal(t, tt.wantOpts.oldRepoName, opts.oldRepoName)
assert.Equal(t, tt.wantOpts.newRepoName, opts.newRepoName)
})
}
}