@@ -21,6 +21,12 @@ class InvalidationTester {
2121 /// The source assets on disk before the first build.
2222 final Set <AssetId > _sourceAssets = {};
2323
24+ /// Import statements.
25+ ///
26+ /// If an asset has an entry here then sources and generated sources will
27+ /// start with the import statements specified.
28+ final Map <String , List <String >> _importGraph = {};
29+
2430 /// The builders that will run.
2531 final List <TestBuilder > _builders = [];
2632
@@ -43,6 +49,9 @@ class InvalidationTester {
4349 /// The build number for "printOnFailure" output.
4450 int _buildNumber = 0 ;
4551
52+ /// Output number, for writing outputs that are different.
53+ int _outputNumber = 0 ;
54+
4655 /// Sets the assets that will be on disk before the first build.
4756 ///
4857 /// See the note on "names" in the class dartdoc.
@@ -53,6 +62,12 @@ class InvalidationTester {
5362 }
5463 }
5564
65+ // Sets the import graph for source files and generated files.
66+ void importGraph (Map <String , List <String >> importGraph) {
67+ _importGraph.clear ();
68+ _importGraph.addAll (importGraph);
69+ }
70+
5671 /// Adds a builder to the test.
5772 ///
5873 /// [from] and [to] are the input and output extension of the builder,
@@ -106,6 +121,13 @@ class InvalidationTester {
106121 _failureStrategies[name.assetId] = FailureStrategy .succeed;
107122 }
108123
124+ String _imports (AssetId id) {
125+ final imports = _importGraph[_assetIdToName (id)];
126+ return imports == null
127+ ? ''
128+ : imports.map ((i) => "import '${i .pathForImport }';" ).join ('\n ' );
129+ }
130+
109131 /// Does a build.
110132 ///
111133 /// For the initial build, do not pass [change] [delete] or [create] .
@@ -120,7 +142,7 @@ class InvalidationTester {
120142 throw StateError ('Do a build without change, delete or create first.' );
121143 }
122144 for (final id in _sourceAssets) {
123- assets[id] = '// initial source' ;
145+ assets[id] = '${ _imports ( id )} // initial source' ;
124146 }
125147 } else {
126148 // Create the new filesystem from the previous build state.
@@ -131,7 +153,8 @@ class InvalidationTester {
131153
132154 // Make the requested updates.
133155 if (change != null ) {
134- assets[change.assetId] = '${assets [change .assetId ]}\n // changed' ;
156+ assets[change.assetId] =
157+ '${_imports (change .assetId )}}\n // ${++_outputNumber }' ;
135158 }
136159 if (delete != null ) {
137160 if (assets.containsKey (delete.assetId)) {
@@ -148,7 +171,7 @@ class InvalidationTester {
148171 if (assets.containsKey (create.assetId)) {
149172 throw StateError ('Asset $create to create already exists in: $assets ' );
150173 }
151- assets[create.assetId] = '// initial source' ;
174+ assets[create.assetId] = '${ _imports ( create . assetId )} // initial source' ;
152175 }
153176
154177 // Build and check what changed.
@@ -259,6 +282,11 @@ class TestBuilderBuilder {
259282 _builder.otherReads.add (name);
260283 }
261284
285+ /// Test setup: the builder will parse the Dart source asset with [name] .
286+ void resolvesOther (String name) {
287+ _builder.otherResolves.add (name);
288+ }
289+
262290 /// Test setup: the builder will write the asset that is [extension] applied
263291 /// to the primary input.
264292 ///
@@ -287,6 +315,9 @@ class TestBuilder implements Builder {
287315 /// Names of assets that the builder will read.
288316 List <String > otherReads = [];
289317
318+ /// Names of assets that the builder will resolve.
319+ List <String > otherResolves = [];
320+
290321 /// Extensions of assets that the builder will write.
291322 ///
292323 /// The extensions are applied to the primary input asset ID with
@@ -312,14 +343,21 @@ class TestBuilder implements Builder {
312343 content.add (await buildStep.readAsString (read.assetId));
313344 }
314345 }
346+ for (final resolve in otherResolves) {
347+ content.add (resolve.assetId.toString ());
348+ await buildStep.resolver.libraryFor (resolve.assetId);
349+ }
315350 for (final write in writes) {
316351 final writeId = buildStep.inputId.replaceAllPathExtensions (write);
317352 final outputStrategy =
318353 _tester._outputStrategies[writeId] ?? OutputStrategy .inputDigest;
354+ final inputHash = base64.encode (
355+ md5.convert (utf8.encode (content.join ('\n\n ' ))).bytes,
356+ );
319357 final output = switch (outputStrategy) {
320- OutputStrategy .fixed => '' ,
358+ OutputStrategy .fixed => _tester. _imports (writeId) ,
321359 OutputStrategy .inputDigest =>
322- '// ${ base64 . encode ( md5 . convert ( utf8 . encode ( content . join ( ' \n\n ' ))). bytes )} ' ,
360+ '${ _tester . _imports ( writeId )} \n // $ inputHash ' ,
323361 OutputStrategy .none => null ,
324362 };
325363 if (output != null ) {
@@ -354,6 +392,9 @@ extension StringExtension on String {
354392 AssetId get generatedAssetId =>
355393 AssetId ('pkg' , '.dart_tool/build/generated/pkg/lib/$this .dart' );
356394
395+ /// Maps "names" to relative import path.
396+ String get pathForImport => '$this .dart' ;
397+
357398 /// Displays trimmed and with two space indent.
358399 String get trimAndIndent => ' ${toString ().trim ().replaceAll ('\n ' , '\n ' )}' ;
359400}
0 commit comments