-
Notifications
You must be signed in to change notification settings - Fork 7
/
autoreadme_test.go
145 lines (128 loc) · 3.19 KB
/
autoreadme_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
package main
import (
"context"
"io/fs"
"os"
"path/filepath"
"testing"
"golang.org/x/tools/txtar"
)
var join = filepath.Join
func mkdirp(t *testing.T, path ...string) {
t.Helper()
if err := os.MkdirAll(join(path...), 0777); err != nil {
t.Fatal(err)
}
}
func writeFile(t *testing.T, data []byte, path ...string) {
t.Helper()
if err := os.WriteFile(join(path...), data, 0666); err != nil {
t.Fatal(err)
}
}
func cd(t *testing.T, path ...string) {
t.Helper()
if err := os.Chdir(join(path...)); err != nil {
t.Fatalf("failed to cd: %s", err)
}
}
// See testdata/README.md for instructions on how to add additional test cases.
func TestAutoreadme(t *testing.T) {
// cd back to the original directory when we're done
originalDir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
os.Chdir(originalDir)
})
// switch out the default template for one specialized for testing
src, err := readFile(join("testdata", "test.template"))
if err != nil {
t.Fatalf("reading testdata/test.template: %s", err)
}
originalSrc := defaultTemplateSrc
defaultTemplateSrc = string(src)
t.Cleanup(func() {
defaultTemplateSrc = originalSrc
})
// use testdata/*.txtar files to build out scenarios for us to test
cases, err := filepath.Glob(join("testdata", "*.txtar"))
if err != nil {
t.Fatalf("getting testdata/*.txtar files: %s", err)
}
for _, c := range cases {
name := filepath.Base(c)
name = name[:len(name)-len(".txtar")]
t.Run(name, func(t *testing.T) {
cd(t, originalDir)
arc, err := txtar.ParseFile(c)
if err != nil {
t.Fatal(err)
}
tmp := t.TempDir()
cd(t, tmp)
// always create a .git/ directory at the root
// to avoid having to specify it in every single test case
mkdirp(t, ".git")
// write out the archive in tmp
for _, file := range arc.Files {
name := filepath.FromSlash(file.Name)
dir := filepath.Dir(name)
if dir != "" {
mkdirp(t, dir)
}
writeFile(t, file.Data, name)
}
// do the thing
if err = Main(context.Background()); err != nil {
t.Fatalf("running autoreadme: %s", err)
}
// collect all generated README.md and README.md.expect contents, keyed by location
expect := map[string]string{}
got := map[string]string{}
err = filepath.WalkDir(".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
name := d.Name()
switch name {
default:
return nil
case "README.md", "README.md.expect":
}
bs, err := readFile(path)
if err != nil {
return err
}
contents := string(bs)
path = filepath.Dir(path)
if name == "README.md.expect" {
expect[path] = contents
} else {
got[path] = contents
}
return nil
})
if err != nil {
t.Fatal(err)
}
for k, v := range expect {
if _, ok := got[k]; !ok {
t.Errorf("README.md.expect with no README.md at %s\n %s", k, v)
}
}
for k, v := range got {
v2, ok := expect[k]
if !ok {
t.Errorf("README.md without README.md.expect at %s:\n%s", k, v)
} else if v != v2 {
t.Errorf("README.md difference at %s:\nGOT:\n%s\nEXPECTED:\n%s", k, v, v2)
}
}
})
}
}