forbidden_imports_test.dart 8.62 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// 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';
6
import 'package:flutter_tools/src/globals.dart' as globals;
7

8
import '../src/common.dart';
9

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

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

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

40 41 42 43 44 45 46 47 48 49
  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));

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

61
  test('no unauthorized imports of dart:io', () {
62
    final List<String> whitelistedPaths = <String>[
63 64
      globals.fs.path.join(flutterTools, 'lib', 'src', 'base', 'io.dart'),
      globals.fs.path.join(flutterTools, 'lib', 'src', 'base', 'error_handling_file_system.dart'),
65 66
    ];
    bool _isNotWhitelisted(FileSystemEntity entity) => whitelistedPaths.every((String path) => path != entity.path);
67

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

86 87
  test('no unauthorized imports of test_api', () {
    final List<String> whitelistedPaths = <String>[
88 89 90 91
      globals.fs.path.join(flutterTools, 'lib', 'src', 'build_runner', 'build_script.dart'),
      globals.fs.path.join(flutterTools, 'lib', 'src', 'test', 'flutter_platform.dart'),
      globals.fs.path.join(flutterTools, 'lib', 'src', 'test', 'flutter_web_platform.dart'),
      globals.fs.path.join(flutterTools, 'lib', 'src', 'test', 'test_wrapper.dart'),
92 93 94
    ];
    bool _isNotWhitelisted(FileSystemEntity entity) => whitelistedPaths.every((String path) => path != entity.path);

95
    for (final String dirName in <String>['lib']) {
96
      final Iterable<File> files = globals.fs.directory(globals.fs.path.join(flutterTools, dirName))
97 98 99 100
        .listSync(recursive: true)
        .where(_isDartFile)
        .where(_isNotWhitelisted)
        .map(_asFile);
101 102
      for (final File file in files) {
        for (final String line in file.readAsLinesSync()) {
103 104
          if (line.startsWith(RegExp(r'import.*package:test_api')) &&
              !line.contains('ignore: test_api_import')) {
105
            final String relativePath = globals.fs.path.relative(file.path, from:flutterTools);
106 107 108 109 110 111 112
            fail("$relativePath imports 'package:test_api/test_api.dart';");
          }
        }
      }
    }
  });

113
  test('no unauthorized imports of package:path', () {
114 115 116 117
    final List<String> whitelistedPath = <String>[
      globals.fs.path.join(flutterTools, 'lib', 'src', 'build_runner', 'web_compilation_delegate.dart'),
      globals.fs.path.join(flutterTools, 'test', 'general.shard', 'platform_plugins_test.dart'),
    ];
118
    for (final String dirName in <String>['lib', 'bin', 'test']) {
119
      final Iterable<File> files =  globals.fs.directory(globals.fs.path.join(flutterTools, dirName))
120 121
        .listSync(recursive: true)
        .where(_isDartFile)
122
        .where((FileSystemEntity entity) => !whitelistedPath.contains(entity.path))
123
        .map(_asFile);
124 125
      for (final File file in files) {
        for (final String line in file.readAsLinesSync()) {
126
          if (line.startsWith(RegExp(r'import.*package:path/path.dart')) &&
127
              !line.contains('ignore: package_path_import')) {
128 129
            final String relativePath = globals.fs.path.relative(file.path, from:flutterTools);
            fail("$relativePath imports 'package:path/path.dart'; use 'globals.fs.path' instead");
130 131
          }
        }
132
      }
133 134
    }
  });
135 136

  test('no unauthorized imports of dart:convert', () {
137
    final List<String> whitelistedPaths = <String>[
138 139
      globals.fs.path.join(flutterTools, 'lib', 'src', 'convert.dart'),
      globals.fs.path.join(flutterTools, 'lib', 'src', 'base', 'error_handling_file_system.dart'),
140 141
    ];
    bool _isNotWhitelisted(FileSystemEntity entity) => whitelistedPaths.every((String path) => path != entity.path);
142

143
    for (final String dirName in <String>['lib']) {
144
      final Iterable<File> files = globals.fs.directory(globals.fs.path.join(flutterTools, dirName))
145 146 147 148
        .listSync(recursive: true)
        .where(_isDartFile)
        .where(_isNotWhitelisted)
        .map(_asFile);
149 150
      for (final File file in files) {
        for (final String line in file.readAsLinesSync()) {
151
          if (line.startsWith(RegExp(r'import.*dart:convert')) &&
152
              !line.contains('ignore: dart_convert_import')) {
153
            final String relativePath = globals.fs.path.relative(file.path, from:flutterTools);
154 155 156 157 158 159
            fail("$relativePath imports 'dart:convert'; import 'lib/src/convert.dart' instead");
          }
        }
      }
    }
  });
160 161 162

  test('no unauthorized imports of build_runner', () {
    final List<String> whitelistedPaths = <String>[
163 164 165
      globals.fs.path.join(flutterTools, 'test', 'src', 'build_runner'),
      globals.fs.path.join(flutterTools, 'lib', 'src', 'build_runner'),
      globals.fs.path.join(flutterTools, 'lib', 'executable.dart'),
166 167 168
    ];
    bool _isNotWhitelisted(FileSystemEntity entity) => whitelistedPaths.every((String path) => !entity.path.contains(path));

169
    for (final String dirName in <String>['lib']) {
170
      final Iterable<File> files = globals.fs.directory(globals.fs.path.join(flutterTools, dirName))
171 172 173 174
        .listSync(recursive: true)
        .where(_isDartFile)
        .where(_isNotWhitelisted)
        .map(_asFile);
175 176
      for (final File file in files) {
        for (final String line in file.readAsLinesSync()) {
177 178 179 180
          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'))) {
181
            final String relativePath = globals.fs.path.relative(file.path, from:flutterTools);
182 183 184 185 186 187
            fail('$relativePath imports a build_runner package');
          }
        }
      }
    }
  });
188
}
189

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

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