From aac4c59c319597d188fb06cbca42e43411568f8f Mon Sep 17 00:00:00 2001 From: bchadwic Date: Mon, 26 Jul 2021 00:22:25 -0700 Subject: [PATCH] fixing operating system dependant regex, and tests --- pkg/cmd/browse/browse.go | 2 +- pkg/cmd/browse/browse_test.go | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/pkg/cmd/browse/browse.go b/pkg/cmd/browse/browse.go index 77d191691..c8a6f4c42 100644 --- a/pkg/cmd/browse/browse.go +++ b/pkg/cmd/browse/browse.go @@ -193,7 +193,7 @@ func parsePathFromFileArg(fileArg string) string { return fileArg } path := filepath.Join(git.PathFromRepoRoot(), fileArg) - match, _ := regexp.Match("(^\\.$)|(^\\.\\./)", []byte(path)) + match, _ := regexp.Match("(^\\.$)|(^\\.\\."+string(os.PathSeparator)+")", []byte(path)) if match { return "" } diff --git a/pkg/cmd/browse/browse_test.go b/pkg/cmd/browse/browse_test.go index 86a66ff9b..53c6f0e5c 100644 --- a/pkg/cmd/browse/browse_test.go +++ b/pkg/cmd/browse/browse_test.go @@ -3,6 +3,7 @@ package browse import ( "fmt" "net/http" + "os" "testing" "github.com/cli/cli/internal/ghrepo" @@ -134,6 +135,7 @@ func TestNewCmdBrowse(t *testing.T) { } func Test_runBrowse(t *testing.T) { + s := string(os.PathSeparator) tests := []struct { name string opts BrowseOptions @@ -257,7 +259,7 @@ func Test_runBrowse(t *testing.T) { { name: "relative path from browse_test.go", opts: BrowseOptions{ - SelectorArg: "./browse_test.go", + SelectorArg: "." + s + "browse_test.go", }, baseRepo: ghrepo.New("bchadwic", "gh-graph"), defaultBranch: "trunk", @@ -267,7 +269,7 @@ func Test_runBrowse(t *testing.T) { { name: "relative path to file in parent folder from browse_test.go", opts: BrowseOptions{ - SelectorArg: "../pr", + SelectorArg: ".." + s + "pr", }, baseRepo: ghrepo.New("bchadwic", "gh-graph"), defaultBranch: "trunk", @@ -357,6 +359,7 @@ func Test_parseFileArg(t *testing.T) { func Test_parsePathFromFileArg(t *testing.T) { + s := string(os.PathSeparator) // tests assume path is pkg/cmd/browse tests := []struct { name string @@ -365,27 +368,27 @@ func Test_parsePathFromFileArg(t *testing.T) { }{ { name: "go to parent folder", - fileArg: "../", + fileArg: ".." + s, expectedPath: "pkg/cmd", }, { name: "file in current folder", - fileArg: "./browse.go", + fileArg: "." + s + "browse.go", expectedPath: "pkg/cmd/browse/browse.go", }, { name: "file within parent folder", - fileArg: "../browse.go", + fileArg: ".." + s + "browse.go", expectedPath: "pkg/cmd/browse.go", }, { name: "file within parent folder uncleaned", - fileArg: ".././//browse.go", + fileArg: ".." + s + "." + s + s + s + "browse.go", expectedPath: "pkg/cmd/browse.go", }, { name: "different path from root directory", - fileArg: "../../../internal/build/build.go", + fileArg: ".." + s + ".." + s + ".." + s + "internal/build/build.go", expectedPath: "internal/build/build.go", }, { @@ -400,12 +403,12 @@ func Test_parsePathFromFileArg(t *testing.T) { }, { name: "go out of repository", - fileArg: "../../../../../../", + fileArg: ".." + s + ".." + s + ".." + s + ".." + s + ".." + s + ".." + s + "", expectedPath: "", }, { name: "go to root of repository", - fileArg: "../../../", + fileArg: ".." + s + ".." + s + ".." + s + "", expectedPath: "", }, }