Skip to content

Commit c894587

Browse files
committed
added function tests
1 parent 3001afd commit c894587

File tree

2 files changed

+63
-33
lines changed

2 files changed

+63
-33
lines changed

pkg/cmd/repo/rename/rename.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ import (
1717
)
1818

1919
type RenameOptions struct {
20-
HttpClient func() (*http.Client, error)
21-
IO *iostreams.IOStreams
22-
Config func() (config.Config, error)
23-
RepoName []string
20+
HttpClient func() (*http.Client, error)
21+
IO *iostreams.IOStreams
22+
Config func() (config.Config, error)
23+
oldRepoName string
24+
newRepoName string
2425
}
2526

2627
type renameRepo struct {
@@ -43,7 +44,8 @@ func NewCmdRename(f *cmdutil.Factory, runf func(*RenameOptions) error) *cobra.Co
4344
Long: "Rename a GitHub repository",
4445
Args: cmdutil.ExactArgs(2, "cannot rename: repository argument required"),
4546
RunE: func(cmd *cobra.Command, args []string) error {
46-
opts.RepoName = append(opts.RepoName, args[0], args[1])
47+
opts.oldRepoName = args[0]
48+
opts.newRepoName = args[1]
4749
if runf != nil {
4850
return runf(opts)
4951
}
@@ -66,11 +68,11 @@ func renameRun(opts *RenameOptions) error {
6668
return err
6769
}
6870

69-
oldRepoName := opts.RepoName[0]
71+
oldRepoName := opts.oldRepoName
7072
if !strings.Contains(oldRepoName, "/") {
7173
oldRepoName = currentUser + "/" + oldRepoName
7274
}
73-
newRepoName := opts.RepoName[1]
75+
newRepoName := opts.newRepoName
7476

7577
repo, err := ghrepo.FromFullName(oldRepoName)
7678
if err != nil {

pkg/cmd/repo/rename/rename_test.go

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,67 @@ package rename
22

33
import (
44
"testing"
5+
6+
"github.com/cli/cli/v2/pkg/cmdutil"
7+
"github.com/cli/cli/v2/pkg/iostreams"
8+
"github.com/google/shlex"
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
511
)
612

7-
func TestNewCmdRename(t * testing.T) {
8-
tests := []struct {
9-
name string
10-
tty bool
11-
input string
12-
output RenameOptions
13-
wantErr bool
14-
errMsg string
13+
func TestNewCmdRename(t *testing.T) {
14+
testCases := []struct {
15+
name string
16+
args string
17+
wantOpts RenameOptions
18+
wantErr string
1519
}{
1620
{
17-
name: "no argument",
18-
tty: true,
19-
input: "",
20-
output: RenameOptions{},
21-
},
22-
{
23-
name: "argument",
24-
tty: true,
25-
input: "cli/cli",
26-
output: RenameOptions{
27-
DestArg: "cli comand-line-interface",
28-
},
21+
name: "no arguments",
22+
args: "",
23+
wantErr: "cannot rename: repository argument required",
2924
},
3025
{
31-
name: "incorrect argument",
32-
tty: true,
33-
input: "",
34-
output: RenameOptions{
35-
DestArg: "cli ",
26+
name: "correct argument",
27+
args: "OWNER/REPO REPOS",
28+
wantOpts: RenameOptions{
29+
oldRepoName: "OWNER/REPO",
30+
newRepoName: "REPOS",
3631
},
3732
},
3833
}
39-
}
34+
for _, tt := range testCases {
35+
t.Run(tt.name, func(t *testing.T) {
36+
io, stdin, stdout, stderr := iostreams.Test()
37+
fac := &cmdutil.Factory{IOStreams: io}
38+
39+
var opts *RenameOptions
40+
cmd := NewCmdRename(fac, func(co *RenameOptions) error {
41+
opts = co
42+
return nil
43+
})
44+
45+
argv, err := shlex.Split(tt.args)
46+
require.NoError(t, err)
47+
cmd.SetArgs(argv)
4048

49+
cmd.SetIn(stdin)
50+
cmd.SetOut(stdout)
51+
cmd.SetErr(stderr)
52+
53+
_, err = cmd.ExecuteC()
54+
if tt.wantErr != "" {
55+
assert.EqualError(t, err, tt.wantErr)
56+
return
57+
} else {
58+
assert.NoError(t, err)
59+
}
60+
61+
assert.Equal(t, "", stdout.String())
62+
assert.Equal(t, "", stderr.String())
63+
64+
assert.Equal(t, tt.wantOpts.oldRepoName, opts.oldRepoName)
65+
assert.Equal(t, tt.wantOpts.newRepoName, opts.newRepoName)
66+
})
67+
}
68+
}

0 commit comments

Comments
 (0)