[flutter_tools] Ensure that global variables are easily identifiable (#47398)
This commit is contained in:
@@ -8,11 +8,12 @@ import 'package:args/command_runner.dart';
|
||||
import 'package:flutter_tools/src/base/common.dart';
|
||||
import 'package:flutter_tools/src/base/context.dart';
|
||||
import 'package:flutter_tools/src/base/file_system.dart';
|
||||
import 'package:flutter_tools/src/base/platform.dart';
|
||||
|
||||
import 'package:flutter_tools/src/base/process.dart';
|
||||
import 'package:flutter_tools/src/commands/create.dart';
|
||||
import 'package:flutter_tools/src/runner/flutter_command.dart';
|
||||
import 'package:flutter_tools/src/runner/flutter_command_runner.dart';
|
||||
import 'package:flutter_tools/src/globals.dart' as globals;
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:test_api/test_api.dart' as test_package show TypeMatcher, test; // ignore: deprecated_member_use
|
||||
import 'package:test_api/test_api.dart' hide TypeMatcher, isInstanceOf; // ignore: deprecated_member_use
|
||||
@@ -40,20 +41,20 @@ void tryToDelete(Directory directory) {
|
||||
/// environment variable is set, it will be returned. Otherwise, this will
|
||||
/// deduce the path from `platform.script`.
|
||||
String getFlutterRoot() {
|
||||
if (platform.environment.containsKey('FLUTTER_ROOT')) {
|
||||
return platform.environment['FLUTTER_ROOT'];
|
||||
if (globals.platform.environment.containsKey('FLUTTER_ROOT')) {
|
||||
return globals.platform.environment['FLUTTER_ROOT'];
|
||||
}
|
||||
|
||||
Error invalidScript() => StateError('Could not determine flutter_tools/ path from script URL (${platform.script}); consider setting FLUTTER_ROOT explicitly.');
|
||||
Error invalidScript() => StateError('Could not determine flutter_tools/ path from script URL (${globals.platform.script}); consider setting FLUTTER_ROOT explicitly.');
|
||||
|
||||
Uri scriptUri;
|
||||
switch (platform.script.scheme) {
|
||||
switch (globals.platform.script.scheme) {
|
||||
case 'file':
|
||||
scriptUri = platform.script;
|
||||
scriptUri = globals.platform.script;
|
||||
break;
|
||||
case 'data':
|
||||
final RegExp flutterTools = RegExp(r'(file://[^"]*[/\\]flutter_tools[/\\][^"]+\.dart)', multiLine: true);
|
||||
final Match match = flutterTools.firstMatch(Uri.decodeFull(platform.script.path));
|
||||
final Match match = flutterTools.firstMatch(Uri.decodeFull(globals.platform.script.path));
|
||||
if (match == null) {
|
||||
throw invalidScript();
|
||||
}
|
||||
@@ -63,13 +64,13 @@ String getFlutterRoot() {
|
||||
throw invalidScript();
|
||||
}
|
||||
|
||||
final List<String> parts = fs.path.split(fs.path.fromUri(scriptUri));
|
||||
final List<String> parts = globals.fs.path.split(globals.fs.path.fromUri(scriptUri));
|
||||
final int toolsIndex = parts.indexOf('flutter_tools');
|
||||
if (toolsIndex == -1) {
|
||||
throw invalidScript();
|
||||
}
|
||||
final String toolsPath = fs.path.joinAll(parts.sublist(0, toolsIndex + 1));
|
||||
return fs.path.normalize(fs.path.join(toolsPath, '..', '..'));
|
||||
final String toolsPath = globals.fs.path.joinAll(parts.sublist(0, toolsIndex + 1));
|
||||
return globals.fs.path.normalize(globals.fs.path.join(toolsPath, '..', '..'));
|
||||
}
|
||||
|
||||
CommandRunner<void> createTestCommandRunner([ FlutterCommand command ]) {
|
||||
@@ -87,7 +88,7 @@ void updateFileModificationTime(
|
||||
int seconds,
|
||||
) {
|
||||
final DateTime modificationTime = baseTime.add(Duration(seconds: seconds));
|
||||
fs.file(path).setLastModifiedSync(modificationTime);
|
||||
globals.fs.file(path).setLastModifiedSync(modificationTime);
|
||||
}
|
||||
|
||||
/// Matcher for functions that throw [ToolExit].
|
||||
@@ -120,12 +121,12 @@ final Matcher isProcessExit = isInstanceOf<ProcessExit>();
|
||||
/// Returns the path to the flutter project.
|
||||
Future<String> createProject(Directory temp, { List<String> arguments }) async {
|
||||
arguments ??= <String>['--no-pub'];
|
||||
final String projectPath = fs.path.join(temp.path, 'flutter_project');
|
||||
final String projectPath = globals.fs.path.join(temp.path, 'flutter_project');
|
||||
final CreateCommand command = CreateCommand();
|
||||
final CommandRunner<void> runner = createTestCommandRunner(command);
|
||||
await runner.run(<String>['create', ...arguments, projectPath]);
|
||||
// Created `.packages` since it's not created when the flag `--no-pub` is passed.
|
||||
fs.file(fs.path.join(projectPath, '.packages')).createSync();
|
||||
globals.fs.file(globals.fs.path.join(projectPath, '.packages')).createSync();
|
||||
return projectPath;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@ import 'package:flutter_tools/src/project.dart';
|
||||
import 'package:flutter_tools/src/reporting/github_template.dart';
|
||||
import 'package:flutter_tools/src/reporting/reporting.dart';
|
||||
import 'package:flutter_tools/src/version.dart';
|
||||
import 'package:flutter_tools/src/globals.dart' as globals;
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
|
||||
@@ -73,16 +74,16 @@ void testUsingContext(
|
||||
}
|
||||
});
|
||||
Config buildConfig(FileSystem fs) {
|
||||
configDir ??= fs.systemTempDirectory.createTempSync('flutter_config_dir_test.');
|
||||
final File settingsFile = fs.file(
|
||||
fs.path.join(configDir.path, '.flutter_settings')
|
||||
configDir ??= globals.fs.systemTempDirectory.createTempSync('flutter_config_dir_test.');
|
||||
final File settingsFile = globals.fs.file(
|
||||
globals.fs.path.join(configDir.path, '.flutter_settings')
|
||||
);
|
||||
return Config(settingsFile);
|
||||
}
|
||||
PersistentToolState buildPersistentToolState(FileSystem fs) {
|
||||
configDir ??= fs.systemTempDirectory.createTempSync('flutter_config_dir_test.');
|
||||
final File toolStateFile = fs.file(
|
||||
fs.path.join(configDir.path, '.flutter_tool_state'));
|
||||
configDir ??= globals.fs.systemTempDirectory.createTempSync('flutter_config_dir_test.');
|
||||
final File toolStateFile = globals.fs.file(
|
||||
globals.fs.path.join(configDir.path, '.flutter_tool_state'));
|
||||
return PersistentToolState(toolStateFile);
|
||||
}
|
||||
|
||||
@@ -91,7 +92,7 @@ void testUsingContext(
|
||||
return context.run<dynamic>(
|
||||
name: 'mocks',
|
||||
overrides: <Type, Generator>{
|
||||
Config: () => buildConfig(fs),
|
||||
Config: () => buildConfig(globals.fs),
|
||||
DeviceManager: () => FakeDeviceManager(),
|
||||
Doctor: () => FakeDoctor(),
|
||||
FlutterVersion: () => MockFlutterVersion(),
|
||||
@@ -104,7 +105,7 @@ void testUsingContext(
|
||||
OutputPreferences: () => OutputPreferences.test(),
|
||||
Logger: () => BufferLogger(),
|
||||
OperatingSystemUtils: () => FakeOperatingSystemUtils(),
|
||||
PersistentToolState: () => buildPersistentToolState(fs),
|
||||
PersistentToolState: () => buildPersistentToolState(globals.fs),
|
||||
SimControl: () => MockSimControl(),
|
||||
Usage: () => FakeUsage(),
|
||||
XcodeProjectInterpreter: () => FakeXcodeProjectInterpreter(),
|
||||
@@ -410,10 +411,10 @@ class LocalFileSystemBlockingSetCurrentDirectory extends LocalFileSystem {
|
||||
|
||||
@override
|
||||
set currentDirectory(dynamic value) {
|
||||
throw 'fs.currentDirectory should not be set on the local file system during '
|
||||
throw 'globals.fs.currentDirectory should not be set on the local file system during '
|
||||
'tests as this can cause race conditions with concurrent tests. '
|
||||
'Consider using a MemoryFileSystem for testing if possible or refactor '
|
||||
'code to not require setting fs.currentDirectory.';
|
||||
'code to not require setting globals.fs.currentDirectory.';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,13 +6,14 @@ import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io' as io show IOSink, ProcessSignal, Stdout, StdoutException;
|
||||
|
||||
import 'package:platform/platform.dart';
|
||||
|
||||
import 'package:flutter_tools/src/android/android_device.dart';
|
||||
import 'package:flutter_tools/src/android/android_sdk.dart' show AndroidSdk;
|
||||
import 'package:flutter_tools/src/application_package.dart';
|
||||
import 'package:flutter_tools/src/base/context.dart';
|
||||
import 'package:flutter_tools/src/base/file_system.dart' hide IOSink;
|
||||
import 'package:flutter_tools/src/base/io.dart';
|
||||
import 'package:flutter_tools/src/base/platform.dart';
|
||||
import 'package:flutter_tools/src/build_info.dart';
|
||||
import 'package:flutter_tools/src/compile.dart';
|
||||
import 'package:flutter_tools/src/devfs.dart';
|
||||
@@ -21,6 +22,7 @@ import 'package:flutter_tools/src/ios/devices.dart';
|
||||
import 'package:flutter_tools/src/ios/simulators.dart';
|
||||
import 'package:flutter_tools/src/project.dart';
|
||||
import 'package:flutter_tools/src/runner/flutter_command.dart';
|
||||
import 'package:flutter_tools/src/globals.dart' as globals;
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:process/process.dart';
|
||||
|
||||
@@ -36,7 +38,7 @@ class MockApplicationPackageStore extends ApplicationPackageStore {
|
||||
MockApplicationPackageStore() : super(
|
||||
android: AndroidApk(
|
||||
id: 'io.flutter.android.mock',
|
||||
file: fs.file('/mock/path/to/android/SkyShell.apk'),
|
||||
file: globals.fs.file('/mock/path/to/android/SkyShell.apk'),
|
||||
versionCode: 1,
|
||||
launchActivity: 'io.flutter.android.mock.MockActivity',
|
||||
),
|
||||
@@ -67,9 +69,9 @@ class MockAndroidSdk extends Mock implements AndroidSdk {
|
||||
bool withPlatformTools = true,
|
||||
bool withBuildTools = true,
|
||||
}) {
|
||||
final Directory dir = fs.systemTempDirectory.createTempSync('flutter_mock_android_sdk.');
|
||||
final String exe = platform.isWindows ? '.exe' : '';
|
||||
final String bat = platform.isWindows ? '.bat' : '';
|
||||
final Directory dir = globals.fs.systemTempDirectory.createTempSync('flutter_mock_android_sdk.');
|
||||
final String exe = globals.platform.isWindows ? '.exe' : '';
|
||||
final String bat = globals.platform.isWindows ? '.bat' : '';
|
||||
|
||||
_createDir(dir, 'licenses');
|
||||
|
||||
@@ -98,7 +100,7 @@ class MockAndroidSdk extends Mock implements AndroidSdk {
|
||||
}
|
||||
|
||||
if (withNdkDir != null) {
|
||||
final String ndkToolchainBin = fs.path.join(
|
||||
final String ndkToolchainBin = globals.fs.path.join(
|
||||
'ndk-bundle',
|
||||
'toolchains',
|
||||
'arm-linux-androideabi-4.9',
|
||||
@@ -106,24 +108,24 @@ class MockAndroidSdk extends Mock implements AndroidSdk {
|
||||
withNdkDir,
|
||||
'bin',
|
||||
);
|
||||
final String ndkCompiler = fs.path.join(
|
||||
final String ndkCompiler = globals.fs.path.join(
|
||||
ndkToolchainBin,
|
||||
'arm-linux-androideabi-gcc',
|
||||
);
|
||||
final String ndkLinker = fs.path.join(
|
||||
final String ndkLinker = globals.fs.path.join(
|
||||
ndkToolchainBin,
|
||||
'arm-linux-androideabi-ld',
|
||||
);
|
||||
_createSdkFile(dir, ndkCompiler);
|
||||
_createSdkFile(dir, ndkLinker);
|
||||
_createSdkFile(dir, fs.path.join('ndk-bundle', 'source.properties'), contents: '''
|
||||
_createSdkFile(dir, globals.fs.path.join('ndk-bundle', 'source.properties'), contents: '''
|
||||
Pkg.Desc = Android NDK[]
|
||||
Pkg.Revision = $ndkVersion.1.5063045
|
||||
|
||||
''');
|
||||
}
|
||||
if (withNdkSysroot) {
|
||||
final String armPlatform = fs.path.join(
|
||||
final String armPlatform = globals.fs.path.join(
|
||||
'ndk-bundle',
|
||||
'platforms',
|
||||
'android-9',
|
||||
@@ -144,7 +146,7 @@ Pkg.Revision = $ndkVersion.1.5063045
|
||||
}
|
||||
|
||||
static void _createDir(Directory dir, String path) {
|
||||
final Directory directory = fs.directory(fs.path.join(dir.path, path));
|
||||
final Directory directory = globals.fs.directory(globals.fs.path.join(dir.path, path));
|
||||
directory.createSync(recursive: true);
|
||||
}
|
||||
|
||||
@@ -675,8 +677,8 @@ class MockResidentCompiler extends BasicMock implements ResidentCompiler {
|
||||
}
|
||||
@override
|
||||
Future<CompilerOutput> recompile(String mainPath, List<Uri> invalidatedFiles, { String outputPath, String packagesFilePath }) async {
|
||||
fs.file(outputPath).createSync(recursive: true);
|
||||
fs.file(outputPath).writeAsStringSync('compiled_kernel_output');
|
||||
globals.fs.file(outputPath).createSync(recursive: true);
|
||||
globals.fs.file(outputPath).writeAsStringSync('compiled_kernel_output');
|
||||
return CompilerOutput(outputPath, 0, <Uri>[]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import 'package:flutter_tools/src/base/file_system.dart';
|
||||
import 'package:flutter_tools/src/base/io.dart';
|
||||
import 'package:flutter_tools/src/base/logger.dart';
|
||||
import 'package:flutter_tools/src/base/os.dart';
|
||||
import 'package:flutter_tools/src/base/platform.dart';
|
||||
|
||||
import 'package:flutter_tools/src/base/signals.dart';
|
||||
import 'package:flutter_tools/src/base/terminal.dart';
|
||||
import 'package:flutter_tools/src/cache.dart';
|
||||
@@ -21,6 +21,7 @@ import 'package:flutter_tools/src/dart/pub.dart';
|
||||
import 'package:flutter_tools/src/features.dart';
|
||||
import 'package:flutter_tools/src/reporting/reporting.dart';
|
||||
import 'package:flutter_tools/src/version.dart';
|
||||
import 'package:flutter_tools/src/globals.dart' as globals;
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:process/process.dart';
|
||||
|
||||
@@ -35,7 +36,7 @@ export 'package:flutter_tools/src/base/context.dart' show Generator;
|
||||
// this provider. For example, [BufferLogger], [MemoryFileSystem].
|
||||
final Map<Type, Generator> _testbedDefaults = <Type, Generator>{
|
||||
// Keeps tests fast by avoiding the actual file system.
|
||||
FileSystem: () => MemoryFileSystem(style: platform.isWindows ? FileSystemStyle.windows : FileSystemStyle.posix),
|
||||
FileSystem: () => MemoryFileSystem(style: globals.platform.isWindows ? FileSystemStyle.windows : FileSystemStyle.posix),
|
||||
ProcessManager: () => FakeProcessManager.any(),
|
||||
Logger: () => BufferLogger(), // Allows reading logs and prevents stdout.
|
||||
OperatingSystemUtils: () => FakeOperatingSystemUtils(),
|
||||
@@ -61,14 +62,14 @@ final Map<Type, Generator> _testbedDefaults = <Type, Generator>{
|
||||
///
|
||||
/// setUp(() {
|
||||
/// testbed = Testbed(setUp: () {
|
||||
/// fs.file('foo').createSync()
|
||||
/// globals.fs.file('foo').createSync()
|
||||
/// });
|
||||
/// })
|
||||
///
|
||||
/// test('Can delete a file', () => testbed.run(() {
|
||||
/// expect(fs.file('foo').existsSync(), true);
|
||||
/// fs.file('foo').deleteSync();
|
||||
/// expect(fs.file('foo').existsSync(), false);
|
||||
/// expect(globals.fs.file('foo').existsSync(), true);
|
||||
/// globals.fs.file('foo').deleteSync();
|
||||
/// expect(globals.fs.file('foo').existsSync(), false);
|
||||
/// }));
|
||||
/// });
|
||||
/// }
|
||||
@@ -843,32 +844,32 @@ class FakeCache implements Cache {
|
||||
|
||||
@override
|
||||
Directory getArtifactDirectory(String name) {
|
||||
return fs.currentDirectory;
|
||||
return globals.fs.currentDirectory;
|
||||
}
|
||||
|
||||
@override
|
||||
Directory getCacheArtifacts() {
|
||||
return fs.currentDirectory;
|
||||
return globals.fs.currentDirectory;
|
||||
}
|
||||
|
||||
@override
|
||||
Directory getCacheDir(String name) {
|
||||
return fs.currentDirectory;
|
||||
return globals.fs.currentDirectory;
|
||||
}
|
||||
|
||||
@override
|
||||
Directory getDownloadDir() {
|
||||
return fs.currentDirectory;
|
||||
return globals.fs.currentDirectory;
|
||||
}
|
||||
|
||||
@override
|
||||
Directory getRoot() {
|
||||
return fs.currentDirectory;
|
||||
return globals.fs.currentDirectory;
|
||||
}
|
||||
|
||||
@override
|
||||
File getLicenseFile() {
|
||||
return fs.currentDirectory.childFile('LICENSE');
|
||||
return globals.fs.currentDirectory.childFile('LICENSE');
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -893,7 +894,7 @@ class FakeCache implements Cache {
|
||||
|
||||
@override
|
||||
Directory getWebSdkDirectory() {
|
||||
return fs.currentDirectory;
|
||||
return globals.fs.currentDirectory;
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
Reference in New Issue
Block a user