💡 (gh repo delete) Add warning when --yes is ignored without a repository, Closes: #12033

This commit is contained in:
Shion Ichikawa 2025-10-29 03:12:07 +09:00
parent eb79e4a2f2
commit 7083f6dda4
No known key found for this signature in database
GPG key ID: ECD53000D7B04C1A
2 changed files with 41 additions and 30 deletions

View file

@ -62,6 +62,7 @@ func NewCmdDelete(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Co
if !opts.IO.CanPrompt() {
return cmdutil.FlagErrorf("cannot non-interactively delete current repository. Please specify a repository or run interactively")
}
_, _ = fmt.Fprintln(opts.IO.ErrOut, "Warning: `--yes` is ignored since no repository was specified")
opts.Confirmed = false
}

View file

@ -16,12 +16,13 @@ import (
func TestNewCmdDelete(t *testing.T) {
tests := []struct {
name string
input string
tty bool
output DeleteOptions
wantErr bool
errMsg string
name string
input string
tty bool
output DeleteOptions
wantErr bool
wantErrMsg string
wantStderr string
}{
{
name: "confirm flag",
@ -36,11 +37,12 @@ func TestNewCmdDelete(t *testing.T) {
output: DeleteOptions{RepoArg: "OWNER/REPO", Confirmed: true},
},
{
name: "no confirmation notty",
input: "OWNER/REPO",
output: DeleteOptions{RepoArg: "OWNER/REPO"},
wantErr: true,
errMsg: "--yes required when not running interactively",
name: "no confirmation notty",
input: "OWNER/REPO",
output: DeleteOptions{RepoArg: "OWNER/REPO"},
wantErr: true,
wantErrMsg: "--yes required when not running interactively",
wantStderr: "Error: --yes required when not running interactively\n",
},
{
name: "base repo resolution",
@ -49,33 +51,39 @@ func TestNewCmdDelete(t *testing.T) {
output: DeleteOptions{},
},
{
name: "yes flag ignored when no argument tty",
tty: true,
input: "--yes",
output: DeleteOptions{Confirmed: false}, // --yes should be ignored
name: "yes flag ignored when no argument tty",
tty: true,
input: "--yes",
output: DeleteOptions{Confirmed: false}, // --yes should be ignored
wantErr: false,
wantStderr: "Warning: `--yes` is ignored since no repository was specified\n",
},
{
name: "yes flag error when no argument notty",
input: "--yes",
wantErr: true,
errMsg: "cannot non-interactively delete current repository. Please specify a repository or run interactively",
name: "yes flag error when no argument notty",
input: "--yes",
wantErr: true,
wantErrMsg: "cannot non-interactively delete current repository. Please specify a repository or run interactively",
wantStderr: "Error: cannot non-interactively delete current repository. Please specify a repository or run interactively\n",
},
{
name: "confirm flag error when no argument notty",
input: "--confirm",
wantErr: true,
errMsg: "cannot non-interactively delete current repository. Please specify a repository or run interactively",
name: "confirm flag error when no argument notty",
input: "--confirm",
wantErr: true,
wantErrMsg: "cannot non-interactively delete current repository. Please specify a repository or run interactively",
wantStderr: "Error: cannot non-interactively delete current repository. Please specify a repository or run interactively\n",
},
{
name: "confirm flag also ignored when no argument tty",
tty: true,
input: "--confirm",
output: DeleteOptions{Confirmed: false}, // --confirm should also be ignored
name: "confirm flag also ignored when no argument tty",
tty: true,
input: "--confirm",
output: DeleteOptions{Confirmed: false}, // --confirm should also be ignored
wantStderr: "Warning: `--yes` is ignored since no repository was specified\n",
// Note: This does not confuse the user, as deprecation warnings are shown: "Flag --confirm has been deprecated, use `--yes` instead"
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ios, _, _, _ := iostreams.Test()
ios, _, _, stdErr := iostreams.Test()
ios.SetStdinTTY(tt.tty)
ios.SetStdoutTTY(tt.tty)
f := &cmdutil.Factory{
@ -91,12 +99,14 @@ func TestNewCmdDelete(t *testing.T) {
cmd.SetArgs(argv)
cmd.SetIn(&bytes.Buffer{})
cmd.SetOut(&bytes.Buffer{})
cmd.SetErr(&bytes.Buffer{})
cmd.SetErr(stdErr)
_, err = cmd.ExecuteC()
assert.Equal(t, tt.wantStderr, stdErr.String())
if tt.wantErr {
assert.Error(t, err)
assert.Equal(t, tt.errMsg, err.Error())
assert.Equal(t, tt.wantErrMsg, err.Error())
return
}
assert.NoError(t, err)