From 183db995357839cab601f389b8b14a8864ac7c8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Thu, 17 Oct 2019 15:58:26 +0200 Subject: [PATCH] Ensure remote URL parsing tests don't read user SSH config files --- command/root.go | 6 +----- context/remote_test.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/command/root.go b/command/root.go index 1df3a2b0d..ff4a3e706 100644 --- a/command/root.go +++ b/command/root.go @@ -17,9 +17,7 @@ var ( func init() { RootCmd.PersistentFlags().StringVarP(¤tRepo, "repo", "R", "", "current GitHub repository") RootCmd.PersistentFlags().StringVarP(¤tBranch, "current-branch", "B", "", "current git branch") -} -func initContext() { ctx := context.InitDefaultContext() ctx.SetBranch(currentBranch) repo := currentRepo @@ -27,6 +25,7 @@ func initContext() { repo = os.Getenv("GH_REPO") } ctx.SetBaseRepo(repo) + git.InitSSHAliasMap(nil) } @@ -36,9 +35,6 @@ var RootCmd = &cobra.Command{ Short: "GitHub CLI", Long: `Do things with GitHub from your terminal`, Args: cobra.MinimumNArgs(1), - PersistentPreRun: func(cmd *cobra.Command, args []string) { - initContext() - }, Run: func(cmd *cobra.Command, args []string) { fmt.Println("root") }, diff --git a/context/remote_test.go b/context/remote_test.go index 661d142e8..096af07e2 100644 --- a/context/remote_test.go +++ b/context/remote_test.go @@ -2,15 +2,36 @@ package context import ( "testing" + + "github.com/github/gh-cli/git" ) func Test_repoFromURL(t *testing.T) { + git.InitSSHAliasMap(nil) + r, err := repoFromURL("http://github.com/monalisa/octo-cat.git") eq(t, err, nil) eq(t, r, &GitHubRepository{Owner: "monalisa", Name: "octo-cat"}) } +func Test_repoFromURL_SSH(t *testing.T) { + git.InitSSHAliasMap(map[string]string{ + "gh": "github.com", + "github.com": "ssh.github.com", + }) + + r, err := repoFromURL("git@gh:monalisa/octo-cat") + eq(t, err, nil) + eq(t, r, &GitHubRepository{Owner: "monalisa", Name: "octo-cat"}) + + r, err = repoFromURL("git@github.com:monalisa/octo-cat") + eq(t, err, nil) + eq(t, r, &GitHubRepository{Owner: "monalisa", Name: "octo-cat"}) +} + func Test_parseRemotes(t *testing.T) { + git.InitSSHAliasMap(nil) + remoteList := []string{ "mona\tgit@github.com:monalisa/myfork.git (fetch)", "origin\thttps://github.com/monalisa/octo-cat.git (fetch)",