beginning testing

This commit is contained in:
bchadwic 2022-03-23 21:29:57 -07:00 committed by Sam Coe
parent a140bea9bf
commit 08b02da3fc
No known key found for this signature in database
GPG key ID: 8E322C20F811D086
4 changed files with 103 additions and 29 deletions

View file

@ -15,18 +15,16 @@ import (
type DefaultOptions struct {
IO *iostreams.IOStreams
BaseRepo func() (ghrepo.Interface, error)
Remotes func() (context.Remotes, error)
HttpClient func() (*http.Client, error)
ViewFlag bool
}
func NewCmdDefault(f *cmdutil.Factory, runF func(*DefaultOptions) error) *cobra.Command {
func NewCmdDefault(f *cmdutil.Factory) *cobra.Command {
opts := &DefaultOptions{
IO: f.IOStreams,
HttpClient: f.HttpClient,
BaseRepo: f.BaseRepo,
Remotes: f.Remotes,
}
@ -34,23 +32,15 @@ func NewCmdDefault(f *cmdutil.Factory, runF func(*DefaultOptions) error) *cobra.
Use: "default",
Short: "Configure the default repository used for various commands",
Long: heredoc.Doc(`
The default repository is used to determine which repository gh
should automatically query for the commands:
issue, pr, browse, run, repo rename, secret, workflow
The default repository is used to determine which remote
repository gh should automatically point to.
`),
Example: heredoc.Doc(`
$ gh repo default cli/cli
`),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if !opts.IO.CanPrompt() && !opts.ViewFlag {
return cmdutil.FlagErrorf("a repository name is required when not running interactively")
}
if runF != nil {
return runF(opts)
}
return defaultRun(opts)
return runDefault(opts)
},
}
@ -58,7 +48,7 @@ func NewCmdDefault(f *cmdutil.Factory, runF func(*DefaultOptions) error) *cobra.
return cmd
}
func defaultRun(opts *DefaultOptions) error {
func runDefault(opts *DefaultOptions) error {
remotes, err := opts.Remotes()
if err != nil {
return err
@ -78,10 +68,10 @@ func defaultRun(opts *DefaultOptions) error {
return err
}
apiClient := api.NewClientFromHTTP(httpClient)
context.RemoveBaseRepo(remotes)
repoContext, err := context.ResolveRemotesToRepos(remotes, apiClient, "")
if err != nil {
return err
}
return repoContext.SetGitConfigBaseRepo(opts.IO)
context.RemoveBaseRepo(remotes)
return repoContext.SetBaseRepo(opts.IO)
}

View file

@ -1 +1,86 @@
package base
package base
import (
"errors"
"fmt"
"net/http"
"testing"
"github.com/cli/cli/v2/context"
"github.com/cli/cli/v2/git"
"github.com/cli/cli/v2/internal/ghrepo"
"github.com/cli/cli/v2/pkg/httpmock"
"github.com/cli/cli/v2/pkg/iostreams"
"github.com/stretchr/testify/assert"
)
func Test_defaultRun(t *testing.T) {
tests := []struct {
name string
opts DefaultOptions
wantedErr error
wantedStdOut string
}{
{
opts: DefaultOptions{
Remotes: func() (context.Remotes, error) {
return []*context.Remote{
{
Remote: &git.Remote{
Name: "origin",
Resolved: "base",
},
Repo: ghrepo.New("hubot", "Spoon-Knife"),
},
}, nil
},
ViewFlag: true,
},
wantedStdOut: "hubot/Spoon-Knife",
},
{
opts: DefaultOptions{
Remotes: func() (context.Remotes, error) {
return []*context.Remote{
{
Remote: &git.Remote{
Name: "origin",
},
Repo: ghrepo.New("hubot", "Spoon-Knife"),
},
}, nil
},
ViewFlag: true,
},
wantedErr: errors.New("a default repo has not been set, use `gh repo default` to set a default repo"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
io, _, stdout, stderr := iostreams.Test()
reg := httpmock.Registry{}
defer reg.Verify(t)
opts := tt.opts
opts.IO = io
opts.HttpClient = func() (*http.Client, error) {
return &http.Client{Transport: &reg}, nil
}
err := runDefault(&opts)
if tt.wantedErr != nil {
assert.EqualError(t, tt.wantedErr, err.Error())
} else {
assert.NoError(t, err)
if opts.ViewFlag {
assert.Equal(t, fmt.Sprintf("%s\n", tt.wantedStdOut), stdout.String())
} else {
assert.Equal(t, "", stdout.String())
assert.Equal(t, "", stderr.String())
}
}
})
}
}