forbidden_imports_test.dart 11.5 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

7
import 'package:file/file.dart';
8

9
import '../src/common.dart';
10
import 'test_utils.dart';
11

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

15 16
  test('no imports of commands/* or test/* in lib/src/*', () {
    final List<String> skippedPaths = <String> [
17 18
      fileSystem.path.join(flutterTools, 'lib', 'src', 'commands'),
      fileSystem.path.join(flutterTools, 'lib', 'src', 'test'),
19 20 21
    ];
    bool _isNotSkipped(FileSystemEntity entity) => skippedPaths.every((String path) => !entity.path.startsWith(path));

22
    final Iterable<File> files = fileSystem.directory(fileSystem.path.join(flutterTools, 'lib', 'src'))
23 24 25 26
      .listSync(recursive: true)
      .where(_isDartFile)
      .where(_isNotSkipped)
      .map(_asFile);
27 28
    for (final File file in files) {
      for (final String line in file.readAsLinesSync()) {
29 30 31 32 33
        if (line.startsWith(RegExp(r'import.*package:'))) {
          continue;
        }
        if (line.startsWith(RegExp(r'import.*commands/'))
         || line.startsWith(RegExp(r'import.*test/'))) {
34
          final String relativePath = fileSystem.path.relative(file.path, from:flutterTools);
35 36 37 38 39 40 41
          fail('$relativePath imports $line. This import introduces a layering violation. '
               'Please find another way to access the information you are using.');
        }
      }
    }
  });

42 43 44 45
  test('no imports of globals without a global prefix', () {
    final List<String> skippedPaths = <String> [];
    bool _isNotSkipped(FileSystemEntity entity) => skippedPaths.every((String path) => !entity.path.startsWith(path));

46
    final Iterable<File> files = fileSystem.directory(fileSystem.path.join(flutterTools, 'lib', 'src'))
47
      .listSync(recursive: true)
48
      .followedBy(fileSystem.directory(fileSystem.path.join(flutterTools, 'test',)).listSync(recursive: true))
49 50 51
      .where(_isDartFile)
      .where(_isNotSkipped)
      .map(_asFile);
52 53
    for (final File file in files) {
      for (final String line in file.readAsLinesSync()) {
54 55 56
        if ((line.startsWith(RegExp(r'import.*globals.dart')) ||
                line.startsWith(RegExp(r'import.*globals_null_migrated.dart'))) &&
            !line.contains(r'as globals')) {
57
          final String relativePath = fileSystem.path.relative(file.path, from:flutterTools);
58
          fail('$relativePath imports globals_null_migrated.dart or globals.dart without a globals prefix.');
59 60 61 62 63
        }
      }
    }
  });

64
  test('no unauthorized imports of dart:io', () {
65
    final List<String> allowedPaths = <String>[
66 67 68
      fileSystem.path.join(flutterTools, 'lib', 'src', 'base', 'io.dart'),
      fileSystem.path.join(flutterTools, 'lib', 'src', 'base', 'platform.dart'),
      fileSystem.path.join(flutterTools, 'lib', 'src', 'base', 'error_handling_io.dart'),
69
    ];
70
    bool _isNotAllowed(FileSystemEntity entity) => allowedPaths.every((String path) => path != entity.path);
71

72
    for (final String dirName in <String>['lib', 'bin']) {
73
      final Iterable<File> files = fileSystem.directory(fileSystem.path.join(flutterTools, dirName))
74 75
        .listSync(recursive: true)
        .where(_isDartFile)
76
        .where(_isNotAllowed)
77
        .map(_asFile);
78 79
      for (final File file in files) {
        for (final String line in file.readAsLinesSync()) {
80
          if (line.startsWith(RegExp(r'import.*dart:io')) &&
81
              !line.contains('flutter_ignore: dart_io_import')) {
82
            final String relativePath = fileSystem.path.relative(file.path, from:flutterTools);
83
            fail("$relativePath imports 'dart:io'; import 'lib/src/base/io.dart' instead");
84 85
          }
        }
86
      }
87 88 89
    }
  });

90 91 92
  test('no unauthorized imports of package:http', () {
    final List<String> allowedPaths = <String>[
      // Used only for multi-part file uploads, which are non-trivial to reimplement.
93
      fileSystem.path.join(flutterTools, 'lib', 'src', 'reporting', 'crash_reporting.dart'),
94 95 96 97 98 99 100 101 102 103 104 105
    ];
    bool _isNotAllowed(FileSystemEntity entity) => allowedPaths.every((String path) => path != entity.path);

    for (final String dirName in <String>['lib', 'bin']) {
      final Iterable<File> files = fileSystem.directory(fileSystem.path.join(flutterTools, dirName))
        .listSync(recursive: true)
        .where(_isDartFile)
        .where(_isNotAllowed)
        .map(_asFile);
      for (final File file in files) {
        for (final String line in file.readAsLinesSync()) {
          if (line.startsWith(RegExp(r'import.*package:http/')) &&
106
              !line.contains('flutter_ignore: package_http_import')) {
107 108 109 110 111 112 113 114
            final String relativePath = fileSystem.path.relative(file.path, from:flutterTools);
            fail("$relativePath imports 'package:http'; import 'lib/src/base/io.dart' instead");
          }
        }
      }
    }
  });

115
  test('no unauthorized imports of test_api', () {
116
    final List<String> allowedPaths = <String>[
117 118 119
      fileSystem.path.join(flutterTools, 'lib', 'src', 'test', 'flutter_platform.dart'),
      fileSystem.path.join(flutterTools, 'lib', 'src', 'test', 'flutter_web_platform.dart'),
      fileSystem.path.join(flutterTools, 'lib', 'src', 'test', 'test_wrapper.dart'),
120
    ];
121
    bool _isNotAllowed(FileSystemEntity entity) => allowedPaths.every((String path) => path != entity.path);
122

123
    for (final String dirName in <String>['lib']) {
124
      final Iterable<File> files = fileSystem.directory(fileSystem.path.join(flutterTools, dirName))
125 126
        .listSync(recursive: true)
        .where(_isDartFile)
127
        .where(_isNotAllowed)
128
        .map(_asFile);
129 130
      for (final File file in files) {
        for (final String line in file.readAsLinesSync()) {
131
          if (line.startsWith(RegExp(r'import.*package:test_api')) &&
132
              !line.contains('flutter_ignore: test_api_import')) {
133
            final String relativePath = fileSystem.path.relative(file.path, from:flutterTools);
134 135 136 137 138 139 140
            fail("$relativePath imports 'package:test_api/test_api.dart';");
          }
        }
      }
    }
  });

141
  test('no unauthorized imports of package:path', () {
142
    final List<String> allowedPath = <String>[
143
      fileSystem.path.join(flutterTools, 'lib', 'src', 'isolated', 'web_compilation_delegate.dart'),
144
      fileSystem.path.join(flutterTools, 'test', 'general.shard', 'platform_plugins_test.dart'),
145
    ];
146
    for (final String dirName in <String>['lib', 'bin', 'test']) {
147
      final Iterable<File> files =  fileSystem.directory(fileSystem.path.join(flutterTools, dirName))
148 149
        .listSync(recursive: true)
        .where(_isDartFile)
150
        .where((FileSystemEntity entity) => !allowedPath.contains(entity.path))
151
        .map(_asFile);
152 153
      for (final File file in files) {
        for (final String line in file.readAsLinesSync()) {
154
          if (line.startsWith(RegExp(r'import.*package:path/path.dart')) &&
155
              !line.contains('flutter_ignore: package_path_import')) {
156 157
            final String relativePath = fileSystem.path.relative(file.path, from:flutterTools);
            fail("$relativePath imports 'package:path/path.dart'; use 'fileSystem.path' instead");
158 159
          }
        }
160
      }
161 162
    }
  });
163

164
  test('no unauthorized imports of package:file/local.dart', () {
165
    final List<String> allowedPath = <String>[
166 167
      fileSystem.path.join(flutterTools, 'test', 'integration.shard', 'test_utils.dart'),
      fileSystem.path.join(flutterTools, 'lib', 'src', 'base', 'file_system.dart'),
168 169
    ];
    for (final String dirName in <String>['lib', 'bin', 'test']) {
170
      final Iterable<File> files =  fileSystem.directory(fileSystem.path.join(flutterTools, dirName))
171 172
        .listSync(recursive: true)
        .where(_isDartFile)
173
        .where((FileSystemEntity entity) => !allowedPath.contains(entity.path))
174 175 176 177
        .map(_asFile);
      for (final File file in files) {
        for (final String line in file.readAsLinesSync()) {
          if (line.startsWith(RegExp(r'import.*package:file/local.dart'))) {
178
            final String relativePath = fileSystem.path.relative(file.path, from:flutterTools);
179 180 181 182 183 184 185
            fail("$relativePath imports 'package:file/local.dart'; use 'lib/src/base/file_system.dart' instead");
          }
        }
      }
    }
  });

186
  test('no unauthorized imports of dart:convert', () {
187
    final List<String> allowedPaths = <String>[
188 189
      fileSystem.path.join(flutterTools, 'lib', 'src', 'convert.dart'),
      fileSystem.path.join(flutterTools, 'lib', 'src', 'base', 'error_handling_io.dart'),
190
    ];
191
    bool _isNotAllowed(FileSystemEntity entity) => allowedPaths.every((String path) => path != entity.path);
192

193
    for (final String dirName in <String>['lib']) {
194
      final Iterable<File> files = fileSystem.directory(fileSystem.path.join(flutterTools, dirName))
195 196
        .listSync(recursive: true)
        .where(_isDartFile)
197
        .where(_isNotAllowed)
198
        .map(_asFile);
199 200
      for (final File file in files) {
        for (final String line in file.readAsLinesSync()) {
201
          if (line.startsWith(RegExp(r'import.*dart:convert')) &&
202
              !line.contains('flutter_ignore: dart_convert_import')) {
203
            final String relativePath = fileSystem.path.relative(file.path, from:flutterTools);
204 205 206 207 208 209
            fail("$relativePath imports 'dart:convert'; import 'lib/src/convert.dart' instead");
          }
        }
      }
    }
  });
210

211
  test('no unauthorized imports of build_runner/dwds/devtools', () {
212
    final List<String> allowedPaths = <String>[
213 214
      fileSystem.path.join(flutterTools, 'test', 'src', 'isolated'),
      fileSystem.path.join(flutterTools, 'lib', 'src', 'isolated'),
215 216 217
      fileSystem.path.join(flutterTools, 'lib', 'executable.dart'),
      fileSystem.path.join(flutterTools, 'lib', 'devfs_web.dart'),
      fileSystem.path.join(flutterTools, 'lib', 'resident_web_runner.dart'),
218
    ];
219
    bool _isNotAllowed(FileSystemEntity entity) => allowedPaths.every((String path) => !entity.path.contains(path));
220

221
    for (final String dirName in <String>['lib']) {
222
      final Iterable<File> files = fileSystem.directory(fileSystem.path.join(flutterTools, dirName))
223 224
        .listSync(recursive: true)
        .where(_isDartFile)
225
        .where(_isNotAllowed)
226
        .map(_asFile);
227 228
      for (final File file in files) {
        for (final String line in file.readAsLinesSync()) {
229 230 231
          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')) ||
232
              line.startsWith(RegExp(r'import.*dwds:*.dart')) ||
233
              line.startsWith(RegExp(r'import.*devtools_server:*.dart')) ||
234
              line.startsWith(RegExp(r'import.*build_runner/.*.dart'))) {
235
            final String relativePath = fileSystem.path.relative(file.path, from:flutterTools);
236
            fail('$relativePath imports a build_runner/dwds/devtools package');
237 238 239 240 241
          }
        }
      }
    }
  });
242 243

  test('no import of packages in tool_backend.dart', () {
244
    final File file = fileSystem.file(fileSystem.path.join(flutterTools, 'bin', 'tool_backend.dart'));
245 246
    for (final String line in file.readAsLinesSync()) {
      if (line.startsWith(RegExp(r'import.*package:.*'))) {
247
        final String relativePath = fileSystem.path.relative(file.path, from:flutterTools);
248 249 250 251
        fail('$relativePath imports a package');
      }
    }
  });
252
}
253

254
bool _isDartFile(FileSystemEntity entity) => entity is File && entity.path.endsWith('.dart');
255

256
File _asFile(FileSystemEntity entity) => entity as File;