Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(coverde): use optional filterd paths prefix #69

Merged
merged 8 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test(coverde): update filter command test
  • Loading branch information
mrverdant13 committed Aug 17, 2023
commit 6a994a136ccb0e15908235d617ca930d734448bc
308 changes: 302 additions & 6 deletions packages/coverde_cli/test/src/commands/filter/filter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ Filter a coverage trace file.

Filter the coverage info by ignoring data related to files with paths that matches the given FILTERS.
The coverage data is taken from the INPUT_LCOV_FILE file and the result is appended to the OUTPUT_LCOV_FILE file.

All the relative paths in the resulting coverage trace file will be prefixed with PATHS_PARENT, if provided.
If an absolute path is found in the coverage trace file, the process will fail.
''';

// ACT
Expand All @@ -70,6 +73,84 @@ The coverage data is taken from the INPUT_LCOV_FILE file and the result is appen
'''

AND an existing trace file to filter
├─ THAT does not contain any absolute path
AND a set of unquoted patterns to be filtered
WHEN the command is invoked
THEN a filtered trace file should be created
├─ BY dumping the filtered content to the default destination
''',
() async {
// ARRANGE
const patterns = <String>['.g.dart'];
final patternsRegex = patterns.map(RegExp.new);
final originalFilePath = 'original.relative.lcov.info'.fixturePath;
final filteredFilePath =
'unquoted.relative.filtered.lcov.info'.fixturePath;
final expectedFilteredFilePath =
'expected.unquoted.relative.filtered.lcov.info'.fixturePath;
final originalFile = File(originalFilePath);
final filteredFile = File(filteredFilePath);
final expectedFilteredFile = File(expectedFilteredFilePath);
if (filteredFile.existsSync()) {
filteredFile.deleteSync(recursive: true);
}
final originalTraceFile = TraceFile.parse(
originalFile.readAsStringSync(),
);
final originalFileIncludeFileThatMatchPatterns =
originalTraceFile.includeFileThatMatchPatterns(patterns);
final filesDataToBeRemoved =
originalTraceFile.sourceFilesCovData.where(
(d) => patternsRegex.any(
(r) => r.hasMatch(d.source.path),
),
);

expect(originalFile.existsSync(), isTrue);
expect(filteredFile.existsSync(), isFalse);
expect(originalFileIncludeFileThatMatchPatterns, isTrue);

// ACT
await cmdRunner.run([
filterCmd.name,
'--${FilterCommand.inputOption}',
originalFilePath,
'--${FilterCommand.outputOption}',
filteredFilePath,
'--${FilterCommand.filtersOption}',
patterns.join(','),
]);

// ASSERT
const splitter = LineSplitter();
expect(originalFile.existsSync(), isTrue);
expect(filteredFile.existsSync(), isTrue);
final filteredFileContent = filteredFile.readAsStringSync();
final expectedFilteredFileContent =
expectedFilteredFile.readAsStringSync();
final filteredFileIncludeFileThatMatchPatterns =
TraceFile.parse(filteredFileContent)
.includeFileThatMatchPatterns(patterns);
expect(filteredFileIncludeFileThatMatchPatterns, isFalse);
expect(
splitter.convert(filteredFileContent),
splitter.convert(expectedFilteredFileContent),
reason: 'Error: Non-matching filtered file content.',
);
for (final fileData in filesDataToBeRemoved) {
final path = fileData.source.path;
verify(
() => out.writeln('<$path> coverage data ignored.'),
).called(1);
}
},
);

test(
'''

AND an existing trace file to filter
├─ THAT contains at least one absolute path
AND a set of unquoted patterns to be filtered
WHEN the command is invoked
THEN a filtered trace file should be created
Expand All @@ -79,10 +160,11 @@ THEN a filtered trace file should be created
// ARRANGE
const patterns = <String>['.g.dart'];
final patternsRegex = patterns.map(RegExp.new);
final originalFilePath = 'original.lcov.info'.fixturePath;
final filteredFilePath = 'unquoted.filtered.lcov.info'.fixturePath;
final originalFilePath = 'original.absolute.lcov.info'.fixturePath;
final filteredFilePath =
'unquoted.absolute.filtered.lcov.info'.fixturePath;
final expectedFilteredFilePath =
'expected.unquoted.filtered.lcov.info'.fixturePath;
'expected.unquoted.absolute.filtered.lcov.info'.fixturePath;
final originalFile = File(originalFilePath);
final filteredFile = File(filteredFilePath);
final expectedFilteredFile = File(expectedFilteredFilePath);
Expand Down Expand Up @@ -145,6 +227,7 @@ THEN a filtered trace file should be created
'''

AND an existing trace file to filter
├─ THAT does not contain any absolute path
AND a set of raw patterns to be filtered
WHEN the command is invoked
THEN a filtered trace file should be created
Expand All @@ -154,10 +237,11 @@ THEN a filtered trace file should be created
// ARRANGE
const patterns = <String>[r'\.g\.dart'];
final patternsRegex = patterns.map(RegExp.new);
final originalFilePath = 'original.lcov.info'.fixturePath;
final filteredFilePath = 'raw.filtered.lcov.info'.fixturePath;
final originalFilePath = 'original.relative.lcov.info'.fixturePath;
final filteredFilePath =
'raw.relative.filtered.lcov.info'.fixturePath;
final expectedFilteredFilePath =
'expected.raw.filtered.lcov.info'.fixturePath;
'expected.raw.relative.filtered.lcov.info'.fixturePath;
final originalFile = File(originalFilePath);
final filteredFile = File(filteredFilePath);
final expectedFilteredFile = File(expectedFilteredFilePath);
Expand Down Expand Up @@ -219,6 +303,218 @@ THEN a filtered trace file should be created
test(
'''

AND an existing trace file to filter
├─ THAT contains at least one absolute path
AND a set of raw patterns to be filtered
WHEN the command is invoked
THEN a filtered trace file should be created
├─ BY dumping the filtered content to the default destination
''',
() async {
// ARRANGE
const patterns = <String>[r'\.g\.dart'];
final patternsRegex = patterns.map(RegExp.new);
final originalFilePath = 'original.absolute.lcov.info'.fixturePath;
final filteredFilePath =
'raw.absolute.filtered.lcov.info'.fixturePath;
final expectedFilteredFilePath =
'expected.raw.absolute.filtered.lcov.info'.fixturePath;
final originalFile = File(originalFilePath);
final filteredFile = File(filteredFilePath);
final expectedFilteredFile = File(expectedFilteredFilePath);
if (filteredFile.existsSync()) {
filteredFile.deleteSync(recursive: true);
}
final originalTraceFile = TraceFile.parse(
originalFile.readAsStringSync(),
);
final originalFileIncludeFileThatMatchPatterns =
originalTraceFile.includeFileThatMatchPatterns(patterns);
final filesDataToBeRemoved =
originalTraceFile.sourceFilesCovData.where(
(d) => patternsRegex.any(
(r) => r.hasMatch(d.source.path),
),
);

expect(originalFile.existsSync(), isTrue);
expect(filteredFile.existsSync(), isFalse);
expect(originalFileIncludeFileThatMatchPatterns, isTrue);

// ACT
await cmdRunner.run([
filterCmd.name,
'--${FilterCommand.inputOption}',
originalFilePath,
'--${FilterCommand.outputOption}',
filteredFilePath,
'--${FilterCommand.filtersOption}',
patterns.join(','),
]);

// ASSERT
const splitter = LineSplitter();
expect(originalFile.existsSync(), isTrue);
expect(filteredFile.existsSync(), isTrue);
final filteredFileContent = filteredFile.readAsStringSync();
final expectedFilteredFileContent =
expectedFilteredFile.readAsStringSync();
final filteredFileIncludeFileThatMatchPatterns =
TraceFile.parse(filteredFileContent)
.includeFileThatMatchPatterns(patterns);
expect(filteredFileIncludeFileThatMatchPatterns, isFalse);
expect(
splitter.convert(filteredFileContent),
splitter.convert(expectedFilteredFileContent),
reason: 'Error: Non-matching filtered file content.',
);
for (final fileData in filesDataToBeRemoved) {
final path = fileData.source.path;
verify(
() => out.writeln('<$path> coverage data ignored.'),
).called(1);
}
},
);

test(
'''

AND an existing trace file to filter
├─ THAT does not contain any absolute path
AND a set of raw patterns to be filtered
AND a path to be used as prefix for the tested file paths
WHEN the command is invoked
THEN a filtered trace file should be created
├─ BY dumping the filtered content to the default destination
''',
() async {
// ARRANGE
const patterns = <String>[r'\.g\.dart'];
const pathsPrefix = 'root/parent/';
final patternsRegex = patterns.map(RegExp.new);
final originalFilePath = 'original.relative.lcov.info'.fixturePath;
final filteredFilePath =
'prefixed.relative.filtered.lcov.info'.fixturePath;
final expectedFilteredFilePath =
'expected.prefixed.relative.filtered.lcov.info'.fixturePath;
final originalFile = File(originalFilePath);
final filteredFile = File(filteredFilePath);
final expectedFilteredFile = File(expectedFilteredFilePath);
if (filteredFile.existsSync()) {
filteredFile.deleteSync(recursive: true);
}
final originalTraceFile = TraceFile.parse(
originalFile.readAsStringSync(),
);
final originalFileIncludeFileThatMatchPatterns =
originalTraceFile.includeFileThatMatchPatterns(patterns);
final filesDataToBeRemoved =
originalTraceFile.sourceFilesCovData.where(
(d) => patternsRegex.any(
(r) => r.hasMatch(d.source.path),
),
);

expect(originalFile.existsSync(), isTrue);
expect(filteredFile.existsSync(), isFalse);
expect(originalFileIncludeFileThatMatchPatterns, isTrue);

// ACT
await cmdRunner.run([
filterCmd.name,
'--${FilterCommand.inputOption}',
originalFilePath,
'--${FilterCommand.outputOption}',
filteredFilePath,
'--${FilterCommand.pathsParentOption}',
pathsPrefix,
'--${FilterCommand.filtersOption}',
patterns.join(','),
]);

// ASSERT
expect(originalFile.existsSync(), isTrue);
expect(filteredFile.existsSync(), isTrue);
final filteredFileContent = filteredFile.readAsStringSync();
final expectedFilteredFileContent =
expectedFilteredFile.readAsStringSync();
final filteredTraceFile = TraceFile.parse(filteredFileContent);
final expectedTraceFile =
TraceFile.parse(expectedFilteredFileContent);
final filteredFileIncludeFileThatMatchPatterns =
filteredTraceFile.includeFileThatMatchPatterns(patterns);
expect(filteredFileIncludeFileThatMatchPatterns, isFalse);
expect(
filteredTraceFile,
expectedTraceFile,
reason: 'Error: Non-matching trace files.',
);
for (final fileData in filesDataToBeRemoved) {
final path = fileData.source.path;
verify(
() => out.writeln('<$path> coverage data ignored.'),
).called(1);
}
},
);

test(
'''

AND a trace file content to filter
├─ THAT contains at least one absolute path
AND a set of raw patterns to be filtered
AND a path to be used as prefix for the tested file paths
WHEN the command is invoked
THEN an error indicating the issue should be thrown
AND no filtered file should be created
''',
() async {
// ARRANGE
const patterns = <String>[r'\.g\.dart'];
const pathsPrefix = 'root/parent/';
final originalFilePath = 'original.absolute.lcov.info'.fixturePath;
final filteredFilePath =
'prefixed.absolute.filtered.lcov.info'.fixturePath;
final originalFile = File(originalFilePath);
final filteredFile = File(filteredFilePath);
if (filteredFile.existsSync()) {
filteredFile.deleteSync(recursive: true);
}
final originalTraceFile = TraceFile.parse(
originalFile.readAsStringSync(),
);
final originalFileIncludeFileThatMatchPatterns =
originalTraceFile.includeFileThatMatchPatterns(patterns);

expect(originalFile.existsSync(), isTrue);
expect(filteredFile.existsSync(), isFalse);
expect(originalFileIncludeFileThatMatchPatterns, isTrue);

// ACT
Future<void> action() => cmdRunner.run([
filterCmd.name,
'--${FilterCommand.inputOption}',
originalFilePath,
'--${FilterCommand.outputOption}',
filteredFilePath,
'--${FilterCommand.pathsParentOption}',
pathsPrefix,
'--${FilterCommand.filtersOption}',
patterns.join(','),
]);

// ASSERT
expect(originalFile.existsSync(), isTrue);
expect(filteredFile.existsSync(), isFalse);
expect(action, throwsA(isA<UsageException>()));
},
);

test(
'''

AND a non-existing trace file to filter
AND a set of patterns to be filtered
WHEN the command is invoked
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
unquoted.filtered.lcov.info
raw.filtered.lcov.info
unquoted.relative.filtered.lcov.info
raw.relative.filtered.lcov.info
prefixed.relative.filtered.lcov.info
Loading