commit @samcoe's suggestions

This commit is contained in:
meiji163 2021-11-10 17:38:11 -08:00
parent 0596834c95
commit 35131058ed
2 changed files with 51 additions and 47 deletions

View file

@ -31,6 +31,7 @@ func NewCmdArchive(f *cmdutil.Factory, runF func(*ArchiveOptions) error) *cobra.
IO: f.IOStreams,
HttpClient: f.HttpClient,
Config: f.Config,
BaseRepo: f.BaseRepo,
}
cmd := &cobra.Command{
@ -44,10 +45,9 @@ With no argument, archives the current repository.`),
if len(args) > 0 {
opts.RepoArg = args[0]
}
opts.BaseRepo = f.BaseRepo
if !opts.Confirmed && !opts.IO.CanPrompt() {
return cmdutil.FlagErrorf("could not prompt: confirmation with prompt or --confirm flag required")
return cmdutil.FlagErrorf("--confirm required when not running interactively")
}
if runF != nil {
return runF(opts)
@ -56,7 +56,7 @@ With no argument, archives the current repository.`),
},
}
cmd.Flags().BoolVarP(&opts.Confirmed, "confirm", "y", false, "skip confirmation prompt")
cmd.Flags().BoolVarP(&opts.Confirmed, "confirm", "y", false, "Skip the confirmation prompt")
return cmd
}
@ -68,36 +68,37 @@ func archiveRun(opts *ArchiveOptions) error {
}
apiClient := api.NewClientFromHTTP(httpClient)
repoSelector := opts.RepoArg
if repoSelector == "" {
currRepo, err := opts.BaseRepo()
var toArchive ghrepo.Interface
if opts.RepoArg == "" {
toArchive, err = opts.BaseRepo()
if err != nil {
return err
}
repoSelector = ghrepo.FullName(currRepo)
}
} else {
repoSelector := opts.RepoArg
if !strings.Contains(repoSelector, "/") {
cfg, err := opts.Config()
if err != nil {
return err
}
if !strings.Contains(repoSelector, "/") {
cfg, err := opts.Config()
hostname, err := cfg.DefaultHost()
if err != nil {
return err
}
currentUser, err := api.CurrentLoginName(apiClient, hostname)
if err != nil {
return err
}
repoSelector = currentUser + "/" + repoSelector
}
toArchive, err = ghrepo.FromFullName(repoSelector)
if err != nil {
return err
}
hostname, err := cfg.DefaultHost()
if err != nil {
return err
}
currentUser, err := api.CurrentLoginName(apiClient, hostname)
if err != nil {
return err
}
repoSelector = currentUser + "/" + repoSelector
}
toArchive, err := ghrepo.FromFullName(repoSelector)
if err != nil {
return err
}
fields := []string{"name", "owner", "isArchived", "id"}
@ -122,7 +123,7 @@ func archiveRun(opts *ArchiveOptions) error {
return fmt.Errorf("failed to prompt: %w", err)
}
if !opts.Confirmed {
return nil
return cmdutil.CancelError
}
}

View file

@ -20,14 +20,20 @@ func TestNewCmdArchive(t *testing.T) {
name string
input string
wantErr bool
output ArchiveOptions
errMsg string
}{
{
name: "no arguments tty",
name: "no arguments no tty",
input: "",
errMsg: "could not prompt: confirmation with prompt or --confirm flag required",
errMsg: "--confirm required when not running interactively",
wantErr: true,
},
{
name: "repo argument tty",
input: "OWNER/REPO --confirm",
output: ArchiveOptions{RepoArg: "OWNER/REPO", Confirmed: true},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@ -37,7 +43,9 @@ func TestNewCmdArchive(t *testing.T) {
}
argv, err := shlex.Split(tt.input)
assert.NoError(t, err)
var gotOpts *ArchiveOptions
cmd := NewCmdArchive(f, func(opts *ArchiveOptions) error {
gotOpts = opts
return nil
})
cmd.SetArgs(argv)
@ -51,15 +59,14 @@ func TestNewCmdArchive(t *testing.T) {
return
}
assert.NoError(t, err)
assert.Equal(t, tt.output.RepoArg, gotOpts.RepoArg)
assert.Equal(t, tt.output.Confirmed, gotOpts.Confirmed)
})
}
}
func Test_ArchiveRun(t *testing.T) {
queryResponse := `{ "data": { "repository":
{ "id": "THE-ID",
"isArchived": %s}
} }`
queryResponse := `{ "data": { "repository": { "id": "THE-ID","isArchived": %s} } }`
tests := []struct {
name string
opts ArchiveOptions
@ -107,9 +114,6 @@ func Test_ArchiveRun(t *testing.T) {
name: "archived repo tty",
wantStderr: "! Repository OWNER/REPO is already archived\n",
opts: ArchiveOptions{RepoArg: "OWNER/REPO"},
askStubs: func(q *prompt.AskStubber) {
q.StubOne(true)
},
httpStubs: func(reg *httpmock.Registry) {
reg.Register(
httpmock.GraphQL(`query RepositoryInfo\b`),
@ -120,9 +124,19 @@ func Test_ArchiveRun(t *testing.T) {
for _, tt := range tests {
repo, _ := ghrepo.FromFullName("OWNER/REPO")
reg := &httpmock.Registry{}
if tt.httpStubs != nil {
tt.httpStubs(reg)
}
tt.opts.BaseRepo = func() (ghrepo.Interface, error) {
return repo, nil
}
tt.opts.HttpClient = func() (*http.Client, error) {
return &http.Client{Transport: reg}, nil
}
io, _, stdout, stderr := iostreams.Test()
tt.opts.IO = io
q, teardown := prompt.InitAskStubber()
defer teardown()
@ -130,17 +144,6 @@ func Test_ArchiveRun(t *testing.T) {
tt.askStubs(q)
}
reg := &httpmock.Registry{}
if tt.httpStubs != nil {
tt.httpStubs(reg)
}
tt.opts.HttpClient = func() (*http.Client, error) {
return &http.Client{Transport: reg}, nil
}
io, _, stdout, stderr := iostreams.Test()
tt.opts.IO = io
t.Run(tt.name, func(t *testing.T) {
defer reg.Verify(t)
io.SetStdoutTTY(tt.isTTY)