forbidden_imports_test.dart 2.09 KB
Newer Older
1 2 3 4 5 6 7
// 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:test/test.dart';

8 9
import 'src/common.dart';

10
void main() {
11
  final String flutterTools = fs.path.join(getFlutterRoot(), 'packages', 'flutter_tools');
12 13

  test('no unauthorized imports of dart:io', () {
14 15
    final String whitelistedPath = fs.path.join(flutterTools, 'lib', 'src', 'base', 'io.dart');
    bool _isNotWhitelisted(FileSystemEntity entity) => entity.path != whitelistedPath;
16

17
    for (String dirName in <String>['lib', 'bin']) {
18
      final Iterable<File> files = fs.directory(fs.path.join(flutterTools, dirName))
19 20 21
        .listSync(recursive: true)
        .where(_isDartFile)
        .where(_isNotWhitelisted)
22 23 24 25 26 27 28
        .map(_asFile);
      for (File file in files) {
        for (String line in file.readAsLinesSync()) {
          if (line.startsWith(new RegExp(r'import.*dart:io')) &&
              !line.contains('ignore: dart_io_import')) {
            final String relativePath = fs.path.relative(file.path, from:flutterTools);
            fail("$relativePath imports 'dart:io'; import 'lib/src/base/io.dart' instead");
29 30
          }
        }
31
      }
32 33 34
    }
  });

35
  test('no unauthorized imports of package:path', () {
36
    for (String dirName in <String>['lib', 'bin', 'test']) {
37
      final Iterable<File> files = fs.directory(fs.path.join(flutterTools, dirName))
38 39
        .listSync(recursive: true)
        .where(_isDartFile)
40 41 42 43 44 45
        .map(_asFile);
      for (File file in files) {
        for (String line in file.readAsLinesSync()) {
          if (line.startsWith(new RegExp(r'import.*package:path/path.dart'))) {
            final String relativePath = fs.path.relative(file.path, from:flutterTools);
            fail("$relativePath imports 'package:path/path.dart'; use 'fs.path' instead");
46 47
          }
        }
48
      }
49 50 51
    }
  });
}
52

53
bool _isDartFile(FileSystemEntity entity) => entity is File && entity.path.endsWith('.dart');
54 55

File _asFile(FileSystemEntity entity) => entity;