Skip to content

Commit 6f4cbe9

Browse files
feat: add config severities tests
1 parent 807ea4a commit 6f4cbe9

File tree

2 files changed

+151
-1
lines changed

2 files changed

+151
-1
lines changed

packages/custom_lint_builder/test/analyzer_converter_test.dart

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,72 @@ void main() {
8383
},
8484
);
8585
});
86+
87+
test('Respects configSeverities when converting errors', () {
88+
final resourceProvider = MemoryResourceProvider();
89+
final source = FileSource(
90+
resourceProvider.newFile(
91+
'/home/user/project/lib/main.dart',
92+
'void main() {}',
93+
),
94+
);
95+
96+
// Create an analysis error with INFO severity
97+
final error = AnalysisError.tmp(
98+
source: source,
99+
offset: 0,
100+
length: 4,
101+
errorCode: const LintCode(
102+
name: 'rule_name_1',
103+
problemMessage: 'This is a lint',
104+
),
105+
);
106+
107+
// Create config severities map that changes rule_name_1 to ERROR
108+
final configSeverities = <String, ErrorSeverity>{
109+
'rule_name_1': ErrorSeverity.ERROR,
110+
'rule_name_2': ErrorSeverity.WARNING,
111+
};
112+
113+
final converter = CustomAnalyzerConverter();
114+
115+
// Convert the error without config severities - should be INFO
116+
final defaultResult = converter.convertAnalysisError(error);
117+
expect(defaultResult.severity.name, 'INFO');
118+
119+
// Convert the error with direct severity override
120+
final withSeverityParam = converter.convertAnalysisError(
121+
error,
122+
severity: ErrorSeverity.ERROR,
123+
);
124+
expect(withSeverityParam.severity.name, 'ERROR');
125+
126+
// Convert the error with config severities through convertAnalysisErrors
127+
final withConfigSeverities = converter.convertAnalysisErrors(
128+
[error],
129+
configSeverities: configSeverities,
130+
).single;
131+
132+
// Config severities should have overridden the default severity
133+
expect(withConfigSeverities.severity.name, 'ERROR');
134+
135+
// Create an error with a rule name that doesn't have a config severity
136+
final errorWithoutConfigSeverity = AnalysisError.tmp(
137+
source: source,
138+
offset: 0,
139+
length: 4,
140+
errorCode: const LintCode(
141+
name: 'no_config_rule',
142+
problemMessage: 'This is another lint',
143+
),
144+
);
145+
146+
final noConfigResult = converter.convertAnalysisErrors(
147+
[errorWithoutConfigSeverity],
148+
configSeverities: configSeverities,
149+
).single;
150+
151+
// Should use default severity when not in config
152+
expect(noConfigResult.severity.name, 'INFO');
153+
});
86154
}

packages/custom_lint_core/test/configs_test.dart

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:convert';
22
import 'dart:io' as io;
33

4+
import 'package:analyzer/error/error.dart';
45
import 'package:analyzer/file_system/file_system.dart';
56
import 'package:analyzer/file_system/physical_file_system.dart';
67
import 'package:custom_lint_core/custom_lint_core.dart';
@@ -115,6 +116,7 @@ custom_lint:
115116
test('Empty config', () {
116117
expect(CustomLintConfigs.empty.enableAllLintRules, null);
117118
expect(CustomLintConfigs.empty.rules, isEmpty);
119+
expect(CustomLintConfigs.empty.errors, isEmpty);
118120
});
119121

120122
group('parse', () {
@@ -188,6 +190,25 @@ custom_lint:
188190
);
189191
});
190192

193+
test('has an immutable map of errors', () {
194+
final analysisOptions = createAnalysisOptions('''
195+
custom_lint:
196+
errors:
197+
a: error
198+
''');
199+
final configs = CustomLintConfigs.parse(analysisOptions, packageConfig);
200+
201+
expect(
202+
configs.errors,
203+
{'a': ErrorSeverity.ERROR},
204+
);
205+
206+
expect(
207+
() => configs.errors['a'] = ErrorSeverity.INFO,
208+
throwsUnsupportedError,
209+
);
210+
});
211+
191212
test(
192213
'if custom_lint is present and defines some properties, merges with "include"',
193214
() {
@@ -338,6 +359,68 @@ foo:
338359
});
339360
});
340361

362+
test('Parses error severities from configs', () {
363+
final analysisOptions = createAnalysisOptions('''
364+
custom_lint:
365+
errors:
366+
rule_name_1: error
367+
rule_name_2: warning
368+
rule_name_3: info
369+
rule_name_4: none
370+
''');
371+
final configs = CustomLintConfigs.parse(analysisOptions, packageConfig);
372+
373+
expect(configs.errors, {
374+
'rule_name_1': ErrorSeverity.ERROR,
375+
'rule_name_2': ErrorSeverity.WARNING,
376+
'rule_name_3': ErrorSeverity.INFO,
377+
'rule_name_4': ErrorSeverity.NONE,
378+
});
379+
});
380+
381+
test('Handles unknown error severity values', () {
382+
final analysisOptions = createAnalysisOptions('''
383+
custom_lint:
384+
errors:
385+
rule_name_1: invalid_severity
386+
''');
387+
expect(
388+
() => CustomLintConfigs.parse(analysisOptions, packageConfig),
389+
throwsA(
390+
isArgumentError.having(
391+
(e) => e.message,
392+
'message',
393+
'Provided error severity: invalid_severity specified for key: rule_name_1 is not valid. '
394+
'Valid error severities are: none, info, warning, error',
395+
),
396+
),
397+
);
398+
});
399+
400+
test('Merges error severities from included config file', () {
401+
final includedFile = createAnalysisOptions('''
402+
custom_lint:
403+
errors:
404+
rule_name_1: error
405+
rule_name_2: warning
406+
''');
407+
408+
final analysisOptions = createAnalysisOptions('''
409+
include: ${includedFile.path}
410+
custom_lint:
411+
errors:
412+
rule_name_2: info
413+
rule_name_3: error
414+
''');
415+
final configs = CustomLintConfigs.parse(analysisOptions, packageConfig);
416+
417+
expect(configs.errors, {
418+
'rule_name_1': ErrorSeverity.ERROR,
419+
'rule_name_2': ErrorSeverity.INFO,
420+
'rule_name_3': ErrorSeverity.ERROR,
421+
});
422+
});
423+
341424
group('package config', () {
342425
test('single package', () async {
343426
final dir = createDir();
@@ -353,7 +436,6 @@ foo:
353436

354437
test('workspace', () async {
355438
final dir = createDir();
356-
print(dir);
357439
final projectPath = await createTempProject(
358440
tempDirPath: dir.path,
359441
projectName: testPackageName,

0 commit comments

Comments
 (0)