From e5b81fb6a5a7a2dd59188969ee5530aec89eaf7b Mon Sep 17 00:00:00 2001 From: Jessica Sestak Date: Tue, 8 Jun 2021 22:09:11 -0700 Subject: [PATCH] Started working on testing for parseFileArgs --- pkg/cmd/browse/browse.go | 10 ++--- pkg/cmd/browse/browse_test.go | 81 +++++++++++++++++++++++++++-------- 2 files changed, 67 insertions(+), 24 deletions(-) diff --git a/pkg/cmd/browse/browse.go b/pkg/cmd/browse/browse.go index ea133d3c9..186514867 100644 --- a/pkg/cmd/browse/browse.go +++ b/pkg/cmd/browse/browse.go @@ -150,7 +150,7 @@ func addBranch(opts *BrowseOptions, url string) (error, string) { if opts.SelectorArg == "" { url += "/tree/" + opts.Branch } else { - err, arr := parseFileArg(opts) + err, arr := parseFileArg(opts.SelectorArg) if err != nil { return err, url } @@ -181,7 +181,7 @@ func addArg(opts *BrowseOptions, url string, branchName string) (error, string) url += "/issues/" + opts.SelectorArg fmt.Fprintf(opts.IO.Out, "now opening issue/pr in browser . . .\n") } else { - err, arr := parseFileArg(opts) + err, arr := parseFileArg(opts.SelectorArg) if err != nil { return err, url } @@ -195,13 +195,13 @@ func addArg(opts *BrowseOptions, url string, branchName string) (error, string) return nil, url } -func parseFileArg(opts *BrowseOptions) (error, []string) { - arr := strings.Split(opts.SelectorArg, ":") +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("nvalid line number after colon\nUse 'gh browse --help' for more information about browse\n"), arr + return fmt.Errorf("invalid line number after colon\nUse 'gh browse --help' for more information about browse\n"), arr } return nil, arr } diff --git a/pkg/cmd/browse/browse_test.go b/pkg/cmd/browse/browse_test.go index 0912884ca..0ad97af00 100644 --- a/pkg/cmd/browse/browse_test.go +++ b/pkg/cmd/browse/browse_test.go @@ -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]) + } + + } +}