/
build_script_generate_test.dart
260 lines (238 loc) · 7.52 KB
/
build_script_generate_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
@Timeout.factor(4)
import 'package:_test_common/descriptors.dart';
import 'package:_test_common/sdk.dart';
import 'package:build_runner/src/build_script_generate/build_script_generate.dart';
import 'package:build_runner_core/build_runner_core.dart';
import 'package:test/test.dart';
import 'package:test_descriptor/test_descriptor.dart' as d;
void main() {
group('validation', () {
setUpAll(() async {
await d.dir('a', [
await pubspec('a', currentIsolateDependencies: [
'build',
'build_config',
'build_daemon',
'build_resolvers',
'build_runner',
'build_runner_core',
'code_builder',
]),
]).create();
await runPub('a', 'get');
});
group('of builder imports', () {
test('warn about deprecated ../ style imports', () async {
await d.dir('a', [
d.file('build.yaml', '''
builders:
fake:
import: "../../../tool/builder.dart"
builder_factories: ["myFactory"]
build_extensions: {"foo": ["bar"]}
'''),
]).create();
var result = await runPub('a', 'run', args: ['build_runner', 'build']);
expect(result.stderr, isEmpty,
skip: 'https://github.com/dart-lang/sdk/issues/50592');
expect(
result.stdout,
contains(
'The `../` import syntax in build.yaml is now deprecated'));
});
test('support package relative imports', () async {
await d.dir('a', [
d.file('build.yaml', '''
builders:
fake:
import: "tool/builder.dart"
builder_factories: ["myFactory"]
build_extensions: {"foo": ["bar"]}
'''),
]).create();
var result = await runPub('a', 'run', args: ['build_runner', 'build']);
expect(result.stderr, isEmpty,
skip: 'https://github.com/dart-lang/sdk/issues/50592');
expect(
result.stdout,
isNot(contains(
'The `../` import syntax in build.yaml is now deprecated')));
await d.dir('a', [
d.dir('.dart_tool', [
d.dir('build', [
d.dir('entrypoint', [
d.file('build.dart',
contains("import '../../../tool/builder.dart'"))
])
])
])
]).validate();
});
test('warns for builder config that leaves unparseable Dart', () async {
await d.dir('a', [
d.file('build.yaml', '''
builders:
fake:
import: "tool/builder.dart"
builder_factories: ["not an identifier"]
build_extensions: {"foo": ["bar"]}
''')
]).create();
var result = await runPub('a', 'run', args: ['build_runner', 'build']);
expect(result.stderr, isEmpty,
skip: 'https://github.com/dart-lang/sdk/issues/50592');
expect(result.stdout, contains('could not be parsed'));
});
});
test('checks builder keys in global_options', () async {
await d.dir('a', [
d.file('build.yaml', '''
global_options:
a:a:
runs_before:
- b:b
'''),
]).create();
var result = await runPub('a', 'run', args: ['build_runner', 'build']);
expect(result.stderr, isEmpty,
skip: 'https://github.com/dart-lang/sdk/issues/50592');
expect(
result.stdout,
allOf(
contains(
'Invalid builder key `a:a` found in global_options config of '
'build.yaml. This configuration will have no effect.'),
contains(
'Invalid builder key `b:b` found in global_options config of '
'build.yaml. This configuration will have no effect.'),
));
});
});
group('determines whether a build script can be null safe', () {
final buildYaml = d.file('build.yaml', '''
builders:
a:
import: 'package:a/builder.dart'
build_extensions: {'.foo': ['.bar']}
builder_factories: ['builder']
''');
test('when all builders are opted-in', () async {
await d.dir('a', [
d.file('pubspec.yaml', '''
name: a
environment:
sdk: '>=2.12.0 <3.0.0'
'''),
buildYaml,
d.dir('lib', [d.file('builder.dart', '')]),
]).create();
await runPub('a', 'get');
final options = await findBuildScriptOptions(
packageGraph: await PackageGraph.forPath('${d.sandbox}/a'));
expect(options.canRunWithSoundNullSafety, isTrue);
});
test('when the root package opts out', () async {
await d.dir('a', [
d.file('pubspec.yaml', '''
name: a
environment:
sdk: '>=2.9.0 <3.0.0'
'''),
d.dir('lib', [d.file('builder.dart', '')]),
]).create();
await runPub('a', 'get');
final options = await findBuildScriptOptions(
packageGraph: await PackageGraph.forPath('${d.sandbox}/a'));
expect(options.canRunWithSoundNullSafety, isFalse);
});
test('when a builder-defining package opts out', () async {
await d.dir('a', [
d.file('pubspec.yaml', '''
name: a
environment:
sdk: '>=2.12.0 <3.0.0'
dependencies:
b:
path: ../b/
'''),
]).create();
await d.dir('b', [
d.file('pubspec.yaml', '''
name: b
environment:
sdk: '>=2.9.0 <3.0.0'
'''),
buildYaml,
d.dir('lib', [
d.file('builder.dart', ''),
]),
]).create();
await runPub('a', 'get');
final options = await findBuildScriptOptions(
packageGraph: await PackageGraph.forPath('${d.sandbox}/a'));
expect(options.canRunWithSoundNullSafety, isFalse);
});
test('when a builder-defining library ops out', () async {
await d.dir('a', [
d.file('pubspec.yaml', '''
name: a
environment:
sdk: '>=2.12.0 <3.0.0'
'''),
buildYaml,
d.dir('lib', [d.file('builder.dart', '// @dart=2.9')]),
]).create();
await runPub('a', 'get');
final options = await findBuildScriptOptions(
packageGraph: await PackageGraph.forPath('${d.sandbox}/a'));
expect(options.canRunWithSoundNullSafety, isFalse);
});
test('when a builder-defining library ops out through a relative path',
() async {
await d.dir('a', [
d.file('pubspec.yaml', '''
name: a
environment:
sdk: '>=2.12.0 <3.0.0'
'''),
d.file('build.yaml', '''
builders:
a:
import: '../../../tool/builder.dart'
build_extensions: {'.foo': ['.bar']}
builder_factories: ['builder']
'''),
d.dir('tool', [d.file('builder.dart', '//@dart=2.9')]),
]).create();
await runPub('a', 'get');
final options = await findBuildScriptOptions(
packageGraph: await PackageGraph.forPath('${d.sandbox}/a'));
expect(options.canRunWithSoundNullSafety, isFalse);
});
test('when a builder-defining library uses a top-level relative path',
() async {
await d.dir('a', [
d.file('pubspec.yaml', '''
name: a
environment:
sdk: '>=2.12.0 <3.0.0'
'''),
d.file('build.yaml', '''
builders:
a:
import: 'tool/builder.dart'
build_extensions: {'.foo': ['.bar']}
builder_factories: ['builder']
'''),
d.dir('tool', [d.file('builder.dart', '')]),
]).create();
await runPub('a', 'get');
final options = await findBuildScriptOptions(
packageGraph: await PackageGraph.forPath('${d.sandbox}/a'));
expect(options.canRunWithSoundNullSafety, isTrue);
});
});
}