Skip to content

Commit 9f07c7e

Browse files
authored
Add flattenOutput to testBuilder. (#4244)
Add a test for `generateFor`, fix a typo.
1 parent 17e38a6 commit 9f07c7e

File tree

5 files changed

+63
-5
lines changed

5 files changed

+63
-5
lines changed

build_runner/lib/src/build/build_series.dart

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import '../commands/watch/asset_change.dart';
1515
import '../constants.dart';
1616
import '../io/asset_tracker.dart';
1717
import '../io/filesystem_cache.dart';
18+
import '../io/generated_asset_hider.dart';
1819
import '../io/reader_writer.dart';
1920
import '../logging/build_log.dart';
2021
import 'asset_graph/graph.dart';
@@ -69,7 +70,10 @@ class BuildSeries {
6970
factory BuildSeries(BuildPlan buildPlan) {
7071
final assetGraph = buildPlan.takeAssetGraph();
7172
final readerWriter = buildPlan.readerWriter.copyWith(
72-
generatedAssetHider: assetGraph,
73+
generatedAssetHider:
74+
buildPlan.testingOverrides.flattenOutput
75+
? const NoopGeneratedAssetHider()
76+
: assetGraph,
7377
cache:
7478
buildPlan.buildOptions.enableLowResourcesMode
7579
? const PassthroughFilesystemCache()
@@ -247,7 +251,10 @@ class BuildSeries {
247251
}
248252
_assetGraph = _buildPlan.takeAssetGraph();
249253
_readerWriter = _buildPlan.readerWriter.copyWith(
250-
generatedAssetHider: _assetGraph,
254+
generatedAssetHider:
255+
_buildPlan.testingOverrides.flattenOutput
256+
? const NoopGeneratedAssetHider()
257+
: _assetGraph,
251258
cache:
252259
_buildPlan.buildOptions.enableLowResourcesMode
253260
? const PassthroughFilesystemCache()

build_runner/lib/src/build_plan/testing_overrides.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class TestingOverrides {
2929
final void Function(AssetId, Iterable<AssetId>)? reportUnusedAssetsForInput;
3030
final Resolvers? resolvers;
3131
final Stream<ProcessSignal>? terminateEventStream;
32+
final bool flattenOutput;
3233

3334
const TestingOverrides({
3435
this.builderApplications,
@@ -43,6 +44,7 @@ class TestingOverrides {
4344
this.reportUnusedAssetsForInput,
4445
this.resolvers,
4546
this.terminateEventStream,
47+
this.flattenOutput = false,
4648
});
4749

4850
TestingOverrides copyWith({
@@ -62,5 +64,6 @@ class TestingOverrides {
6264
reportUnusedAssetsForInput: reportUnusedAssetsForInput,
6365
resolvers: resolvers,
6466
terminateEventStream: terminateEventStream,
67+
flattenOutput: flattenOutput,
6568
);
6669
}

build_test/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
Deprecate `buildResult` in favor of these new members.
55
- Add `verbose` to `testBuilders` and related methods. Like the command line
66
flag it enables info logging from builders.
7+
- Add `flattenOutput` to `testBuilders`. Use it for a less realistic but simpler
8+
build in which generated outputs are always written directly to each package
9+
instead of "hidden" output being written to `.dart_tool/build/generated`.
710

811
## 3.4.1
912

build_test/lib/src/test_builder.dart

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ Future<TestBuilderResult> testBuilder(
139139
TestReaderWriter? readerWriter,
140140
bool verbose = false,
141141
bool enableLowResourceMode = false,
142+
bool flattenOutput = false,
142143
}) async {
143144
return testBuilders(
144145
[builder],
@@ -154,6 +155,7 @@ Future<TestBuilderResult> testBuilder(
154155
readerWriter: readerWriter,
155156
verbose: verbose,
156157
enableLowResourceMode: enableLowResourceMode,
158+
flattenOutput: flattenOutput,
157159
);
158160
}
159161

@@ -186,6 +188,7 @@ Future<TestBuilderResult> testBuilders(
186188
TestReaderWriter? readerWriter,
187189
bool verbose = false,
188190
bool enableLowResourceMode = false,
191+
bool flattenOutput = false,
189192
}) {
190193
final builderFactories = <BuilderFactory>[];
191194
final optionalBuilderFactories = Set<BuilderFactory>.identity();
@@ -227,6 +230,7 @@ Future<TestBuilderResult> testBuilders(
227230
readerWriter: readerWriter,
228231
verbose: verbose,
229232
enableLowResourceMode: enableLowResourceMode,
233+
flattenOutput: flattenOutput,
230234
);
231235
}
232236

@@ -293,6 +297,11 @@ Future<TestBuilderResult> testBuilders(
293297
/// Optionally pass [enableLowResourceMode], which acts like the command
294298
/// line flag; in particular it disables file caching.
295299
///
300+
/// By default generated outputs are written to the `TestReaderWriter` where
301+
/// they would be written in a real `build_runner` build, which means "hidden"
302+
/// outputs go in the `.dart_tool/build/generated` folder in the root package.
303+
/// Pass [flattenOutput] to instead output next to each package source.
304+
///
296305
/// Returns a [TestBuilderResult] with the [BuildResult] and the
297306
/// [TestReaderWriter] used for the build, which can be used for further
298307
/// checks.
@@ -315,6 +324,7 @@ Future<TestBuilderResult> testBuilderFactories(
315324
TestReaderWriter? readerWriter,
316325
bool verbose = false,
317326
bool enableLowResourceMode = false,
327+
bool flattenOutput = false,
318328
}) async {
319329
onLog ??= _printOnFailureOrWrite;
320330

@@ -412,14 +422,14 @@ Future<TestBuilderResult> testBuilderFactories(
412422
),
413423
);
414424
}
415-
for (final postProcessBuiderFactory in postProcessBuilderFactories) {
425+
for (final postProcessBuilderFactory in postProcessBuilderFactories) {
416426
String name;
417427
try {
418-
name = builderName(postProcessBuiderFactory(const BuilderOptions({})));
428+
name = builderName(postProcessBuilderFactory(const BuilderOptions({})));
419429
} catch (e) {
420430
name = e.toString();
421431
}
422-
builderApplications.add(applyPostProcess(name, postProcessBuiderFactory));
432+
builderApplications.add(applyPostProcess(name, postProcessBuilderFactory));
423433
}
424434

425435
final testingOverrides = TestingOverrides(
@@ -461,6 +471,7 @@ Future<TestBuilderResult> testBuilderFactories(
461471
}.build()
462472
: null,
463473
reportUnusedAssetsForInput: reportUnusedAssetsForInput,
474+
flattenOutput: flattenOutput,
464475
);
465476

466477
final buildPlan = await BuildPlan.load(

build_test/test/test_builder_test.dart

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,40 @@ import 'package:glob/glob.dart';
483483
expect(logs.join('\n'), contains('Exception: some exception'));
484484
expect(result.errors.join('\n'), contains('Exception: some exception'));
485485
});
486+
487+
test('restricts inputs to `generateFor`', () {
488+
return testBuilder(
489+
TestBuilder(),
490+
{'a|a.txt': '', 'a|b.txt': ''},
491+
generateFor: {'a|a.txt'},
492+
outputs: {'a|a.txt.copy': ''},
493+
);
494+
});
495+
496+
test('output is under `.dart_tool` by default', () async {
497+
final result = await testBuilder(
498+
TestBuilder(),
499+
{'a|a.txt': ''},
500+
outputs: {'a|a.txt.copy': ''},
501+
);
502+
expect(
503+
result.readerWriter.testing.assets,
504+
contains(AssetId('a', '.dart_tool/build/generated/a/a.txt.copy')),
505+
);
506+
});
507+
508+
test('output is flattened with `flattenOutput`', () async {
509+
final result = await testBuilder(
510+
TestBuilder(),
511+
{'a|a.txt': ''},
512+
outputs: {'a|a.txt.copy': ''},
513+
flattenOutput: true,
514+
);
515+
expect(
516+
result.readerWriter.testing.assets,
517+
contains(AssetId('a', 'a.txt.copy')),
518+
);
519+
});
486520
}
487521

488522
/// Concatenates the contents of multiple text files into a single output.

0 commit comments

Comments
 (0)