File: delete_test.go

package info (click to toggle)
gh 2.46.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 15,548 kB
  • sloc: sh: 227; makefile: 117
file content (184 lines) | stat: -rw-r--r-- 3,956 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package delete

import (
	"bytes"
	"io"
	"testing"

	"github.com/MakeNowJust/heredoc"
	"github.com/cli/cli/v2/internal/config"
	"github.com/cli/cli/v2/pkg/cmdutil"
	"github.com/cli/cli/v2/pkg/iostreams"
	"github.com/google/shlex"
	"github.com/stretchr/testify/assert"
)

func TestNewCmdDelete(t *testing.T) {
	tests := []struct {
		name    string
		input   string
		output  DeleteOptions
		wantErr bool
		errMsg  string
	}{
		{
			name:    "no arguments",
			input:   "",
			wantErr: true,
			errMsg:  "specify an alias to delete or `--all`",
		},
		{
			name:  "specified alias",
			input: "co",
			output: DeleteOptions{
				Name: "co",
			},
		},
		{
			name:  "all flag",
			input: "--all",
			output: DeleteOptions{
				All: true,
			},
		},
		{
			name:    "specified alias and all flag",
			input:   "co --all",
			wantErr: true,
			errMsg:  "cannot use `--all` with alias name",
		},
		{
			name:    "too many arguments",
			input:   "il co",
			wantErr: true,
			errMsg:  "accepts at most 1 arg(s), received 2",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			ios, _, _, _ := iostreams.Test()
			f := &cmdutil.Factory{
				IOStreams: ios,
			}
			argv, err := shlex.Split(tt.input)
			assert.NoError(t, err)
			var gotOpts *DeleteOptions
			cmd := NewCmdDelete(f, func(opts *DeleteOptions) error {
				gotOpts = opts
				return nil
			})
			cmd.SetArgs(argv)
			cmd.SetIn(&bytes.Buffer{})
			cmd.SetOut(io.Discard)
			cmd.SetErr(io.Discard)
			_, err = cmd.ExecuteC()
			if tt.wantErr {
				assert.EqualError(t, err, tt.errMsg)
				return
			}
			assert.NoError(t, err)
			assert.Equal(t, tt.output.Name, gotOpts.Name)
			assert.Equal(t, tt.output.All, gotOpts.All)
		})
	}
}

func TestDeleteRun(t *testing.T) {
	tests := []struct {
		name        string
		config      string
		isTTY       bool
		opts        *DeleteOptions
		wantAliases map[string]string
		wantStdout  string
		wantStderr  string
		wantErrMsg  string
	}{
		{
			name: "delete alias",
			config: heredoc.Doc(`
				aliases:
				  il: issue list
				  co: pr checkout
			`),
			isTTY: true,
			opts: &DeleteOptions{
				Name: "co",
				All:  false,
			},
			wantAliases: map[string]string{
				"il": "issue list",
			},
			wantStderr: "āœ“ Deleted alias co; was pr checkout\n",
		},
		{
			name: "delete all aliases",
			config: heredoc.Doc(`
				aliases:
				  il: issue list
				  co: pr checkout
			`),
			isTTY: true,
			opts: &DeleteOptions{
				All: true,
			},
			wantAliases: map[string]string{},
			wantStderr:  "āœ“ Deleted alias co; was pr checkout\nāœ“ Deleted alias il; was issue list\n",
		},
		{
			name: "delete alias that does not exist",
			config: heredoc.Doc(`
				aliases:
				  il: issue list
				  co: pr checkout
			`),
			isTTY: true,
			opts: &DeleteOptions{
				Name: "unknown",
			},
			wantAliases: map[string]string{
				"il": "issue list",
				"co": "pr checkout",
			},
			wantErrMsg: "no such alias unknown",
		},
		{
			name:  "delete all aliases when none exist",
			isTTY: true,
			opts: &DeleteOptions{
				All: true,
			},
			wantAliases: map[string]string{},
			wantErrMsg:  "no aliases configured",
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			ios, _, stdout, stderr := iostreams.Test()
			ios.SetStdinTTY(tt.isTTY)
			ios.SetStdoutTTY(tt.isTTY)
			ios.SetStderrTTY(tt.isTTY)
			tt.opts.IO = ios

			cfg := config.NewFromString(tt.config)
			tt.opts.Config = func() (config.Config, error) {
				return cfg, nil
			}

			err := deleteRun(tt.opts)
			if tt.wantErrMsg != "" {
				assert.EqualError(t, err, tt.wantErrMsg)
				writeCalls := cfg.WriteCalls()
				assert.Equal(t, 0, len(writeCalls))
			} else {
				assert.NoError(t, err)
				writeCalls := cfg.WriteCalls()
				assert.Equal(t, 1, len(writeCalls))
			}

			assert.Equal(t, tt.wantStdout, stdout.String())
			assert.Equal(t, tt.wantStderr, stderr.String())
			assert.Equal(t, tt.wantAliases, cfg.Aliases().All())
		})
	}
}