• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

dart-lang / coverage / 4841411363

pending completion
4841411363

push

github

GitHub
Run no response bot daily (#440)

550 of 589 relevant lines covered (93.38%)

3.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.04
/lib/src/util.dart
1
// Copyright (c) 2014, the Dart project authors.  Please see the AUTHORS file
2
// for details. All rights reserved. Use of this source code is governed by a
3
// BSD-style license that can be found in the LICENSE file.
4

5
import 'dart:async';
6
import 'dart:convert';
7
import 'dart:io';
8

9
// TODO(cbracken) make generic
10
/// Retries the specified function with the specified interval and returns
11
/// the result on successful completion.
12
Future<dynamic> retry(Future Function() f, Duration interval,
4✔
13
    {Duration? timeout}) async {
14
  var keepGoing = true;
15

16
  Future<dynamic> withTimeout(Future Function() f, {Duration? duration}) {
4✔
17
    if (duration == null) {
18
      return f();
3✔
19
    }
20

21
    return f().timeout(duration, onTimeout: () {
5✔
22
      keepGoing = false;
23
      final msg = duration.inSeconds == 0
2✔
24
          ? '${duration.inMilliseconds}ms'
1✔
25
          : '${duration.inSeconds}s';
1✔
26
      throw StateError('Failed to complete within $msg');
2✔
27
    });
28
  }
29

30
  return withTimeout(() async {
8✔
31
    while (keepGoing) {
32
      try {
33
        return await f();
4✔
34
      } catch (_) {
35
        if (keepGoing) {
36
          await Future<dynamic>.delayed(interval);
4✔
37
        }
38
      }
39
    }
40
  }, duration: timeout);
41
}
42

43
/// Scrapes and returns the Dart VM service URI from a string, or null if not
44
/// found.
45
///
46
/// Potentially useful as a means to extract it from log statements.
47
Uri? extractVMServiceUri(String str) {
6✔
48
  final listeningMessageRegExp = RegExp(
6✔
49
    r'(?:Observatory|The Dart VM service is) listening on ((http|//)[a-zA-Z0-9:/=_\-\.\[\]]+)',
50
  );
51
  final match = listeningMessageRegExp.firstMatch(str);
6✔
52
  if (match != null) {
53
    return Uri.parse(match[1]!);
12✔
54
  }
55
  return null;
56
}
57

58
/// Returns an open port by creating a temporary Socket
59
Future<int> getOpenPort() async {
4✔
60
  ServerSocket socket;
61

62
  try {
63
    socket = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0);
8✔
64
  } catch (_) {
65
    // try again v/ V6 only. Slight possibility that V4 is disabled
66
    socket =
67
        await ServerSocket.bind(InternetAddress.loopbackIPv6, 0, v6Only: true);
×
68
  }
69

70
  try {
71
    return socket.port;
4✔
72
  } finally {
73
    await socket.close();
4✔
74
  }
75
}
76

77
final muliLineIgnoreStart = RegExp(r'// coverage:ignore-start\s*$');
9✔
78
final muliLineIgnoreEnd = RegExp(r'// coverage:ignore-end\s*$');
9✔
79
final singleLineIgnore = RegExp(r'// coverage:ignore-line\s*$');
9✔
80
final ignoreFile = RegExp(r'// coverage:ignore-file\s*$');
9✔
81

82
/// Return list containing inclusive range of lines to be ignored by coverage.
83
/// If there is a error in balancing the statements it will ignore nothing,
84
/// unless `coverage:ignore-file` is found.
85
/// Return [0, lines.length] if the whole file is ignored.
86
///
87
/// ```
88
/// 1.  final str = ''; // coverage:ignore-line
89
/// 2.  final str = '';
90
/// 3.  final str = ''; // coverage:ignore-start
91
/// 4.  final str = '';
92
/// 5.  final str = ''; // coverage:ignore-end
93
/// ```
94
///
95
/// Returns
96
/// ```
97
/// [
98
///   [1,1],
99
///   [3,5],
100
/// ]
101
/// ```
102
///
103
List<List<int>> getIgnoredLines(List<String>? lines) {
3✔
104
  final ignoredLines = <List<int>>[];
3✔
105
  if (lines == null) return ignoredLines;
106

107
  final allLines = [
3✔
108
    [0, lines.length]
6✔
109
  ];
110

111
  var isError = false;
112
  var i = 0;
113
  while (i < lines.length) {
6✔
114
    if (lines[i].contains(ignoreFile)) return allLines;
9✔
115

116
    if (lines[i].contains(muliLineIgnoreEnd)) isError = true;
9✔
117

118
    if (lines[i].contains(singleLineIgnore)) ignoredLines.add([i + 1, i + 1]);
21✔
119

120
    if (lines[i].contains(muliLineIgnoreStart)) {
9✔
121
      final start = i;
122
      ++i;
3✔
123
      while (i < lines.length) {
6✔
124
        if (lines[i].contains(ignoreFile)) return allLines;
9✔
125
        if (lines[i].contains(muliLineIgnoreStart)) {
9✔
126
          isError = true;
127
          break;
128
        }
129

130
        if (lines[i].contains(muliLineIgnoreEnd)) {
9✔
131
          ignoredLines.add([start + 1, i + 1]);
12✔
132
          break;
133
        }
134
        ++i;
3✔
135
      }
136
    }
137
    ++i;
3✔
138
  }
139

140
  return isError ? [] : ignoredLines;
1✔
141
}
142

143
extension StandardOutExtension on Stream<List<int>> {
144
  Stream<String> lines() =>
1✔
145
      transform(SystemEncoding().decoder).transform(const LineSplitter());
4✔
146
}
147

148
Future<Uri> serviceUriFromProcess(Stream<String> procStdout) {
5✔
149
  // Capture the VM service URI.
150
  final serviceUriCompleter = Completer<Uri>();
5✔
151
  procStdout.listen((line) {
10✔
152
    if (!serviceUriCompleter.isCompleted) {
5✔
153
      final serviceUri = extractVMServiceUri(line);
5✔
154
      if (serviceUri != null) {
155
        serviceUriCompleter.complete(serviceUri);
5✔
156
      }
157
    }
158
  });
159
  return serviceUriCompleter.future;
5✔
160
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc