-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprettier_test.go
159 lines (132 loc) · 3.27 KB
/
prettier_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
package prettier
import (
"context"
"embed"
"fmt"
"io/fs"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"github.com/wasilibs/go-prettier/v3/internal/runner"
)
//go:embed testdata/in
var testFiles embed.FS
//go:embed testdata/exp
var expFiles embed.FS
//go:embed testdata/exptabwidth4
var expFilesTabWidth4 embed.FS
//go:embed testdata/config/.prettierrc
var prettierrc []byte
//go:embed testdata/config/prettierrc.yaml
var prettierrcYAML []byte
//go:embed testdata/config/prettierrc.toml
var prettierrcTOML []byte
//go:embed testdata/config/.editorconfig
var editorconfig []byte
func TestRun(t *testing.T) {
t.Parallel()
testFiles, _ := fs.Sub(testFiles, "testdata/in")
expFiles, _ := fs.Sub(expFiles, "testdata/exp")
expFilesTabWidth4, _ := fs.Sub(expFilesTabWidth4, "testdata/exptabwidth4")
tests := []struct {
name string
args runner.RunArgs
expFS fs.FS
prepare func(dir string) error
}{
{
name: "no config, write",
args: runner.RunArgs{
Patterns: []string{"."},
Write: true,
},
expFS: expFiles,
},
{
name: "json config, write",
prepare: func(dir string) error {
return os.WriteFile(filepath.Join(dir, ".prettierrc"), prettierrc, 0o644)
},
args: runner.RunArgs{
Patterns: []string{"."},
Write: true,
},
expFS: expFilesTabWidth4,
},
{
name: "yaml config, write",
prepare: func(dir string) error {
return os.WriteFile(filepath.Join(dir, ".prettierrc.yaml"), prettierrcYAML, 0o644)
},
args: runner.RunArgs{
Patterns: []string{"."},
Write: true,
},
expFS: expFilesTabWidth4,
},
{
name: "toml config, write",
prepare: func(dir string) error {
return os.WriteFile(filepath.Join(dir, ".prettierrc.toml"), prettierrcTOML, 0o644)
},
args: runner.RunArgs{
Patterns: []string{"."},
Write: true,
},
expFS: expFilesTabWidth4,
},
{
name: "editorconfig, write",
prepare: func(dir string) error {
return os.WriteFile(filepath.Join(dir, ".editorconfig"), editorconfig, 0o644)
},
args: runner.RunArgs{
Patterns: []string{"."},
Write: true,
},
expFS: expFilesTabWidth4,
},
}
r := runner.NewRunner()
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
require.NoError(t, fs.WalkDir(testFiles, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
c, _ := fs.ReadFile(testFiles, path)
if err := os.WriteFile(filepath.Join(dir, path), c, 0o644); err != nil {
return fmt.Errorf("failed to write to temp dir: %w", err)
}
return nil
}))
if tc.prepare != nil {
require.NoError(t, tc.prepare(dir))
}
args := tc.args
args.Cwd = dir
require.NoError(t, r.Run(context.Background(), args))
require.NoError(t, fs.WalkDir(tc.expFS, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
have, err := os.ReadFile(filepath.Join(dir, path))
if err != nil {
return fmt.Errorf("failed to read from temp dir: %w", err)
}
exp, _ := fs.ReadFile(tc.expFS, path)
require.Equal(t, string(exp), string(have))
return nil
}))
})
}
}