We added new tests to bring the coverage to 90%+

This commit is contained in:
ravocean 2021-06-14 21:40:52 -07:00
parent b08a6d2bd0
commit 79da79fb68
2 changed files with 76 additions and 59 deletions

View file

@ -78,22 +78,13 @@ func NewCmdBrowse(f *cmdutil.Factory, runF func(*BrowseOptions) error) *cobra.Co
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 {
if err := cmdutil.MutuallyExclusive(
"specify only one of `--branch`, `--projects`, `--wiki`, or `--settings`",
opts.Branch != "",
opts.WikiFlag,
opts.SettingsFlag,
opts.ProjectsFlag,
); err != nil {
return err
}
@ -127,54 +118,54 @@ func runBrowse(opts *BrowseOptions) error {
url := ghrepo.GenerateRepoURL(baseRepo, "")
if opts.ProjectsFlag {
err := opts.Browser.Browse(url + "/projects")
return err
}
if opts.SelectorArg == "" {
if opts.SettingsFlag {
err := opts.Browser.Browse(url + "/settings")
return err
}
if opts.ProjectsFlag {
url += "/projects"
}
if opts.WikiFlag {
err := opts.Browser.Browse(url + "/wiki")
return err
}
if opts.SettingsFlag {
url += "/settings"
}
if isNumber(opts.SelectorArg) {
url += "/issues/" + opts.SelectorArg
err := opts.Browser.Browse(url)
return err
}
if opts.Branch != "" {
url += "/tree/" + opts.Branch + "/"
if opts.WikiFlag {
url += "/wiki"
}
if opts.Branch != "" {
url += "/tree/" + opts.Branch + "/"
}
} else {
apiClient := api.NewClientFromHTTP(httpClient)
branchName, err := api.RepoDefaultBranch(apiClient, baseRepo)
if err != nil {
return err
}
url += "/tree/" + branchName + "/"
}
if opts.SelectorArg != "" {
arr, err := parseFileArg(opts.SelectorArg)
if err != nil {
return err
}
if len(arr) > 1 {
url += arr[0] + "#L" + arr[1]
if isNumber(opts.SelectorArg) {
url += "/issues/" + opts.SelectorArg
} else {
url += arr[0]
arr, err := parseFileArg(opts.SelectorArg)
if err != nil {
return err
}
if opts.Branch != "" {
url += "/tree/" + opts.Branch + "/"
} else {
apiClient := api.NewClientFromHTTP(httpClient)
branchName, err := api.RepoDefaultBranch(apiClient, baseRepo)
if err != nil {
return err
}
url += "/tree/" + branchName + "/"
}
if opts.SelectorArg != "" {
if len(arr) > 1 {
url += arr[0] + "#L" + arr[1]
} else {
url += arr[0]
}
}
}
}
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 parseFileArg(fileArg string) ([]string, error) {

View file

@ -138,9 +138,8 @@ func Test_runBrowse(t *testing.T) {
opts: BrowseOptions{
SelectorArg: "",
},
baseRepo: ghrepo.New("jessica", "cli"),
defaultBranch: "trunk",
expectedURL: "https://github.com/jessica/cli/tree/trunk/",
baseRepo: ghrepo.New("jessica", "cli"),
expectedURL: "https://github.com/jessica/cli",
},
{
name: "file argument",
@ -157,6 +156,15 @@ func Test_runBrowse(t *testing.T) {
baseRepo: ghrepo.New("thanh", "cli"),
expectedURL: "https://github.com/thanh/cli/tree/trunk/",
},
{
name: "branch flag with file",
opts: BrowseOptions{
Branch: "trunk",
SelectorArg: "main.go",
},
baseRepo: ghrepo.New("thanh", "cli"),
expectedURL: "https://github.com/thanh/cli/tree/trunk/main.go",
},
{
name: "settings flag",
opts: BrowseOptions{
@ -203,9 +211,27 @@ func Test_runBrowse(t *testing.T) {
opts: BrowseOptions{
SelectorArg: "path/to/file.txt:32:32",
},
baseRepo: ghrepo.New("bchadwic", "cli"),
defaultBranch: "trunk",
wantsErr: true,
baseRepo: ghrepo.New("bchadwic", "cli"),
wantsErr: true,
},
{
name: "branch with issue number",
opts: BrowseOptions{
SelectorArg: "217",
Branch: "trunk",
},
baseRepo: ghrepo.New("bchadwic", "cli"),
wantsErr: false,
expectedURL: "https://github.com/bchadwic/cli/issues/217",
},
{
name: "opening branch",
opts: BrowseOptions{
Branch: "first-browse-pull",
},
baseRepo: ghrepo.New("ravocean", "cli"),
wantsErr: false,
expectedURL: "https://github.com/ravocean/cli/tree/first-browse-pull/",
},
{
name: "file with line argument",