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

7 8
import 'src/common.dart';

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

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

16
    for (String dirName in <String>['lib', 'bin']) {
17
      final Iterable<File> files = fs.directory(fs.path.join(flutterTools, dirName))
18 19 20
        .listSync(recursive: true)
        .where(_isDartFile)
        .where(_isNotWhitelisted)
21 22 23
        .map(_asFile);
      for (File file in files) {
        for (String line in file.readAsLinesSync()) {
24
          if (line.startsWith(RegExp(r'import.*dart:io')) &&
25 26 27
              !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");
28 29
          }
        }
30
      }
31 32 33
    }
  });

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

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

File _asFile(FileSystemEntity entity) => entity;