fix: revise test cases in TestNewCmdRename in rename_test.go
This commit is contained in:
parent
9e1d34f473
commit
b705ea94a0
1 changed files with 32 additions and 20 deletions
|
|
@ -5,6 +5,7 @@ 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"
|
||||
)
|
||||
|
|
@ -19,37 +20,44 @@ func TestNewCmdRename(t *testing.T) {
|
|||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "no flags",
|
||||
input: "123",
|
||||
output: RenameOptions{
|
||||
Selector: "123",
|
||||
},
|
||||
name: "no arguments",
|
||||
input: "",
|
||||
errMsg: "not enough arguments",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "no new filename",
|
||||
input: "123 old.md",
|
||||
output: RenameOptions{
|
||||
Selector: "123",
|
||||
OldFileName: "old.md",
|
||||
},
|
||||
name: "missing old filename and new filename",
|
||||
input: "123",
|
||||
errMsg: "<oldFilename> and <newFilename> are missing",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "with both old and new filename",
|
||||
input: "123 old.md new.md",
|
||||
name: "missing new filename",
|
||||
input: "123 old.txt",
|
||||
errMsg: "<newFilename> is missing",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "rename",
|
||||
input: "123 old.txt new.txt",
|
||||
output: RenameOptions{
|
||||
Selector: "123",
|
||||
OldFileName: "old.md",
|
||||
NewFileName: "new.md",
|
||||
OldFileName: "old.txt",
|
||||
NewFileName: "new.txt",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
f := &cmdutil.Factory{}
|
||||
ios, _, _, _ := iostreams.Test()
|
||||
ios.SetStdinTTY(tt.tty)
|
||||
ios.SetStdoutTTY(tt.tty)
|
||||
f := &cmdutil.Factory{
|
||||
IOStreams: ios,
|
||||
}
|
||||
|
||||
argv, err := shlex.Split(tt.input)
|
||||
assert.NoError(t, err)
|
||||
|
||||
var gotOpts *RenameOptions
|
||||
cmd := NewCmdRename(f, func(opts *RenameOptions) error {
|
||||
gotOpts = opts
|
||||
|
|
@ -61,11 +69,15 @@ func TestNewCmdRename(t *testing.T) {
|
|||
cmd.SetErr(&bytes.Buffer{})
|
||||
|
||||
_, err = cmd.ExecuteC()
|
||||
if tt.wantErr {
|
||||
assert.EqualError(t, err, tt.errMsg)
|
||||
return
|
||||
}
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Equal(t, tt.wants.Selector, gotOpts.Selector)
|
||||
assert.Equal(t, tt.wants.OldFileName, gotOpts.OldFileName)
|
||||
assert.Equal(t, tt.wants.NewFileName, gotOpts.NewFileName)
|
||||
assert.Equal(t, tt.output.Selector, gotOpts.Selector)
|
||||
assert.Equal(t, tt.output.OldFileName, gotOpts.OldFileName)
|
||||
assert.Equal(t, tt.output.NewFileName, gotOpts.NewFileName)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue