Merge pull request #34 from bchadwic/trunk

Added in a rough test file
This commit is contained in:
Benjamin Chadwick 2021-06-07 11:59:53 -07:00 committed by GitHub
commit f9a5818ddc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 133 additions and 9 deletions

View file

@ -31,7 +31,6 @@ type BrowseOptions struct {
WikiFlag bool
SettingsFlag bool
BranchFlag bool
//LineFlag bool
}
type exitCode int
@ -139,7 +138,6 @@ func openInBrowser(cmd *cobra.Command, opts *BrowseOptions) error {
}
return printExit(response, cmd, opts, repoUrl)
}
func addCombined(opts *BrowseOptions, url string, branchName string) (exitCode, string) {
@ -158,7 +156,6 @@ func addCombined(opts *BrowseOptions, url string, branchName string) (exitCode,
}
return exitUrlSuccess, url + "/tree/" + opts.AdditionalArg + "/" + arr[0]
}
func addFlag(opts *BrowseOptions, url string) (exitCode, string) {
@ -169,7 +166,7 @@ func addFlag(opts *BrowseOptions, url string) (exitCode, string) {
} else if opts.WikiFlag {
return exitUrlSuccess, url + "/wiki"
}
return exitExpectedArg, "" // Flag is a branch and needs an argument
return exitExpectedArg, ""
}
func addArg(opts *BrowseOptions, url string, branchName string) (exitCode, string) {
@ -203,11 +200,11 @@ func printExit(exit exitCode, cmd *cobra.Command, opts *BrowseOptions, url strin
switch exit {
case exitUrlSuccess:
fmt.Fprintf(w, "now opening %s in browser . . .\n", cs.Bold(url))
break
//fmt.Fprintf(w, "now opening %s in browser . . .\n", cs.Bold(url))
fmt.Fprintln(w, "Hello World One")
case exitNonUrlSuccess:
fmt.Fprintf(w, "now opening issue/pr in browser . . .\n")
break
//fmt.Fprintf(w, "now opening issue/pr in browser . . .\n")
fmt.Fprintln(w, "Hello World Two")
case exitNotInRepo:
return fmt.Errorf("change directory to a repository to open in browser\n%s", help)
case exitTooManyFlags:

View file

@ -0,0 +1,127 @@
package browse
import (
"bytes"
"io/ioutil"
"net/http"
"testing"
"github.com/cli/cli/internal/ghrepo"
"github.com/cli/cli/internal/run"
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/pkg/httpmock"
"github.com/cli/cli/pkg/iostreams"
"github.com/cli/cli/test"
"github.com/google/shlex"
"github.com/stretchr/testify/assert"
)
type args struct {
repo ghrepo.Interface
cli string
}
type testCase struct {
name string
args args
errorExpected bool
stdoutExpected string
stderrExpected string
}
func runCommand(rt http.RoundTripper, t testCase) (*test.CmdOut, error) {
io, _, stdout, stderr := iostreams.Test()
browser := &cmdutil.TestBrowser{}
factory := &cmdutil.Factory{
IOStreams: io,
Browser: browser,
HttpClient: func() (*http.Client, error) {
return &http.Client{Transport: rt}, nil
},
BaseRepo: func() (ghrepo.Interface, error) {
return ghrepo.New("OWNER", "REPO"), nil
},
}
cmd := NewCmdBrowse(factory)
argv, err := shlex.Split(t.args.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,
BrowsedURL: browser.BrowsedURL(),
}, err
}
func TestNewCmdBrowse(t *testing.T) {
var tests = []testCase{
{
name: "test1",
args: args{
repo: ghrepo.New("bchadwic", "cli"),
cli: "--settings --projects",
},
errorExpected: true,
stdoutExpected: "",
stderrExpected: "Error: accepts 1 flag, 2 flag(s) were recieved\nUse 'gh browse --help' for more information about browse\n\n",
},
{
name: "test2",
args: args{
repo: ghrepo.New("bchadwic", "cli"),
cli: "--settings",
},
errorExpected: false,
stdoutExpected: "hello world",
stderrExpected: "hello world",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
http := &httpmock.Registry{}
defer http.Verify(t)
_, cmdTeardown := run.Stub()
defer cmdTeardown(t)
output, err := runCommand(http, tt)
if tt.errorExpected {
assert.Error(t, err)
}
assert.Contains(t, output.String(), tt.stdoutExpected) // success outputs
assert.Contains(t, output.Stderr(), tt.stderrExpected) // error outputs
})
}
}
// http := initFakeHTTP()
// defer http.Verify(t)
// _, cmdTeardown := run.Stub()
// defer cmdTeardown(t)
// output, err := runCommand(http, true, "--web -a peter -l bug -l docs -L 10 -s merged -B trunk")
// if err != nil {
// t.Errorf("error running command `pr list` with `--web` flag: %v", err)
// }
// assert.Equal(t, "", output.String())
// assert.Equal(t, "Opening github.com/OWNER/REPO/pulls in your browser.\n", output.Stderr())
// assert.Equal(t, "https://github.com/OWNER/REPO/pulls?q=is%3Apr+is%3Amerged+assignee%3Apeter+label%3Abug+label%3Adocs+base%3Atrunk", output.BrowsedURL)

View file

@ -96,7 +96,7 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command {
repoResolvingCmdFactory := *f
repoResolvingCmdFactory.BaseRepo = resolvedBaseRepo(f)
cmd.AddCommand(browseCmd.NewCmdBrowse(f)) // adds to the commands Commands()
cmd.AddCommand(browseCmd.NewCmdBrowse(&repoResolvingCmdFactory)) // adds to the commands Commands()
cmd.AddCommand(prCmd.NewCmdPR(&repoResolvingCmdFactory))
cmd.AddCommand(issueCmd.NewCmdIssue(&repoResolvingCmdFactory))
cmd.AddCommand(releaseCmd.NewCmdRelease(&repoResolvingCmdFactory))