-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathrule_tester.go
More file actions
229 lines (193 loc) · 7.12 KB
/
Copy pathrule_tester.go
File metadata and controls
229 lines (193 loc) · 7.12 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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package rule_tester
import (
"slices"
"strconv"
"sync"
"testing"
"github.com/microsoft/typescript-go/shim/ast"
"github.com/microsoft/typescript-go/shim/compiler"
"github.com/microsoft/typescript-go/shim/scanner"
"github.com/microsoft/typescript-go/shim/tspath"
"github.com/web-infra-dev/rslint/internal/linter"
"github.com/web-infra-dev/rslint/internal/rule"
"github.com/web-infra-dev/rslint/internal/utils"
"gotest.tools/v3/assert"
)
type LanguageOptions struct {
ParserOptions *ParserOptions `json:"parserOptions,omitempty"`
}
type ParserOptions struct {
Project string `json:"project,omitempty"`
ProjectService bool `json:"projectService,omitempty"`
}
type ValidTestCase struct {
Code string
Only bool
Skip bool
Options any
TSConfig string
Tsx bool
LanguageOptions *LanguageOptions
}
type InvalidTestCaseError struct {
MessageId string
Line int
Column int
EndLine int
EndColumn int
Suggestions []InvalidTestCaseSuggestion
}
type InvalidTestCaseSuggestion struct {
MessageId string
Output string
}
type InvalidTestCase struct {
Code string
Only bool
Skip bool
Output []string
Errors []InvalidTestCaseError
TSConfig string
Options any
Tsx bool
LanguageOptions *LanguageOptions
}
func RunRuleTester(rootDir string, tsconfigPath string, t *testing.T, r *rule.Rule, validTestCases []ValidTestCase, invalidTestCases []InvalidTestCase) {
t.Parallel()
onlyMode := slices.ContainsFunc(validTestCases, func(c ValidTestCase) bool { return c.Only }) ||
slices.ContainsFunc(invalidTestCases, func(c InvalidTestCase) bool { return c.Only })
runLinter := func(t *testing.T, code string, options any, tsconfigPathOverride string, tsx bool, languageOptions *LanguageOptions) []rule.RuleDiagnostic {
var diagnosticsMu sync.Mutex
diagnostics := make([]rule.RuleDiagnostic, 0, 3)
fileName := "file.ts"
if tsx {
fileName = "react.tsx"
}
fs := utils.NewOverlayVFSForFile(tspath.ResolvePath(rootDir, fileName), code)
host := utils.CreateCompilerHost(rootDir, fs)
tsconfigPath := tsconfigPath
if tsconfigPathOverride != "" {
tsconfigPath = tsconfigPathOverride
}
// Override with languageOptions.parserOptions.project if provided
if languageOptions != nil && languageOptions.ParserOptions != nil && languageOptions.ParserOptions.Project != "" {
tsconfigPath = tspath.ResolvePath(rootDir, languageOptions.ParserOptions.Project)
}
program, err := utils.CreateProgram(true, fs, rootDir, tsconfigPath, host)
assert.NilError(t, err, "couldn't create program. code: "+code)
sourceFile := program.GetSourceFile(fileName)
allowedFiles := []string{sourceFile.FileName()}
_, err = linter.RunLinter(
[]*compiler.Program{program},
true,
allowedFiles,
func(sourceFile *ast.SourceFile) []linter.ConfiguredRule {
return []linter.ConfiguredRule{
{
Name: r.Name,
Severity: rule.SeverityError,
Run: func(ctx rule.RuleContext) rule.RuleListeners {
return r.Run(ctx, options)
},
},
}
},
func(diagnostic rule.RuleDiagnostic) {
diagnosticsMu.Lock()
defer diagnosticsMu.Unlock()
diagnostics = append(diagnostics, diagnostic)
},
)
assert.NilError(t, err, "error running linter. code:\n", code)
return diagnostics
}
for i, testCase := range validTestCases {
t.Run("valid-"+strconv.Itoa(i), func(t *testing.T) {
t.Parallel()
if (onlyMode && !testCase.Only) || testCase.Skip {
t.SkipNow()
}
diagnostics := runLinter(t, testCase.Code, testCase.Options, testCase.TSConfig, testCase.Tsx, testCase.LanguageOptions)
if len(diagnostics) != 0 {
// TODO: pretty errors
t.Errorf("Expected valid test case not to contain errors. Code:\n%v", testCase.Code)
for i, d := range diagnostics {
t.Errorf("error %v - (%v-%v) %v", i+1, d.Range.Pos(), d.Range.End(), d.Message.Description)
}
t.FailNow()
}
})
}
for i, testCase := range invalidTestCases {
t.Run("invalid-"+strconv.Itoa(i), func(t *testing.T) {
t.Parallel()
if (onlyMode && !testCase.Only) || testCase.Skip {
t.SkipNow()
}
var initialDiagnostics []rule.RuleDiagnostic
outputs := make([]string, 0, 1)
code := testCase.Code
for i := range 10 {
diagnostics := runLinter(t, code, testCase.Options, testCase.TSConfig, testCase.Tsx, testCase.LanguageOptions)
if i == 0 {
initialDiagnostics = diagnostics
}
fixedCode, _, fixed := linter.ApplyRuleFixes(code, diagnostics)
if !fixed {
break
}
code = fixedCode
outputs = append(outputs, fixedCode)
}
if len(testCase.Output) == len(outputs) {
for i, expected := range testCase.Output {
assert.Equal(t, expected, outputs[i], "Expected code after fix")
}
} else {
t.Errorf("Expected to have %v outputs but had %v: %v", len(testCase.Output), len(outputs), outputs)
}
if len(initialDiagnostics) != len(testCase.Errors) {
t.Fatalf("Expected invalid test case to contain exactly %v errors (reported %v errors - %v). Code:\n%v", len(testCase.Errors), len(initialDiagnostics), initialDiagnostics, testCase.Code)
}
for i, expected := range testCase.Errors {
diagnostic := initialDiagnostics[i]
if expected.MessageId != diagnostic.Message.Id {
t.Errorf("Invalid message id %v. Expected %v", diagnostic.Message.Id, expected.MessageId)
}
lineIndex, columnIndex := scanner.GetLineAndCharacterOfPosition(diagnostic.SourceFile, diagnostic.Range.Pos())
line, column := lineIndex+1, columnIndex+1
endLineIndex, endColumnIndex := scanner.GetLineAndCharacterOfPosition(diagnostic.SourceFile, diagnostic.Range.End())
endLine, endColumn := endLineIndex+1, endColumnIndex+1
if expected.Line != 0 && expected.Line != line {
t.Errorf("Error line should be %v. Got %v", expected.Line, line)
}
if expected.Column != 0 && expected.Column != column {
t.Errorf("Error column should be %v. Got %v", expected.Column, column)
}
if expected.EndLine != 0 && expected.EndLine != endLine {
t.Errorf("Error end line should be %v. Got %v", expected.EndLine, endLine)
}
if expected.EndColumn != 0 && expected.EndColumn != endColumn {
t.Errorf("Error end column should be %v. Got %v", expected.EndColumn, endColumn)
}
suggestionsCount := 0
if diagnostic.Suggestions != nil {
suggestionsCount = len(*diagnostic.Suggestions)
}
if len(expected.Suggestions) != suggestionsCount {
t.Errorf("Expected to have %v suggestions but had %v", len(expected.Suggestions), suggestionsCount)
} else {
for i, expectedSuggestion := range expected.Suggestions {
suggestion := (*diagnostic.Suggestions)[i]
if expectedSuggestion.MessageId != suggestion.Message.Id {
t.Errorf("Invalid suggestion message id %v. Expected %v", suggestion.Message.Id, expectedSuggestion.MessageId)
} else {
output, _, _ := linter.ApplyRuleFixes(testCase.Code, []rule.RuleSuggestion{suggestion})
assert.Equal(t, expectedSuggestion.Output, output, "Expected code after suggestion fix")
}
}
}
}
})
}
}