Skip to content

Commit

Permalink
add file path to err message and test err message
Browse files Browse the repository at this point in the history
  • Loading branch information
kturney committed Jul 4, 2023
1 parent d7fe912 commit c72d047
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/src/hitmap.dart
Expand Up @@ -76,7 +76,7 @@ class HitMap {
final path = resolver.resolve(source);
if (path != null) {
final lines = loader.loadSync(path) ?? [];
ignoredLinesList = getIgnoredLines(lines);
ignoredLinesList = getIgnoredLines(path, lines);

// Ignore the whole file.
if (ignoredLinesList.length == 1 &&
Expand Down
8 changes: 4 additions & 4 deletions lib/src/util.dart
Expand Up @@ -100,7 +100,7 @@ final ignoreFile = RegExp(r'//\s*coverage:ignore-file[\w\d\s]*$');
/// ]
/// ```
///
List<List<int>> getIgnoredLines(List<String>? lines) {
List<List<int>> getIgnoredLines(String filePath, List<String>? lines) {
final ignoredLines = <List<int>>[];
if (lines == null) return ignoredLines;

Expand All @@ -115,7 +115,7 @@ List<List<int>> getIgnoredLines(List<String>? lines) {

if (lines[i].contains(muliLineIgnoreEnd)) {
err ??= FormatException(
'unmatched coverage:ignore-end found on line ${i + 1}',
'unmatched coverage:ignore-end found at $filePath:${i + 1}',
);
}

Expand All @@ -129,7 +129,7 @@ List<List<int>> getIgnoredLines(List<String>? lines) {
if (lines[i].contains(ignoreFile)) return allLines;
if (lines[i].contains(muliLineIgnoreStart)) {
err ??= FormatException(
'coverage:ignore-start found on line ${i + 1}'
'coverage:ignore-start found at $filePath:${i + 1}'
' before previous coverage:ignore-start ended',
);
break;
Expand All @@ -145,7 +145,7 @@ List<List<int>> getIgnoredLines(List<String>? lines) {

if (isUnmatched) {
err ??= FormatException(
'coverage:ignore-start found on line ${start + 1}'
'coverage:ignore-start found at $filePath:${start + 1}'
' has no matching coverage:ignore-end',
);
}
Expand Down
64 changes: 52 additions & 12 deletions test/util_test.dart
Expand Up @@ -177,11 +177,51 @@ void main() {
];

test('throws FormatException when the annotations are not balanced', () {
for (final content in invalidSources) {
for (var i = 0; i < invalidSources.length; ++i) {
late String errMsg;
switch (i) {
case 0:
errMsg = 'coverage:ignore-start found at content-0.dart:3'
' before previous coverage:ignore-start ended';
break;
case 1:
errMsg = 'coverage:ignore-start found at content-1.dart:3'
' before previous coverage:ignore-start ended';
break;
case 2:
errMsg = 'unmatched coverage:ignore-end found at content-2.dart:5';
break;
case 3:
errMsg = 'unmatched coverage:ignore-end found at content-3.dart:1';
break;
case 4:
errMsg = 'unmatched coverage:ignore-end found at content-4.dart:1';
break;
case 5:
errMsg = 'unmatched coverage:ignore-end found at content-5.dart:1';
break;
case 6:
errMsg = 'unmatched coverage:ignore-end found at content-6.dart:1';
break;
case 7:
errMsg =
'coverage:ignore-start found at content-7.dart:1 has no matching'
' coverage:ignore-end';
break;
default:
throw UnimplementedError('expectation for not balanced case $i');
}

final content = invalidSources[i].split('\n');
expect(
() => getIgnoredLines(content.split('\n')),
throwsFormatException,
reason: content,
() => getIgnoredLines('content-$i.dart', content),
throwsA(
allOf(
isFormatException,
predicate((FormatException e) => e.message == errMsg),
),
),
reason: 'expected FormatException with message "$errMsg"',
);
}
});
Expand All @@ -192,7 +232,7 @@ void main() {
for (final content in invalidSources) {
final lines = content.split('\n');
lines.add(' // coverage:ignore-file');
expect(getIgnoredLines(lines), [
expect(getIgnoredLines('', lines), [
[0, lines.length]
]);
}
Expand All @@ -205,7 +245,7 @@ void main() {
'''
.split('\n');

expect(getIgnoredLines(lines), [
expect(getIgnoredLines('', lines), [
[0, lines.length]
]);
});
Expand All @@ -221,7 +261,7 @@ void main() {
'''
.split('\n');

expect(getIgnoredLines(lines), [
expect(getIgnoredLines('', lines), [
[1, 3],
[4, 6],
]);
Expand All @@ -235,7 +275,7 @@ void main() {
'''
.split('\n');

expect(getIgnoredLines(lines), [
expect(getIgnoredLines('', lines), [
[1, 1],
[2, 2],
[3, 3],
Expand All @@ -252,7 +292,7 @@ void main() {
'''
.split('\n');

expect(getIgnoredLines(lines), [
expect(getIgnoredLines('', lines), [
[3, 3],
]);
});
Expand All @@ -269,7 +309,7 @@ void main() {
"final str = ''; // coverage:ignore-end \t \t ",
];

expect(getIgnoredLines(lines), [
expect(getIgnoredLines('', lines), [
[1, 3],
[4, 4],
[5, 6],
Expand All @@ -286,7 +326,7 @@ void main() {
"final str = ''; //coverage:ignore-end",
];

expect(getIgnoredLines(lines), [
expect(getIgnoredLines('', lines), [
[1, 3],
[4, 4],
[5, 6],
Expand All @@ -303,7 +343,7 @@ void main() {
"final str = ''; // coverage:ignore-end it is done",
];

expect(getIgnoredLines(lines), [
expect(getIgnoredLines('', lines), [
[1, 3],
[4, 4],
[5, 6],
Expand Down

0 comments on commit c72d047

Please sign in to comment.