Skip to content

Commit 91838b2

Browse files
authored
Add resolveAsset. (#301)
* Add resolveAsset. * Update CHANGELOG.md
1 parent 2a2b70f commit 91838b2

File tree

5 files changed

+89
-10
lines changed

5 files changed

+89
-10
lines changed

build_test/CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
## 0.6.3
2+
3+
- Added `resolveAsset`, which is similar to `resolveSource` but specifies a
4+
real asset that lives on the file system. For example, to resolve the main
5+
library of `package:collection`:
6+
7+
```dart
8+
var pkgCollection = new AssetId('collection', 'lib/collection.dart');
9+
var resolver = await resolveAsset(pkgCollection);
10+
// ...
11+
```
12+
13+
## 0.6.2
14+
15+
- Internal version bump.
16+
117
## 0.6.1
218

319
- Declare an output extension in `_ResolveSourceBuilder` so it is not skipped

build_test/lib/src/resolve_source.dart

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,66 @@ import 'package_reader.dart';
5050
/// - by default, [PackageAssetReader.currentIsolate]. A custom [resolver] may
5151
/// be provided to map files not visible to the current package's runtime.
5252
Future<Resolver> resolveSource(String inputSource,
53-
{AssetId inputId, PackageResolver resolver, Future<Null> tearDown}) async {
54-
// If not provided use a fake asset and package.
55-
inputId ??= new AssetId('_resolve_source', '_resolve_source.dart');
53+
{AssetId inputId, PackageResolver resolver, Future<Null> tearDown}) =>
54+
_resolveAsset(
55+
inputId ?? new AssetId('_resolver_source', 'lib/_resolve_source.dart'),
56+
inputContents: inputSource,
57+
resolver: resolver,
58+
tearDown: tearDown);
59+
60+
/// Returns a future that completes with a source [Resolver] for [input].
61+
///
62+
/// Example use:
63+
/// ```dart
64+
/// var pkgBuildTest = new AssetId('build_test', 'lib/build_test.dart');
65+
/// var resolver = await resolveSource(pkgBuildTest);
66+
/// ```
67+
///
68+
/// By default, the returned [Resolver] is destroyed after the event loop is
69+
/// completed. In order to control the lifecycle, pass a [Completer] and
70+
/// complete it turning the `tearDown` phase of testing:
71+
/// ```dart
72+
/// Completer<Null> onTearDown;
73+
///
74+
/// setUp(() {
75+
/// onTearDown = new Completer<Null>();
76+
/// });
77+
///
78+
/// tearDown(() => onTearDown.complete());
79+
///
80+
/// test('...', () async {
81+
/// var resolver = await resolveAsset('...', tearDown: onTearDown.future);
82+
/// // Use resolver.
83+
/// });
84+
/// ```
85+
///
86+
/// **NOTE**: All `package` dependencies are resolved using [PackageAssetReader]
87+
/// - by default, [PackageAssetReader.currentIsolate]. A custom [resolver] may
88+
/// be provided to map files not visible to the current package's runtime.
89+
Future<Resolver> resolveAsset(AssetId input,
90+
{PackageResolver resolver, Future<Null> tearDown}) =>
91+
_resolveAsset(input, resolver: resolver, tearDown: tearDown);
92+
93+
/// Internal only backing implementation of `resolveAsset` and `resolveSource`.
94+
///
95+
/// If [inputContents] is non-null, it is used instead of reading [input] from
96+
/// the file system.
97+
Future<Resolver> _resolveAsset(AssetId input,
98+
{String inputContents,
99+
PackageResolver resolver,
100+
Future<Null> tearDown}) async {
56101
resolver ??= PackageResolver.current;
57102
tearDown ??= new Future.delayed(Duration.ZERO);
58103
var syncResolver = await resolver.asSync;
59-
var reader = new PackageAssetReader(syncResolver, inputId.package);
104+
var reader = new PackageAssetReader(syncResolver, input.package);
60105
var completer = new Completer<Resolver>();
61106
var builder = new _ResolveSourceBuilder(completer, tearDown);
62-
var inputs = [inputId];
107+
var inputs = [input];
63108
var inMemory = new InMemoryAssetReader(
64109
sourceAssets: {
65-
inputId: new DatedString(inputSource),
110+
input: new DatedString(inputContents ?? await reader.readAsString(input)),
66111
},
67-
rootPackage: inputId.package,
112+
rootPackage: input.package,
68113
);
69114
// We don't care about the results of this build.
70115
// ignore: unawaited_futures
@@ -74,7 +119,7 @@ Future<Resolver> resolveSource(String inputSource,
74119
new MultiAssetReader([inMemory, reader]),
75120
new InMemoryAssetWriter(),
76121
const BarbackResolvers(),
77-
rootPackage: inputId.package,
122+
rootPackage: input.package,
78123
);
79124
return completer.future;
80125
}

build_test/pubspec.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: build_test
2-
description: Testing utilities for users of package:build.
3-
version: 0.6.2
2+
description: Utilities for writing unit tests of Builders.
3+
version: 0.6.3-dev
44
author: Dart Team <[email protected]>
55
homepage: https://github.com/dart-lang/build
66

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright (c) 2017, 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+
library example_lib;
6+
7+
class Example {}

build_test/test/resolve_source_test.dart

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,17 @@ void main() {
5353
);
5454
});
5555
});
56+
57+
group('should resolveAsset', () {
58+
Resolver resolver;
59+
60+
test('asset:build_test/test/_files/example_lib.dart', () async {
61+
var asset = new AssetId('build_test', 'test/_files/example_lib.dart');
62+
resolver = await resolveAsset(asset);
63+
var libExample = resolver.getLibraryByName('example_lib');
64+
expect(libExample.getType('Example'), isNotNull);
65+
});
66+
});
5667
}
5768

5869
String _toStringId(InterfaceType t) =>

0 commit comments

Comments
 (0)