|
| 1 | +package list |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/cli/cli/v2/internal/config" |
| 8 | + "github.com/cli/cli/v2/pkg/cmdutil" |
| 9 | + "github.com/cli/cli/v2/pkg/iostreams" |
| 10 | + "github.com/google/shlex" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func TestNewCmdConfigList(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + input string |
| 18 | + output ListOptions |
| 19 | + wantsErr bool |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "no arguments", |
| 23 | + input: "", |
| 24 | + output: ListOptions{}, |
| 25 | + wantsErr: false, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "list with host", |
| 29 | + input: "--host HOST.com", |
| 30 | + output: ListOptions{Hostname: "HOST.com"}, |
| 31 | + wantsErr: false, |
| 32 | + }, |
| 33 | + } |
| 34 | + |
| 35 | + for _, tt := range tests { |
| 36 | + t.Run(tt.name, func(t *testing.T) { |
| 37 | + f := &cmdutil.Factory{ |
| 38 | + Config: func() (config.Config, error) { |
| 39 | + return config.ConfigStub{}, nil |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + argv, err := shlex.Split(tt.input) |
| 44 | + assert.NoError(t, err) |
| 45 | + |
| 46 | + var gotOpts *ListOptions |
| 47 | + cmd := NewCmdConfigList(f, func(opts *ListOptions) error { |
| 48 | + gotOpts = opts |
| 49 | + return nil |
| 50 | + }) |
| 51 | + cmd.Flags().BoolP("help", "x", false, "") |
| 52 | + |
| 53 | + cmd.SetArgs(argv) |
| 54 | + cmd.SetIn(&bytes.Buffer{}) |
| 55 | + cmd.SetOut(&bytes.Buffer{}) |
| 56 | + cmd.SetErr(&bytes.Buffer{}) |
| 57 | + |
| 58 | + _, err = cmd.ExecuteC() |
| 59 | + if tt.wantsErr { |
| 60 | + assert.Error(t, err) |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + assert.NoError(t, err) |
| 65 | + assert.Equal(t, tt.output.Hostname, gotOpts.Hostname) |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func Test_listRun(t *testing.T) { |
| 71 | + tests := []struct { |
| 72 | + name string |
| 73 | + input *ListOptions |
| 74 | + config config.ConfigStub |
| 75 | + stdout string |
| 76 | + wantErr bool |
| 77 | + }{ |
| 78 | + { |
| 79 | + name: "list", |
| 80 | + config: config.ConfigStub{ |
| 81 | + "HOST:git_protocol": "ssh", |
| 82 | + "HOST:editor": "/usr/bin/vim", |
| 83 | + "HOST:prompt": "disabled", |
| 84 | + "HOST:pager": "less", |
| 85 | + "HOST:http_unix_socket": "", |
| 86 | + "HOST:browser": "brave", |
| 87 | + }, |
| 88 | + input: &ListOptions{Hostname: "HOST"}, // ConfigStub gives empty DefaultHost |
| 89 | + stdout: `git_protocol=ssh |
| 90 | +editor=/usr/bin/vim |
| 91 | +prompt=disabled |
| 92 | +pager=less |
| 93 | +http_unix_socket= |
| 94 | +browser=brave |
| 95 | +`, |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + for _, tt := range tests { |
| 100 | + io, _, stdout, _ := iostreams.Test() |
| 101 | + tt.input.IO = io |
| 102 | + tt.input.Config = func() (config.Config, error) { |
| 103 | + return tt.config, nil |
| 104 | + } |
| 105 | + |
| 106 | + t.Run(tt.name, func(t *testing.T) { |
| 107 | + err := listRun(tt.input) |
| 108 | + assert.NoError(t, err) |
| 109 | + assert.Equal(t, tt.stdout, stdout.String()) |
| 110 | + //assert.Equal(t, tt.stderr, stderr.String()) |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
0 commit comments