-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathcoverage_options.dart
More file actions
120 lines (103 loc) · 3.56 KB
/
Copy pathcoverage_options.dart
File metadata and controls
120 lines (103 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import 'dart:io';
import 'package:cli_config/cli_config.dart';
import 'package:path/path.dart' as path;
class CoverageOptions {
const CoverageOptions({
this.outputDirectory,
required this.scopeOutput,
required this.functionCoverage,
required this.branchCoverage,
required this.packageDirectory,
required this.testScript,
});
factory CoverageOptions.fromConfig(
Config options, CoverageOptions defaultOptions, String? optionsFilePath) {
var outputDirectory = options.optionalString('output_directory') ??
defaultOptions.outputDirectory;
var packageDirectory = options.optionalString('package_directory') ??
defaultOptions.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(
outputDirectory: outputDirectory,
scopeOutput: options.optionalStringList('scope_output') ??
defaultOptions.scopeOutput,
functionCoverage: options.optionalBool('function_coverage') ??
defaultOptions.functionCoverage,
branchCoverage: options.optionalBool('branch_coverage') ??
defaultOptions.branchCoverage,
packageDirectory: packageDirectory,
testScript:
options.optionalString('test_script') ?? defaultOptions.testScript,
);
}
final String? outputDirectory;
final List<String> scopeOutput;
final bool functionCoverage;
final bool branchCoverage;
final String packageDirectory;
final String testScript;
}
class CoverageOptionsProvider {
CoverageOptionsProvider({
String? filePath,
}) {
final file = _getOptionsFile(filePath);
final fileContents = file?.readAsStringSync();
// Pass null to fileContents if the file is empty
final options = Config.fromConfigFileContents(
fileContents: fileContents,
fileSourceUri: file?.uri,
);
coverageOptions =
CoverageOptions.fromConfig(options, defaultOptions, optionsFilePath);
}
late final CoverageOptions coverageOptions;
late final String? optionsFilePath;
static const defaultFilePath = 'coverage_options.yaml';
File? _getOptionsFile(String? filePath) {
filePath ??= findOptionsFilePath();
optionsFilePath =
filePath != null ? path.normalize(path.absolute(filePath)) : null;
if (optionsFilePath == null) {
return null;
}
final file = File(optionsFilePath!);
return file.existsSync() ? file : null;
}
static String? findOptionsFilePath({Directory? directory}) {
var currentDir = directory ?? Directory.current;
while (true) {
final pubSpecFilePath = path.join(currentDir.path, 'pubspec.yaml');
if (File(pubSpecFilePath).existsSync()) {
final optionsFilePath = path.join(currentDir.path, defaultFilePath);
if (File(optionsFilePath).existsSync()) {
return optionsFilePath;
} else {
return null;
}
}
final parentDir = currentDir.parent;
if (parentDir.path == currentDir.path) {
return null;
}
currentDir = parentDir;
}
}
static const defaultOptions = CoverageOptions(
outputDirectory: null,
scopeOutput: [],
functionCoverage: false,
branchCoverage: false,
packageDirectory: '.',
testScript: 'test',
);
}