forbidden_imports_test.dart 5.73 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
import '../src/common.dart';
8

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

12 13 14
  test('no imports of commands/* or test/* in lib/src/*', () {
    final List<String> skippedPaths = <String> [
      fs.path.join(flutterTools, 'lib', 'src', 'commands'),
15
      fs.path.join(flutterTools, 'lib', 'src', 'test'),
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    ];
    bool _isNotSkipped(FileSystemEntity entity) => skippedPaths.every((String path) => !entity.path.startsWith(path));

    final Iterable<File> files = fs.directory(fs.path.join(flutterTools, 'lib', 'src'))
      .listSync(recursive: true)
      .where(_isDartFile)
      .where(_isNotSkipped)
      .map(_asFile);
    for (File file in files) {
      for (String line in file.readAsLinesSync()) {
        if (line.startsWith(RegExp(r'import.*package:'))) {
          continue;
        }
        if (line.startsWith(RegExp(r'import.*commands/'))
         || line.startsWith(RegExp(r'import.*test/'))) {
          final String relativePath = fs.path.relative(file.path, from:flutterTools);
          fail('$relativePath imports $line. This import introduces a layering violation. '
               'Please find another way to access the information you are using.');
        }
      }
    }
  });

39
  test('no unauthorized imports of dart:io', () {
40 41 42 43
    final List<String> whitelistedPaths = <String>[
      fs.path.join(flutterTools, 'lib', 'src', 'base', 'io.dart'),
    ];
    bool _isNotWhitelisted(FileSystemEntity entity) => whitelistedPaths.every((String path) => path != entity.path);
44

45
    for (String dirName in <String>['lib', 'bin']) {
46
      final Iterable<File> files = fs.directory(fs.path.join(flutterTools, dirName))
47 48 49
        .listSync(recursive: true)
        .where(_isDartFile)
        .where(_isNotWhitelisted)
50 51 52
        .map(_asFile);
      for (File file in files) {
        for (String line in file.readAsLinesSync()) {
53
          if (line.startsWith(RegExp(r'import.*dart:io')) &&
54 55 56
              !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");
57 58
          }
        }
59
      }
60 61 62
    }
  });

63
  test('no unauthorized imports of package:path', () {
64
    final String whitelistedPath = fs.path.join(flutterTools, 'lib', 'src', 'build_runner', 'web_compilation_delegate.dart');
65
    for (String dirName in <String>['lib', 'bin', 'test']) {
66
      final Iterable<File> files = fs.directory(fs.path.join(flutterTools, dirName))
67 68
        .listSync(recursive: true)
        .where(_isDartFile)
69
        .where((FileSystemEntity entity) => entity.path != whitelistedPath)
70 71 72
        .map(_asFile);
      for (File file in files) {
        for (String line in file.readAsLinesSync()) {
73 74
          if (line.startsWith(RegExp(r'import.*package:path/path.dart')) &&
              !line.contains('ignore: package_path_import')) {
75 76
            final String relativePath = fs.path.relative(file.path, from:flutterTools);
            fail("$relativePath imports 'package:path/path.dart'; use 'fs.path' instead");
77 78
          }
        }
79
      }
80 81
    }
  });
82 83

  test('no unauthorized imports of dart:convert', () {
84 85 86 87
    final List<String> whitelistedPaths = <String>[
      fs.path.join(flutterTools, 'lib', 'src', 'convert.dart'),
    ];
    bool _isNotWhitelisted(FileSystemEntity entity) => whitelistedPaths.every((String path) => path != entity.path);
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

    for (String dirName in <String>['lib']) {
      final Iterable<File> files = fs.directory(fs.path.join(flutterTools, dirName))
        .listSync(recursive: true)
        .where(_isDartFile)
        .where(_isNotWhitelisted)
        .map(_asFile);
      for (File file in files) {
        for (String line in file.readAsLinesSync()) {
          if (line.startsWith(RegExp(r'import.*dart:convert')) &&
              !line.contains('ignore: dart_convert_import')) {
            final String relativePath = fs.path.relative(file.path, from:flutterTools);
            fail("$relativePath imports 'dart:convert'; import 'lib/src/convert.dart' instead");
          }
        }
      }
    }
  });
106 107 108 109 110

  test('no unauthorized imports of build_runner', () {
    final List<String> whitelistedPaths = <String>[
      fs.path.join(flutterTools, 'test', 'src', 'build_runner'),
      fs.path.join(flutterTools, 'lib', 'src', 'build_runner'),
111
      fs.path.join(flutterTools, 'lib', 'executable.dart'),
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    ];
    bool _isNotWhitelisted(FileSystemEntity entity) => whitelistedPaths.every((String path) => !entity.path.contains(path));

    for (String dirName in <String>['lib']) {
      final Iterable<File> files = fs.directory(fs.path.join(flutterTools, dirName))
        .listSync(recursive: true)
        .where(_isDartFile)
        .where(_isNotWhitelisted)
        .map(_asFile);
      for (File file in files) {
        for (String line in file.readAsLinesSync()) {
          if (line.startsWith(RegExp(r'import.*package:build_runner_core/build_runner_core.dart')) ||
              line.startsWith(RegExp(r'import.*package:build_runner/build_runner.dart')) ||
              line.startsWith(RegExp(r'import.*package:build_config/build_config.dart')) ||
              line.startsWith(RegExp(r'import.*build_runner/.*.dart'))) {
            final String relativePath = fs.path.relative(file.path, from:flutterTools);
            fail('$relativePath imports a build_runner package');
          }
        }
      }
    }
  });
134
}
135

136
bool _isDartFile(FileSystemEntity entity) => entity is File && entity.path.endsWith('.dart');
137 138

File _asFile(FileSystemEntity entity) => entity;