-
Notifications
You must be signed in to change notification settings - Fork 1
/
ghs_test.go
201 lines (171 loc) · 5.49 KB
/
ghs_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"testing"
)
func Test_Ghs(t *testing.T) {
assert := func(result interface{}, want interface{}) {
if !reflect.DeepEqual(result, want) {
t.Errorf("Returned %+v, want %+v", result, want)
}
}
Setup()
defer Teardown()
// Normal response
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Link", HeaderLink(100, 10))
var items []string
for i := 1; i < 100+1; i++ {
items = append(items, fmt.Sprintf(`{"id":%d, "full_name": "test/search_word%d", "description":"%d"}`, i, i, i))
}
fmt.Fprintf(w, `{"total_count": 1000, "items": [%s]}`, strings.Join(items, ","))
}
mux.HandleFunc("/search/repositories", handler)
num, err := ghs(strings.Split(fmt.Sprintf("-e %s -m 1000 SEARCH_WORD", server.URL), " "))
assert(num, 1000)
assert(err, nil)
num, err = ghs(strings.Split(fmt.Sprintf("-e %s -m 110 SEARCH_WORD", server.URL), " "))
assert(num, 110)
assert(err, nil)
num, err = ghs(strings.Split("-v", " "))
assert(num, 0)
assert(err, nil)
num, err = ghs(strings.Split("-h", " "))
assert(num, 0)
assert(err, errors.New("help or parse error"))
num, err = ghs(strings.Split("-wrong_option", " "))
assert(num, 0)
assert(err, errors.New("help or parse error"))
num, err = ghs(strings.Split("-s stars", " "))
assert(num, 0)
assert(err, errors.New("Parse option error."))
}
func Test_GhsTokenTest(t *testing.T) {
assert := func(result interface{}, want interface{}) {
if !reflect.DeepEqual(result, want) {
t.Errorf("Returned %+v, want %+v", result, want)
}
}
Setup()
defer Teardown()
// Normal response
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Link", HeaderLink(100, 10))
var items []string
for i := 1; i < 100+1; i++ {
items = append(items, fmt.Sprintf(`{"id":%d, "full_name": "test/search_word%d", "description":"%d"}`, i, i, i))
}
fmt.Fprintf(w, `{"total_count": 1000, "items": [%s]}`, strings.Join(items, ","))
}
mux.HandleFunc("/search/repositories", handler)
num, err := ghs(strings.Split(fmt.Sprintf("-t abcdefg -e %s -m 100 SEARCH_WORD", server.URL), " "))
assert(num, 100)
assert(err, nil)
os.Setenv("GITHUB_TOKEN", "abcdefg")
num, err = ghs(strings.Split(fmt.Sprintf("-e %s -m 100 SEARCH_WORD", server.URL), " "))
assert(num, 100)
assert(err, nil)
os.Setenv("GITHUB_TOKEN", "")
panicIf := func(err error) {
if err != nil {
panic(err)
}
}
must := panicIf
run := func(cmd string, args ...string) error {
return exec.Command(cmd, args...).Run()
}
tmpHome, err := ioutil.TempDir("", "go-gitconfig")
panicIf(err)
repoDir := filepath.Join(tmpHome, "repo")
must(os.Setenv("HOME", tmpHome))
must(os.MkdirAll(repoDir, 0777))
must(os.Chdir(repoDir))
must(run("git", "init"))
must(run("git", "config", "--global", "github.token", "abcdefg"))
num, err = ghs(strings.Split(fmt.Sprintf("-e %s -m 100 SEARCH_WORD", server.URL), " "))
assert(num, 100)
assert(err, nil)
must(os.RemoveAll(tmpHome))
}
func Test_GhsInvalidResponse(t *testing.T) {
assert := func(result interface{}, want interface{}) {
if !reflect.DeepEqual(result, want) {
t.Errorf("Returned %+v, want %+v", result, want)
}
}
Setup()
defer Teardown()
// Invalid response
handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusNotFound)
}
mux.HandleFunc("/search/repositories", handler)
num, err := ghs(strings.Split(fmt.Sprintf("-e %s -m 100 SEARCH_WORD", server.URL), " "))
assert(num, 0)
assert(strings.Contains(err.Error(), "404"), true)
}
func Example_CheckVersion() {
os.Setenv("GHS_PRINT", "yes")
defer os.Setenv("GHS_PRINT", "no")
CheckVersion("0.0.1")
// Output:
// 0.0.1 is not latest, you should upgrade to 0.0.10
// -> $ brew update && brew upgrade sona-tar/tools/ghs
}
func Example_PrintHelp() {
os.Setenv("GHS_PRINT", "yes")
defer os.Setenv("GHS_PRINT", "no")
args := strings.Split("ghs -h", " ")[1:]
flags, _ := NewFlags(args)
flags.PrintHelp()
// Output:
// Usage:
// ghs [OPTION] "QUERY"
//
// Application Options:
// -f, --fields= limits what fields are searched. 'name', 'description', or
// 'readme'.
// -k, --fork= Forked repositories icluded in results. 'true', 'only' or
// 'false'.
// -s, --sort= The sort field. 'stars', 'forks', or 'updated'. (default:
// best match)
// -o, --order= The sort order. 'asc' or 'desc'. (default: desc)
// -l, --language= searches repositories based on the language they’re
// written in.
// -u, --user= limits searches to a specific user name.
// -r, --repo= limits searches to a specific repository.
// -m, --max= limits number of result. range 1-1000 (default: 100)
// -v, --version print version infomation and exit.
// -e, --enterprise= search from github enterprise.
// -t, --token= Github API token to avoid Github API rate
// -h, --help= Show this help message
//
// Github search APIv3 QUERY infomation:
// https://developer.github.com/v3/search/
// https://help.github.com/articles/searching-repositories/
//
// Version:
// ghs 0.0.10 (https://github.com/sona-tar/ghs.git)
}
func Example_PrintfDebug() {
os.Setenv("GHS_PRINT", "yes")
Printf("test")
// Output:
// test
os.Setenv("GHS_PRINT", "no")
os.Setenv("GHS_DEBUG", "yes")
Debug("test")
// Output:
// test
os.Setenv("GHS_DEBUG", "")
}