Skip to content

Commit 23b5a66

Browse files
authored
feat: add problem matcher for dart analyze and integration tests (#178)
1 parent cb71272 commit 23b5a66

8 files changed

Lines changed: 242 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v1.8.0-wip
2+
3+
* Add a problem matcher for `dart analyze` to automatically create inline annotations on GitHub pull requests.
4+
15
## v1.7.2
26

37
* Update Node.js requirement to Node 24.

‎action.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ inputs:
1717
flavor:
1818
description: "The build flavor ('release' or 'raw')."
1919
required: false
20+
problem-matcher:
21+
description: "Register a problem matcher for dart analyze. Set to 'false' to disable."
22+
required: false
23+
default: "true"
2024
outputs:
2125
dart-version:
2226
description: 'The installed Dart version.'

‎dart-analyzer.json‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"problemMatcher": [
3+
{
4+
"owner": "dart-analyzer-bullet",
5+
"pattern": [
6+
{
7+
"regexp": "^\\s*(error|warning|info)\\s*•\\s*(.+):(\\d+):(\\d+)\\s*•\\s*(.+?)\\s*•\\s*(.+)$",
8+
"severity": 1,
9+
"file": 2,
10+
"line": 3,
11+
"column": 4,
12+
"message": 5,
13+
"code": 6
14+
}
15+
]
16+
},
17+
{
18+
"owner": "dart-analyzer-dash",
19+
"pattern": [
20+
{
21+
"regexp": "^\\s*(error|warning|info)\\s*-\\s*(.+):(\\d+):(\\d+)\\s*-\\s*(.+?)\\s*-\\s*(.+)$",
22+
"severity": 1,
23+
"file": 2,
24+
"line": 3,
25+
"column": 4,
26+
"message": 5,
27+
"code": 6
28+
}
29+
]
30+
}
31+
]
32+
}

‎dist/main.cjs‎

Lines changed: 21 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/sig.txt‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/main.dart‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@ Future<void> _impl(List<String> args) async {
137137
// Configure the outputs.
138138
core.setOutput('dart-version', getVersionFromSdk(sdkPath));
139139

140+
// Register problem matcher.
141+
final problemMatcher = core.getInput('problem-matcher');
142+
if (problemMatcher != 'false') {
143+
final actionPath = process.env('GITHUB_ACTION_PATH') ?? '.';
144+
print('::add-matcher::${path.join(actionPath, 'dart-analyzer.json')}');
145+
}
146+
140147
// Report success; print version.
141148
await exec.exec('dart', ['--version'.toJS].toJS).toDart;
142149
}

‎pubspec.yaml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ dev_dependencies:
1414
args: ^2.4.0
1515
crypto: ^3.0.0
1616
dart_flutter_team_lints: ^3.0.0
17+
test: ^1.24.0
18+
test_descriptor: ^2.0.0
19+
test_process: ^2.0.0

‎test/matcher_test.dart‎

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import 'dart:convert';
2+
import 'dart:io';
3+
4+
import 'package:path/path.dart' as path;
5+
import 'package:test/test.dart';
6+
import 'package:test_descriptor/test_descriptor.dart' as d;
7+
import 'package:test_process/test_process.dart';
8+
9+
void main() {
10+
late RegExp bulletRegExp;
11+
late RegExp dashRegExp;
12+
13+
setUpAll(() {
14+
final matcherFile = File('dart-analyzer.json');
15+
expect(matcherFile.existsSync(), isTrue,
16+
reason: 'dart-analyzer.json should exist');
17+
18+
final json = jsonDecode(matcherFile.readAsStringSync()) as Map;
19+
final matchers = json['problemMatcher'] as List;
20+
21+
final bulletMatcher = matchers[0] as Map;
22+
final bulletPattern = (bulletMatcher['pattern'] as List).single as Map;
23+
bulletRegExp = RegExp(bulletPattern['regexp'] as String);
24+
25+
final dashMatcher = matchers[1] as Map;
26+
final dashPattern = (dashMatcher['pattern'] as List).single as Map;
27+
dashRegExp = RegExp(dashPattern['regexp'] as String);
28+
});
29+
30+
group('Bullet Matcher', () {
31+
test('matches error line', () {
32+
const line =
33+
" error • lib/main.dart:10:20 • The argument type 'String' can't be assigned to the parameter type 'int' • argument_type_not_assignable";
34+
final match = bulletRegExp.firstMatch(line);
35+
36+
expect(match, isNotNull);
37+
expect(match!.group(1), 'error');
38+
expect(match.group(2), 'lib/main.dart');
39+
expect(match.group(3), '10');
40+
expect(match.group(4), '20');
41+
expect(
42+
match.group(5),
43+
"The argument type 'String' can't be assigned to the "
44+
"parameter type 'int'");
45+
expect(match.group(6), 'argument_type_not_assignable');
46+
});
47+
48+
test('matches warning line', () {
49+
const line =
50+
' warning • lib/file.dart:1:8 • Unused import • unused_import';
51+
final match = bulletRegExp.firstMatch(line);
52+
53+
expect(match, isNotNull);
54+
expect(match!.group(1), 'warning');
55+
expect(match.group(2), 'lib/file.dart');
56+
expect(match.group(3), '1');
57+
expect(match.group(4), '8');
58+
expect(match.group(5), 'Unused import');
59+
expect(match.group(6), 'unused_import');
60+
});
61+
62+
test('matches info line from user', () {
63+
const line =
64+
' info • test/matcher_test.dart:14:81 • The line length exceeds the 80-character limit. Try breaking the line across multiple lines. • lines_longer_than_80_chars';
65+
final match = bulletRegExp.firstMatch(line);
66+
67+
expect(match, isNotNull);
68+
expect(match!.group(1), 'info');
69+
expect(match.group(2), 'test/matcher_test.dart');
70+
expect(match.group(3), '14');
71+
expect(match.group(4), '81');
72+
expect(
73+
match.group(5),
74+
'The line length exceeds the 80-character limit. Try '
75+
'breaking the line across multiple lines.');
76+
expect(match.group(6), 'lines_longer_than_80_chars');
77+
});
78+
});
79+
80+
group('Dash Matcher', () {
81+
test('matches error line', () {
82+
const line =
83+
" error - bin/main.dart:2:11 - A value of type 'String' can't be assigned to a variable of type 'int'. - invalid_assignment";
84+
final match = dashRegExp.firstMatch(line);
85+
86+
expect(match, isNotNull);
87+
expect(match!.group(1), 'error');
88+
expect(match.group(2), 'bin/main.dart');
89+
expect(match.group(3), '2');
90+
expect(match.group(4), '11');
91+
expect(
92+
match.group(5),
93+
"A value of type 'String' can't be assigned to a "
94+
"variable of type 'int'.");
95+
expect(match.group(6), 'invalid_assignment');
96+
});
97+
98+
test('matches warning line', () {
99+
const line =
100+
" warning - bin/main.dart:2:7 - The value of the local variable 'x' isn't used. - unused_local_variable";
101+
final match = dashRegExp.firstMatch(line);
102+
103+
expect(match, isNotNull);
104+
expect(match!.group(1), 'warning');
105+
expect(match.group(2), 'bin/main.dart');
106+
expect(match.group(3), '2');
107+
expect(match.group(4), '7');
108+
expect(match.group(5), "The value of the local variable 'x' isn't used.");
109+
expect(match.group(6), 'unused_local_variable');
110+
});
111+
});
112+
113+
test('does not match unrelated lines', () {
114+
const line = 'Analyzing setup-dart...';
115+
expect(bulletRegExp.firstMatch(line), isNull);
116+
expect(dashRegExp.firstMatch(line), isNull);
117+
});
118+
119+
test('matches real analyzer output', () async {
120+
// Setup a dummy project with an error
121+
await d.dir('project', [
122+
d.file('pubspec.yaml', 'name: project\nenvironment:\n sdk: ^3.0.0'),
123+
d.dir('bin', [
124+
d.file('main.dart', '''
125+
void main() {
126+
int x = 'string'; // This is an error
127+
}
128+
'''),
129+
]),
130+
]).create();
131+
132+
// Run analyzer
133+
final process = await TestProcess.start(
134+
Platform.resolvedExecutable,
135+
['analyze'],
136+
workingDirectory: path.join(d.sandbox, 'project'),
137+
);
138+
139+
// Wait for process to finish
140+
await process.shouldExit();
141+
142+
// Read stdout
143+
final stdoutLines = await process.stdout.rest.toList();
144+
145+
print('Analyzer output:');
146+
for (final line in stdoutLines) {
147+
print(' $line');
148+
}
149+
150+
// Find the error line and verify it matches either regex
151+
var foundError = false;
152+
for (final line in stdoutLines) {
153+
final matchBullet = bulletRegExp.firstMatch(line);
154+
final matchDash = dashRegExp.firstMatch(line);
155+
156+
if (matchBullet != null || matchDash != null) {
157+
foundError = true;
158+
final match = matchBullet ?? matchDash;
159+
print('Matched line: $line');
160+
print(' Matched by: ${matchBullet != null ? "bullet" : "dash"}');
161+
print(' Severity: ${match!.group(1)}');
162+
print(' File: ${match.group(2)}'); // Both are group 2 now!
163+
break;
164+
}
165+
}
166+
167+
expect(foundError, isTrue,
168+
reason: 'Should have found an error line matching the regex');
169+
});
170+
}

0 commit comments

Comments
 (0)