working on testing browse.go

This commit is contained in:
bchadwic 2021-05-31 15:21:15 -07:00
parent 8fa2457163
commit 798e03cf15
2 changed files with 72 additions and 13 deletions

View file

@ -55,11 +55,10 @@ func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
}
cmd := &cobra.Command{
Long: "Work with GitHub in the browser", // displays when you are on the help page of this command
Short: "Open GitHub in the browser", // displays in the gh root help
Use: "browse", // necessary!!! This is the cmd that gets passed on the prompt
Args: cobra.RangeArgs(0, 2), // make sure only one arg at most is passed
Long: "Work with GitHub in the browser", // displays when you are on the help page of this command
Short: "Open GitHub in the browser", // displays in the gh root help
Use: "browse {<number> | <file> | <branch>}", // necessary!!! This is the cmd that gets passed on the prompt
Args: cobra.RangeArgs(0, 2), // make sure only one arg at most is passed
Example: heredoc.Doc(`
$ gh browse
#=> Opens repository in browser
@ -73,7 +72,6 @@ func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
$ gh browse src/fileName:312 --branch main
#=> Opens src/fileName at line 312 in main branch
`),
Annotations: map[string]string{
"IsCore": "true",
"help:arguments": heredoc.Doc(`
@ -82,7 +80,6 @@ func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
- by file or branch name, e.g. "main.java" or "trunk".
`),
},
Run: func(cmd *cobra.Command, args []string) {
if len(args) > 1 {
@ -104,25 +101,25 @@ func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
return cmd
}
func openInBrowser(cmd *cobra.Command, opts *BrowseOptions) {
func openInBrowser(cmd *cobra.Command, opts *BrowseOptions) (exitCode, string) {
baseRepo, err := opts.BaseRepo()
httpClient, _ := opts.HttpClient()
apiClient := api.NewClientFromHTTP(httpClient)
branchName, err := api.RepoDefaultBranch(apiClient, baseRepo)
response := exitSuccess
if !inRepo(err) { // must be in a repo to execute
printExit(exitNotInRepo, cmd, opts, "")
return
return exitNotInRepo, ""
}
if getFlagAmount(cmd) > 1 { // command can't have more than one flag
printExit(exitTooManyFlags, cmd, opts, "")
return
return exitTooManyFlags, ""
}
repoUrl := ghrepo.GenerateRepoURL(baseRepo, "")
response := exitSuccess
if !hasArg(opts) && hasFlag(cmd) {
response, repoUrl = addFlag(opts, repoUrl)
@ -137,7 +134,7 @@ func openInBrowser(cmd *cobra.Command, opts *BrowseOptions) {
}
printExit(response, cmd, opts, repoUrl) // print success
return response, repoUrl
}
func addCombined(opts *BrowseOptions, url string, branchName string) (exitCode, string) {

View file

@ -1,7 +1,17 @@
package browse
import (
"bytes"
"io/ioutil"
"net/http"
"testing"
"github.com/cli/cli/internal/config"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/pkg/iostreams"
"github.com/cli/cli/test"
"github.com/google/shlex"
)
func TestNewCmdBrowse(t *testing.T) {
@ -9,4 +19,56 @@ func TestNewCmdBrowse(t *testing.T) {
// instead of opening multiple browsers for each test,
// we can test the http code sent back after calling a site
}
}
func runCommand(isTTY bool, cli string) (*test.CmdOut, error) {
io, _, stdout, stderr := iostreams.Test()
io.SetStdoutTTY(isTTY)
io.SetStdinTTY(isTTY)
io.SetStderrTTY(isTTY)
factory := &cmdutil.Factory{
IOStreams: io,
HttpClient: func() (*http.Client, error) {
return &http.Client{Transport: rt}, nil
},
Config: func() (config.Config, error) {
return config.NewBlankConfig(), nil
},
BaseRepo: func() (ghrepo.Interface, error) {
return ghrepo.New("OWNER", "REPO"), nil
},
}
cmd := NewCmdBrowse(factory)
argv, err := shlex.Split(cli)
if err != nil {
return nil, err
}
cmd.SetArgs(argv)
cmd.SetIn(&bytes.Buffer{})
cmd.SetOut(ioutil.Discard)
cmd.SetErr(ioutil.Discard)
_, err = cmd.ExecuteC()
return &test.CmdOut{
OutBuf: stdout,
ErrBuf: stderr,
}, err
}
func TestBrowseOpen(t *testing.T) {
runCommand(true, "")
}
func Test_browseList(t *testing.T) {
type args struct {
repo ghrepo.Interface
cli string
expectedOutputs string
}
tests := []struct {
}{}
}