@@ -4,8 +4,10 @@ import (
44 "fmt"
55 "net/http"
66 "os"
7+ "path/filepath"
78 "testing"
89
10+ "github.com/cli/cli/v2/git"
911 "github.com/cli/cli/v2/internal/ghrepo"
1012 "github.com/cli/cli/v2/pkg/cmdutil"
1113 "github.com/cli/cli/v2/pkg/httpmock"
@@ -152,6 +154,7 @@ func setGitDir(t *testing.T, dir string) {
152154}
153155
154156func Test_runBrowse (t * testing.T ) {
157+ s := string (os .PathSeparator )
155158 setGitDir (t , "../../../git/fixtures/simple.git" )
156159 tests := []struct {
157160 name string
@@ -334,6 +337,32 @@ func Test_runBrowse(t *testing.T) {
334337 wantsErr : false ,
335338 expectedURL : "https://github.com/vilmibm/gh-user-status/tree/6f1a2405cace1633d89a79c74c65f22fe78f9659/main.go" ,
336339 },
340+ {
341+ name : "relative path from browse_test.go" ,
342+ opts : BrowseOptions {
343+ SelectorArg : filepath .Join ("." , "browse_test.go" ),
344+ PathFromRepoRoot : func () string {
345+ return "pkg/cmd/browse/"
346+ },
347+ },
348+ baseRepo : ghrepo .New ("bchadwic" , "gh-graph" ),
349+ defaultBranch : "trunk" ,
350+ expectedURL : "https://github.com/bchadwic/gh-graph/tree/trunk/pkg/cmd/browse/browse_test.go" ,
351+ wantsErr : false ,
352+ },
353+ {
354+ name : "relative path to file in parent folder from browse_test.go" ,
355+ opts : BrowseOptions {
356+ SelectorArg : ".." + s + "pr" ,
357+ PathFromRepoRoot : func () string {
358+ return "pkg/cmd/browse/"
359+ },
360+ },
361+ baseRepo : ghrepo .New ("bchadwic" , "gh-graph" ),
362+ defaultBranch : "trunk" ,
363+ expectedURL : "https://github.com/bchadwic/gh-graph/tree/trunk/pkg/cmd/pr" ,
364+ wantsErr : false ,
365+ },
337366 }
338367
339368 for _ , tt := range tests {
@@ -356,6 +385,9 @@ func Test_runBrowse(t *testing.T) {
356385 return & http.Client {Transport : & reg }, nil
357386 }
358387 opts .Browser = & browser
388+ if opts .PathFromRepoRoot == nil {
389+ opts .PathFromRepoRoot = git .PathFromRepoRoot
390+ }
359391
360392 err := runBrowse (& opts )
361393 if tt .wantsErr {
@@ -376,3 +408,70 @@ func Test_runBrowse(t *testing.T) {
376408 })
377409 }
378410}
411+
412+ func Test_parsePathFromFileArg (t * testing.T ) {
413+ s := string (os .PathSeparator )
414+ tests := []struct {
415+ name string
416+ fileArg string
417+ expectedPath string
418+ }{
419+ {
420+ name : "go to parent folder" ,
421+ fileArg : ".." + s ,
422+ expectedPath : "pkg/cmd" ,
423+ },
424+ {
425+ name : "current folder" ,
426+ fileArg : "." ,
427+ expectedPath : "pkg/cmd/browse" ,
428+ },
429+ {
430+ name : "current folder (alternative)" ,
431+ fileArg : "." + s ,
432+ expectedPath : "pkg/cmd/browse" ,
433+ },
434+ {
435+ name : "file that starts with '.'" ,
436+ fileArg : ".gitignore" ,
437+ expectedPath : "pkg/cmd/browse/.gitignore" ,
438+ },
439+ {
440+ name : "file in current folder" ,
441+ fileArg : filepath .Join ("." , "browse.go" ),
442+ expectedPath : "pkg/cmd/browse/browse.go" ,
443+ },
444+ {
445+ name : "file within parent folder" ,
446+ fileArg : filepath .Join (".." , "browse.go" ),
447+ expectedPath : "pkg/cmd/browse.go" ,
448+ },
449+ {
450+ name : "file within parent folder uncleaned" ,
451+ fileArg : filepath .Join (".." , "." ) + s + s + s + "browse.go" ,
452+ expectedPath : "pkg/cmd/browse.go" ,
453+ },
454+ {
455+ name : "different path from root directory" ,
456+ fileArg : filepath .Join (".." , ".." , ".." , "internal/build/build.go" ),
457+ expectedPath : "internal/build/build.go" ,
458+ },
459+ {
460+ name : "go out of repository" ,
461+ fileArg : filepath .Join (".." , ".." , ".." , ".." , ".." , ".." ) + s + "" ,
462+ expectedPath : "" ,
463+ },
464+ {
465+ name : "go to root of repository" ,
466+ fileArg : filepath .Join (".." , ".." , ".." ) + s + "" ,
467+ expectedPath : "" ,
468+ },
469+ }
470+ for _ , tt := range tests {
471+ path , _ , _ , _ := parseFile (BrowseOptions {
472+ PathFromRepoRoot : func () string {
473+ return "pkg/cmd/browse/"
474+ }}, tt .fileArg )
475+ assert .Equal (t , tt .expectedPath , path , tt .name )
476+ }
477+ }
0 commit comments