forked from tcnksm/ghr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_test.go
68 lines (51 loc) · 1.28 KB
/
git_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
. "github.com/onsi/gomega"
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func TestGitConfig(t *testing.T) {
RegisterTestingT(t)
reset := withGitConfigFile(`
[user]
name = tc
[remote "origin"]
url = https://github.com/tcnksm/test.git
`)
defer reset()
owner, err := gitConfig("user.name")
Expect(err).NotTo(HaveOccurred())
Expect(owner).To(Equal("tc"))
url, err := gitConfig("remote.origin.url")
Expect(err).NotTo(HaveOccurred())
Expect(url).To(Equal("https://github.com/tcnksm/test.git"))
blank, err := gitConfig("nothing.nothing")
Expect(err).To(HaveOccurred())
Expect(blank).To(Equal(""))
}
func TestRetrieveRepoName(t *testing.T) {
RegisterTestingT(t)
repo := retrieveRepoName("https://github.com/tcnksm/ghr.git")
Expect(repo).To(Equal("ghr"))
repo = retrieveRepoName("https://github.com/tcnksm/ghr")
Expect(repo).To(Equal("ghr"))
}
func withGitConfigFile(content string) func() {
tmpdir, err := ioutil.TempDir("", "ghr-test")
if err != nil {
panic(err)
}
tmpGitConfigFile := filepath.Join(tmpdir, "gitconfig")
ioutil.WriteFile(
tmpGitConfigFile,
[]byte(content),
0777,
)
prevGitConfigEnv := os.Getenv("GIT_CONFIG")
os.Setenv("GIT_CONFIG", tmpGitConfigFile)
return func() {
os.Setenv("GIT_CONFIG", prevGitConfigEnv)
}
}