|
| 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