Skip to content

Commit

Permalink
Remove unnecessary copy of serialized graph in tests. (#3904)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmorgan authored Mar 5, 2025
1 parent 1e5eda2 commit 4dae408
Showing 1 changed file with 27 additions and 103 deletions.
130 changes: 27 additions & 103 deletions build_runner_core/test/generate/build_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,10 @@ void main() {
});

group('incremental builds with cached graph', () {
// Using `resumeFrom: result` to pass the filesystem between `testBuilders`
// calls causes the serialized graph from the previous build to be loaded,
// exactly as in real builds.

test('one new asset, one modified asset, one unchanged asset', () async {
var builders = [copyABuilderApplication];

Expand All @@ -1489,9 +1493,6 @@ void main() {
);

// Followup build with modified inputs.
var serializedGraph = result.readerWriter.testing.readBytes(
makeAssetId('a|$assetGraphPath'),
);
await testBuilders(
builders,
{
Expand All @@ -1500,7 +1501,6 @@ void main() {
'a|lib/b.txt': 'b',
'a|lib/b.txt.copy': 'b',
'a|lib/c.txt': 'c',
'a|$assetGraphPath': serializedGraph,
},
outputs: {'a|web/a.txt.copy': 'a2', 'a|lib/c.txt.copy': 'c'},
resumeFrom: result,
Expand Down Expand Up @@ -1528,17 +1528,10 @@ void main() {
);

// Followup build with the 2nd output missing.
var serializedGraph = result.readerWriter.testing.readBytes(
makeAssetId('a|$assetGraphPath'),
);
result.readerWriter.testing.delete(AssetId('a', 'lib/a.txt.2'));
await testBuilders(
builders,
{
'a|lib/a.txt': 'a',
'a|lib/a.txt.1': 'a',
'a|$assetGraphPath': serializedGraph,
},
{'a|lib/a.txt': 'a', 'a|lib/a.txt.1': 'a'},
outputs: {'a|lib/a.txt.1': 'a', 'a|lib/a.txt.2': 'a'},
resumeFrom: result,
);
Expand Down Expand Up @@ -1581,51 +1574,39 @@ void main() {
);

// Followup build with modified unused inputs should have no outputs.
var serializedGraph = result.readerWriter.testing.readBytes(
makeAssetId('a|$assetGraphPath'),
);
await testBuilders(
builders,
{
'a|lib/a.txt': 'a',
'a|lib/a.txt.used': 'b',
'a|lib/a.txt.unused': 'd', // changed the content of this one
'a|lib/a.txt.copy': 'ab',
'a|$assetGraphPath': serializedGraph,
},
outputs: {},
resumeFrom: result,
);

// And now modify a real input.
serializedGraph = result.readerWriter.testing.readBytes(
makeAssetId('a|$assetGraphPath'),
);
await testBuilders(
builders,
{
'a|lib/a.txt': 'a',
'a|lib/a.txt.used': 'e',
'a|lib/a.txt.unused': 'd',
'a|lib/a.txt.copy': 'ab',
'a|$assetGraphPath': serializedGraph,
},
outputs: {'a|lib/a.txt.copy': 'ae'},
resumeFrom: result,
);

// Finally modify the primary input.
serializedGraph = result.readerWriter.testing.readBytes(
makeAssetId('a|$assetGraphPath'),
);
await testBuilders(
builders,
{
'a|lib/a.txt': 'f',
'a|lib/a.txt.used': 'e',
'a|lib/a.txt.unused': 'd',
'a|lib/a.txt.copy': 'ae',
'a|$assetGraphPath': serializedGraph,
},
outputs: {'a|lib/a.txt.copy': 'fe'},
resumeFrom: result,
Expand Down Expand Up @@ -1653,32 +1634,20 @@ void main() {
);

// Followup build with modified primary input should have no outputs.
var serializedGraph = result.readerWriter.testing.readBytes(
makeAssetId('a|$assetGraphPath'),
);
await testBuilders(
builders,
{
'a|lib/a.txt': 'b',
'a|lib/a.txt.used': '',
'a|lib/a.txt.copy': 'a',
'a|$assetGraphPath': serializedGraph,
},
{'a|lib/a.txt': 'b', 'a|lib/a.txt.used': '', 'a|lib/a.txt.copy': 'a'},
outputs: {},
resumeFrom: result,
);

// But modifying other inputs still causes a rebuild.
serializedGraph = result.readerWriter.testing.readBytes(
makeAssetId('a|$assetGraphPath'),
);
await testBuilders(
builders,
{
'a|lib/a.txt': 'b',
'a|lib/a.txt.used': 'b',
'a|lib/a.txt.copy': 'a',
'a|$assetGraphPath': serializedGraph,
},
outputs: {'a|lib/a.txt.copy': 'b'},
resumeFrom: result,
Expand Down Expand Up @@ -1706,13 +1675,10 @@ void main() {
);

// Delete the primary input, the output shoud still be deleted
var serializedGraph = result.readerWriter.testing.readBytes(
makeAssetId('a|$assetGraphPath'),
);
result.readerWriter.testing.delete(AssetId('a', 'lib/a.txt'));
await testBuilders(
builders,
{'a|lib/a.txt.copy': 'a', 'a|$assetGraphPath': serializedGraph},
{'a|lib/a.txt.copy': 'a'},
outputs: {},
resumeFrom: result,
);
Expand Down Expand Up @@ -1743,17 +1709,10 @@ void main() {
);

// Followup build with deleted input + cached graph.
var serializedGraph = result.readerWriter.testing.readBytes(
makeAssetId('a|$assetGraphPath'),
);
result.readerWriter.testing.delete(AssetId('a', 'lib/a.txt'));
await testBuilders(
builders,
{
'a|lib/a.txt.copy': 'a',
'a|lib/a.txt.clone': 'a',
'a|$assetGraphPath': serializedGraph,
},
{'a|lib/a.txt.copy': 'a', 'a|lib/a.txt.clone': 'a'},
outputs: {},
resumeFrom: result,
);
Expand Down Expand Up @@ -1784,14 +1743,7 @@ void main() {
);

// Followup build with same sources + cached graph.
var serializedGraph = result.readerWriter.testing.readBytes(
makeAssetId('a|$assetGraphPath'),
);
await testBuilders(builders, {
'a|web/a.txt': 'a',
'a|web/a.txt.copy': 'a',
'a|$assetGraphPath': serializedGraph,
}, outputs: {});
await testBuilders(builders, {}, outputs: {}, resumeFrom: result);
});

test('no outputs if no changed sources using `hideOutput: true`', () async {
Expand All @@ -1809,14 +1761,7 @@ void main() {
);

// Followup build with same sources + cached graph.
var serializedGraph = result.readerWriter.testing.readBytes(
makeAssetId('a|$assetGraphPath'),
);
await testBuilders(builders, {
'a|web/a.txt': 'a',
'a|web/a.txt.copy': 'a',
'a|$assetGraphPath': serializedGraph,
}, outputs: {});
await testBuilders(builders, {}, outputs: {}, resumeFrom: result);
});

test('inputs/outputs are updated if they change', () async {
Expand All @@ -1836,10 +1781,6 @@ void main() {

// Followup build with same sources + cached graph, but configure the
// builder to read a different file.
var serializedGraph = result.readerWriter.testing.readBytes(
makeAssetId('a|$assetGraphPath'),
);

await testBuilders(
[
applyToRoot(
Expand All @@ -1856,7 +1797,6 @@ void main() {
// builder but pretending its the same.
'a|lib/file.b': 'b2',
'a|lib/file.c': 'c',
'a|$assetGraphPath': serializedGraph,
},
outputs: {'a|lib/file.a.copy': 'c'},
resumeFrom: result,
Expand Down Expand Up @@ -1901,17 +1841,13 @@ void main() {

// Modify the primary input of `file.a.copy`, but its output doesn't
// change so `file.a.copy.copy` shouldn't be rebuilt.
var serializedGraph = result.readerWriter.testing.readBytes(
makeAssetId('a|$assetGraphPath'),
);
await testBuilders(
builders,
{
'a|lib/file.a': 'a2',
'a|lib/file.b': 'b',
'a|lib/file.a.copy': 'b',
'a|lib/file.a.copy.copy': 'b',
'a|$assetGraphPath': serializedGraph,
},
outputs: {'a|lib/file.a.copy': 'b'},
resumeFrom: result,
Expand All @@ -1922,22 +1858,24 @@ void main() {
var builders = [applyToRoot(SiblingCopyBuilder())];

// Initial build.
final result = await testBuilders(
var result = await testBuilders(
builders,
{'a|web/a.txt': 'a', 'a|web/a.txt.sibling': 'sibling'},
outputs: {'a|web/a.txt.new': 'sibling'},
);

// Followup build with cached graph and a changed primary input, but the
// actual file that was read has not changed.
await testBuilders(builders, {
'a|web/a.txt': 'b',
'a|web/a.txt.sibling': 'sibling',
'a|web/a.txt.new': 'sibling',
'a|$assetGraphPath': result.readerWriter.testing.readBytes(
makeAssetId('a|$assetGraphPath'),
),
}, outputs: {});
result = await testBuilders(
builders,
{
'a|web/a.txt': 'b',
'a|web/a.txt.sibling': 'sibling',
'a|web/a.txt.new': 'sibling',
},
outputs: {},
resumeFrom: result,
);

// And now try modifying the sibling to make sure that still works.
await testBuilders(
Expand All @@ -1946,11 +1884,9 @@ void main() {
'a|web/a.txt': 'b',
'a|web/a.txt.sibling': 'new!',
'a|web/a.txt.new': 'sibling',
'a|$assetGraphPath': result.readerWriter.testing.readBytes(
makeAssetId('a|$assetGraphPath'),
),
},
outputs: {'a|web/a.txt.new': 'new!'},
resumeFrom: result,
);
});
});
Expand Down Expand Up @@ -1986,13 +1922,9 @@ void main() {
'a|lib/a.source': 'true',
}, status: BuildStatus.failure);

var serializedGraph = result.readerWriter.testing.readBytes(
makeAssetId('a|$assetGraphPath'),
);

await testBuilders(
builders,
{'a|lib/a.source': 'false', 'a|$assetGraphPath': serializedGraph},
{'a|lib/a.source': 'false'},
outputs: {},
resumeFrom: result,
);
Expand Down Expand Up @@ -2070,30 +2002,22 @@ void main() {
),
),
];
final result = await testBuilders(builders, {
var result = await testBuilders(builders, {
'a|web/a.source': 'true',
}, status: BuildStatus.failure);

var serializedGraph = result.readerWriter.testing.readBytes(
makeAssetId('a|$assetGraphPath'),
);

await testBuilders(
result = await testBuilders(
builders,
{'a|web/a.source': 'false', 'a|$assetGraphPath': serializedGraph},
{'a|web/a.source': 'false'},
outputs: {'a|web/a.g1': '', 'a|web/a.g2': '', 'a|web/a.g3': ''},
resumeFrom: result,
);

serializedGraph = result.readerWriter.testing.readBytes(
makeAssetId('a|$assetGraphPath'),
);

// Make sure if we mark the original node as a failure again, that we
// also mark all its primary outputs as failures.
await testBuilders(
builders,
{'a|web/a.source': 'true', 'a|$assetGraphPath': serializedGraph},
{'a|web/a.source': 'true'},
outputs: {},
status: BuildStatus.failure,
resumeFrom: result,
Expand Down

0 comments on commit 4dae408

Please sign in to comment.