Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 7723022

Browse files
[tools] Check integration tests for test (#5936)
1 parent 3d7d7af commit 7723022

File tree

5 files changed

+76
-3
lines changed

5 files changed

+76
-3
lines changed

packages/path_provider/path_provider/example/integration_test/path_provider_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ void main() {
7070
];
7171

7272
for (final StorageDirectory? type in _allDirs) {
73-
test('getExternalStorageDirectories (type: $type)', () async {
73+
testWidgets('getExternalStorageDirectories (type: $type)',
74+
(WidgetTester tester) async {
7475
if (Platform.isIOS) {
7576
final Future<List<Directory>?> result =
7677
getExternalStorageDirectories(type: null);

packages/path_provider/path_provider_android/example/integration_test/path_provider_test.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ void main() {
6161
];
6262

6363
for (final StorageDirectory? type in _allDirs) {
64-
test('getExternalStorageDirectories (type: $type)', () async {
64+
testWidgets('getExternalStorageDirectories (type: $type)',
65+
(WidgetTester tester) async {
6566
final PathProviderPlatform provider = PathProviderPlatform.instance;
6667

6768
final List<String>? directories =

script/tool/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
## NEXT
22

33
- Supports empty custom analysis allow list files.
4+
- `drive-examples` now validates files to ensure that they don't accidentally
5+
use `test(...)`.
46

57
## 0.8.6
68

script/tool/lib/src/drive_examples_command.dart

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,16 @@ class DriveExamplesCommand extends PackageLoopingCommand {
182182
if (legacyTestFile != null) {
183183
testTargets.add(legacyTestFile);
184184
} else {
185-
(await _getIntegrationTests(example)).forEach(testTargets.add);
185+
for (final File testFile in await _getIntegrationTests(example)) {
186+
// Check files for known problematic patterns.
187+
final bool passesValidation = _validateIntegrationTest(testFile);
188+
if (!passesValidation) {
189+
// Report the issue, but continue with the test as the validation
190+
// errors don't prevent running.
191+
errors.add('${testFile.basename} failed validation');
192+
}
193+
testTargets.add(testFile);
194+
}
186195
}
187196

188197
if (testTargets.isEmpty) {
@@ -310,6 +319,25 @@ class DriveExamplesCommand extends PackageLoopingCommand {
310319
return tests;
311320
}
312321

322+
/// Checks [testFile] for known bad patterns in integration tests, logging
323+
/// any issues.
324+
///
325+
/// Returns true if the file passes validation without issues.
326+
bool _validateIntegrationTest(File testFile) {
327+
final List<String> lines = testFile.readAsLinesSync();
328+
329+
final RegExp badTestPattern = RegExp(r'\s*test\(');
330+
if (lines.any((String line) => line.startsWith(badTestPattern))) {
331+
final String filename = testFile.basename;
332+
printError(
333+
'$filename uses "test", which will not report failures correctly. '
334+
'Use testWidgets instead.');
335+
return false;
336+
}
337+
338+
return true;
339+
}
340+
313341
/// For each file in [targets], uses
314342
/// `flutter drive --driver [driver] --target <target>`
315343
/// to drive [example], returning a list of any failing test targets.

script/tool/test/drive_examples_command_test.dart

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,47 @@ void main() {
307307
);
308308
});
309309

310+
test('integration tests using test(...) fail validation', () async {
311+
setMockFlutterDevicesOutput();
312+
final RepositoryPackage package = createFakePlugin(
313+
'plugin',
314+
packagesDir,
315+
extraFiles: <String>[
316+
'example/test_driver/integration_test.dart',
317+
'example/integration_test/foo_test.dart',
318+
'example/android/android.java',
319+
],
320+
platformSupport: <String, PlatformDetails>{
321+
platformAndroid: const PlatformDetails(PlatformSupport.inline),
322+
platformIOS: const PlatformDetails(PlatformSupport.inline),
323+
},
324+
);
325+
package.directory
326+
.childDirectory('example')
327+
.childDirectory('integration_test')
328+
.childFile('foo_test.dart')
329+
.writeAsStringSync('''
330+
test('this is the wrong kind of test!'), () {
331+
...
332+
}
333+
''');
334+
335+
Error? commandError;
336+
final List<String> output = await runCapturingPrint(
337+
runner, <String>['drive-examples', '--android'],
338+
errorHandler: (Error e) {
339+
commandError = e;
340+
});
341+
342+
expect(commandError, isA<ToolExit>());
343+
expect(
344+
output,
345+
containsAllInOrder(<Matcher>[
346+
contains('foo_test.dart failed validation'),
347+
]),
348+
);
349+
});
350+
310351
test(
311352
'driving under folder "test_driver" when targets are under "integration_test"',
312353
() async {

0 commit comments

Comments
 (0)