Skip to content

Commit

Permalink
refactor: use absolute and normalize instead of canonicalize for path…
Browse files Browse the repository at this point in the history
… handling
  • Loading branch information
Dhruv-Maradiya committed Jan 20, 2025
1 parent b1efad0 commit beac649
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
4 changes: 2 additions & 2 deletions pkgs/coverage/bin/collect_coverage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ Options _parseArgs(List<String> 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();
}
Expand Down
9 changes: 4 additions & 5 deletions pkgs/coverage/bin/format_coverage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,8 @@ Environment parseArgs(List<String> 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.');
Expand All @@ -256,8 +255,8 @@ Environment parseArgs(List<String> 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();
}
Expand Down
31 changes: 17 additions & 14 deletions pkgs/coverage/lib/src/coverage_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ output-directory: 'custom_coverage'
scope-output:
- 'lib'
- 'test'
package-name: 'Custom Dart Package'
package-name: 'Custom Dart Package'
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ branch-coverage: false
output-directory: 'custom_lcov'
package-directory: '.'
test_script: 'custom_test'
function-coverage: true
function-coverage: true

0 comments on commit beac649

Please sign in to comment.