-
Notifications
You must be signed in to change notification settings - Fork 17
/
cli_client.dart
125 lines (101 loc) · 3.84 KB
/
cli_client.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
library dart_coveralls.cli_client;
import 'dart:async' show Future, Completer;
import 'dart:convert';
import 'dart:io';
import 'package:path/path.dart' as p;
import 'package:stack_trace/stack_trace.dart';
import 'collect_lcov.dart';
import 'coveralls_endpoint.dart';
import 'coveralls_entities.dart';
import 'log.dart';
import 'process_system.dart';
import 'services/travis.dart' as travis;
class CommandLineClient {
final String projectDirectory;
final String packageRoot;
final String token;
CommandLineClient._(this.projectDirectory, this.packageRoot, this.token);
factory CommandLineClient({String projectDirectory, String packageRoot,
String token, Map<String, String> environment}) {
if (projectDirectory == null) {
projectDirectory = p.current;
}
packageRoot = _calcPackageRoot(projectDirectory, packageRoot);
token = getToken(token, environment);
return new CommandLineClient._(projectDirectory, packageRoot, token);
}
Future<CoverageResult<String>> getLcovResult(String testFile,
{int workers, ProcessSystem processSystem: const ProcessSystem()}) {
var collector =
new LcovCollector(packageRoot, testFile, processSystem: processSystem);
return collector.getLcovInformation(workers: workers);
}
/// Returns [candidate] if not null, otherwise environment's REPO_TOKEN
///
/// This first checks if the given candidate is null. If it is not null,
/// the candidate will be returned. Otherwise, it searches the given
/// environment for "REPO_TOKEN" and returns the content of it. If
/// the given environment is null, it will be [Platform].environment.
static String getToken(String candidate, [Map<String, String> environment]) {
if (candidate != null && candidate.isNotEmpty) return candidate;
if (null == environment) environment = Platform.environment;
return environment["REPO_TOKEN"];
}
Future reportToCoveralls(String testFile, {int workers,
ProcessSystem processSystem: const ProcessSystem(),
String coverallsAddress, bool dryRun: false,
bool throwOnConnectivityError: false, int retry: 0,
bool excludeTestFiles: false, bool printJson}) async {
var rawLcov = await getLcovResult(testFile,
workers: workers, processSystem: processSystem);
rawLcov.printSummary();
var lcov = LcovDocument.parse(rawLcov.result.toString());
var serviceName = travis.getServiceName(Platform.environment);
var serviceJobId = travis.getServiceJobId(Platform.environment);
var report = CoverallsReport.parse(token, lcov, projectDirectory,
excludeTestFiles: excludeTestFiles,
serviceName: serviceName,
serviceJobId: serviceJobId);
if (printJson) {
print(const JsonEncoder.withIndent(' ').convert(report));
}
if (dryRun) return;
var endpoint = new CoverallsEndpoint(coverallsAddress);
try {
var encoded = JSON.encode(report);
await _sendLoop(endpoint, encoded, retry: retry);
} catch (e, stack) {
if (throwOnConnectivityError) rethrow;
stderr.writeln('Error sending results');
stderr.writeln(e);
stderr.writeln(new Chain.forTrace(stack).terse);
}
}
}
String _calcPackageRoot(String packageDir, String packageRoot) {
assert(p.isAbsolute(packageDir));
if (packageRoot == null) {
packageRoot = 'packages';
}
if (p.isRelative(packageRoot)) {
packageRoot = p.join(packageDir, packageRoot);
}
return p.normalize(packageRoot);
}
Future _sendLoop(CoverallsEndpoint endpoint, String covString,
{int retry: 0}) async {
var currentRetryCount = 0;
while (true) {
try {
await endpoint.sendToCoveralls(covString);
return;
} catch (e) {
if (currentRetryCount >= retry) {
rethrow;
}
currentRetryCount++;
log.warning('Error sending', e);
log.info("Retrying $currentRetryCount of $retry.");
}
}
}