-
Notifications
You must be signed in to change notification settings - Fork 33
/
goreadme_test.go
92 lines (77 loc) · 1.91 KB
/
goreadme_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 goreadme
import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/golang/gddo/gosrc"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/oauth2"
)
// writeReadmes is used to write the expected output instead of asserting equality.
var writeReadmes = os.Getenv("WRITE_READMES") == "1"
var gr = New(oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
)))
func init() {
path, err := filepath.Abs("./testdata")
if err != nil {
panic(err)
}
gosrc.SetLocalDevMode(path)
}
func TestCreate(t *testing.T) {
t.Parallel()
for _, dir := range testDirs(t) {
t.Run(dir, func(t *testing.T) {
dir := "./" + dir
t.Parallel()
buf := bytes.NewBuffer(nil)
cfg := loadConfig(t, dir)
t.Logf("Running with config: %+v", cfg)
err := gr.WithConfig(cfg).Create(context.Background(), dir, buf)
require.NoError(t, err)
if writeReadmes {
// Helper with writing the README files.
require.NoError(t, ioutil.WriteFile(readmeFileName(dir), buf.Bytes(), 0664))
}
assertReadme(t, dir, buf.String())
})
}
}
func assertReadme(t *testing.T, dir string, got string) {
t.Helper()
want, err := ioutil.ReadFile(readmeFileName(dir))
require.NoError(t, err)
assert.Equal(t, string(want), got)
}
func testDirs(t *testing.T) []string {
t.Helper()
files, err := ioutil.ReadDir("testdata")
require.NoError(t, err)
dirs := make([]string, 0, len(files))
for _, f := range files {
if f.IsDir() {
dirs = append(dirs, "testdata/"+f.Name())
}
}
return dirs
}
func loadConfig(t *testing.T, dir string) Config {
t.Helper()
c := Config{}
b, err := ioutil.ReadFile(dir + "/goreadme.json")
if err != nil {
return c
}
err = json.Unmarshal(b, &c)
require.NoError(t, err)
return c
}
func readmeFileName(dir string) string {
return dir + "/README.md"
}