forbidden_imports_test.dart 11.4 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
        if (line.startsWith(RegExp(r'import.*globals.dart'))
         && !line.contains(r'as globals')) {
56
          final String relativePath = fileSystem.path.relative(file.path, from:flutterTools);
57 58 59 60 61 62
          fail('$relativePath imports globals.dart without a globals prefix.');
        }
      }
    }
  });

63
  test('no unauthorized imports of dart:io', () {
64
    final List<String> allowedPaths = <String>[
65 66 67
      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'),
68
    ];
69
    bool _isNotAllowed(FileSystemEntity entity) => allowedPaths.every((String path) => path != entity.path);
70

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

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  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.
      fileSystem.path.join(flutterTools, 'lib', 'src', 'reporting', 'reporting.dart'),
    ];
    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/')) &&
105
              !line.contains('flutter_ignore: package_http_import')) {
106 107 108 109 110 111 112 113
            final String relativePath = fileSystem.path.relative(file.path, from:flutterTools);
            fail("$relativePath imports 'package:http'; import 'lib/src/base/io.dart' instead");
          }
        }
      }
    }
  });

114
  test('no unauthorized imports of test_api', () {
115
    final List<String> allowedPaths = <String>[
116 117 118
      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'),
119
    ];
120
    bool _isNotAllowed(FileSystemEntity entity) => allowedPaths.every((String path) => path != entity.path);
121

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

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

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

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

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

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

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

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

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

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