Skip to content

Commit bc06f9f

Browse files
committed
Make builder not merge generic extension.
The previous behaviour was that if the user adds a build_extension, the build would inject the generic build extension to cover all files. This was useful to simplify the build.yaml file at the cost of unexpected behaviour. To avoid potencial unexpected behaviour, if the user provides custom build_extensions we assume that the patterns cover all files and therefore we do not merge the generic build_extension.
1 parent af043a0 commit bc06f9f

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

FAQ.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,9 @@ to the filename.
182182
To use `build_extensions` you can use `^` on the input string to match on the project root, and `{{}}` to capture the remaining path/filename.
183183

184184
You can also have multiple build_extensions options, but they can't conflict with each other.
185-
For consistency, the output pattern must always end with `.mocks.dart` and the input pattern must always end with `.dart`
185+
For consistency, the output pattern must always end with `.mocks.dart` and the input pattern must always end with `.dart`.
186+
187+
If you specify a build extension, you **MUST** ensure that your patterns cover all input files that you want generate mocks from. Failing to do so will lead to the unmatched file from not being generated at all.
186188

187189
```yaml
188190
targets:

build.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,13 @@ targets:
1010
# to your desired path. The default behaviour is to the .mocks.dart file to be in the same
1111
# directory as the source .dart file. As seen below this is customizable, but the generated
1212
# file must always end in `.mocks.dart`.
13+
#
14+
# If you specify custom build_extensions you MUST ensure that they cover all input files
1315
build_extensions:
1416
'^example/build_extensions/{{}}.dart' : 'example/build_extensions/mocks/{{}}.mocks.dart'
17+
'^example/example.dart' : 'example/example.mocks.dart'
18+
'^example/iss/{{}}.dart' : 'example/iss/{{}}.mocks.dart'
19+
'^test/end2end/{{}}.dart' : 'test/end2end/{{}}.mocks.dart'
1520

1621
builders:
1722
mockBuilder:

lib/src/builder.dart

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,34 +63,26 @@ import 'package:source_gen/source_gen.dart';
6363
/// 'foo.mocks.dart' will be created.
6464
class MockBuilder implements Builder {
6565
@override
66-
final Map<String, List<String>> buildExtensions = {
67-
'.dart': ['.mocks.dart']
68-
};
66+
final Map<String, List<String>> buildExtensions;
6967

70-
MockBuilder({Map<String, List<String>>? buildExtensions}) {
71-
this.buildExtensions.addAll(buildExtensions ?? {});
72-
}
68+
const MockBuilder(
69+
{this.buildExtensions = const {
70+
'.dart': ['.mocks.dart']
71+
}});
7372

7473
@override
7574
Future<void> build(BuildStep buildStep) async {
7675
if (!await buildStep.resolver.isLibrary(buildStep.inputId)) return;
7776
final entryLib = await buildStep.inputLibrary;
7877
final sourceLibIsNonNullable = entryLib.isNonNullableByDefault;
7978

80-
// While it can be acceptable that we get more than 2 allowedOutputs,
81-
// because it's the general one and the user defined one. Having
82-
// more, means that user has conflicting patterns so we should throw.
83-
if (buildStep.allowedOutputs.length > 2) {
79+
if (buildStep.allowedOutputs.length > 1) {
8480
throw ArgumentError('Build_extensions has conflicting outputs on file '
8581
'`${buildStep.inputId.path}`, it usually caused by missconfiguration '
8682
'on your `build.yaml` file');
8783
}
88-
// if not single, we always choose the user defined one.
89-
final mockLibraryAsset = buildStep.allowedOutputs.singleOrNull ??
90-
buildStep.allowedOutputs
91-
.where((element) =>
92-
element != buildStep.inputId.changeExtension('.mocks.dart'))
93-
.single;
84+
final mockLibraryAsset = buildStep.allowedOutputs.single;
85+
9486
final inheritanceManager = InheritanceManager3();
9587
final mockTargetGatherer =
9688
_MockTargetGatherer(entryLib, inheritanceManager);

0 commit comments

Comments
 (0)