-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathtest_suites.dart
441 lines (390 loc) · 14.3 KB
/
test_suites.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// ignore_for_file: avoid_print
import 'dart:io' show File, Directory;
import 'package:meta/meta.dart';
import 'package:path/path.dart' as p;
import 'flutter_utils.dart';
import 'generation.dart';
import 'native_project_runners.dart';
import 'process_utils.dart';
const int _noDeviceAvailableExitCode = 100;
const String _testPluginRelativePath = 'platform_tests/test_plugin';
const String _alternateLanguageTestPluginRelativePath =
'platform_tests/alternate_language_test_plugin';
const String _integrationTestFileRelativePath = 'integration_test/test.dart';
/// Information about a test suite.
@immutable
class TestInfo {
const TestInfo({required this.function, this.description});
/// The function to run the test suite.
final Future<int> Function() function;
/// A user-facing description of the test suite.
final String? description;
}
// Test suite names.
const String androidJavaUnitTests = 'android_java_unittests';
const String androidJavaLint = 'android_java_lint';
const String androidJavaIntegrationTests = 'android_java_integration_tests';
const String androidKotlinUnitTests = 'android_kotlin_unittests';
const String androidKotlinIntegrationTests = 'android_kotlin_integration_tests';
const String iOSObjCUnitTests = 'ios_objc_unittests';
const String iOSObjCIntegrationTests = 'ios_objc_integration_tests';
const String iOSSwiftUnitTests = 'ios_swift_unittests';
const String iOSSwiftIntegrationTests = 'ios_swift_integration_tests';
const String macOSObjCIntegrationTests = 'macos_objc_integration_tests';
const String macOSSwiftUnitTests = 'macos_swift_unittests';
const String macOSSwiftIntegrationTests = 'macos_swift_integration_tests';
const String windowsUnitTests = 'windows_unittests';
const String windowsIntegrationTests = 'windows_integration_tests';
const String dartUnitTests = 'dart_unittests';
const String flutterUnitTests = 'flutter_unittests';
const String mockHandlerTests = 'mock_handler_tests';
const String commandLineTests = 'command_line_tests';
const Map<String, TestInfo> testSuites = <String, TestInfo>{
windowsUnitTests: TestInfo(
function: _runWindowsUnitTests,
description: 'Unit tests on generated Windows C++ code.'),
windowsIntegrationTests: TestInfo(
function: _runWindowsIntegrationTests,
description: 'Integration tests on generated Windows C++ code.'),
androidJavaUnitTests: TestInfo(
function: _runAndroidJavaUnitTests,
description: 'Unit tests on generated Java code.'),
androidJavaIntegrationTests: TestInfo(
function: _runAndroidJavaIntegrationTests,
description: 'Integration tests on generated Java code.'),
androidJavaLint: TestInfo(
function: _runAndroidJavaLint, description: 'Lint generated Java code.'),
androidKotlinUnitTests: TestInfo(
function: _runAndroidKotlinUnitTests,
description: 'Unit tests on generated Kotlin code.'),
androidKotlinIntegrationTests: TestInfo(
function: _runAndroidKotlinIntegrationTests,
description: 'Integration tests on generated Kotlin code.'),
dartUnitTests: TestInfo(
function: _runDartUnitTests,
description: "Unit tests on and analysis on Pigeon's implementation."),
flutterUnitTests: TestInfo(
function: _runFlutterUnitTests,
description: 'Unit tests on generated Dart code.'),
iOSObjCUnitTests: TestInfo(
function: _runIOSObjCUnitTests,
description: 'Unit tests on generated Objective-C code.'),
iOSObjCIntegrationTests: TestInfo(
function: _runIOSObjCIntegrationTests,
description: 'Integration tests on generated Objective-C code.'),
iOSSwiftUnitTests: TestInfo(
function: _runIOSSwiftUnitTests,
description: 'Unit tests on generated Swift code.'),
iOSSwiftIntegrationTests: TestInfo(
function: _runIOSSwiftIntegrationTests,
description: 'Integration tests on generated Swift code.'),
macOSObjCIntegrationTests: TestInfo(
function: _runMacOSObjCIntegrationTests,
description: 'Integration tests on generated Objective-C code on macOS.'),
macOSSwiftUnitTests: TestInfo(
function: _runMacOSSwiftUnitTests,
description: 'Unit tests on generated Swift code on macOS.'),
macOSSwiftIntegrationTests: TestInfo(
function: _runMacOSSwiftIntegrationTests,
description: 'Integration tests on generated Swift code on macOS.'),
mockHandlerTests: TestInfo(
function: _runMockHandlerTests,
description: 'Unit tests on generated Dart mock handler code.'),
commandLineTests: TestInfo(
function: _runCommandLineTests,
description: 'Tests running pigeon with various command-line options.'),
};
Future<int> _runAndroidJavaUnitTests() async {
return _runAndroidUnitTests(_alternateLanguageTestPluginRelativePath);
}
Future<int> _runAndroidJavaIntegrationTests() async {
return _runMobileIntegrationTests(
'Android', _alternateLanguageTestPluginRelativePath);
}
Future<int> _runAndroidJavaLint() async {
const String examplePath =
'./$_alternateLanguageTestPluginRelativePath/example';
const String androidProjectPath = '$examplePath/android';
final File gradleFile = File(p.join(androidProjectPath, 'gradlew'));
if (!gradleFile.existsSync()) {
final int compileCode = await runFlutterBuild(examplePath, 'apk',
flags: <String>['--config-only']);
if (compileCode != 0) {
return compileCode;
}
}
return runGradleBuild(
androidProjectPath, 'alternate_language_test_plugin:lintDebug');
}
Future<int> _runAndroidKotlinUnitTests() async {
return _runAndroidUnitTests(_testPluginRelativePath);
}
Future<int> _runAndroidUnitTests(String testPluginPath) async {
final String examplePath = './$testPluginPath/example';
final String androidProjectPath = '$examplePath/android';
final File gradleFile = File(p.join(androidProjectPath, 'gradlew'));
if (!gradleFile.existsSync()) {
final int compileCode = await runFlutterBuild(examplePath, 'apk');
if (compileCode != 0) {
return compileCode;
}
}
return runGradleBuild(androidProjectPath, 'testDebugUnitTest');
}
Future<int> _runAndroidKotlinIntegrationTests() async {
return _runMobileIntegrationTests('Android', _testPluginRelativePath);
}
Future<int> _runMobileIntegrationTests(
String platform, String testPluginPath) async {
final String? device = await getDeviceForPlatform(platform.toLowerCase());
if (device == null) {
print('No $platform device available. Attach an $platform device or start '
'an emulator/simulator to run integration tests');
return _noDeviceAvailableExitCode;
}
final String examplePath = './$testPluginPath/example';
return runFlutterCommand(
examplePath,
'test',
<String>[_integrationTestFileRelativePath, '-d', device],
);
}
Future<int> _runDartUnitTests() async {
int exitCode = await runProcess('dart', <String>['analyze', 'bin']);
if (exitCode != 0) {
return exitCode;
}
exitCode = await runProcess('dart', <String>['analyze', 'lib']);
if (exitCode != 0) {
return exitCode;
}
exitCode = await runProcess('dart', <String>['test']);
return exitCode;
}
/// Generates multiple dart files based on the jobs defined in [jobs] which is
/// in the format of (key: input pigeon file path, value: output dart file
/// path).
Future<int> _generateDart(Map<String, String> jobs) async {
for (final MapEntry<String, String> job in jobs.entries) {
// TODO(gaaclarke): Make this run the jobs in parallel. A bug in Dart
// blocked this (https://github.com/dart-lang/pub/pull/3285).
final int result = await runPigeon(input: job.key, dartOut: job.value);
if (result != 0) {
return result;
}
}
return 0;
}
Future<int> _analyzeFlutterUnitTests(String flutterUnitTestsPath) async {
final String messagePath = '$flutterUnitTestsPath/lib/message.gen.dart';
final String messageTestPath = '$flutterUnitTestsPath/test/message_test.dart';
final int generateTestCode = await runPigeon(
input: 'pigeons/message.dart',
dartOut: messagePath,
dartTestOut: messageTestPath,
);
if (generateTestCode != 0) {
return generateTestCode;
}
final int analyzeCode =
await runFlutterCommand(flutterUnitTestsPath, 'analyze');
if (analyzeCode != 0) {
return analyzeCode;
}
// Delete these files that were just generated to help with the analyzer step.
File(messagePath).deleteSync();
File(messageTestPath).deleteSync();
return 0;
}
Future<int> _runFlutterUnitTests() async {
// TODO(stuartmorgan): Migrate Dart unit tests to use the generated output in
// shared_test_plugin_code instead of having multiple copies of generation.
const String flutterUnitTestsPath =
'platform_tests/flutter_null_safe_unit_tests';
// Files from the pigeons/ directory to generate output for.
const List<String> inputPigeons = <String>[
'flutter_unittests',
'core_tests',
'primitive',
'multiple_arity',
'non_null_fields',
'null_fields',
'nullable_returns',
];
final int generateCode = await _generateDart(<String, String>{
for (final String name in inputPigeons)
'pigeons/$name.dart': '$flutterUnitTestsPath/lib/$name.gen.dart'
});
if (generateCode != 0) {
return generateCode;
}
final int analyzeCode = await _analyzeFlutterUnitTests(flutterUnitTestsPath);
if (analyzeCode != 0) {
return analyzeCode;
}
final int testCode = await runFlutterCommand(flutterUnitTestsPath, 'test');
if (testCode != 0) {
return testCode;
}
return 0;
}
Future<int> _runIOSObjCUnitTests() async {
return _runIOSPluginUnitTests(_alternateLanguageTestPluginRelativePath);
}
Future<int> _runIOSObjCIntegrationTests() async {
final String? device = await getDeviceForPlatform('ios');
if (device == null) {
print('No iOS device available. Attach an iOS device or start '
'a simulator to run integration tests');
return _noDeviceAvailableExitCode;
}
const String examplePath =
'./$_alternateLanguageTestPluginRelativePath/example';
return runFlutterCommand(
examplePath,
'test',
<String>[_integrationTestFileRelativePath, '-d', device],
);
}
Future<int> _runMacOSObjCIntegrationTests() async {
const String examplePath =
'./$_alternateLanguageTestPluginRelativePath/example';
return runFlutterCommand(
examplePath,
'test',
<String>[_integrationTestFileRelativePath, '-d', 'macos'],
);
}
Future<int> _runMacOSSwiftUnitTests() async {
const String examplePath = './$_testPluginRelativePath/example';
final int compileCode = await runFlutterBuild(examplePath, 'macos');
if (compileCode != 0) {
return compileCode;
}
return runXcodeBuild(
'$examplePath/macos',
extraArguments: <String>[
'-configuration',
'Debug',
'test',
],
);
}
Future<int> _runMacOSSwiftIntegrationTests() async {
const String examplePath = './$_testPluginRelativePath/example';
return runFlutterCommand(
examplePath,
'test',
<String>[_integrationTestFileRelativePath, '-d', 'macos'],
);
}
Future<int> _runIOSSwiftUnitTests() async {
return _runIOSPluginUnitTests(_testPluginRelativePath);
}
Future<int> _runIOSPluginUnitTests(String testPluginPath) async {
final String examplePath = './$testPluginPath/example';
final int compileCode = await runFlutterBuild(
examplePath,
'ios',
flags: <String>['--simulator', '--no-codesign'],
);
if (compileCode != 0) {
return compileCode;
}
return runXcodeBuild(
'$examplePath/ios',
sdk: 'iphonesimulator',
destination: 'platform=iOS Simulator,name=iPhone 14',
extraArguments: <String>['test'],
);
}
Future<int> _runIOSSwiftIntegrationTests() async {
return _runMobileIntegrationTests('iOS', _testPluginRelativePath);
}
Future<int> _runMockHandlerTests() async {
const String unitTestsPath = './mock_handler_tester';
final int generateCode = await runPigeon(
input: './pigeons/message.dart',
dartOut: './mock_handler_tester/test/message.dart',
dartTestOut: './mock_handler_tester/test/test.dart',
);
if (generateCode != 0) {
return generateCode;
}
final int testCode = await runFlutterCommand(unitTestsPath, 'test');
if (testCode != 0) {
return testCode;
}
return 0;
}
Future<int> _runWindowsUnitTests() async {
const String examplePath = './$_testPluginRelativePath/example';
final int compileCode = await runFlutterBuild(examplePath, 'windows');
if (compileCode != 0) {
return compileCode;
}
return runProcess(
'$examplePath/build/windows/plugins/test_plugin/Debug/test_plugin_test.exe',
<String>[]);
}
Future<int> _runWindowsIntegrationTests() async {
const String examplePath = './$_testPluginRelativePath/example';
return runFlutterCommand(
examplePath,
'test',
<String>[_integrationTestFileRelativePath, '-d', 'windows'],
);
}
Future<int> _runCommandLineTests() async {
final Directory tempDir = Directory.systemTemp.createTempSync('pigeon');
final String tempOutput = p.join(tempDir.path, 'pigeon_output');
const String pigeonScript = 'bin/pigeon.dart';
final String snapshot = p.join(tempDir.path, 'pigeon.dart.dill');
// Precompile to make the repeated calls faster.
if (await runProcess('dart', <String>[
'--snapshot-kind=kernel',
'--snapshot=$snapshot',
pigeonScript
]) !=
0) {
print('Unable to generate $snapshot from $pigeonScript');
return 1;
}
final List<List<String>> testArguments = <List<String>>[
// Test with no arguments.
<String>[],
// Test one_language flag. With this flag specified, java_out can be
// generated without dart_out.
<String>[
'--input',
'pigeons/message.dart',
'--one_language',
'--java_out',
tempOutput
],
// Test dartOut in ConfigurePigeon overrides output.
<String>['--input', 'pigeons/configure_pigeon_dart_out.dart'],
// Make sure AST generation exits correctly.
<String>[
'--input',
'pigeons/message.dart',
'--one_language',
'--ast_out',
tempOutput
],
];
int exitCode = 0;
for (final List<String> arguments in testArguments) {
print('Testing dart $pigeonScript ${arguments.join(', ')}');
exitCode = await runProcess('dart', <String>[snapshot, ...arguments],
streamOutput: false, logFailure: true);
if (exitCode != 0) {
break;
}
}
tempDir.deleteSync(recursive: true);
return exitCode;
}