Merge pull request #41 from jlsestak/trunk
Made test for parse files args, fixed parse file args, fixed repo flag
This commit is contained in:
commit
170f7fa305
2 changed files with 107 additions and 48 deletions
|
|
@ -19,26 +19,27 @@ type browser interface {
|
|||
}
|
||||
|
||||
type BrowseOptions struct {
|
||||
HttpClient func() (*http.Client, error)
|
||||
IO *iostreams.IOStreams
|
||||
BaseRepo func() (ghrepo.Interface, error)
|
||||
Browser browser
|
||||
HttpClient func() (*http.Client, error)
|
||||
IO *iostreams.IOStreams
|
||||
|
||||
SelectorArg string
|
||||
FlagAmount int
|
||||
SelectorArg string
|
||||
|
||||
ProjectsFlag bool
|
||||
WikiFlag bool
|
||||
SettingsFlag bool
|
||||
Branch string
|
||||
ProjectsFlag bool
|
||||
RepoFlag bool
|
||||
SettingsFlag bool
|
||||
WikiFlag bool
|
||||
}
|
||||
|
||||
func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
|
||||
|
||||
opts := &BrowseOptions{
|
||||
IO: f.IOStreams,
|
||||
HttpClient: f.HttpClient,
|
||||
Browser: f.Browser,
|
||||
HttpClient: f.HttpClient,
|
||||
IO: f.IOStreams,
|
||||
}
|
||||
|
||||
cmd := &cobra.Command{
|
||||
|
|
@ -80,8 +81,10 @@ func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
|
|||
if opts.FlagAmount > 2 {
|
||||
return &cmdutil.FlagError{Err: fmt.Errorf("cannot have more than two flags, %d were recieved", opts.FlagAmount)}
|
||||
}
|
||||
|
||||
if repoOverride, _ := cmd.Flags().GetString("repo"); repoOverride == "" && opts.FlagAmount > 1 {
|
||||
if repoOverride, _ := cmd.Flags().GetString("repo"); repoOverride != "" {
|
||||
opts.RepoFlag = true
|
||||
}
|
||||
if opts.FlagAmount > 1 && !opts.RepoFlag {
|
||||
return &cmdutil.FlagError{Err: fmt.Errorf("these two flags are incompatible, see below for instructions")}
|
||||
}
|
||||
|
||||
|
|
@ -102,16 +105,15 @@ func NewCmdBrowse(f *cmdutil.Factory) *cobra.Command {
|
|||
}
|
||||
|
||||
func runBrowse(opts *BrowseOptions) error {
|
||||
help := "Use 'gh browse --help' for more information about browse\n"
|
||||
|
||||
baseRepo, err := opts.BaseRepo()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to determine base repository: %w\n%s", err, help)
|
||||
return fmt.Errorf("unable to determine base repository: %w\nUse 'gh browse --help' for more information about browse\n", err)
|
||||
}
|
||||
|
||||
httpClient, err := opts.HttpClient()
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create an http client: %w\n%s", err, help)
|
||||
return fmt.Errorf("unable to create an http client: %w\nUse 'gh browse --help' for more information about browse\n", err)
|
||||
}
|
||||
|
||||
apiClient := api.NewClientFromHTTP(httpClient)
|
||||
|
|
@ -126,9 +128,15 @@ func runBrowse(opts *BrowseOptions) error {
|
|||
hasFlag := opts.FlagAmount > 0
|
||||
|
||||
if opts.Branch != "" {
|
||||
repoUrl = addBranch(opts, repoUrl)
|
||||
} else if hasArg && !hasFlag {
|
||||
repoUrl = addArg(opts, repoUrl, branchName)
|
||||
err, repoUrl = addBranch(opts, repoUrl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if hasArg && (!hasFlag || opts.RepoFlag) {
|
||||
err, repoUrl = addArg(opts, repoUrl, branchName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if !hasArg && hasFlag {
|
||||
repoUrl = addFlag(opts, repoUrl)
|
||||
} else {
|
||||
|
|
@ -138,11 +146,14 @@ func runBrowse(opts *BrowseOptions) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func addBranch(opts *BrowseOptions, url string) string {
|
||||
func addBranch(opts *BrowseOptions, url string) (error, string) {
|
||||
if opts.SelectorArg == "" {
|
||||
url += "/tree/" + opts.Branch
|
||||
} else {
|
||||
arr := parseFileArg(opts)
|
||||
err, arr := parseFileArg(opts.SelectorArg)
|
||||
if err != nil {
|
||||
return err, url
|
||||
}
|
||||
if len(arr) > 1 {
|
||||
url += "/tree/" + opts.Branch + "/" + arr[0] + "#L" + arr[1]
|
||||
} else {
|
||||
|
|
@ -150,7 +161,7 @@ func addBranch(opts *BrowseOptions, url string) string {
|
|||
}
|
||||
}
|
||||
fmt.Fprintf(opts.IO.Out, "now opening %s in browser . . .\n", url)
|
||||
return url
|
||||
return nil, url
|
||||
}
|
||||
|
||||
func addFlag(opts *BrowseOptions, url string) string {
|
||||
|
|
@ -165,15 +176,15 @@ func addFlag(opts *BrowseOptions, url string) string {
|
|||
return url
|
||||
}
|
||||
|
||||
func addArg(opts *BrowseOptions, url string, branchName string) string {
|
||||
func addArg(opts *BrowseOptions, url string, branchName string) (error, string) {
|
||||
if isNumber(opts.SelectorArg) {
|
||||
url += "/issues/" + opts.SelectorArg
|
||||
fmt.Fprintf(opts.IO.Out, "now opening issue/pr in browser . . .\n")
|
||||
} else {
|
||||
arr := parseFileArg(opts)
|
||||
// if err != nil {
|
||||
|
||||
// }
|
||||
err, arr := parseFileArg(opts.SelectorArg)
|
||||
if err != nil {
|
||||
return err, url
|
||||
}
|
||||
if len(arr) > 1 {
|
||||
url += "/tree/" + branchName + "/" + arr[0] + "#L" + arr[1]
|
||||
} else {
|
||||
|
|
@ -181,13 +192,18 @@ func addArg(opts *BrowseOptions, url string, branchName string) string {
|
|||
}
|
||||
fmt.Fprintf(opts.IO.Out, "now opening %s in browser . . .\n", url)
|
||||
}
|
||||
return url
|
||||
return nil, url
|
||||
}
|
||||
|
||||
func parseFileArg(opts *BrowseOptions) []string {
|
||||
// TODO make sure the line number is the second arg, and that there are only two elements in arr
|
||||
arr := strings.Split(opts.SelectorArg, ":")
|
||||
return arr
|
||||
func parseFileArg(fileArg string) (error, []string) {
|
||||
arr := strings.Split(fileArg, ":")
|
||||
if len(arr) > 2 {
|
||||
return fmt.Errorf("invalid use of colon\nUse 'gh browse --help' for more information about browse\n"), arr
|
||||
}
|
||||
if len(arr) > 1 && !isNumber(arr[1]) {
|
||||
return fmt.Errorf("invalid line number after colon\nUse 'gh browse --help' for more information about browse\n"), arr
|
||||
}
|
||||
return nil, arr
|
||||
}
|
||||
|
||||
func isNumber(arg string) bool {
|
||||
|
|
|
|||
|
|
@ -16,20 +16,7 @@ import (
|
|||
"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
|
||||
urlExpected string
|
||||
}
|
||||
|
||||
func runCommand(rt http.RoundTripper, t testCase) (*test.CmdOut, error) {
|
||||
func runCommand(rt http.RoundTripper, repo ghrepo.Interface, cli string) (*test.CmdOut, error) {
|
||||
io, _, stdout, stderr := iostreams.Test()
|
||||
|
||||
browser := &cmdutil.TestBrowser{}
|
||||
|
|
@ -41,13 +28,13 @@ func runCommand(rt http.RoundTripper, t testCase) (*test.CmdOut, error) {
|
|||
return &http.Client{Transport: rt}, nil
|
||||
},
|
||||
BaseRepo: func() (ghrepo.Interface, error) {
|
||||
return t.args.repo, nil
|
||||
return repo, nil
|
||||
},
|
||||
}
|
||||
|
||||
cmd := NewCmdBrowse(factory)
|
||||
|
||||
argv, err := shlex.Split(t.args.cli)
|
||||
argv, err := shlex.Split(cli)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
@ -65,7 +52,20 @@ func runCommand(rt http.RoundTripper, t testCase) (*test.CmdOut, error) {
|
|||
}, err
|
||||
}
|
||||
func TestNewCmdBrowse(t *testing.T) {
|
||||
var tests = []testCase{
|
||||
|
||||
type args struct {
|
||||
repo ghrepo.Interface
|
||||
cli string
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
errorExpected bool
|
||||
stdoutExpected string
|
||||
stderrExpected string
|
||||
urlExpected string
|
||||
}{
|
||||
{
|
||||
name: "multiple flag",
|
||||
args: args{
|
||||
|
|
@ -74,7 +74,7 @@ func TestNewCmdBrowse(t *testing.T) {
|
|||
},
|
||||
errorExpected: true,
|
||||
stdoutExpected: "",
|
||||
stderrExpected: "accepts 1 flag, 2 flag(s) were recieved",
|
||||
stderrExpected: "these two flags are incompatible, see below for instructions",
|
||||
urlExpected: "",
|
||||
},
|
||||
{
|
||||
|
|
@ -131,7 +131,7 @@ func TestNewCmdBrowse(t *testing.T) {
|
|||
_, cmdTeardown := run.Stub()
|
||||
defer cmdTeardown(t)
|
||||
|
||||
output, err := runCommand(http, tt)
|
||||
output, err := runCommand(http, tt.args.repo, tt.args.cli)
|
||||
|
||||
if tt.errorExpected {
|
||||
assert.Equal(t, err.Error(), tt.stderrExpected)
|
||||
|
|
@ -143,3 +143,46 @@ func TestNewCmdBrowse(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
func TestFileArgParsing(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
arg string
|
||||
errorExpected bool
|
||||
fileArg string
|
||||
lineArg string
|
||||
stderrExpected string
|
||||
}{
|
||||
{
|
||||
name: "non line number",
|
||||
arg: "main.go",
|
||||
errorExpected: false,
|
||||
fileArg: "main.go",
|
||||
},
|
||||
{
|
||||
name: "line number",
|
||||
arg: "main.go:32",
|
||||
errorExpected: false,
|
||||
fileArg: "main.go",
|
||||
lineArg: "32",
|
||||
},
|
||||
{
|
||||
name: "non line number error",
|
||||
arg: "ma:in.go",
|
||||
errorExpected: true,
|
||||
stderrExpected: "invalid line number after colon\nUse 'gh browse --help' for more information about browse\n",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
err, arr := parseFileArg(tt.arg)
|
||||
if tt.errorExpected {
|
||||
assert.Equal(t, err.Error(), tt.stderrExpected)
|
||||
} else {
|
||||
assert.Equal(t, err, nil)
|
||||
if len(arr) > 1 {
|
||||
assert.Equal(t, tt.lineArg, arr[1])
|
||||
}
|
||||
assert.Equal(t, tt.fileArg, arr[0])
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue