提交 44d1fa7f 编写于 作者: J Jingwen Owen Ou

Fix Go tests on Windows

A couple of Go tests failure on Windows, fix code or tests to make them green:

--- FAIL: TestSaveAlwaysReportOption (0.15 seconds)
panic: remove C:\Users\IEUser\AppData\Local\Temp\test-repo999546479\test.git: Th
e process cannot access the file because it is being used by another process. [r
ecovered]
        panic: remove C:\Users\IEUser\AppData\Local\Temp\test-repo999546479\test
.git: The process cannot access the file because it is being used by another pro
cess.

--- FAIL: TestRunnerCallCommands (0.00 seconds)
        assert.go:15: V:/src/github.com/github/hub/commands/runner_test.go:42
        assert.go:24: ! 0 != 1

--- FAIL: TestEditor_openAndEdit_deleteFileWhenOpeningEditorFails (0.00 seconds)
        assert.go:15: V:/src/github.com/github/hub/github/editor_test.go:34
        assert.go:36: !  Failure

--- FAIL: TestGitHubRepo_OriginRemote (0.16 seconds)
        assert.go:15: V:/src/github.com/github/hub/github/localrepo_test.go:18
        assert.go:24: ! "V:\\src\\github.com\\github\\hub\\fixtures\\test.git" !
= "ssh://V/%5Csrc%5Cgithub.com%5Cgithub%5Chub%5Cfixtures%5Ctest.git"
上级 f5a140f0
package commands package commands
import ( import (
"github.com/bmizerany/assert"
"testing" "testing"
"github.com/bmizerany/assert"
) )
func TestRunner_splitAliasCmd(t *testing.T) { func TestRunner_splitAliasCmd(t *testing.T) {
...@@ -29,7 +30,7 @@ func TestRunnerCallCommands(t *testing.T) { ...@@ -29,7 +30,7 @@ func TestRunnerCallCommands(t *testing.T) {
var result string var result string
f := func(c *Command, args *Args) { f := func(c *Command, args *Args) {
result = args.FirstParam() result = args.FirstParam()
args.Replace("echo", "", "true") args.Replace("git", "version", "")
} }
r := NewRunner() r := NewRunner()
......
...@@ -57,17 +57,18 @@ func (r *TestRepo) clone(repo, dir string) error { ...@@ -57,17 +57,18 @@ func (r *TestRepo) clone(repo, dir string) error {
} }
func (r *TestRepo) TearDown() { func (r *TestRepo) TearDown() {
err := os.RemoveAll(r.dir) err := os.Chdir(r.pwd)
if err != nil { if err != nil {
panic(err) panic(err)
} }
err = os.Chdir(r.pwd) os.Setenv("HOME", r.home)
err = os.RemoveAll(r.dir)
if err != nil { if err != nil {
panic(err) panic(err)
} }
os.Setenv("HOME", r.home)
} }
func SetupTestRepo() *TestRepo { func SetupTestRepo() *TestRepo {
......
...@@ -16,7 +16,10 @@ type URLParser struct { ...@@ -16,7 +16,10 @@ type URLParser struct {
} }
func (p *URLParser) Parse(rawURL string) (u *url.URL, err error) { 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) rawURL = "ssh://" + strings.Replace(rawURL, ":", "/", 1)
} }
...@@ -25,7 +28,7 @@ func (p *URLParser) Parse(rawURL string) (u *url.URL, err error) { ...@@ -25,7 +28,7 @@ func (p *URLParser) Parse(rawURL string) (u *url.URL, err error) {
return return
} }
if u.Scheme == "http" || u.Scheme == "https" { if u.Scheme != "ssh" {
return return
} }
......
...@@ -6,32 +6,34 @@ import ( ...@@ -6,32 +6,34 @@ import (
"github.com/bmizerany/assert" "github.com/bmizerany/assert"
) )
func TestURLParser_ParseURL(t *testing.T) { func createURLParser() *URLParser {
c := make(SSHConfig) c := make(SSHConfig)
c["github.com"] = "ssh.github.com" c["github.com"] = "ssh.github.com"
c["gh"] = "github.com"
c["git.company.com"] = "ssh.git.company.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") u, err := p.Parse("https://github.com/octokit/go-octokit.git")
assert.Equal(t, nil, err) assert.Equal(t, nil, err)
assert.Equal(t, "github.com", u.Host) assert.Equal(t, "github.com", u.Host)
assert.Equal(t, "https", u.Scheme) assert.Equal(t, "https", u.Scheme)
assert.Equal(t, "/octokit/go-octokit.git", u.Path) 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, nil, err)
assert.Equal(t, "github.com", u.Host) assert.Equal(t, "github.com", u.Host)
assert.Equal(t, "git", u.Scheme) assert.Equal(t, "git", u.Scheme)
assert.Equal(t, "/octokit/go-octokit.git", u.Path) 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") u, err = p.Parse("https://git.company.com/octokit/go-octokit.git")
assert.Equal(t, nil, err) assert.Equal(t, nil, err)
assert.Equal(t, "git.company.com", u.Host) assert.Equal(t, "git.company.com", u.Host)
...@@ -40,9 +42,26 @@ func TestURLParser_ParseURL(t *testing.T) { ...@@ -40,9 +42,26 @@ func TestURLParser_ParseURL(t *testing.T) {
u, err = p.Parse("git://git.company.com/octokit/go-octokit.git") u, err = p.Parse("git://git.company.com/octokit/go-octokit.git")
assert.Equal(t, nil, err) 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, "git", u.Scheme)
assert.Equal(t, "/octokit/go-octokit.git", u.Path) 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") u, err = p.Parse("git@git.company.com:octokit/go-octokit")
assert.Equal(t, nil, err) assert.Equal(t, nil, err)
...@@ -50,3 +69,17 @@ func TestURLParser_ParseURL(t *testing.T) { ...@@ -50,3 +69,17 @@ func TestURLParser_ParseURL(t *testing.T) {
assert.Equal(t, "ssh", u.Scheme) assert.Equal(t, "ssh", u.Scheme)
assert.Equal(t, "/octokit/go-octokit", u.Path) 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())
}
...@@ -14,6 +14,8 @@ import ( ...@@ -14,6 +14,8 @@ import (
func TestEditor_openAndEdit_deleteFileWhenOpeningEditorFails(t *testing.T) { func TestEditor_openAndEdit_deleteFileWhenOpeningEditorFails(t *testing.T) {
tempFile, _ := ioutil.TempFile("", "editor-test") tempFile, _ := ioutil.TempFile("", "editor-test")
tempFile.Close()
ioutil.WriteFile(tempFile.Name(), []byte("hello"), 0644) ioutil.WriteFile(tempFile.Name(), []byte("hello"), 0644)
editor := Editor{ editor := Editor{
Program: "memory", Program: "memory",
...@@ -26,10 +28,13 @@ func TestEditor_openAndEdit_deleteFileWhenOpeningEditorFails(t *testing.T) { ...@@ -26,10 +28,13 @@ func TestEditor_openAndEdit_deleteFileWhenOpeningEditorFails(t *testing.T) {
}, },
} }
_, err := editor.openAndEdit() _, err := os.Stat(tempFile.Name())
assert.NotEqual(t, nil, err) assert.Equal(t, nil, err)
_, err = editor.openAndEdit()
assert.Equal(t, "error using text editor for test message", fmt.Sprintf("%s", err)) 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()) _, err = os.Stat(tempFile.Name())
assert.T(t, os.IsNotExist(err)) assert.T(t, os.IsNotExist(err))
} }
......
...@@ -15,7 +15,9 @@ func TestGitHubRepo_OriginRemote(t *testing.T) { ...@@ -15,7 +15,9 @@ func TestGitHubRepo_OriginRemote(t *testing.T) {
localRepo, _ := LocalRepo() localRepo, _ := LocalRepo()
gitRemote, _ := localRepo.OriginRemote() gitRemote, _ := localRepo.OriginRemote()
assert.Equal(t, "origin", gitRemote.Name) 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) { func TestGitHubRepo_remotesForPublish(t *testing.T) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册