diff --git a/commands/runner_test.go b/commands/runner_test.go index de61ba52b8eb2d82dfb9efdcc9d4b02aa80a4bec..f3640223244f95b443352f038356ec6ce5b92f12 100644 --- a/commands/runner_test.go +++ b/commands/runner_test.go @@ -1,8 +1,9 @@ package commands import ( - "github.com/bmizerany/assert" "testing" + + "github.com/bmizerany/assert" ) func TestRunner_splitAliasCmd(t *testing.T) { @@ -29,7 +30,7 @@ func TestRunnerCallCommands(t *testing.T) { var result string f := func(c *Command, args *Args) { result = args.FirstParam() - args.Replace("echo", "", "true") + args.Replace("git", "version", "") } r := NewRunner() diff --git a/fixtures/test_repo.go b/fixtures/test_repo.go index fc15e7d38c637101594e77038bc7f1a4b679d64b..2beedf43c56d81b50aec9ae8680484a905f4af8f 100644 --- a/fixtures/test_repo.go +++ b/fixtures/test_repo.go @@ -57,17 +57,18 @@ func (r *TestRepo) clone(repo, dir string) error { } func (r *TestRepo) TearDown() { - err := os.RemoveAll(r.dir) + err := os.Chdir(r.pwd) if err != nil { panic(err) } - err = os.Chdir(r.pwd) + os.Setenv("HOME", r.home) + + err = os.RemoveAll(r.dir) if err != nil { panic(err) } - os.Setenv("HOME", r.home) } func SetupTestRepo() *TestRepo { diff --git a/git/url.go b/git/url.go index e7bf08d2e0be77536386f43f068d3ee076887bf7..03432354725f6eb7f4fd85a9d8b580f14f05809d 100644 --- a/git/url.go +++ b/git/url.go @@ -16,7 +16,10 @@ type URLParser struct { } func (p *URLParser) Parse(rawURL string) (u *url.URL, err error) { - if !protocolRe.MatchString(rawURL) && strings.Contains(rawURL, ":") { + if !protocolRe.MatchString(rawURL) && + strings.Contains(rawURL, ":") && + // not a Windows path + !strings.Contains(rawURL, "\\") { rawURL = "ssh://" + strings.Replace(rawURL, ":", "/", 1) } @@ -25,7 +28,7 @@ func (p *URLParser) Parse(rawURL string) (u *url.URL, err error) { return } - if u.Scheme == "http" || u.Scheme == "https" { + if u.Scheme != "ssh" { return } diff --git a/git/url_test.go b/git/url_test.go index fdec518ad57d109d3ad49cb79dabb133b95da7b1..9fa8f94d4c0aceda244faab0471538075fa86e6f 100644 --- a/git/url_test.go +++ b/git/url_test.go @@ -6,32 +6,34 @@ import ( "github.com/bmizerany/assert" ) -func TestURLParser_ParseURL(t *testing.T) { +func createURLParser() *URLParser { c := make(SSHConfig) c["github.com"] = "ssh.github.com" + c["gh"] = "github.com" c["git.company.com"] = "ssh.git.company.com" - p := &URLParser{c} + return &URLParser{c} +} + +func TestURLParser_ParseURL_HTTPURL(t *testing.T) { + p := createURLParser() u, err := p.Parse("https://github.com/octokit/go-octokit.git") assert.Equal(t, nil, err) assert.Equal(t, "github.com", u.Host) assert.Equal(t, "https", u.Scheme) assert.Equal(t, "/octokit/go-octokit.git", u.Path) +} + +func TestURLParser_ParseURL_GitURL(t *testing.T) { + p := createURLParser() - u, err = p.Parse("git://github.com/octokit/go-octokit.git") + u, err := p.Parse("git://github.com/octokit/go-octokit.git") assert.Equal(t, nil, err) assert.Equal(t, "github.com", u.Host) assert.Equal(t, "git", u.Scheme) assert.Equal(t, "/octokit/go-octokit.git", u.Path) - u, err = p.Parse("git@github.com:lostisland/go-sawyer.git") - assert.Equal(t, nil, err) - assert.Equal(t, "github.com", u.Host) - assert.Equal(t, "ssh", u.Scheme) - assert.Equal(t, "git", u.User.Username()) - assert.Equal(t, "/lostisland/go-sawyer.git", u.Path) - u, err = p.Parse("https://git.company.com/octokit/go-octokit.git") assert.Equal(t, nil, err) assert.Equal(t, "git.company.com", u.Host) @@ -40,9 +42,26 @@ func TestURLParser_ParseURL(t *testing.T) { u, err = p.Parse("git://git.company.com/octokit/go-octokit.git") assert.Equal(t, nil, err) - assert.Equal(t, "ssh.git.company.com", u.Host) + assert.Equal(t, "git.company.com", u.Host) assert.Equal(t, "git", u.Scheme) assert.Equal(t, "/octokit/go-octokit.git", u.Path) +} + +func TestURLParser_ParseURL_SSHURL(t *testing.T) { + p := createURLParser() + + u, err := p.Parse("git@github.com:lostisland/go-sawyer.git") + assert.Equal(t, nil, err) + assert.Equal(t, "github.com", u.Host) + assert.Equal(t, "ssh", u.Scheme) + assert.Equal(t, "git", u.User.Username()) + assert.Equal(t, "/lostisland/go-sawyer.git", u.Path) + + u, err = p.Parse("gh:octokit/go-octokit") + assert.Equal(t, nil, err) + assert.Equal(t, "github.com", u.Host) + assert.Equal(t, "ssh", u.Scheme) + assert.Equal(t, "/octokit/go-octokit", u.Path) u, err = p.Parse("git@git.company.com:octokit/go-octokit") assert.Equal(t, nil, err) @@ -50,3 +69,17 @@ func TestURLParser_ParseURL(t *testing.T) { assert.Equal(t, "ssh", u.Scheme) assert.Equal(t, "/octokit/go-octokit", u.Path) } + +func TestURLParser_ParseURL_LocalPath(t *testing.T) { + p := createURLParser() + + u, err := p.Parse("/path/to/repo.git") + assert.Equal(t, nil, err) + assert.Equal(t, "", u.Host) + assert.Equal(t, "", u.Scheme) + assert.Equal(t, "/path/to/repo.git", u.Path) + + u, err = p.Parse(`c:\path\to\repo.git`) + assert.Equal(t, nil, err) + assert.Equal(t, `c:\path\to\repo.git`, u.String()) +} diff --git a/github/editor_test.go b/github/editor_test.go index 6dd4c5f7464a5c6d73a112f529591d876b019bd2..68e138cecadb420f2c343f09853f77cc14b84ef0 100644 --- a/github/editor_test.go +++ b/github/editor_test.go @@ -14,6 +14,8 @@ import ( func TestEditor_openAndEdit_deleteFileWhenOpeningEditorFails(t *testing.T) { tempFile, _ := ioutil.TempFile("", "editor-test") + tempFile.Close() + ioutil.WriteFile(tempFile.Name(), []byte("hello"), 0644) editor := Editor{ Program: "memory", @@ -26,10 +28,13 @@ func TestEditor_openAndEdit_deleteFileWhenOpeningEditorFails(t *testing.T) { }, } - _, err := editor.openAndEdit() - assert.NotEqual(t, nil, err) + _, err := os.Stat(tempFile.Name()) + assert.Equal(t, nil, err) + + _, err = editor.openAndEdit() assert.Equal(t, "error using text editor for test message", fmt.Sprintf("%s", err)) + // file is removed if there's error _, err = os.Stat(tempFile.Name()) assert.T(t, os.IsNotExist(err)) } diff --git a/github/localrepo_test.go b/github/localrepo_test.go index 006e27e2f09aeef63a361ea92d89f4010e7a8450..b7a28aa3c80630f21778f9c9a7b392af19c74c47 100644 --- a/github/localrepo_test.go +++ b/github/localrepo_test.go @@ -15,7 +15,9 @@ func TestGitHubRepo_OriginRemote(t *testing.T) { localRepo, _ := LocalRepo() gitRemote, _ := localRepo.OriginRemote() assert.Equal(t, "origin", gitRemote.Name) - assert.Equal(t, repo.Remote, gitRemote.URL.String()) + + u, _ := url.Parse(repo.Remote) + assert.Equal(t, u, gitRemote.URL) } func TestGitHubRepo_remotesForPublish(t *testing.T) {