-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme_fs_test.go
More file actions
143 lines (123 loc) · 3.04 KB
/
theme_fs_test.go
File metadata and controls
143 lines (123 loc) · 3.04 KB
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
package wpry
import (
"context"
"errors"
"io/fs"
"testing"
"testing/fstest"
"testing/synctest"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)
func TestParseThemeFS(t *testing.T) {
t.Parallel()
tests := []struct {
name string
fsys fs.FS
want Theme
wantPath string
wantErr bool
}{
{
name: "finds-main-style-css",
fsys: fstest.MapFS{
"not-style.css": &fstest.MapFile{Data: []byte("/* Theme Name: Foo */")},
"style.css": &fstest.MapFile{Data: []byte("/* Theme Name: Bar */")},
"readme.txt": &fstest.MapFile{Data: []byte("/* Theme Name: Baz */")},
},
want: Theme{Name: "Bar"},
wantPath: "style.css",
},
{
name: "no-header",
fsys: fstest.MapFS{
"style.css": &fstest.MapFile{Data: []byte("body { }")},
},
wantErr: true,
},
{
name: "empty-file",
fsys: fstest.MapFS{"style.css": &fstest.MapFile{Data: []byte{}}},
wantErr: true,
},
{
name: "no-style-css",
fsys: fstest.MapFS{
"not-style.css": &fstest.MapFile{Data: []byte("body { }")},
},
wantErr: true,
},
{
name: "no-files",
fsys: fstest.MapFS{},
wantErr: true,
},
{
name: "read-dir-error",
fsys: errReadDirFS{err: errors.New("boom")},
wantErr: true,
},
{
name: "with-max-workers-option",
fsys: fstest.MapFS{
"style.css": &fstest.MapFile{Data: []byte("/* Theme Name: My Theme */")},
},
want: Theme{Name: "My Theme"},
wantPath: "style.css",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got, gotPath, err := ParseThemeFS(t.Context(), tt.fsys)
if tt.wantErr && err == nil {
t.Fatal("TestParseThemeFS() unexpected success")
}
if !tt.wantErr && err != nil {
t.Fatalf("TestParseThemeFS() unexpected error: %v", err)
}
if gotPath != tt.wantPath {
t.Errorf("TestParseThemeFS() path = %q, want %q", gotPath, tt.wantPath)
}
if diff := cmp.Diff(tt.want, got, cmpopts.EquateEmpty()); diff != "" {
t.Errorf("TestParseThemeFS() mismatch (-want +got):\n%s", diff)
}
})
}
}
func TestParseThemeFS_Cancellation(t *testing.T) {
t.Parallel()
synctest.Test(t, func(t *testing.T) {
fsys := &blockingFS{
MapFS: fstest.MapFS{
"not-style.css": &fstest.MapFile{Data: []byte("/* Theme Name: Not My Theme */")},
"style.css": &fstest.MapFile{Data: []byte("/* Theme Name: My Theme */")},
},
blocks: map[string]chan struct{}{},
}
t.Cleanup(func() {
fsys.unblockAll()
})
fsys.block("style.css")
ctx, cancel := context.WithCancel(t.Context())
defer cancel()
type result struct {
theme Theme
path string
err error
}
out := make(chan result, 1)
go func() {
defer close(out)
theme, path, err := ParseThemeFS(ctx, fsys)
out <- result{theme: theme, path: path, err: err}
}()
// Ensure the ParseThemeFS goroutine is being blocked.
synctest.Wait()
cancel()
got := <-out
if !errors.Is(got.err, context.Canceled) {
t.Errorf("ParseThemeFS() error = %v, want %v", got.err, context.Canceled)
}
})
}