-
Notifications
You must be signed in to change notification settings - Fork 51
/
test_with_coverage_test.dart
156 lines (123 loc) · 4.2 KB
/
test_with_coverage_test.dart
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
@Timeout(Duration(seconds: 60))
import 'dart:convert';
import 'dart:io';
import 'package:path/path.dart' as p;
import 'package:test/test.dart';
import 'package:test_descriptor/test_descriptor.dart' as d;
import 'package:test_process/test_process.dart';
import 'test_util.dart';
// this package
final _pkgDir = p.absolute('');
final _testWithCoveragePath = p.join(_pkgDir, 'bin', 'test_with_coverage.dart');
// test package
final _testPkgDirPath = p.join(_pkgDir, 'test', 'test_with_coverage_package');
/// Override PUB_CACHE
///
/// Use a subdirectory different from `test/` just in case there is a problem
/// with the clean up. If other packages are present under the `test/`
/// subdirectory their tests may accidentally get run when running `dart test`
final _pubCachePathInTestPkgSubDir = p.join(_pkgDir, 'var', 'pub-cache');
final _env = {'PUB_CACHE': _pubCachePathInTestPkgSubDir};
const _testPackageName = 'coverage_integration_test_for_test_with_coverage';
int _port = 9300;
Iterable<File> _dartFiles(String dir) =>
Directory(p.join(_testPkgDirPath, dir)).listSync().whereType<File>();
String _fixTestFile(String content) => content.replaceAll(
"import '../lib/",
"import 'package:$_testPackageName/",
);
void main() {
setUpAll(() async {
for (var dir in const ['lib', 'test']) {
await d.dir(dir, [
for (var dartFile in _dartFiles(dir))
d.file(
p.basename(dartFile.path),
_fixTestFile(dartFile.readAsStringSync()),
),
]).create();
}
var pubspecContent =
File(p.join(_testPkgDirPath, 'pubspec.yaml')).readAsStringSync();
expect(
pubspecContent.replaceAll('\r\n', '\n'),
contains(r'''
dependency_overrides:
coverage:
path: ../../
'''),
);
pubspecContent =
pubspecContent.replaceFirst('path: ../../', 'path: $_pkgDir');
await d.file('pubspec.yaml', pubspecContent).create();
final localPub = await _run(['pub', 'get']);
await localPub.shouldExit(0);
});
test('dart run bin/test_with_coverage.dart -f', () async {
final list = await _runTest(['run', _testWithCoveragePath, '-f']);
final sources = list.sources();
final functionHits = functionInfoFromSources(sources);
expect(
functionHits['package:$_testPackageName/validate_lib.dart'],
{
'product': 1,
'sum': 1,
},
);
});
test('dart run bin/test_with_coverage.dart -f -- -N sum', () async {
final list = await _runTest(
['run', _testWithCoveragePath, '-f'],
extraArgs: ['--', '-N', 'sum'],
);
final sources = list.sources();
final functionHits = functionInfoFromSources(sources);
expect(
functionHits['package:$_testPackageName/validate_lib.dart'],
{
'product': 0,
'sum': 1,
},
reason: 'only `sum` tests should be run',
);
});
test('dart run coverage:test_with_coverage', () async {
await _runTest(['run', 'coverage:test_with_coverage']);
});
test('dart pub global run coverage:test_with_coverage', () async {
final globalPub =
await _run(['pub', 'global', 'activate', '-s', 'path', _pkgDir]);
await globalPub.shouldExit(0);
await _runTest(
['pub', 'global', 'run', 'coverage:test_with_coverage'],
);
});
}
Future<TestProcess> _run(List<String> args) => TestProcess.start(
Platform.executable,
args,
workingDirectory: d.sandbox,
environment: _env,
);
Future<List<Map<String, dynamic>>> _runTest(
List<String> invokeArgs, {
List<String>? extraArgs,
}) async {
final process = await _run([
...invokeArgs,
'--port',
'${_port++}',
...?extraArgs,
]);
await process.shouldExit(0);
await d.dir(
'coverage',
[d.file('coverage.json', isNotEmpty), d.file('lcov.info', isNotEmpty)],
).validate();
final coverageDataFile = File(p.join(d.sandbox, 'coverage', 'coverage.json'));
final json = jsonDecode(coverageDataFile.readAsStringSync());
return coverageDataFromJson(json as Map<String, dynamic>);
}