tentative updates for acceptance criteria

- pending confirmation re description in prompt
This commit is contained in:
danochoa 2025-01-08 02:22:21 -06:00
parent 785cf43428
commit a8f0aaeb3d
2 changed files with 123 additions and 44 deletions

View file

@ -35,7 +35,7 @@ func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Co
} }
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "delete [<id> | <url>]", Use: "delete {<id> | <url>}",
Short: "Delete a gist", Short: "Delete a gist",
Long: heredoc.Docf(` Long: heredoc.Docf(`
Delete a GitHub gist. Delete a GitHub gist.
@ -53,9 +53,18 @@ func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Co
`), `),
Args: cobra.MaximumNArgs(1), Args: cobra.MaximumNArgs(1),
RunE: func(c *cobra.Command, args []string) error { RunE: func(c *cobra.Command, args []string) error {
if !opts.IO.CanPrompt() && !opts.Confirmed {
return cmdutil.FlagErrorf("--yes required when not running interactively")
}
if !opts.IO.CanPrompt() && len(args) == 0 {
return cmdutil.FlagErrorf("id or url argument required in non-interactive mode")
}
if len(args) == 1 { if len(args) == 1 {
opts.Selector = args[0] opts.Selector = args[0]
} }
if runF != nil { if runF != nil {
return runF(&opts) return runF(&opts)
} }
@ -97,9 +106,11 @@ func deleteRun(opts *DeleteOptions) error {
} }
if !opts.Confirmed { if !opts.Confirmed {
err = opts.Prompter.ConfirmDeletion("delete") confirmed, err := opts.Prompter.Confirm(fmt.Sprintf("Delete \"%s\" gist?", gistID), false)
if err != nil { if err != nil {
return err
}
if !confirmed {
return cmdutil.CancelError return cmdutil.CancelError
} }
} }
@ -128,7 +139,7 @@ func deleteGist(apiClient *api.Client, hostname string, gistID string, opts *Del
if opts.IO.IsStdoutTTY() { if opts.IO.IsStdoutTTY() {
cs := opts.IO.ColorScheme() cs := opts.IO.ColorScheme()
fmt.Fprintf(opts.IO.Out, fmt.Fprintf(opts.IO.Out,
"%s Deleted gist %s\n", "%s Gist \"%s\" deleted\n",
cs.SuccessIcon(), cs.SuccessIcon(),
gistID) gistID)
} }

View file

@ -19,36 +19,75 @@ import (
func TestNewCmdDelete(t *testing.T) { func TestNewCmdDelete(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
cli string cli string
wants DeleteOptions tty bool
want DeleteOptions
wantErr bool
wantErrMsg string
}{ }{
{ {
name: "valid selector", name: "valid selector",
cli: "123", cli: "123",
wants: DeleteOptions{ tty: true,
want: DeleteOptions{
Selector: "123", Selector: "123",
}, },
}, },
{ {
name: "no ID supplied", name: "valid selector, no ID supplied",
cli: "", cli: "",
wants: DeleteOptions{ tty: true,
want: DeleteOptions{
Selector: "", Selector: "",
}, },
}, },
{ {
name: "yes flag", name: "no ID supplied with --yes",
cli: "--yes",
tty: true,
want: DeleteOptions{
Selector: "",
},
},
{
name: "selector with --yes, no tty",
cli: "123 --yes", cli: "123 --yes",
wants: DeleteOptions{ tty: false,
want: DeleteOptions{
Selector: "123", Selector: "123",
}, },
}, },
{
name: "ID arg without --yes, no tty",
cli: "123",
tty: false,
want: DeleteOptions{
Selector: "",
},
wantErr: true,
wantErrMsg: "--yes required when not running interactively",
},
{
name: "no ID supplied with --yes, no tty",
cli: "--yes",
tty: false,
want: DeleteOptions{
Selector: "",
},
wantErr: true,
wantErrMsg: "id or url argument required in non-interactive mode",
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
f := &cmdutil.Factory{} io, _, _, _ := iostreams.Test()
f := &cmdutil.Factory{
IOStreams: io,
}
io.SetStdinTTY(tt.tty)
io.SetStdoutTTY(tt.tty)
argv, err := shlex.Split(tt.cli) argv, err := shlex.Split(tt.cli)
assert.NoError(t, err) assert.NoError(t, err)
@ -64,9 +103,13 @@ func TestNewCmdDelete(t *testing.T) {
cmd.SetErr(&bytes.Buffer{}) cmd.SetErr(&bytes.Buffer{})
_, err = cmd.ExecuteC() _, err = cmd.ExecuteC()
assert.NoError(t, err) if tt.wantErr {
assert.EqualError(t, err, tt.wantErrMsg)
return
}
assert.Equal(t, tt.wants.Selector, gotOpts.Selector) assert.NoError(t, err)
assert.Equal(t, tt.want.Selector, gotOpts.Selector)
}) })
} }
} }
@ -75,6 +118,7 @@ func Test_deleteRun(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
opts *DeleteOptions opts *DeleteOptions
cancel bool
httpStubs func(*httpmock.Registry) httpStubs func(*httpmock.Registry)
mockGistList bool mockGistList bool
wantErr bool wantErr bool
@ -91,7 +135,7 @@ func Test_deleteRun(t *testing.T) {
httpmock.StatusStringResponse(200, "{}")) httpmock.StatusStringResponse(200, "{}"))
}, },
wantErr: false, wantErr: false,
wantStdout: "✓ Deleted gist 1234\n", wantStdout: "✓ Gist \"1234\" deleted\n",
wantStderr: "", wantStderr: "",
}, },
{ {
@ -105,7 +149,7 @@ func Test_deleteRun(t *testing.T) {
}, },
mockGistList: true, mockGistList: true,
wantErr: false, wantErr: false,
wantStdout: "✓ Deleted gist 1234\n", wantStdout: "✓ Gist \"1234\" deleted\n",
wantStderr: "", wantStderr: "",
}, },
{ {
@ -119,7 +163,7 @@ func Test_deleteRun(t *testing.T) {
httpmock.StatusStringResponse(200, "{}")) httpmock.StatusStringResponse(200, "{}"))
}, },
wantErr: false, wantErr: false,
wantStdout: "✓ Deleted gist 1234\n", wantStdout: "✓ Gist \"1234\" deleted\n",
wantStderr: "", wantStderr: "",
}, },
{ {
@ -134,9 +178,29 @@ func Test_deleteRun(t *testing.T) {
}, },
mockGistList: true, mockGistList: true,
wantErr: false, wantErr: false,
wantStdout: "✓ Deleted gist 1234\n", wantStdout: "✓ Gist \"1234\" deleted\n",
wantStderr: "", wantStderr: "",
}, },
{
name: "cancel delete with id",
opts: &DeleteOptions{
Selector: "1234",
},
cancel: true,
wantErr: true,
wantStdout: "",
wantStderr: "",
},
{
name: "cancel delete with prompt",
opts: &DeleteOptions{
Selector: "",
},
cancel: true,
wantErr: true,
wantStdout: "",
wantStderr: "",
},
{ {
name: "not found", name: "not found",
opts: &DeleteOptions{ opts: &DeleteOptions{
@ -153,43 +217,47 @@ func Test_deleteRun(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
reg := &httpmock.Registry{}
tt.httpStubs(reg)
pm := prompter.NewMockPrompter(t) pm := prompter.NewMockPrompter(t)
if !tt.opts.Confirmed { if !tt.opts.Confirmed {
pm.RegisterConfirmDeletion("Type delete to confirm deletion:", func(_ string) error { pm.RegisterConfirm("Delete \"1234\" gist?", func(_ string, _ bool) (bool, error) {
return nil return !tt.cancel, nil
}) })
} }
if tt.mockGistList { reg := &httpmock.Registry{}
sixHours, _ := time.ParseDuration("6h") if !tt.cancel {
sixHoursAgo := time.Now().Add(-sixHours) tt.httpStubs(reg)
reg.Register( if tt.mockGistList {
httpmock.GraphQL(`query GistList\b`), sixHours, _ := time.ParseDuration("6h")
httpmock.StringResponse(fmt.Sprintf( sixHoursAgo := time.Now().Add(-sixHours)
`{ "data": { "viewer": { "gists": { "nodes": [ reg.Register(
{ httpmock.GraphQL(`query GistList\b`),
"name": "1234", httpmock.StringResponse(fmt.Sprintf(
"files": [{ "name": "cool.txt" }], `{ "data": { "viewer": { "gists": { "nodes": [
"description": "", {
"updatedAt": "%s", "name": "1234",
"isPublic": true "files": [{ "name": "cool.txt" }],
} "description": "",
] } } } }`, "updatedAt": "%s",
sixHoursAgo.Format(time.RFC3339), "isPublic": true
)), }
) ] } } } }`,
sixHoursAgo.Format(time.RFC3339),
)),
)
pm.RegisterSelect("Select a gist", []string{"cool.txt about 6 hours ago"}, func(_, _ string, opts []string) (int, error) {
return 0, nil
})
}
pm.RegisterSelect("Select a gist", []string{"cool.txt about 6 hours ago"}, func(_, _ string, opts []string) (int, error) {
return 0, nil
})
} }
tt.opts.Prompter = pm tt.opts.Prompter = pm
tt.opts.HttpClient = func() (*http.Client, error) { tt.opts.HttpClient = func() (*http.Client, error) {
return &http.Client{Transport: reg}, nil return &http.Client{Transport: reg}, nil
} }
tt.opts.Config = func() (gh.Config, error) { tt.opts.Config = func() (gh.Config, error) {
return config.NewBlankConfig(), nil return config.NewBlankConfig(), nil
} }