diff --git a/pkgs/coverage/bin/collect_coverage.dart b/pkgs/coverage/bin/collect_coverage.dart index e25cd0049..f8d5bcc29 100644 --- a/pkgs/coverage/bin/collect_coverage.dart +++ b/pkgs/coverage/bin/collect_coverage.dart @@ -136,8 +136,8 @@ Options _parseArgs(List arguments, CoverageOptions defaultOptions) { (outPath == null && defaultOptions.outputDirectory == null)) { out = stdout; } else { - final outFilePath = p.canonicalize( - outPath ?? '${defaultOptions.outputDirectory}/coverage.json'); + final outFilePath = p.normalize(outPath ?? + p.absolute(defaultOptions.outputDirectory!, 'coverage.json')); final outFile = File(outFilePath)..createSync(recursive: true); out = outFile.openWrite(); } diff --git a/pkgs/coverage/bin/format_coverage.dart b/pkgs/coverage/bin/format_coverage.dart index a4537111e..44f06d491 100644 --- a/pkgs/coverage/bin/format_coverage.dart +++ b/pkgs/coverage/bin/format_coverage.dart @@ -242,9 +242,8 @@ Environment parseArgs(List arguments, CoverageOptions defaultOptions) { if (args['in'] == null && defaultOptions.outputDirectory == null) { fail('No input files given.'); } - final inputPath = args['in'] as String? ?? - '${defaultOptions.outputDirectory}/coverage.json'; - final input = p.absolute(p.normalize(inputPath)); + final input = p.normalize((args['in'] as String?) ?? + p.absolute(defaultOptions.outputDirectory!, 'coverage.json')); if (!FileSystemEntity.isDirectorySync(input) && !FileSystemEntity.isFileSync(input)) { fail('Provided input "${args["in"]}" is neither a directory nor a file.'); @@ -256,8 +255,8 @@ Environment parseArgs(List arguments, CoverageOptions defaultOptions) { (outPath == null && defaultOptions.outputDirectory == null)) { output = stdout; } else { - final outFilePath = p - .canonicalize(outPath ?? '${defaultOptions.outputDirectory}/lcov.info'); + final outFilePath = p.normalize( + outPath ?? p.absolute(defaultOptions.outputDirectory!, 'lcov.info')); final outfile = File(outFilePath)..createSync(recursive: true); output = outfile.openWrite(); } diff --git a/pkgs/coverage/lib/src/coverage_options.dart b/pkgs/coverage/lib/src/coverage_options.dart index b981d7dd2..2ee7b5337 100644 --- a/pkgs/coverage/lib/src/coverage_options.dart +++ b/pkgs/coverage/lib/src/coverage_options.dart @@ -14,19 +14,21 @@ class CoverageOptions { }); factory CoverageOptions.fromConfig( - Config options, CoverageOptions defaultOptions, String optionsFilePath) { + Config options, CoverageOptions defaultOptions, String? optionsFilePath) { var outputDirectory = options.optionalString('output_directory') ?? defaultOptions.outputDirectory; var packageDirectory = options.optionalString('package_directory') ?? defaultOptions.packageDirectory; - if (outputDirectory != null && !path.isAbsolute(outputDirectory)) { - outputDirectory = path.canonicalize( - path.join(path.dirname(optionsFilePath), outputDirectory)); - } - if (!path.isAbsolute(packageDirectory)) { - packageDirectory = path.canonicalize( - path.join(path.dirname(optionsFilePath), packageDirectory)); + if (optionsFilePath != null) { + if (outputDirectory != null && !path.isAbsolute(outputDirectory)) { + outputDirectory = path.normalize( + path.absolute(path.dirname(optionsFilePath), outputDirectory)); + } + if (!path.isAbsolute(packageDirectory)) { + packageDirectory = path.normalize( + path.absolute(path.dirname(optionsFilePath), packageDirectory)); + } } return CoverageOptions( @@ -74,18 +76,19 @@ class CoverageOptionsProvider { } late final CoverageOptions coverageOptions; - late final String optionsFilePath; + late final String? optionsFilePath; static const defaultFilePath = 'coverage_options.yaml'; File? _getOptionsFile(String? filePath) { filePath ??= _findOptionsFilePath(); - if (filePath == null) { - throw StateError('Could not find a pubspec.yaml file.'); - } - optionsFilePath = path.canonicalize(filePath); + optionsFilePath = filePath != null ? path.canonicalize(filePath) : null; + + if (optionsFilePath == null) { + return null; + } - final file = File(optionsFilePath); + final file = File(optionsFilePath!); return file.existsSync() ? file : null; } diff --git a/pkgs/coverage/test/test_coverage_options/partial_fields1.yaml b/pkgs/coverage/test/test_coverage_options/partial_fields1.yaml index 5a2036aae..47401c716 100644 --- a/pkgs/coverage/test/test_coverage_options/partial_fields1.yaml +++ b/pkgs/coverage/test/test_coverage_options/partial_fields1.yaml @@ -2,4 +2,4 @@ output-directory: 'custom_coverage' scope-output: - 'lib' - 'test' -package-name: 'Custom Dart Package' \ No newline at end of file +package-name: 'Custom Dart Package' diff --git a/pkgs/coverage/test/test_coverage_options/partial_fields2.yaml b/pkgs/coverage/test/test_coverage_options/partial_fields2.yaml index 01b956efc..0905871ab 100644 --- a/pkgs/coverage/test/test_coverage_options/partial_fields2.yaml +++ b/pkgs/coverage/test/test_coverage_options/partial_fields2.yaml @@ -5,4 +5,4 @@ branch-coverage: false output-directory: 'custom_lcov' package-directory: '.' test_script: 'custom_test' -function-coverage: true \ No newline at end of file +function-coverage: true