Skip to content

Commit 183db99

Browse files
committed
Ensure remote URL parsing tests don't read user SSH config files
1 parent 344906b commit 183db99

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

command/root.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@ var (
1717
func init() {
1818
RootCmd.PersistentFlags().StringVarP(&currentRepo, "repo", "R", "", "current GitHub repository")
1919
RootCmd.PersistentFlags().StringVarP(&currentBranch, "current-branch", "B", "", "current git branch")
20-
}
2120

22-
func initContext() {
2321
ctx := context.InitDefaultContext()
2422
ctx.SetBranch(currentBranch)
2523
repo := currentRepo
2624
if repo == "" {
2725
repo = os.Getenv("GH_REPO")
2826
}
2927
ctx.SetBaseRepo(repo)
28+
3029
git.InitSSHAliasMap(nil)
3130
}
3231

@@ -36,9 +35,6 @@ var RootCmd = &cobra.Command{
3635
Short: "GitHub CLI",
3736
Long: `Do things with GitHub from your terminal`,
3837
Args: cobra.MinimumNArgs(1),
39-
PersistentPreRun: func(cmd *cobra.Command, args []string) {
40-
initContext()
41-
},
4238
Run: func(cmd *cobra.Command, args []string) {
4339
fmt.Println("root")
4440
},

context/remote_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,36 @@ package context
22

33
import (
44
"testing"
5+
6+
"github.com/github/gh-cli/git"
57
)
68

79
func Test_repoFromURL(t *testing.T) {
10+
git.InitSSHAliasMap(nil)
11+
812
r, err := repoFromURL("http://github.com/monalisa/octo-cat.git")
913
eq(t, err, nil)
1014
eq(t, r, &GitHubRepository{Owner: "monalisa", Name: "octo-cat"})
1115
}
1216

17+
func Test_repoFromURL_SSH(t *testing.T) {
18+
git.InitSSHAliasMap(map[string]string{
19+
"gh": "github.com",
20+
"github.com": "ssh.github.com",
21+
})
22+
23+
r, err := repoFromURL("git@gh:monalisa/octo-cat")
24+
eq(t, err, nil)
25+
eq(t, r, &GitHubRepository{Owner: "monalisa", Name: "octo-cat"})
26+
27+
r, err = repoFromURL("git@github.com:monalisa/octo-cat")
28+
eq(t, err, nil)
29+
eq(t, r, &GitHubRepository{Owner: "monalisa", Name: "octo-cat"})
30+
}
31+
1332
func Test_parseRemotes(t *testing.T) {
33+
git.InitSSHAliasMap(nil)
34+
1435
remoteList := []string{
1536
"mona\tgit@github.com:monalisa/myfork.git (fetch)",
1637
"origin\thttps://github.com/monalisa/octo-cat.git (fetch)",

0 commit comments

Comments
 (0)