forked from rhysd/dot-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit_test.go
146 lines (137 loc) · 3.98 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"github.com/mitchellh/go-homedir"
"os"
"path/filepath"
"strings"
"testing"
)
func TestGitHubRemoteURL(t *testing.T) {
o := GitHubRemoteURL("origin")
if !strings.Contains(o, "dot-github") {
t.Fatalf("'origin' remote url is invalid: %v", o)
}
}
func TestGitHubRemoteURLWithEnvVar(t *testing.T) {
os.Setenv("DOT_GITHUB_GIT_CMD", "git")
o := GitHubRemoteURL("origin")
if !strings.Contains(o, "dot-github") {
t.Fatalf("'origin' remote url is invalid: %v", o)
}
os.Setenv("DOT_GITHUB_GIT_CMD", "")
}
func TestGitHubRemoteURLWithInvalidEnvVar(t *testing.T) {
os.Setenv("DOT_GITHUB_GIT_CMD", "unknown-command")
defer func() {
if r := recover(); r == nil {
t.Errorf("Uknown command for Git must cause panic")
}
os.Setenv("DOT_GITHUB_GIT_CMD", "")
}()
GitHubRemoteURL("origin")
}
func TestInvalidRemote(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Uknown remote must cause panic")
}
}()
GitHubRemoteURL("uknown-remote-name")
}
func TestGitRoot(t *testing.T) {
r := GitRoot()
if len(r) == 0 {
t.Fatalf("GitRoot() must return non-empty string")
}
if !filepath.IsAbs(r) {
t.Fatalf("GitRoot() must return absolute path but actually: %v", r)
}
if !strings.Contains(r, "dot-github") {
t.Fatalf("GitRoot() must return path to its repository but actually: %v", r)
}
}
func TestNonGitRepo(t *testing.T) {
home, err := homedir.Dir()
if err != nil {
panic(err)
}
wd, _ := os.Getwd()
os.Chdir(home)
defer func() {
if err := recover(); err == nil {
t.Errorf("panic must occur when current dir is not Git repo")
}
os.Chdir(wd)
}()
GitRoot()
}
func TestValidateURLs(t *testing.T) {
var b bool
b = ValidateGitHubURL("http://github.com/rhysd/dot-github.git")
if !b {
t.Errorf("Correct HTTP GitHub URL was detected as invalid URL")
}
b = ValidateGitHubURL("https://github.com/rhysd/dot-github.git")
if !b {
t.Errorf("Correct HTTPS GitHub URL was detected as invalid URL")
}
b = ValidateGitHubURL("git://github.com/rhysd/dot-github.git")
if !b {
t.Errorf("Correct GIT GitHub URL was detected as invalid URL")
}
b = ValidateGitHubURL("git@github.com:rhysd/dot-github.git")
if !b {
t.Errorf("Correct SSH GitHub URL was detected as invalid URL")
}
b = ValidateGitHubURL("http://github.company.com/rhysd/dot-github.git")
if !b {
t.Errorf("Correct HTTP GHE URL was detected as invalid URL")
}
b = ValidateGitHubURL("https://github.company.com/rhysd/dot-github.git")
if !b {
t.Errorf("Correct HTTPS GHE URL was detected as invalid URL")
}
b = ValidateGitHubURL("git://github.company.com/rhysd/dot-github.git")
if !b {
t.Errorf("Correct GIT GHE URL was detected as invalid URL")
}
b = ValidateGitHubURL("git@github.company.com:rhysd/dot-github.git")
if !b {
t.Errorf("Correct SSH GHE URL was detected as invalid URL")
}
}
func TestInvalidateURLs(t *testing.T) {
var b bool
b = ValidateGitHubURL("http://example.com/rhysd/dot-example.git")
if b {
t.Errorf("Correct HTTP GitHub URL was detected as invalid URL")
}
b = ValidateGitHubURL("https://example.com/rhysd/dot-example.git")
if b {
t.Errorf("Correct HTTPS GitHub URL was detected as invalid URL")
}
b = ValidateGitHubURL("git://example.com/rhysd/dot-example.git")
if b {
t.Errorf("Correct GIT GitHub URL was detected as invalid URL")
}
b = ValidateGitHubURL("git@example.com:rhysd/dot-example.git")
if b {
t.Errorf("Correct SSH GitHub URL was detected as invalid URL")
}
b = ValidateGitHubURL("http://example.company.com/rhysd/dot-example.git")
if b {
t.Errorf("Correct HTTP GHE URL was detected as invalid URL")
}
b = ValidateGitHubURL("https://example.company.com/rhysd/dot-example.git")
if b {
t.Errorf("Correct HTTPS GHE URL was detected as invalid URL")
}
b = ValidateGitHubURL("git://example.company.com/rhysd/dot-example.git")
if b {
t.Errorf("Correct GIT GHE URL was detected as invalid URL")
}
b = ValidateGitHubURL("git@example.company.com:rhysd/dot-example.git")
if b {
t.Errorf("Correct SSH GHE URL was detected as invalid URL")
}
}