Skip to content

Commit e8122b2

Browse files
authored
Run full build in testBuilder, add testBuilders. (#3949)
1 parent 971e278 commit e8122b2

24 files changed

+943
-897
lines changed

_test_common/lib/test_phases.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ void _printOnFailure(LogRecord record) {
6666
/// });
6767
/// }
6868
///
69-
Future<TestBuildersResult> testBuilders(
69+
/// TODO(davidmorgan): this overlaps with the newer `testBuilders` in
70+
/// `package:build_test`, can they be unified?
71+
Future<TestBuildersResult> testPhases(
7072
List<BuilderApplication> builders,
7173
Map<String, /*String|List<int>*/ Object> inputs, {
7274
TestBuildersResult? resumeFrom,

build/CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
## 2.4.3-wip
22

3-
- `runBuilder` supports passing `fakeStartingAssets` for testing.
43
- `AssetNotFoundException` now also reports the missing `path`.
54
- Bump the min sdk to 3.7.0.
65
- Use `build_test` 3.0.0.

build/lib/src/generate/run_builder.dart

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ import 'expected_outputs.dart';
3434
/// If [reportUnusedAssetsForInput] is provided then all calls to
3535
/// `BuildStep.reportUnusedAssets` in [builder] will be forwarded to this
3636
/// function with the associated primary input.
37-
///
38-
/// Optionally pass [fakeStartingAssets] for testing: the builder will behave as
39-
/// if exactly those assets are available from previous phases, and will not be
40-
/// able to access other files except for its own output.
4137
Future<void> runBuilder(
4238
Builder builder,
4339
Iterable<AssetId> inputs,
@@ -50,7 +46,6 @@ Future<void> runBuilder(
5046
void Function(AssetId input, Iterable<AssetId> assets)?
5147
reportUnusedAssetsForInput,
5248
PackageConfig? packageConfig,
53-
Set<AssetId>? fakeStartingAssets,
5449
}) async {
5550
var shouldDisposeResourceManager = resourceManager == null;
5651
final resources = resourceManager ?? ResourceManager();
@@ -89,13 +84,8 @@ Future<void> runBuilder(
8984
// `SingleStepReaderWriter` instance integrated with the build; the `from`
9085
// factory just passes it through.
9186
//
92-
// If there is no build running, this creates a fake build step,
93-
// optionally with `fakeStartingAssets`.
94-
SingleStepReaderWriter.from(
95-
reader: assetReader,
96-
writer: assetWriter,
97-
fakeStartingAssets: fakeStartingAssets,
98-
),
87+
// If there is no build running, this creates a fake build step.
88+
SingleStepReaderWriter.from(reader: assetReader, writer: assetWriter),
9989
resolvers,
10090
resources,
10191
loadPackageConfig,

build_modules/test/kernel_builder_test.dart

Lines changed: 88 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,57 @@ import 'package:logging/logging.dart';
1111
import 'package:path/path.dart' as p;
1212
import 'package:test/test.dart';
1313

14-
import 'util.dart';
15-
1614
void main() {
17-
late Map<String, Object> assets;
1815
final platform = DartPlatform.register('ddc', ['dart:html']);
1916
final kernelOutputExtension = '.test.dill';
2017

2118
group('basic project', () {
22-
setUp(() async {
23-
assets = {
24-
'b|lib/b.dart': '''final world = 'world';''',
25-
'a|lib/a.dart': r'''
26-
import 'package:b/b.dart';
27-
final helloWorld = 'hello $world';
28-
''',
29-
'a|web/index.dart': r'''
30-
import "package:a/a.dart";
19+
final startingAssets = {
20+
'a|web/index.dart': r'''
21+
import "package:b/b.dart";
3122
3223
main() {
3324
print(helloWorld);
3425
}
3526
''',
36-
};
37-
38-
// Set up all the other required inputs for this test.
39-
await testBuilderAndCollectAssets(const ModuleLibraryBuilder(), assets);
40-
await testBuilderAndCollectAssets(MetaModuleBuilder(platform), assets);
41-
await testBuilderAndCollectAssets(
42-
MetaModuleCleanBuilder(platform),
43-
assets,
27+
'b|lib/b.dart': r'''
28+
import 'package:c/c.dart';
29+
final helloWorld = 'hello $world';
30+
''',
31+
'c|lib/c.dart': '''final world = 'world';''',
32+
};
33+
final startingBuilders = [
34+
const ModuleLibraryBuilder(),
35+
MetaModuleBuilder(platform),
36+
MetaModuleCleanBuilder(platform),
37+
ModuleBuilder(platform),
38+
];
39+
final startingExpectedOutputs = {
40+
'a|lib/.ddc.meta_module.clean': isNotNull,
41+
'a|lib/.ddc.meta_module.raw': isNotNull,
42+
'a|web/index.ddc.module': isNotNull,
43+
'a|web/index.module.library': isNotNull,
44+
'b|lib/.ddc.meta_module.clean': isNotNull,
45+
'b|lib/.ddc.meta_module.raw': isNotNull,
46+
'b|lib/b.ddc.module': isNotNull,
47+
'b|lib/b.module.library': isNotNull,
48+
'c|lib/.ddc.meta_module.clean': isNotNull,
49+
'c|lib/.ddc.meta_module.raw': isNotNull,
50+
'c|lib/c.ddc.module': isNotNull,
51+
'c|lib/c.module.library': isNotNull,
52+
};
53+
test('base build', () async {
54+
await testBuilders(
55+
startingBuilders,
56+
startingAssets,
57+
outputs: startingExpectedOutputs,
4458
);
45-
await testBuilderAndCollectAssets(ModuleBuilder(platform), assets);
4659
});
4760

4861
for (var trackUnusedInputs in [true, false]) {
49-
test('can output kernel summaries for modules under lib and web'
50-
'${trackUnusedInputs ? ' and track unused inputs' : ''}', () async {
62+
test('can output kernel summaries for modules under lib and web '
63+
'${trackUnusedInputs ? 'tracking' : 'not tracking'} '
64+
'unused inputs', () async {
5165
var builder = KernelBuilder(
5266
platform: platform,
5367
outputExtension: kernelOutputExtension,
@@ -56,56 +70,34 @@ void main() {
5670
useIncrementalCompiler: trackUnusedInputs,
5771
trackUnusedInputs: trackUnusedInputs,
5872
);
59-
// We need to compile package:b first - so its kernel file is
60-
// available.
61-
var expectedOutputs = <String, Matcher>{
62-
'b|lib/b$kernelOutputExtension': containsAllInOrder(
63-
utf8.encode('package:b/b.dart'),
64-
),
65-
};
66-
67-
await testBuilderAndCollectAssets(
68-
builder,
69-
assets,
70-
outputs: expectedOutputs,
71-
generateFor: {'b|lib/b${moduleExtension(platform)}'},
72-
);
7373

74-
// Next, compile package:a
75-
expectedOutputs = {
76-
'a|lib/a$kernelOutputExtension': containsAllInOrder(
77-
utf8.encode('package:a/a.dart'),
78-
),
79-
};
80-
81-
await testBuilderAndCollectAssets(
82-
builder,
83-
assets,
84-
outputs: expectedOutputs,
85-
generateFor: {'a|lib/a${moduleExtension(platform)}'},
86-
);
87-
88-
expectedOutputs = {
74+
var expectedOutputs = Map.of(startingExpectedOutputs)..addAll({
8975
'a|web/index$kernelOutputExtension': containsAllInOrder(
9076
utf8.encode('web/index.dart'),
9177
),
92-
};
78+
'b|lib/b$kernelOutputExtension': containsAllInOrder(
79+
utf8.encode('package:b/b.dart'),
80+
),
81+
'c|lib/c$kernelOutputExtension': containsAllInOrder(
82+
utf8.encode('package:c/c.dart'),
83+
),
84+
});
9385

94-
// And finally compile a|web/index.dart
9586
var reportedUnused = <AssetId, Iterable<AssetId>>{};
96-
await testBuilder(
97-
builder,
98-
assets,
87+
await testBuilders(
88+
[...startingBuilders, builder],
89+
startingAssets,
9990
outputs: expectedOutputs,
100-
reportUnusedAssetsForInput:
101-
(input, unused) => reportedUnused[input] = unused,
102-
generateFor: {'a|web/index${moduleExtension(platform)}'},
91+
reportUnusedAssetsForInput: (input, unused) {
92+
reportedUnused[input] = unused;
93+
},
10394
);
95+
10496
expect(
10597
reportedUnused[AssetId('a', 'web/index${moduleExtension(platform)}')],
10698
equals(
10799
trackUnusedInputs
108-
? [AssetId('b', 'lib/b$kernelOutputExtension')]
100+
? [AssetId('c', 'lib/c$kernelOutputExtension')]
109101
: null,
110102
),
111103
reason:
@@ -117,45 +109,48 @@ void main() {
117109
});
118110

119111
group('kernel outlines with missing imports', () {
120-
setUp(() async {
121-
assets = {
122-
'a|web/index.dart': 'import "package:a/a.dart";',
123-
'a|lib/a.dart': 'import "package:b/b.dart";',
124-
};
125-
126-
// Set up all the other required inputs for this test.
127-
await testBuilderAndCollectAssets(const ModuleLibraryBuilder(), assets);
128-
await testBuilderAndCollectAssets(MetaModuleBuilder(platform), assets);
129-
await testBuilderAndCollectAssets(
130-
MetaModuleCleanBuilder(platform),
131-
assets,
132-
);
133-
await testBuilderAndCollectAssets(ModuleBuilder(platform), assets);
134-
await testBuilderAndCollectAssets(
135-
KernelBuilder(
136-
platform: platform,
137-
outputExtension: kernelOutputExtension,
138-
summaryOnly: true,
139-
sdkKernelPath: p.url.join('lib', '_internal', 'ddc_sdk.dill'),
140-
),
141-
assets,
112+
final startingAssets = {
113+
'a|web/index.dart': 'import "package:a/a.dart";',
114+
'a|lib/a.dart': 'import "package:b/b.dart";',
115+
};
116+
final startingBuilders = [
117+
const ModuleLibraryBuilder(),
118+
MetaModuleBuilder(platform),
119+
MetaModuleCleanBuilder(platform),
120+
ModuleBuilder(platform),
121+
KernelBuilder(
122+
platform: platform,
123+
outputExtension: kernelOutputExtension,
124+
summaryOnly: true,
125+
sdkKernelPath: p.url.join('lib', '_internal', 'ddc_sdk.dill'),
126+
),
127+
];
128+
final startingExpectedOutputs = <String, Object>{
129+
'a|lib/.ddc.meta_module.clean': isNotNull,
130+
'a|lib/.ddc.meta_module.raw': isNotNull,
131+
'a|lib/a.ddc.module': isNotNull,
132+
'a|lib/a.module.library': isNotNull,
133+
'a|web/index.ddc.module': isNotNull,
134+
'a|web/index.module.library': isNotNull,
135+
};
136+
137+
test('base build', () async {
138+
await testBuilders(
139+
startingBuilders,
140+
startingAssets,
141+
rootPackage: 'a',
142+
outputs: startingExpectedOutputs,
142143
);
143144
});
144145

145146
test(
146147
'print an error if there are any missing transitive modules',
147148
() async {
148-
var expectedOutputs = <String, Matcher>{};
149149
var logs = <LogRecord>[];
150-
await testBuilder(
151-
KernelBuilder(
152-
platform: platform,
153-
outputExtension: kernelOutputExtension,
154-
summaryOnly: true,
155-
sdkKernelPath: p.url.join('lib', '_internal', 'ddc_sdk.dill'),
156-
),
157-
assets,
158-
outputs: expectedOutputs,
150+
await testBuilders(
151+
startingBuilders,
152+
startingAssets,
153+
outputs: startingExpectedOutputs,
159154
onLog: logs.add,
160155
);
161156
expect(

build_modules/test/util.dart

Lines changed: 0 additions & 34 deletions
This file was deleted.

build_runner_core/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
- Use `built_value` for `AssetNode` and related types, and for serialization.
2525
- Add details of what changed and what is built to `--verbose` logging.
2626
- New change detection algorithm.
27+
- Add `reportUnusedAssetsForInput` to `BuildOptions`, to listen for when
28+
a builder notifies that an asset is unused.
2729

2830
## 8.0.0
2931

build_runner_core/lib/src/generate/build.dart

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ class Build {
124124
if (logFine) {
125125
_logger.fine(AssetUpdates.from(updates).render(renderer));
126126
}
127-
128127
var watch = Stopwatch()..start();
129128
var result = await _safeBuild(updates);
130129
var optionalOutputTracker = OptionalOutputTracker(
@@ -481,6 +480,11 @@ class Build {
481480
.add(actionDescription);
482481

483482
var unusedAssets = <AssetId>{};
483+
void reportUnusedAssetsForInput(AssetId input, Iterable<AssetId> assets) {
484+
options.reportUnusedAssetsForInput?.call(input, assets);
485+
unusedAssets.addAll(assets);
486+
}
487+
484488
await tracker.trackStage(
485489
'Build',
486490
() => runBuilder(
@@ -492,8 +496,7 @@ class Build {
492496
logger: logger,
493497
resourceManager: resourceManager,
494498
stageTracker: tracker,
495-
reportUnusedAssetsForInput:
496-
(_, assets) => unusedAssets.addAll(assets),
499+
reportUnusedAssetsForInput: reportUnusedAssetsForInput,
497500
packageConfig: options.packageGraph.asPackageConfig,
498501
).catchError((void _) {
499502
// Errors tracked through the logger

0 commit comments

Comments
 (0)