-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.go
92 lines (80 loc) · 2.45 KB
/
main_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
package main
import (
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"testing"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/require"
)
func TestMain(m *testing.M) {
os.Setenv("TOKEN", "TOKEN")
m.Run()
}
func TestBoot(t *testing.T) {
require := require.New(t)
config := boot()
require.NotNil(config)
}
func TestValidConfigWithMissingToken(t *testing.T) {
require := require.New(t)
require.Panics(func() {
config := boot()
config.Token = ""
validConfig(config)
})
}
func TestRun(t *testing.T) {
require := require.New(t)
httpmock.Activate()
defer httpmock.DeactivateAndReset()
response1Path, err := filepath.Abs("./mock_data/page_1.json")
require.NoError(err)
response2Path, err := filepath.Abs("./mock_data/page_2.json")
require.NoError(err)
httpmock.RegisterResponder(
http.MethodGet,
"https://api.github.com/users/alphawong/starred?page=1&per_page=100",
func(req *http.Request) (*http.Response, error) {
resp := httpmock.NewStringResponse(http.StatusOK, httpmock.File(response1Path).String())
resp.Header.Set("link", `<https://api.github.com/user/5622516/starred?page=2>; rel="next", <https://api.github.com/user/5622516/starred?page=2>; rel="last"`)
if err != nil {
return httpmock.NewStringResponse(http.StatusInternalServerError, ""), nil
}
return resp, nil
},
)
httpmock.RegisterResponder(
http.MethodGet,
"https://api.github.com/users/alphawong/starred?page=2&per_page=100",
httpmock.NewJsonResponderOrPanic(
http.StatusOK,
httpmock.File(response2Path),
),
)
tmpfile, err := ioutil.TempFile(".", "tpl.*.md")
require.NoError(err)
_, err = tmpfile.WriteString(`{{define "layout"}}# Result
Language|⭐️|Repos
---|---|---
{{ range . }}{{.Language}}|{{.Stars}}|{{.Items}}
{{end}}{{end}}`,
)
require.NoError(err)
defer os.Remove(tmpfile.Name())
outputFile, err := ioutil.TempFile(".", "text-out.*.md")
defer os.Remove(outputFile.Name())
config := boot()
config.mu.Lock()
defer config.mu.Unlock()
config.BaseTemplate = tmpfile.Name()
config.OutputPath = outputFile.Name()
run(config)
info := httpmock.GetCallCountInfo()
log.Println(info)
actual, err := ioutil.ReadFile(outputFile.Name())
require.NoError(err)
require.Equal("# Result\nLanguage|⭐️|Repos\n---|---|---\nGo|1|[ [victorspringer/http-cache](https://github.com/victorspringer/http-cache) ]\nJavaScript|1|[ [stefanwuthrich/cached-google-places](https://github.com/stefanwuthrich/cached-google-places) ]\n", string(actual))
}