diff --git a/dev/bots/test.sh b/dev/bots/test.sh index d81d164cbe..1ada7fca09 100755 --- a/dev/bots/test.sh +++ b/dev/bots/test.sh @@ -14,27 +14,6 @@ detect_error_on_exit() { fi } -check_for_dart_io() { - { set +x; } 2>/dev/null - ( - cd packages/flutter_tools - for f in $(grep -Erl '^import.*dart:io' lib bin test); do - if [[ "$f" == "lib/src/base/io.dart" ]]; then - continue - fi - - IFS=$'\n' matches=( $(grep -E '^import.*dart:io' $f) ) - for match in "${matches[@]}"; do - if [[ "$match" != *"ignore: dart_io_import"* ]]; then - echo -e "\x1B[31mError: $f imports 'dart:io'; import 'lib/src/base/io.dart' instead\x1B[0m" - exit 1 - fi - done - done - ) - set -ex -} - set -ex # analyze all the Dart code in the repo @@ -61,7 +40,6 @@ SRC_ROOT=$PWD (cd packages/flutter_driver; dart -c test/all.dart) (cd packages/flutter_test; flutter test) (cd packages/flutter_tools; FLUTTER_ROOT=$SRC_ROOT dart -c test/all.dart) -check_for_dart_io (cd dev/devicelab; dart -c test/all.dart) (cd dev/manual_tests; flutter test) diff --git a/packages/flutter_tools/test/all.dart b/packages/flutter_tools/test/all.dart index c47203e367..741d211abb 100644 --- a/packages/flutter_tools/test/all.dart +++ b/packages/flutter_tools/test/all.dart @@ -15,21 +15,22 @@ import 'analyze_duplicate_names_test.dart' as analyze_duplicate_names_test; import 'analyze_test.dart' as analyze_test; import 'android_device_test.dart' as android_device_test; import 'android_sdk_test.dart' as android_sdk_test; -import 'asset_bundle_test.dart' as asset_bundle_test; import 'application_package_test.dart' as application_package_test; +import 'asset_bundle_test.dart' as asset_bundle_test; import 'base_utils_test.dart' as base_utils_test; -import 'dart_dependencies_test.dart' as dart_dependencies_test; -import 'dependency_checker_test.dart' as dependency_checker_test; import 'channel_test.dart' as channel_test; import 'config_test.dart' as config_test; import 'context_test.dart' as context_test; import 'create_test.dart' as create_test; import 'daemon_test.dart' as daemon_test; +import 'dart_dependencies_test.dart' as dart_dependencies_test; +import 'dependency_checker_test.dart' as dependency_checker_test; import 'devfs_test.dart' as devfs_test; import 'device_test.dart' as device_test; import 'devices_test.dart' as devices_test; import 'doctor_test.dart' as doctor_test; import 'drive_test.dart' as drive_test; +import 'forbid_dart_io_test.dart' as forbid_dart_io_test; import 'format_test.dart' as format_test; import 'hot_test.dart' as hot_test; import 'install_test.dart' as install_test; @@ -57,18 +58,19 @@ void main() { application_package_test.main(); asset_bundle_test.main(); base_utils_test.main(); - dart_dependencies_test.main(); channel_test.main(); config_test.main(); context_test.main(); create_test.main(); daemon_test.main(); + dart_dependencies_test.main(); dependency_checker_test.main(); devfs_test.main(); device_test.main(); devices_test.main(); doctor_test.main(); drive_test.main(); + forbid_dart_io_test.main(); format_test.main(); hot_test.main(); install_test.main(); diff --git a/packages/flutter_tools/test/forbid_dart_io_test.dart b/packages/flutter_tools/test/forbid_dart_io_test.dart new file mode 100644 index 0000000000..c361787d07 --- /dev/null +++ b/packages/flutter_tools/test/forbid_dart_io_test.dart @@ -0,0 +1,41 @@ +// Copyright 2016 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter_tools/src/base/file_system.dart'; +import 'package:flutter_tools/src/base/io.dart'; +import 'package:test/test.dart'; + +void main() { + setUp(() { + String flutterRoot = Platform.environment['FLUTTER_ROOT']; + assert(fs.currentDirectory.path == '$flutterRoot/packages/flutter_tools'); + }); + + test('no unauthorized imports of dart:io', () { + for (String path in ['lib', 'bin', 'test']) { + fs.directory(path) + .listSync(recursive: true) + .where(_isDartFile) + .where(_isNotWhitelisted) + .map(_asFile) + .forEach((File file) { + for (String line in file.readAsLinesSync()) { + if (line.startsWith(new RegExp('import.*dart:io')) && + !line.contains('ignore: dart_io_import')) { + fail("${file.path} imports 'dart:io'; import 'lib/src/base/io.dart' instead"); + } + } + } + ); + } + }); +} + +bool _isDartFile(FileSystemEntity entity) => + entity is File && entity.path.endsWith('.dart'); + +bool _isNotWhitelisted(FileSystemEntity entity) => + entity.path != 'lib/src/base/io.dart'; + +File _asFile(FileSystemEntity entity) => entity;