Skip to content

Commit aac4c59

Browse files
committed
fixing operating system dependant regex, and tests
1 parent 8469441 commit aac4c59

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

pkg/cmd/browse/browse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func parsePathFromFileArg(fileArg string) string {
193193
return fileArg
194194
}
195195
path := filepath.Join(git.PathFromRepoRoot(), fileArg)
196-
match, _ := regexp.Match("(^\\.$)|(^\\.\\./)", []byte(path))
196+
match, _ := regexp.Match("(^\\.$)|(^\\.\\."+string(os.PathSeparator)+")", []byte(path))
197197
if match {
198198
return ""
199199
}

pkg/cmd/browse/browse_test.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package browse
33
import (
44
"fmt"
55
"net/http"
6+
"os"
67
"testing"
78

89
"github.com/cli/cli/internal/ghrepo"
@@ -134,6 +135,7 @@ func TestNewCmdBrowse(t *testing.T) {
134135
}
135136

136137
func Test_runBrowse(t *testing.T) {
138+
s := string(os.PathSeparator)
137139
tests := []struct {
138140
name string
139141
opts BrowseOptions
@@ -257,7 +259,7 @@ func Test_runBrowse(t *testing.T) {
257259
{
258260
name: "relative path from browse_test.go",
259261
opts: BrowseOptions{
260-
SelectorArg: "./browse_test.go",
262+
SelectorArg: "." + s + "browse_test.go",
261263
},
262264
baseRepo: ghrepo.New("bchadwic", "gh-graph"),
263265
defaultBranch: "trunk",
@@ -267,7 +269,7 @@ func Test_runBrowse(t *testing.T) {
267269
{
268270
name: "relative path to file in parent folder from browse_test.go",
269271
opts: BrowseOptions{
270-
SelectorArg: "../pr",
272+
SelectorArg: ".." + s + "pr",
271273
},
272274
baseRepo: ghrepo.New("bchadwic", "gh-graph"),
273275
defaultBranch: "trunk",
@@ -357,6 +359,7 @@ func Test_parseFileArg(t *testing.T) {
357359

358360
func Test_parsePathFromFileArg(t *testing.T) {
359361

362+
s := string(os.PathSeparator)
360363
// tests assume path is pkg/cmd/browse
361364
tests := []struct {
362365
name string
@@ -365,27 +368,27 @@ func Test_parsePathFromFileArg(t *testing.T) {
365368
}{
366369
{
367370
name: "go to parent folder",
368-
fileArg: "../",
371+
fileArg: ".." + s,
369372
expectedPath: "pkg/cmd",
370373
},
371374
{
372375
name: "file in current folder",
373-
fileArg: "./browse.go",
376+
fileArg: "." + s + "browse.go",
374377
expectedPath: "pkg/cmd/browse/browse.go",
375378
},
376379
{
377380
name: "file within parent folder",
378-
fileArg: "../browse.go",
381+
fileArg: ".." + s + "browse.go",
379382
expectedPath: "pkg/cmd/browse.go",
380383
},
381384
{
382385
name: "file within parent folder uncleaned",
383-
fileArg: ".././//browse.go",
386+
fileArg: ".." + s + "." + s + s + s + "browse.go",
384387
expectedPath: "pkg/cmd/browse.go",
385388
},
386389
{
387390
name: "different path from root directory",
388-
fileArg: "../../../internal/build/build.go",
391+
fileArg: ".." + s + ".." + s + ".." + s + "internal/build/build.go",
389392
expectedPath: "internal/build/build.go",
390393
},
391394
{
@@ -400,12 +403,12 @@ func Test_parsePathFromFileArg(t *testing.T) {
400403
},
401404
{
402405
name: "go out of repository",
403-
fileArg: "../../../../../../",
406+
fileArg: ".." + s + ".." + s + ".." + s + ".." + s + ".." + s + ".." + s + "",
404407
expectedPath: "",
405408
},
406409
{
407410
name: "go to root of repository",
408-
fileArg: "../../../",
411+
fileArg: ".." + s + ".." + s + ".." + s + "",
409412
expectedPath: "",
410413
},
411414
}

0 commit comments

Comments
 (0)