resolveSources<T> function

Future<T> resolveSources<T>(
  1. Map<String, String> inputs,
  2. FutureOr<T> action(
    1. Resolver resolver
    ), {
  3. PackageConfig? packageConfig,
  4. Set<AssetId>? nonInputsToReadFromFilesystem,
  5. bool? readAllSourcesFromFilesystem,
  6. String? resolverFor,
  7. String? rootPackage,
  8. FutureOr<void> assetReaderChecks(
    1. TestReaderWriter
    )?,
  9. Future<void>? tearDown,
  10. Resolvers? resolvers,
})

Resolves and runs action using a created resolver for inputs.

Inputs accepts the pattern of <package>|<path>.dart, for example:

{
  'test_lib|lib/test_lib.dart': r'''
    // Contents of test_lib.dart go here.
  ''',
}

You may provide useAssetReader as the value of any input in order to read it from the file system instead of being forced to provide it inline as a string. This is useful for mixing real and mocked assets.

Example use:

import 'package:build_test/build_test.dart';
import 'package:test/test.dart';

void main() {
  test('should find a Foo type', () async {
    var library = await resolveSources({
      'test_lib|lib/test_lib.dart': r'''
        library example;

        class Foo {}
      ''',
    }, (resolver) => resolver.findLibraryByName('example'));
    expect(library.getType('Foo'), isNotNull);
  });
}

May provide resolverFor to return the Resolver for the asset provided, otherwise defaults to the first one in inputs.

NOTE: All package dependencies are resolved using PackageAssetReader

By default, only assets listed in inputs with value useAssetReader are read from the real filesystem. To read more real files: pass nonInputsToReadFromFilesystem for individual files, or readAllSourcesFromFilesystem to read all sources available with packageConfig.

In packages read using readAllSourcesFromFilesystem the asset graph .dart_tool/build/asset_graph.json is not loaded as it would cause generated output from the real build to be deleted, in RAM only, by the test build.

assetReaderChecks, if provided, runs after the action completes and can be used to add expectations on the reader state.

Implementation

Future<T> resolveSources<T>(
  Map<String, String> inputs,
  FutureOr<T> Function(Resolver resolver) action, {
  PackageConfig? packageConfig,
  Set<AssetId>? nonInputsToReadFromFilesystem,
  bool? readAllSourcesFromFilesystem,
  String? resolverFor,
  String? rootPackage,
  FutureOr<void> Function(TestReaderWriter)? assetReaderChecks,
  Future<void>? tearDown,
  Resolvers? resolvers,
}) {
  if (inputs.isEmpty) {
    throw ArgumentError.value(inputs, 'inputs', 'Must be a non-empty Map');
  }
  return _resolveAssets(
    inputs,
    rootPackage ?? AssetId.parse(inputs.keys.first).package,
    action,
    packageConfig: packageConfig,
    nonInputsToReadFromFilesystem: nonInputsToReadFromFilesystem,
    readAllSourcesFromFilesystem: readAllSourcesFromFilesystem,
    resolverFor: AssetId.parse(resolverFor ?? inputs.keys.first),
    assetReaderChecks: assetReaderChecks,
    resolvers: resolvers,
  );
}