Better tests for clone argument parsing

This commit is contained in:
Mislav Marohnić 2020-10-29 16:52:50 +01:00
parent af1d8a60e0
commit fc969281cf
2 changed files with 80 additions and 16 deletions

View file

@ -24,7 +24,6 @@ type CloneOptions struct {
IO *iostreams.IOStreams
GitArgs []string
Directory string
Repository string
}
@ -41,7 +40,7 @@ func NewCmdClone(f *cmdutil.Factory, runF func(*CloneOptions) error) *cobra.Comm
Use: "clone <repository> [<directory>] [-- <gitflags>...]",
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return &cmdutil.FlagError{Err: errors.New("cannot clone: no repository name given")}
return &cmdutil.FlagError{Err: errors.New("cannot clone: repository argument required")}
}
return nil
},

View file

@ -12,8 +12,87 @@ import (
"github.com/cli/cli/test"
"github.com/google/shlex"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewCmdClone(t *testing.T) {
testCases := []struct {
name string
args string
wantOpts CloneOptions
wantErr string
}{
{
name: "no arguments",
args: "",
wantErr: "cannot clone: repository argument required",
},
{
name: "repo argument",
args: "OWNER/REPO",
wantOpts: CloneOptions{
Repository: "OWNER/REPO",
GitArgs: []string{},
},
},
{
name: "directory argument",
args: "OWNER/REPO mydir",
wantOpts: CloneOptions{
Repository: "OWNER/REPO",
GitArgs: []string{"mydir"},
},
},
{
name: "git clone arguments",
args: "OWNER/REPO -- --depth 1 --recurse-submodules",
wantOpts: CloneOptions{
Repository: "OWNER/REPO",
GitArgs: []string{"--depth", "1", "--recurse-submodules"},
},
},
{
name: "unknown argument",
args: "OWNER/REPO --depth 1",
wantErr: "unknown flag: --depth\nSeparate git clone flags with '--'.",
},
}
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 *CloneOptions
cmd := NewCmdClone(fac, func(co *CloneOptions) 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 err != nil {
assert.Equal(t, tt.wantErr, err.Error())
return
} else if tt.wantErr != "" {
t.Errorf("expected error %q, got nil", tt.wantErr)
}
assert.Equal(t, "", stdout.String())
assert.Equal(t, "", stderr.String())
assert.Equal(t, tt.wantOpts.Repository, opts.Repository)
assert.Equal(t, tt.wantOpts.GitArgs, opts.GitArgs)
})
}
}
func runCloneCommand(httpClient *http.Client, cli string) (*test.CmdOut, error) {
io, stdin, stdout, stderr := iostreams.Test()
fac := &cmdutil.Factory{
@ -203,17 +282,3 @@ func Test_RepoClone_withoutUsername(t *testing.T) {
assert.Equal(t, 1, cs.Count)
assert.Equal(t, "git clone https://github.com/OWNER/REPO.git", strings.Join(cs.Calls[0].Args, " "))
}
func Test_RepoClone_flagError(t *testing.T) {
_, err := runCloneCommand(nil, "--depth 1 OWNER/REPO")
if err == nil || err.Error() != "unknown flag: --depth\nSeparate git clone flags with '--'." {
t.Errorf("unexpected error %v", err)
}
}
func Test_RepoClone_noArgError(t *testing.T) {
_, err := runCloneCommand(nil, "")
if err == nil || err.Error() != "cannot clone: no repository name given" {
t.Errorf("unexpected error %v", err)
}
}