Skip to content

Commit 5e2659f

Browse files
authored
Clean up test helpers. (#4095)
* Clean up test helpers. * Address review comments.
1 parent e453c95 commit 5e2659f

File tree

13 files changed

+164
-203
lines changed

13 files changed

+164
-203
lines changed

_test_common/lib/assets.dart

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

_test_common/lib/builders.dart

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:async';
66

77
import 'package:build/build.dart';
8+
import 'package:built_collection/built_collection.dart';
89

910
class CopyingPostProcessBuilder implements PostProcessBuilder {
1011
final String outputExtension;
@@ -23,18 +24,6 @@ class CopyingPostProcessBuilder implements PostProcessBuilder {
2324
}
2425
}
2526

26-
class DeletePostProcessBuilder implements PostProcessBuilder {
27-
@override
28-
final inputExtensions = ['.txt'];
29-
30-
DeletePostProcessBuilder();
31-
32-
@override
33-
Future<void> build(PostProcessBuildStep buildStep) async {
34-
buildStep.deletePrimaryInput();
35-
}
36-
}
37-
3827
/// A [Builder] which behaves exactly like it's [delegate] but has a different
3928
/// runtime type.
4029
class DelegatingBuilder implements Builder {
@@ -48,3 +37,47 @@ class DelegatingBuilder implements Builder {
4837
@override
4938
Future build(BuildStep buildStep) async => delegate.build(buildStep);
5039
}
40+
41+
class PlaceholderBuilder extends Builder {
42+
final String inputPlaceholder;
43+
final BuiltMap<String, String> outputFilenameToContent;
44+
45+
@override
46+
Map<String, List<String>> get buildExtensions => {
47+
// Usually this map is input filename extensions to output filename
48+
// extensions, for example `.dart` to `.g.dart`.
49+
//
50+
// But this builder is about placeholders, which are special keys that
51+
// are not extensions. So: the key is a placeholder, and the values are
52+
// full output filenames relative to the placeholder path.
53+
inputPlaceholder: outputFilenameToContent.keys.toList(),
54+
};
55+
56+
PlaceholderBuilder(
57+
this.outputFilenameToContent, {
58+
this.inputPlaceholder = r'$lib$',
59+
});
60+
61+
@override
62+
Future build(BuildStep buildStep) async {
63+
for (final MapEntry(key: outputFilename, value: content)
64+
in outputFilenameToContent.entries) {
65+
await buildStep.writeAsString(
66+
_outputId(buildStep.inputId, inputPlaceholder, outputFilename),
67+
content,
68+
);
69+
}
70+
}
71+
}
72+
73+
AssetId _outputId(
74+
AssetId inputId,
75+
String inputExtension,
76+
String outputExtension,
77+
) {
78+
assert(inputId.path.endsWith(inputExtension));
79+
var newPath =
80+
inputId.path.substring(0, inputId.path.length - inputExtension.length) +
81+
outputExtension;
82+
return AssetId(inputId.package, newPath);
83+
}

_test_common/lib/common.dart

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'dart:async';
65
import 'dart:convert';
76

87
import 'package:build/build.dart';
98
import 'package:crypto/crypto.dart';
109

1110
export 'package:build_test/build_test.dart';
1211

13-
export 'assets.dart';
1412
export 'builders.dart';
1513
export 'descriptors.dart';
1614
export 'matchers.dart';
@@ -25,40 +23,3 @@ Digest computeDigest(AssetId id, String contents) {
2523

2624
return md5.convert([...utf8.encode(contents), ...idString.codeUnits]);
2725
}
28-
29-
class PlaceholderBuilder extends Builder {
30-
final String inputExtension;
31-
final Map<String, String> outputExtensionsToContent;
32-
33-
@override
34-
Map<String, List<String>> get buildExtensions => {
35-
inputExtension: outputExtensionsToContent.keys.toList(),
36-
};
37-
38-
PlaceholderBuilder(
39-
this.outputExtensionsToContent, {
40-
this.inputExtension = r'$lib$',
41-
});
42-
43-
@override
44-
Future build(BuildStep buildStep) async {
45-
outputExtensionsToContent.forEach((extension, content) {
46-
buildStep.writeAsString(
47-
_outputId(buildStep.inputId, inputExtension, extension),
48-
content,
49-
);
50-
});
51-
}
52-
}
53-
54-
AssetId _outputId(
55-
AssetId inputId,
56-
String inputExtension,
57-
String outputExtension,
58-
) {
59-
assert(inputId.path.endsWith(inputExtension));
60-
var newPath =
61-
inputId.path.substring(0, inputId.path.length - inputExtension.length) +
62-
outputExtension;
63-
return AssetId(inputId.package, newPath);
64-
}

_test_common/lib/matchers.dart

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,12 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
// ignore: implementation_imports
6-
import 'package:build_runner_core/src/asset_graph/exceptions.dart';
75
// ignore: implementation_imports
86
import 'package:build_runner_core/src/asset_graph/graph.dart';
97
// ignore: implementation_imports
108
import 'package:build_runner_core/src/asset_graph/node.dart';
119
import 'package:test/test.dart';
1210

13-
final Matcher throwsCorruptedException = throwsA(
14-
const TypeMatcher<AssetGraphCorruptedException>(),
15-
);
16-
final Matcher duplicateAssetNodeException =
17-
const TypeMatcher<DuplicateAssetNodeException>();
18-
1911
Matcher equalsAssetGraph(AssetGraph expected) => _AssetGraphMatcher(expected);
2012

2113
class _AssetGraphMatcher extends Matcher {

_test_common/lib/test_environment.dart

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

_test_common/pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ dependencies:
1111
build_config: any
1212
build_runner_core: any
1313
build_test: any
14+
built_collection: any
1415
crypto: ^3.0.0
1516
logging: ^1.0.0
1617
package_config: ^2.0.0

build_runner/test/generate/watch_test.dart

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,12 @@ targets:
418418
..add(aTxtCopyNode)
419419
..add(bCopyNode)
420420
..add(
421-
makeAssetNode('a|web/b.txt', [
422-
bCopyNode.id,
423-
], computeDigest(bTxtId, 'b2')),
421+
AssetNode.source(
422+
AssetId.parse('a|web/b.txt'),
423+
outputs: [bCopyNode.id],
424+
primaryOutputs: [bCopyNode.id],
425+
digest: computeDigest(bTxtId, 'b2'),
426+
),
424427
);
425428

426429
var cCopyId = makeAssetId('a|web/c.txt.copy');
@@ -437,9 +440,12 @@ targets:
437440
expectedGraph
438441
..add(cCopyNode)
439442
..add(
440-
makeAssetNode('a|web/c.txt', [
441-
cCopyNode.id,
442-
], computeDigest(cTxtId, 'c')),
443+
AssetNode.source(
444+
AssetId.parse('a|web/c.txt'),
445+
outputs: [cCopyNode.id],
446+
primaryOutputs: [cCopyNode.id],
447+
digest: computeDigest(cTxtId, 'c'),
448+
),
443449
);
444450

445451
expect(cachedGraph, equalsAssetGraph(expectedGraph));

build_runner/test/server/asset_handler_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ void main() {
4848
});
4949

5050
void addAsset(String id, String content, {bool deleted = false}) {
51-
var node = makeAssetNode(id, [], computeDigest(AssetId.parse(id), 'a'));
51+
final parsedId = AssetId.parse(id);
52+
var node = AssetNode.source(parsedId, digest: computeDigest(parsedId, 'a'));
5253
if (deleted) {
5354
node = node.rebuild((b) {
5455
b.deletedBy.add(

build_runner/test/server/serve_handler_test.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,11 @@ void main() {
139139
});
140140

141141
void addSource(String id, String content, {bool deleted = false}) {
142-
var node = makeAssetNode(id, [], computeDigest(AssetId.parse(id), content));
142+
final parsedId = AssetId.parse(id);
143+
var node = AssetNode.source(
144+
parsedId,
145+
digest: computeDigest(parsedId, content),
146+
);
143147
if (deleted) {
144148
node = node.rebuild((b) {
145149
b.deletedBy.add(

build_runner_core/test/asset/finalized_reader_test.dart

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,13 @@ void main() {
4242
});
4343

4444
test('can not read deleted files', () async {
45-
var notDeleted = makeAssetNode(
46-
'a|web/a.txt',
47-
[],
48-
computeDigest(AssetId('a', 'web/a.txt'), 'a'),
45+
var notDeleted = AssetNode.source(
46+
AssetId.parse('a|web/a.txt'),
47+
digest: computeDigest(AssetId('a', 'web/a.txt'), 'a'),
4948
);
50-
var deleted = makeAssetNode(
51-
'a|lib/b.txt',
52-
[],
53-
computeDigest(AssetId('a', 'lib/b.txt'), 'b'),
49+
var deleted = AssetNode.source(
50+
AssetId.parse('a|lib/b.txt'),
51+
digest: computeDigest(AssetId('a', 'lib/b.txt'), 'b'),
5452
);
5553

5654
deleted = deleted.rebuild(

0 commit comments

Comments
 (0)