From 4bc829607f5aef3562528478c6aec39f8d1a4480 Mon Sep 17 00:00:00 2001 From: civts Date: Wed, 10 Mar 2021 10:34:48 +0100 Subject: [PATCH 1/5] Remove lcov dependency. Closes #38 --- lib/src/functions.dart | 12 +++++++----- pubspec.yaml | 1 - 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/src/functions.dart b/lib/src/functions.dart index e42129a..9a31b01 100644 --- a/lib/src/functions.dart +++ b/lib/src/functions.dart @@ -7,7 +7,6 @@ import 'dart:io'; import 'package:coverage/coverage.dart' as coverage; import 'package:glob/glob.dart'; -import 'package:lcov/lcov.dart'; import 'package:path/path.dart' as path; final _sep = path.separator; @@ -153,13 +152,16 @@ Uri _extractObservatoryUri(String str) { } double calculateLineCoverage(File lcovReport) { - final report = Report.fromCoverage(lcovReport.readAsStringSync()); + final lflhRegex = RegExp(r'(LF|LH):\d*$'); + final reportFileContent = lcovReport.readAsLinesSync(); + reportFileContent.retainWhere((l) => lflhRegex.hasMatch(l)); var totalLines = 0; var hitLines = 0; - for (final rec in report.records) { - for (final line in rec.lines.data) { + for (final line in reportFileContent) { + if (line.startsWith('LF')) { totalLines++; - hitLines += (line.executionCount > 0) ? 1 : 0; + } else { + hitLines++; } } return hitLines / totalLines; diff --git a/pubspec.yaml b/pubspec.yaml index 5eb388d..1fb932a 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -13,7 +13,6 @@ executables: dependencies: coverage: ^0.14.1 path: ^1.6.1 - lcov: ^5.3.0 glob: ^1.1.7 args: ^1.5.1 From cf9d9b5a33b97fac55decce87ad4b902f0b6e335 Mon Sep 17 00:00:00 2001 From: civts Date: Wed, 10 Mar 2021 10:38:04 +0100 Subject: [PATCH 2/5] Migrate to null safety --- bin/test_coverage.dart | 4 ++-- lib/src/functions.dart | 24 ++++++++++++++---------- pubspec.yaml | 14 +++++++------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/bin/test_coverage.dart b/bin/test_coverage.dart index 0dc80e4..f7b8e8a 100644 --- a/bin/test_coverage.dart +++ b/bin/test_coverage.dart @@ -45,12 +45,12 @@ Future main(List arguments) async { return; } - Glob excludeGlob; + Glob? excludeGlob; if (options['exclude'] is String) { excludeGlob = Glob(options['exclude']); } - String port = options['port']; + String? port = options['port']; final testFiles = findTestFiles(packageRoot, excludeGlob: excludeGlob); print('Found ${testFiles.length} test files.'); diff --git a/lib/src/functions.dart b/lib/src/functions.dart index 9a31b01..c2cc21d 100644 --- a/lib/src/functions.dart +++ b/lib/src/functions.dart @@ -11,7 +11,7 @@ import 'package:path/path.dart' as path; final _sep = path.separator; -List findTestFiles(Directory packageRoot, {Glob excludeGlob}) { +List findTestFiles(Directory packageRoot, {Glob? excludeGlob}) { final testsPath = path.join(packageRoot.absolute.path, 'test'); final testsRoot = Directory(testsPath); final contents = testsRoot.listSync(recursive: true); @@ -74,7 +74,7 @@ void generateMainScript(Directory packageRoot, List testFiles) { ).writeAsStringSync(buffer.toString()); } -Future runTestsAndCollect(String packageRoot, String port, +Future runTestsAndCollect(String packageRoot, String? port, {bool printOutput = false}) async { final script = path.join(packageRoot, 'test', '.test_coverage.dart'); final dartArgs = [ @@ -85,7 +85,7 @@ Future runTestsAndCollect(String packageRoot, String port, ]; final process = await Process.start('dart', dartArgs, workingDirectory: packageRoot); - final serviceUriCompleter = Completer(); + final serviceUriCompleter = Completer(); process.stdout .transform(utf8.decoder) .transform(const LineSplitter()) @@ -114,7 +114,7 @@ Future runTestsAndCollect(String packageRoot, String port, final data = await coverage.collect(serviceUri, true, true, false, {}); hitmap = await coverage.createHitmap(data['coverage']); } finally { - await process.stderr.drain>(); + await process.stderr.drain?>(); } final exitStatus = await process.exitCode; if (exitStatus != 0) { @@ -138,7 +138,7 @@ Future runTestsAndCollect(String packageRoot, String port, } // copied from `coverage` package -Uri _extractObservatoryUri(String str) { +Uri? _extractObservatoryUri(String str) { const kObservatoryListening = 'Observatory listening on '; final msgPos = str.indexOf(kObservatoryListening); if (msgPos == -1) return null; @@ -189,7 +189,11 @@ class _BadgeMetrics { final int rightX; final int rightLength; - _BadgeMetrics({this.width, this.rightX, this.rightLength}); + _BadgeMetrics({ + required this.width, + required this.rightX, + required this.rightLength, + }); factory _BadgeMetrics.forPercentage(double value) { final pct = (value * 100).floor(); @@ -223,8 +227,8 @@ String _color(double percentage) { 0.9: _Color(0x97, 0xCA, 0x00), 1.0: _Color(0x44, 0xCC, 0x11), }; - double lower; - double upper; + late double lower; + double? upper; for (final key in map.keys) { if (percentage < key) { upper = key; @@ -233,8 +237,8 @@ String _color(double percentage) { if (key < 1.0) lower = key; } upper ??= 1.0; - final lowerColor = map[lower]; - final upperColor = map[upper]; + final lowerColor = map[lower]!; + final upperColor = map[upper]!; final range = upper - lower; final rangePct = (percentage - lower) / range; final pctLower = 1 - rangePct; diff --git a/pubspec.yaml b/pubspec.yaml index 1fb932a..dc813c0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -5,17 +5,17 @@ homepage: https://github.com/pulyaevskiy/test-coverage author: Anatoly Pulyaevskiy environment: - sdk: '>=2.3.0 <3.0.0' + sdk: '>=2.12.0 <3.0.0' executables: test_coverage: dependencies: - coverage: ^0.14.1 - path: ^1.6.1 - glob: ^1.1.7 - args: ^1.5.1 + coverage: ^1.0.1 + path: ^1.8.0 + glob: ^2.0.0 + args: ^2.0.0 dev_dependencies: - test: ^1.0.0 - pedantic: ^1.7.0 + test: ^1.16.7 + pedantic: ^1.11.0 From f01f7d12458af42199f85d5ac874a90403c6ea94 Mon Sep 17 00:00:00 2001 From: civts Date: Wed, 10 Mar 2021 10:40:15 +0100 Subject: [PATCH 3/5] Bump up package version --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index dc813c0..e4fc208 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: test_coverage description: Command line utility to run tests in Dart VM and collect coverage data. -version: 0.5.0 +version: 0.6.0 homepage: https://github.com/pulyaevskiy/test-coverage author: Anatoly Pulyaevskiy From 029668577eda0c5cecc87c52b102e85b318736c0 Mon Sep 17 00:00:00 2001 From: civts Date: Wed, 10 Mar 2021 14:50:16 +0100 Subject: [PATCH 4/5] Update CHANGELOG --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8297529..7d8e2d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.6.0 + +- Migrated code to null safety (#33) +- Removed discontinued lcov dependency (#38) + ## 0.5.0 - Updated dependency on coverage package to ^0.14.1 (#24) From aa6ffb41c28d7dec4e3bb11c3b7c4729e8271a69 Mon Sep 17 00:00:00 2001 From: civts Date: Mon, 15 Mar 2021 12:02:02 +0100 Subject: [PATCH 5/5] Fix incorrect coverage value --- lib/src/functions.dart | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/src/functions.dart b/lib/src/functions.dart index c2cc21d..286bbc0 100644 --- a/lib/src/functions.dart +++ b/lib/src/functions.dart @@ -152,18 +152,21 @@ Uri? _extractObservatoryUri(String str) { } double calculateLineCoverage(File lcovReport) { - final lflhRegex = RegExp(r'(LF|LH):\d*$'); + final lflhRegex = RegExp(r'^L[FH]:(\d*)$'); final reportFileContent = lcovReport.readAsLinesSync(); reportFileContent.retainWhere((l) => lflhRegex.hasMatch(l)); var totalLines = 0; var hitLines = 0; for (final line in reportFileContent) { + var valueString = lflhRegex.matchAsPrefix(line)?.group(1) ?? '0'; + var value = int.tryParse(valueString) ?? 0; if (line.startsWith('LF')) { - totalLines++; + totalLines += value; } else { - hitLines++; + hitLines += value; } } + if (totalLines == 0) return 1; return hitLines / totalLines; }