Merge pull request #47 from bchadwic/trunk
Updated browse tests for Test_runBrowse and TestNewCmdBrowse
This commit is contained in:
commit
6eed0ded39
4 changed files with 230 additions and 119 deletions
2
.vscode/settings.json
vendored
2
.vscode/settings.json
vendored
|
|
@ -2,4 +2,4 @@
|
|||
"search.exclude": {
|
||||
"vendor/**": true
|
||||
}
|
||||
}
|
||||
}
|
||||
BIN
build.exe
BIN
build.exe
Binary file not shown.
|
|
@ -11,7 +11,6 @@ import (
|
|||
"github.com/cli/cli/internal/ghrepo"
|
||||
"github.com/cli/cli/pkg/cmdutil"
|
||||
"github.com/cli/cli/pkg/iostreams"
|
||||
"github.com/cli/cli/utils"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
|
@ -25,12 +24,10 @@ type BrowseOptions struct {
|
|||
HttpClient func() (*http.Client, error)
|
||||
IO *iostreams.IOStreams
|
||||
|
||||
FlagAmount int
|
||||
SelectorArg string
|
||||
|
||||
Branch string
|
||||
ProjectsFlag bool
|
||||
RepoFlag bool
|
||||
SettingsFlag bool
|
||||
WikiFlag bool
|
||||
}
|
||||
|
|
@ -71,33 +68,42 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
|
|||
- by path for opening folders and files, e.g. "cmd/gh/main.go"
|
||||
`),
|
||||
"help:environment": heredoc.Doc(`
|
||||
To configure a web browser other than the default, use the BROWSER environment variable.
|
||||
To configure a web browser other than the default, use the BROWSER environment variable.
|
||||
`),
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
opts.BaseRepo = f.BaseRepo
|
||||
opts.FlagAmount = cmd.Flags().NFlag()
|
||||
|
||||
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.RepoFlag = true
|
||||
}
|
||||
if opts.FlagAmount > 1 && !opts.RepoFlag {
|
||||
return &cmdutil.FlagError{Err: fmt.Errorf("these two flags are incompatible, see below for instructions")}
|
||||
}
|
||||
|
||||
if len(args) > 0 {
|
||||
opts.SelectorArg = args[0]
|
||||
}
|
||||
|
||||
if err := cmdutil.MutuallyExclusive("cannot use --projects with --settings", opts.ProjectsFlag, opts.SettingsFlag); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := cmdutil.MutuallyExclusive("cannot use --projects with --wiki", opts.ProjectsFlag, opts.WikiFlag); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := cmdutil.MutuallyExclusive("cannot use --projects with --branch", opts.ProjectsFlag, opts.Branch != ""); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := cmdutil.MutuallyExclusive("cannot use --settings with --wiki", opts.SettingsFlag, opts.WikiFlag); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := cmdutil.MutuallyExclusive("cannot use --settings with --branch", opts.SettingsFlag, opts.Branch != ""); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := cmdutil.MutuallyExclusive("cannot use --wiki with --branch", opts.WikiFlag, opts.Branch != ""); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if runF != nil {
|
||||
return runF(opts)
|
||||
}
|
||||
return runBrowse(opts)
|
||||
},
|
||||
}
|
||||
|
||||
cmdutil.EnableRepoOverride(cmd, f)
|
||||
cmd.Flags().BoolVarP(&opts.ProjectsFlag, "projects", "p", false, "Open repository projects")
|
||||
cmd.Flags().BoolVarP(&opts.WikiFlag, "wiki", "w", false, "Open repository wiki")
|
||||
|
|
@ -119,96 +125,58 @@ func runBrowse(opts *BrowseOptions) error {
|
|||
return fmt.Errorf("unable to create an http client: %w\nUse 'gh browse --help' for more information about browse\n", err)
|
||||
}
|
||||
|
||||
repoUrl := ghrepo.GenerateRepoURL(baseRepo, "")
|
||||
url := ghrepo.GenerateRepoURL(baseRepo, "")
|
||||
|
||||
hasArg := opts.SelectorArg != ""
|
||||
hasFlag := opts.FlagAmount > 0
|
||||
if opts.ProjectsFlag {
|
||||
err := opts.Browser.Browse(url + "/projects")
|
||||
return err
|
||||
}
|
||||
|
||||
if opts.SettingsFlag {
|
||||
err := opts.Browser.Browse(url + "/settings")
|
||||
return err
|
||||
}
|
||||
|
||||
if opts.WikiFlag {
|
||||
err := opts.Browser.Browse(url + "/wiki")
|
||||
return err
|
||||
}
|
||||
|
||||
if isNumber(opts.SelectorArg) {
|
||||
url += "/issues/" + opts.SelectorArg
|
||||
err := opts.Browser.Browse(url)
|
||||
return err
|
||||
}
|
||||
|
||||
if opts.Branch != "" {
|
||||
repoUrl, err = addBranch(opts, repoUrl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if hasArg && (!hasFlag || opts.RepoFlag) {
|
||||
url += "/tree/" + opts.Branch + "/"
|
||||
} else {
|
||||
apiClient := api.NewClientFromHTTP(httpClient)
|
||||
branchName, err := api.RepoDefaultBranch(apiClient, baseRepo)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to determine default branch for %s: %w", ghrepo.FullName(baseRepo), err)
|
||||
return err
|
||||
}
|
||||
repoUrl, err = addArg(opts, repoUrl, branchName)
|
||||
url += "/tree/" + branchName + "/"
|
||||
}
|
||||
|
||||
if opts.SelectorArg != "" {
|
||||
arr, err := parseFileArg(opts.SelectorArg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if !hasArg && hasFlag {
|
||||
repoUrl = addFlag(opts, repoUrl)
|
||||
} else {
|
||||
if opts.IO.IsStdoutTTY() {
|
||||
fmt.Fprintf(opts.IO.Out, "Opening %s in your browser.\n", utils.DisplayURL(repoUrl))
|
||||
if len(arr) > 1 {
|
||||
url += arr[0] + "#L" + arr[1]
|
||||
} else {
|
||||
url += arr[0]
|
||||
}
|
||||
}
|
||||
opts.Browser.Browse(repoUrl)
|
||||
return nil
|
||||
|
||||
err = opts.Browser.Browse(url)
|
||||
if opts.IO.IsStdoutTTY() && err == nil {
|
||||
fmt.Fprintf(opts.IO.Out, "now opening %s in browser\n", url)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func addBranch(opts *BrowseOptions, url string) (string, error) {
|
||||
if opts.SelectorArg == "" {
|
||||
url += "/tree/" + opts.Branch
|
||||
} else {
|
||||
arr, err := parseFileArg(opts.SelectorArg)
|
||||
if err != nil {
|
||||
return url, err
|
||||
}
|
||||
url += parsedUrl(arr, opts.Branch)
|
||||
}
|
||||
if opts.IO.IsStdoutTTY() {
|
||||
fmt.Fprintf(opts.IO.Out, "now opening %s in browser . . .\n", url)
|
||||
}
|
||||
return url, nil
|
||||
}
|
||||
|
||||
func addFlag(opts *BrowseOptions, url string) string {
|
||||
if opts.ProjectsFlag {
|
||||
url += "/projects"
|
||||
} else if opts.SettingsFlag {
|
||||
url += "/settings"
|
||||
} else if opts.WikiFlag {
|
||||
url += "/wiki"
|
||||
}
|
||||
if opts.IO.IsStdoutTTY() {
|
||||
fmt.Fprintf(opts.IO.Out, "now opening %s in browser . . .\n", url)
|
||||
}
|
||||
return url
|
||||
}
|
||||
|
||||
func addArg(opts *BrowseOptions, url string, branchName string) (string, error) {
|
||||
if isNumber(opts.SelectorArg) {
|
||||
url += "/issues/" + opts.SelectorArg
|
||||
if opts.IO.IsStdoutTTY() {
|
||||
fmt.Fprintf(opts.IO.Out, "now opening issue/pr in browser . . .\n")
|
||||
}
|
||||
} else {
|
||||
arr, err := parseFileArg(opts.SelectorArg)
|
||||
if err != nil {
|
||||
return url, err
|
||||
}
|
||||
|
||||
url += parsedUrl(arr, branchName)
|
||||
|
||||
if opts.IO.IsStdoutTTY() {
|
||||
fmt.Fprintf(opts.IO.Out, "now opening %s in browser . . .\n", url)
|
||||
}
|
||||
}
|
||||
return url, nil
|
||||
}
|
||||
|
||||
func parsedUrl(arr []string, branchName string) string {
|
||||
if len(arr) > 1 {
|
||||
return "/tree/" + branchName + "/" + arr[0] + "#L" + arr[1]
|
||||
} else {
|
||||
return "/tree/" + branchName + "/" + arr[0]
|
||||
}
|
||||
}
|
||||
|
||||
func parseFileArg(fileArg string) ([]string, error) {
|
||||
arr := strings.Split(fileArg, ":")
|
||||
if len(arr) > 2 {
|
||||
|
|
@ -217,6 +185,7 @@ func parseFileArg(fileArg string) ([]string, error) {
|
|||
if len(arr) > 1 && !isNumber(arr[1]) {
|
||||
return arr, fmt.Errorf("invalid line number after colon\nUse 'gh browse --help' for more information about browse\n")
|
||||
}
|
||||
|
||||
return arr, nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,28 +8,120 @@ import (
|
|||
"github.com/cli/cli/pkg/cmdutil"
|
||||
"github.com/cli/cli/pkg/httpmock"
|
||||
"github.com/cli/cli/pkg/iostreams"
|
||||
"github.com/google/shlex"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestNewCmdBrowse(t *testing.T) {
|
||||
f := cmdutil.Factory{}
|
||||
var opts *BrowseOptions
|
||||
// pass a stub implementation of `runBrowse` for testing to avoid having real `runBrowse` called
|
||||
cmd := NewCmdBrowse(&f, func(o *BrowseOptions) error {
|
||||
opts = o
|
||||
return nil
|
||||
})
|
||||
tests := []struct {
|
||||
name string
|
||||
cli string
|
||||
factory func(*cmdutil.Factory) *cmdutil.Factory
|
||||
wants BrowseOptions
|
||||
wantsErr bool
|
||||
}{
|
||||
{
|
||||
name: "no arguments",
|
||||
cli: "",
|
||||
wantsErr: false,
|
||||
},
|
||||
{
|
||||
name: "settings flag",
|
||||
cli: "--settings",
|
||||
wants: BrowseOptions{
|
||||
SettingsFlag: true,
|
||||
},
|
||||
wantsErr: false,
|
||||
},
|
||||
{
|
||||
name: "projects flag",
|
||||
cli: "--projects",
|
||||
wants: BrowseOptions{
|
||||
ProjectsFlag: true,
|
||||
},
|
||||
wantsErr: false,
|
||||
},
|
||||
{
|
||||
name: "wiki flag",
|
||||
cli: "--wiki",
|
||||
wants: BrowseOptions{
|
||||
WikiFlag: true,
|
||||
},
|
||||
wantsErr: false,
|
||||
},
|
||||
{
|
||||
name: "branch flag",
|
||||
cli: "--branch main",
|
||||
wants: BrowseOptions{
|
||||
Branch: "main",
|
||||
},
|
||||
wantsErr: false,
|
||||
},
|
||||
{
|
||||
name: "branch flag without a branch name",
|
||||
cli: "--branch",
|
||||
wantsErr: true,
|
||||
},
|
||||
{
|
||||
name: "combination: settings projects",
|
||||
cli: "--settings --projects",
|
||||
wants: BrowseOptions{
|
||||
SettingsFlag: true,
|
||||
ProjectsFlag: true,
|
||||
},
|
||||
wantsErr: true,
|
||||
},
|
||||
{
|
||||
name: "combination: projects wiki",
|
||||
cli: "--projects --wiki",
|
||||
wants: BrowseOptions{
|
||||
ProjectsFlag: true,
|
||||
WikiFlag: true,
|
||||
},
|
||||
wantsErr: true,
|
||||
},
|
||||
{
|
||||
name: "passed argument",
|
||||
cli: "main.go",
|
||||
wants: BrowseOptions{
|
||||
SelectorArg: "main.go",
|
||||
},
|
||||
wantsErr: false,
|
||||
},
|
||||
{
|
||||
name: "passed two arguments",
|
||||
cli: "main.go main.go",
|
||||
wantsErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
f := cmdutil.Factory{}
|
||||
var opts *BrowseOptions
|
||||
cmd := NewCmdBrowse(&f, func(o *BrowseOptions) error {
|
||||
opts = o
|
||||
return nil
|
||||
})
|
||||
argv, err := shlex.Split(tt.cli)
|
||||
assert.NoError(t, err)
|
||||
cmd.SetArgs(argv)
|
||||
_, err = cmd.ExecuteC()
|
||||
|
||||
cmd.SetArgs([]string{"--branch", "main"})
|
||||
_, err := cmd.ExecuteC()
|
||||
assert.NoError(t, err)
|
||||
if tt.wantsErr {
|
||||
assert.Error(t, err)
|
||||
return
|
||||
|
||||
assert.Equal(t, "main", opts.Branch)
|
||||
assert.Equal(t, "", opts.SelectorArg)
|
||||
assert.Equal(t, false, opts.ProjectsFlag)
|
||||
assert.Equal(t, false, opts.WikiFlag)
|
||||
assert.Equal(t, false, opts.SettingsFlag)
|
||||
assert.Equal(t, 1, opts.FlagAmount)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
assert.Equal(t, tt.wants.Branch, opts.Branch)
|
||||
assert.Equal(t, tt.wants.SelectorArg, opts.SelectorArg)
|
||||
assert.Equal(t, tt.wants.ProjectsFlag, opts.ProjectsFlag)
|
||||
assert.Equal(t, tt.wants.WikiFlag, opts.WikiFlag)
|
||||
assert.Equal(t, tt.wants.SettingsFlag, opts.SettingsFlag)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_runBrowse(t *testing.T) {
|
||||
|
|
@ -45,37 +137,87 @@ func Test_runBrowse(t *testing.T) {
|
|||
name: "no arguments",
|
||||
opts: BrowseOptions{
|
||||
SelectorArg: "",
|
||||
FlagAmount: 0,
|
||||
},
|
||||
baseRepo: ghrepo.New("jessica", "cli"),
|
||||
expectedURL: "https://github.com/jessica/cli",
|
||||
baseRepo: ghrepo.New("jessica", "cli"),
|
||||
defaultBranch: "trunk",
|
||||
expectedURL: "https://github.com/jessica/cli/tree/trunk/",
|
||||
},
|
||||
{
|
||||
name: "file argument",
|
||||
opts: BrowseOptions{SelectorArg: "path/to/file.txt"},
|
||||
baseRepo: ghrepo.New("bchadwic", "cli"),
|
||||
baseRepo: ghrepo.New("ken", "cli"),
|
||||
defaultBranch: "main",
|
||||
expectedURL: "https://github.com/bchadwic/cli/tree/main/path/to/file.txt",
|
||||
expectedURL: "https://github.com/ken/cli/tree/main/path/to/file.txt",
|
||||
},
|
||||
{
|
||||
name: "branch flag",
|
||||
opts: BrowseOptions{
|
||||
Branch: "trunk",
|
||||
FlagAmount: 1,
|
||||
Branch: "trunk",
|
||||
},
|
||||
baseRepo: ghrepo.New("bchadwic", "cli"),
|
||||
expectedURL: "https://github.com/bchadwic/cli/tree/trunk",
|
||||
baseRepo: ghrepo.New("thanh", "cli"),
|
||||
expectedURL: "https://github.com/thanh/cli/tree/trunk/",
|
||||
},
|
||||
{
|
||||
name: "settings flag",
|
||||
opts: BrowseOptions{
|
||||
SettingsFlag: true,
|
||||
FlagAmount: 1,
|
||||
},
|
||||
baseRepo: ghrepo.New("bchadwic", "cli"),
|
||||
expectedURL: "https://github.com/bchadwic/cli/settings",
|
||||
},
|
||||
{
|
||||
name: "projects flag",
|
||||
opts: BrowseOptions{
|
||||
ProjectsFlag: true,
|
||||
},
|
||||
baseRepo: ghrepo.New("bchadwic", "cli"),
|
||||
expectedURL: "https://github.com/bchadwic/cli/projects",
|
||||
},
|
||||
{
|
||||
name: "wiki flag",
|
||||
opts: BrowseOptions{
|
||||
WikiFlag: true,
|
||||
},
|
||||
baseRepo: ghrepo.New("bchadwic", "cli"),
|
||||
expectedURL: "https://github.com/bchadwic/cli/wiki",
|
||||
},
|
||||
{
|
||||
name: "issue argument",
|
||||
opts: BrowseOptions{
|
||||
SelectorArg: "217",
|
||||
},
|
||||
baseRepo: ghrepo.New("bchadwic", "cli"),
|
||||
expectedURL: "https://github.com/bchadwic/cli/issues/217",
|
||||
},
|
||||
{
|
||||
name: "file with line number",
|
||||
opts: BrowseOptions{
|
||||
SelectorArg: "path/to/file.txt:32",
|
||||
},
|
||||
baseRepo: ghrepo.New("bchadwic", "cli"),
|
||||
defaultBranch: "trunk",
|
||||
expectedURL: "https://github.com/bchadwic/cli/tree/trunk/path/to/file.txt#L32",
|
||||
},
|
||||
{
|
||||
name: "file with invalid line number",
|
||||
opts: BrowseOptions{
|
||||
SelectorArg: "path/to/file.txt:32:32",
|
||||
},
|
||||
baseRepo: ghrepo.New("bchadwic", "cli"),
|
||||
defaultBranch: "trunk",
|
||||
wantsErr: true,
|
||||
},
|
||||
{
|
||||
name: "file with line argument",
|
||||
opts: BrowseOptions{
|
||||
SelectorArg: "path/to/file.txt:32",
|
||||
},
|
||||
baseRepo: ghrepo.New("bchadwic", "cli"),
|
||||
defaultBranch: "trunk",
|
||||
expectedURL: "https://github.com/bchadwic/cli/tree/trunk/path/to/file.txt#L32",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
io, _, stdout, stderr := iostreams.Test()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue