• 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

95.12
/lib/src/resolver.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:io';
6

7
import 'package:package_config/package_config.dart';
8
import 'package:path/path.dart' as p;
9

10
/// [Resolver] resolves imports with respect to a given environment.
11
class Resolver {
12
  @Deprecated('Use Resolver.create')
1✔
13
  Resolver({this.packagesPath, this.sdkRoot})
14
      : _packages = packagesPath != null ? _parsePackages(packagesPath) : null,
×
15
        packagePath = null;
16

17
  Resolver._(
7✔
18
      {this.packagesPath,
19
      this.packagePath,
20
      this.sdkRoot,
21
      Map<String, Uri>? packages})
22
      : _packages = packages;
23

24
  static Future<Resolver> create({
7✔
25
    String? packagesPath,
26
    String? packagePath,
27
    String? sdkRoot,
28
  }) async {
29
    return Resolver._(
7✔
30
      packagesPath: packagesPath,
31
      packagePath: packagePath,
32
      sdkRoot: sdkRoot,
33
      packages: packagesPath != null
34
          ? _parsePackages(packagesPath)
1✔
35
          : (packagePath != null ? await _parsePackage(packagePath) : null),
3✔
36
    );
37
  }
38

39
  final String? packagesPath;
40
  final String? packagePath;
41
  final String? sdkRoot;
42
  final List<String> failed = [];
43
  final Map<String, Uri>? _packages;
44

45
  /// Returns the absolute path wrt. to the given environment or null, if the
46
  /// import could not be resolved.
47
  String? resolve(String scriptUri) {
4✔
48
    final uri = Uri.parse(scriptUri);
4✔
49
    if (uri.scheme == 'dart') {
8✔
50
      final sdkRoot = this.sdkRoot;
1✔
51
      if (sdkRoot == null) {
52
        // No sdk-root given, do not resolve dart: URIs.
53
        return null;
54
      }
55
      String filePath;
56
      if (uri.pathSegments.length > 1) {
3✔
57
        var path = uri.pathSegments[0];
2✔
58
        // Drop patch files, since we don't have their source in the compiled
59
        // SDK.
60
        if (path.endsWith('-patch')) {
1✔
61
          failed.add('$uri');
3✔
62
          return null;
63
        }
64
        // Canonicalize path. For instance: _collection-dev => _collection_dev.
65
        path = path.replaceAll('-', '_');
1✔
66
        final pathSegments = [
1✔
67
          sdkRoot,
68
          path,
69
          ...uri.pathSegments.sublist(1),
2✔
70
        ];
71
        filePath = p.joinAll(pathSegments);
1✔
72
      } else {
73
        // Resolve 'dart:something' to be something/something.dart in the SDK.
74
        final lib = uri.path;
1✔
75
        filePath = p.join(sdkRoot, lib, '$lib.dart');
2✔
76
      }
77
      return resolveSymbolicLinks(filePath);
1✔
78
    }
79
    if (uri.scheme == 'package') {
8✔
80
      final packages = _packages;
4✔
81
      if (packages == null) {
82
        return null;
83
      }
84

85
      final packageName = uri.pathSegments[0];
6✔
86
      final packageUri = packages[packageName];
3✔
87
      if (packageUri == null) {
88
        failed.add('$uri');
3✔
89
        return null;
90
      }
91
      final packagePath = p.fromUri(packageUri);
3✔
92
      final pathInPackage = p.joinAll(uri.pathSegments.sublist(1));
9✔
93
      return resolveSymbolicLinks(p.join(packagePath, pathInPackage));
6✔
94
    }
95
    if (uri.scheme == 'file') {
8✔
96
      return resolveSymbolicLinks(p.fromUri(uri));
6✔
97
    }
98
    // We cannot deal with anything else.
99
    failed.add('$uri');
3✔
100
    return null;
101
  }
102

103
  /// Returns a canonicalized path, or `null` if the path cannot be resolved.
104
  String? resolveSymbolicLinks(String path) {
4✔
105
    final normalizedPath = p.normalize(path);
4✔
106
    final type = FileSystemEntity.typeSync(normalizedPath, followLinks: true);
4✔
107
    if (type == FileSystemEntityType.notFound) return null;
4✔
108
    return File(normalizedPath).resolveSymbolicLinksSync();
8✔
109
  }
110

111
  static Map<String, Uri> _parsePackages(String packagesPath) {
1✔
112
    final content = File(packagesPath).readAsStringSync();
2✔
113
    final packagesUri = p.toUri(packagesPath);
1✔
114
    final parsed =
115
        PackageConfig.parseString(content, Uri.base.resolveUri(packagesUri));
3✔
116
    return {
1✔
117
      for (var package in parsed.packages) package.name: package.packageUriRoot
4✔
118
    };
119
  }
120

121
  static Future<Map<String, Uri>?> _parsePackage(String packagePath) async {
3✔
122
    final parsed = await findPackageConfig(Directory(packagePath));
6✔
123
    if (parsed == null) return null;
124
    return {
3✔
125
      for (var package in parsed.packages) package.name: package.packageUriRoot
12✔
126
    };
127
  }
128
}
129

130
/// Bazel URI resolver.
131
class BazelResolver extends Resolver {
132
  /// Creates a Bazel resolver with the specified workspace path, if any.
133
  BazelResolver({this.workspacePath = ''});
1✔
134

135
  final String workspacePath;
136

137
  /// Returns the absolute path wrt. to the given environment or null, if the
138
  /// import could not be resolved.
139
  @override
1✔
140
  String? resolve(String scriptUri) {
141
    final uri = Uri.parse(scriptUri);
1✔
142
    if (uri.scheme == 'dart') {
2✔
143
      // Ignore the SDK
144
      return null;
145
    }
146
    if (uri.scheme == 'package') {
2✔
147
      // TODO(cbracken) belongs in a Bazel package
148
      return _resolveBazelPackage(uri.pathSegments);
2✔
149
    }
150
    if (uri.scheme == 'file') {
2✔
151
      final runfilesPathSegment =
152
          '.runfiles/$workspacePath'.replaceAll(RegExp(r'/*$'), '/');
4✔
153
      final runfilesPos = uri.path.indexOf(runfilesPathSegment);
2✔
154
      if (runfilesPos >= 0) {
1✔
155
        final pathStart = runfilesPos + runfilesPathSegment.length;
2✔
156
        return uri.path.substring(pathStart);
2✔
157
      }
158
      return null;
159
    }
160
    if (uri.scheme == 'https' || uri.scheme == 'http') {
4✔
161
      return _extractHttpPath(uri);
1✔
162
    }
163
    // We cannot deal with anything else.
164
    failed.add('$uri');
×
165
    return null;
166
  }
167

168
  String _extractHttpPath(Uri uri) {
1✔
169
    final packagesPos = uri.pathSegments.indexOf('packages');
2✔
170
    if (packagesPos >= 0) {
1✔
171
      final workspacePath = uri.pathSegments.sublist(packagesPos + 1);
3✔
172
      return _resolveBazelPackage(workspacePath);
1✔
173
    }
174
    return uri.pathSegments.join('/');
2✔
175
  }
176

177
  String _resolveBazelPackage(List<String> pathSegments) {
1✔
178
    // TODO(cbracken) belongs in a Bazel package
179
    final packageName = pathSegments[0];
1✔
180
    final pathInPackage = pathSegments.sublist(1).join('/');
2✔
181
    final packagePath = packageName.contains('.')
1✔
182
        ? packageName.replaceAll('.', '/')
1✔
183
        : 'third_party/dart/$packageName';
1✔
184
    return '$packagePath/lib/$pathInPackage';
1✔
185
  }
186
}
187

188
/// Loads the lines of imported resources.
189
class Loader {
190
  final List<String> failed = [];
191

192
  /// Loads an imported resource and returns a [Future] with a [List] of lines.
193
  /// Returns `null` if the resource could not be loaded.
194
  Future<List<String>?> load(String path) async {
1✔
195
    try {
196
      // Ensure `readAsLines` runs within the try block so errors are caught.
197
      return await File(path).readAsLines();
2✔
198
    } catch (_) {
199
      failed.add(path);
×
200
      return null;
201
    }
202
  }
203

204
  /// Loads an imported resource and returns a [List] of lines.
205
  /// Returns `null` if the resource could not be loaded.
206
  List<String>? loadSync(String path) {
2✔
207
    try {
208
      // Ensure `readAsLinesSync` runs within the try block so errors are
209
      // caught.
210
      return File(path).readAsLinesSync();
4✔
211
    } catch (_) {
212
      failed.add(path);
×
213
      return null;
214
    }
215
  }
216
}
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

© 2026 Coveralls, Inc