github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/pkg/cmd/gist/clone/clone_test.go (about)

     1  package clone
     2  
     3  import (
     4  	"net/http"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/cli/cli/internal/config"
     9  	"github.com/cli/cli/internal/run"
    10  	"github.com/cli/cli/pkg/cmdutil"
    11  	"github.com/cli/cli/pkg/httpmock"
    12  	"github.com/cli/cli/pkg/iostreams"
    13  	"github.com/cli/cli/test"
    14  	"github.com/google/shlex"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func runCloneCommand(httpClient *http.Client, cli string) (*test.CmdOut, error) {
    19  	io, stdin, stdout, stderr := iostreams.Test()
    20  	fac := &cmdutil.Factory{
    21  		IOStreams: io,
    22  		HttpClient: func() (*http.Client, error) {
    23  			return httpClient, nil
    24  		},
    25  		Config: func() (config.Config, error) {
    26  			return config.NewBlankConfig(), nil
    27  		},
    28  	}
    29  
    30  	cmd := NewCmdClone(fac, nil)
    31  
    32  	argv, err := shlex.Split(cli)
    33  	cmd.SetArgs(argv)
    34  
    35  	cmd.SetIn(stdin)
    36  	cmd.SetOut(stdout)
    37  	cmd.SetErr(stderr)
    38  
    39  	if err != nil {
    40  		panic(err)
    41  	}
    42  
    43  	_, err = cmd.ExecuteC()
    44  
    45  	if err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	return &test.CmdOut{OutBuf: stdout, ErrBuf: stderr}, nil
    50  }
    51  
    52  func Test_GistClone(t *testing.T) {
    53  	tests := []struct {
    54  		name string
    55  		args string
    56  		want string
    57  	}{
    58  		{
    59  			name: "shorthand",
    60  			args: "GIST",
    61  			want: "git clone https://gist.github.com/GIST.git",
    62  		},
    63  		{
    64  			name: "shorthand with directory",
    65  			args: "GIST target_directory",
    66  			want: "git clone https://gist.github.com/GIST.git target_directory",
    67  		},
    68  		{
    69  			name: "clone arguments",
    70  			args: "GIST -- -o upstream --depth 1",
    71  			want: "git clone -o upstream --depth 1 https://gist.github.com/GIST.git",
    72  		},
    73  		{
    74  			name: "clone arguments with directory",
    75  			args: "GIST target_directory -- -o upstream --depth 1",
    76  			want: "git clone -o upstream --depth 1 https://gist.github.com/GIST.git target_directory",
    77  		},
    78  		{
    79  			name: "HTTPS URL",
    80  			args: "https://gist.github.com/OWNER/GIST",
    81  			want: "git clone https://gist.github.com/OWNER/GIST",
    82  		},
    83  		{
    84  			name: "SSH URL",
    85  			args: "git@gist.github.com:GIST.git",
    86  			want: "git clone git@gist.github.com:GIST.git",
    87  		},
    88  	}
    89  	for _, tt := range tests {
    90  		t.Run(tt.name, func(t *testing.T) {
    91  			reg := &httpmock.Registry{}
    92  			defer reg.Verify(t)
    93  
    94  			httpClient := &http.Client{Transport: reg}
    95  
    96  			cs, restore := run.Stub()
    97  			defer restore(t)
    98  			cs.Register(`git clone`, 0, "", func(s []string) {
    99  				assert.Equal(t, tt.want, strings.Join(s, " "))
   100  			})
   101  
   102  			output, err := runCloneCommand(httpClient, tt.args)
   103  			if err != nil {
   104  				t.Fatalf("error running command `gist clone`: %v", err)
   105  			}
   106  
   107  			assert.Equal(t, "", output.String())
   108  			assert.Equal(t, "", output.Stderr())
   109  		})
   110  	}
   111  }
   112  
   113  func Test_GistClone_flagError(t *testing.T) {
   114  	_, err := runCloneCommand(nil, "--depth 1 GIST")
   115  	if err == nil || err.Error() != "unknown flag: --depth\nSeparate git clone flags with '--'." {
   116  		t.Errorf("unexpected error %v", err)
   117  	}
   118  }