-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_test.go
212 lines (178 loc) · 4.38 KB
/
file_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
202
203
204
205
206
207
208
209
210
211
212
package main
import (
_ "embed"
"fmt"
"os"
"path"
"testing"
"time"
)
//go:embed testdata/20211004.md
var testdata20211004md []byte
//go:embed testdata/20211001.md
var testdata20211001md []byte
//go:embed testdata/20210930.md
var testdata20210930md []byte
//go:embed testdata/20210929.md
var testdata20210929md []byte
//go:embed testdata/20210928.md
var testdata20210928md []byte
//go:embed testdata/20210927.md
var testdata20210927md []byte
//go:embed testdata/20210926.md
var testdata20210926md []byte
//go:embed testdata/20210924.md
var testdata20210924md []byte
//go:embed testdata/20210923.md
var testdata20210923md []byte
//go:embed testdata/20210922.md
var testdata20210922md []byte
//go:embed testdata/20210916.md
var testdata20210916md []byte
//go:embed testdata/20210915.md
var testdata20210915md []byte
var testNoteFiles = map[string][]byte{
"20211004.md": testdata20211004md,
"20211001.md": testdata20211001md,
"20210930.md": testdata20210930md,
"20210929.md": testdata20210929md,
"20210928.md": testdata20210928md,
"20210927.md": testdata20210927md,
"20210926.md": testdata20210926md,
"20210924.md": testdata20210924md,
"20210923.md": testdata20210923md,
"20210922.md": testdata20210922md,
}
func createTestNoteFiles(dir string, files map[string][]byte) error {
if len(files) == 0 {
files = testNoteFiles
}
for name, contents := range files {
f, err := os.Create(path.Join(dir, name))
if err != nil {
return err
}
_, err = f.Write(contents)
if err != nil {
return err
}
f.Close()
}
return nil
}
func TestGetTodayFilename(t *testing.T) {
t.Parallel()
now := time.Now()
year := now.Year()
month := now.Month()
day := now.Day()
expected := fmt.Sprintf("%04d%02d%02d.md", year, month, day)
got := getTodayFilename()
if got != expected {
t.Errorf("got incorrect today filename: %s", got)
}
}
func TestCreateTodayFile(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
if err := createTestNoteFiles(tempDir, nil); err != nil {
t.Fatal(err.Error())
}
if err := createNotesFile(tempDir, "20221216.md"); err != nil {
t.Fatal(err.Error())
}
}
func TestCreateTodayFileError(t *testing.T) {
t.Parallel()
if err := createNotesFile("/fakedir", "20221216.md"); err == nil {
t.Error("fake path should cause open error")
}
}
func TestGetLastNFiles(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
err := createTestNoteFiles(tempDir, nil)
if err != nil {
t.Fatal(err.Error())
}
files, err := getLastNFiles(tempDir, 2)
if err != nil {
t.Fatal(err)
}
if len(files) != 2 {
t.Error("files should have length 2")
}
files, err = getLastNFiles(tempDir, 5)
if err != nil {
t.Fatal(err)
}
if len(files) != 5 {
t.Error("files should have length 5")
}
}
func TestGetLastNFilesOrder(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
err := createTestNoteFiles(tempDir, nil)
if err != nil {
t.Fatal(err)
}
files, err := getLastNFiles(tempDir, 3)
if files[0].Name() != "20211004.md" {
t.Error("files in incorrect order")
}
if files[1].Name() != "20211001.md" {
t.Error("files in incorrect order")
}
if files[2].Name() != "20210930.md" {
t.Error("files in incorrect order")
}
}
func TestGetLastTaskList(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
testFiles := map[string][]byte{
"20210916.md": testdata20210916md,
"20210915.md": testdata20210915md,
}
err := createTestNoteFiles(tempDir, testFiles)
if err != nil {
t.Fatal(err)
}
if err = createNotesFile(tempDir, "20221216.md"); err != nil {
t.Fatal(err.Error())
}
if err != nil {
t.Fatal(err)
}
}
func TestGetLastNotes(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
err := createTestNoteFiles(tempDir, nil)
if err != nil {
t.Fatal(err.Error())
}
lastNotes, err := getLastNotes(tempDir, "CORE-6229")
testLastNotes := `
- [ ] test task one
- [ ] test task two
- [x] test task three
- [ ] test task four
Don't forget to make sure the bucket is in the right region.
Still need to make sure the bucket is public.
Some example files that I'm using:
infrastructure/zpan/infra-global/environments/zr-public/s3.tf
infrastructure/terraform/modules/s3-bucket/policies/pdx-tier-access.json.tpl
`
if lastNotes != testLastNotes {
t.Error("last notes should match expected last notes")
}
}
func TestGetLastNotesBadPath(t *testing.T) {
t.Parallel()
_, err := getLastNotes("malarky", "CORE-6229")
if err == nil {
t.Error("getlastnotes should have errored")
}
}