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

dart-lang / coverage / 10542474572

12 Aug 2024 11:34PM UTC coverage: 93.945%. Remained the same
10542474572

push

github

web-flow
Replace <> with () in comment (#503)

543 of 578 relevant lines covered (93.94%)

3.85 hits per line

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

98.21
/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'//\s*coverage:ignore-start[\w\d\s]*$');
9✔
78
final muliLineIgnoreEnd = RegExp(r'//\s*coverage:ignore-end[\w\d\s]*$');
9✔
79
final singleLineIgnore = RegExp(r'//\s*coverage:ignore-line[\w\d\s]*$');
9✔
80
final ignoreFile = RegExp(r'//\s*coverage:ignore-file[\w\d\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 throw a
84
/// [FormatException],
85
/// unless `coverage:ignore-file` is found.
86
/// Return [0, lines.length] if the whole file is ignored.
87
///
88
/// ```
89
/// 1.  final str = ''; // coverage:ignore-line
90
/// 2.  final str = '';
91
/// 3.  final str = ''; // coverage:ignore-start
92
/// 4.  final str = '';
93
/// 5.  final str = ''; // coverage:ignore-end
94
/// ```
95
///
96
/// Returns
97
/// ```
98
/// [
99
///   [1,1],
100
///   [3,5],
101
/// ]
102
/// ```
103
///
104
List<List<int>> getIgnoredLines(String filePath, List<String>? lines) {
3✔
105
  final ignoredLines = <List<int>>[];
3✔
106
  if (lines == null) return ignoredLines;
107

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

112
  FormatException? err;
113
  var i = 0;
114
  while (i < lines.length) {
6✔
115
    if (lines[i].contains(ignoreFile)) return allLines;
9✔
116

117
    if (lines[i].contains(muliLineIgnoreEnd)) {
9✔
118
      err ??= FormatException(
1✔
119
        'unmatched coverage:ignore-end found at $filePath:${i + 1}',
2✔
120
      );
121
    }
122

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

125
    if (lines[i].contains(muliLineIgnoreStart)) {
9✔
126
      final start = i;
127
      var isUnmatched = true;
128
      ++i;
3✔
129
      while (i < lines.length) {
6✔
130
        if (lines[i].contains(ignoreFile)) return allLines;
9✔
131
        if (lines[i].contains(muliLineIgnoreStart)) {
9✔
132
          err ??= FormatException(
2✔
133
            'coverage:ignore-start found at $filePath:${i + 1}'
1✔
134
            ' before previous coverage:ignore-start ended',
135
          );
136
          break;
137
        }
138

139
        if (lines[i].contains(muliLineIgnoreEnd)) {
9✔
140
          ignoredLines.add([start + 1, i + 1]);
12✔
141
          isUnmatched = false;
142
          break;
143
        }
144
        ++i;
3✔
145
      }
146

147
      if (isUnmatched) {
148
        err ??= FormatException(
2✔
149
          'coverage:ignore-start found at $filePath:${start + 1}'
1✔
150
          ' has no matching coverage:ignore-end',
151
        );
152
      }
153
    }
154
    ++i;
3✔
155
  }
156

157
  if (err == null) {
158
    return ignoredLines;
159
  }
160

161
  throw err;
162
}
163

164
extension StandardOutExtension on Stream<List<int>> {
165
  Stream<String> lines() =>
1✔
166
      transform(const SystemEncoding().decoder).transform(const LineSplitter());
3✔
167
}
168

169
Future<Uri> serviceUriFromProcess(Stream<String> procStdout) {
5✔
170
  // Capture the VM service URI.
171
  final serviceUriCompleter = Completer<Uri>();
5✔
172
  procStdout.listen((line) {
10✔
173
    if (!serviceUriCompleter.isCompleted) {
5✔
174
      final serviceUri = extractVMServiceUri(line);
5✔
175
      if (serviceUri != null) {
176
        serviceUriCompleter.complete(serviceUri);
5✔
177
      }
178
    }
179
  });
180
  return serviceUriCompleter.future;
5✔
181
}
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