Omit current directory when browse --branch is used with no file argument (#4676)

This commit is contained in:
Boston Cartwright 2021-11-04 09:00:42 -06:00 committed by GitHub
parent 3a4d947603
commit dd0edc8fbd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

View file

@ -192,6 +192,10 @@ func parseSection(baseRepo ghrepo.Interface, opts *BrowseOptions) (string, error
}
func parseFile(opts BrowseOptions, f string) (p string, start int, end int, err error) {
if f == "" {
return
}
parts := strings.SplitN(f, ":", 3)
if len(parts) > 2 {
err = fmt.Errorf("invalid file argument: %q", f)

View file

@ -228,6 +228,35 @@ func Test_runBrowse(t *testing.T) {
baseRepo: ghrepo.New("bchadwic", "LedZeppelinIV"),
expectedURL: "https://github.com/bchadwic/LedZeppelinIV/tree/trunk/main.go",
},
{
name: "branch flag within dir",
opts: BrowseOptions{
Branch: "feature-123",
PathFromRepoRoot: func() string { return "pkg/dir" },
},
baseRepo: ghrepo.New("bstnc", "yeepers"),
expectedURL: "https://github.com/bstnc/yeepers/tree/feature-123/",
},
{
name: "branch flag within dir with .",
opts: BrowseOptions{
Branch: "feature-123",
SelectorArg: ".",
PathFromRepoRoot: func() string { return "pkg/dir" },
},
baseRepo: ghrepo.New("bstnc", "yeepers"),
expectedURL: "https://github.com/bstnc/yeepers/tree/feature-123/pkg/dir",
},
{
name: "branch flag within dir with dir",
opts: BrowseOptions{
Branch: "feature-123",
SelectorArg: "inner/more",
PathFromRepoRoot: func() string { return "pkg/dir" },
},
baseRepo: ghrepo.New("bstnc", "yeepers"),
expectedURL: "https://github.com/bstnc/yeepers/tree/feature-123/pkg/dir/inner/more",
},
{
name: "file with line number",
opts: BrowseOptions{
@ -466,6 +495,11 @@ func Test_parsePathFromFileArg(t *testing.T) {
fileArg: filepath.Join("..", "..", "..") + s + "",
expectedPath: "",
},
{
name: "empty fileArg",
fileArg: "",
expectedPath: "",
},
}
for _, tt := range tests {
path, _, _, _ := parseFile(BrowseOptions{