Unverified Commit 9e30d51a authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] start fixing command tests (#60172)

parent 77bc1ee5
...@@ -34,7 +34,7 @@ void main() { ...@@ -34,7 +34,7 @@ void main() {
expect(testLogger.statusText, containsIgnoringWhitespace('No devices detected')); expect(testLogger.statusText, containsIgnoringWhitespace('No devices detected'));
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
AndroidSdk: () => null, AndroidSdk: () => null,
DeviceManager: () => DeviceManager(), DeviceManager: () => NoDevicesManager(),
ProcessManager: () => MockProcessManager(), ProcessManager: () => MockProcessManager(),
}); });
...@@ -160,3 +160,12 @@ class _FakeDeviceManager extends DeviceManager { ...@@ -160,3 +160,12 @@ class _FakeDeviceManager extends DeviceManager {
<String>['Cannot connect to device ABC'] <String>['Cannot connect to device ABC']
); );
} }
class NoDevicesManager extends DeviceManager {
@override
Future<List<Device>> getAllConnectedDevices() async => <Device>[];
@override
Future<List<Device>> refreshAllConnectedDevices({Duration timeout}) =>
getAllConnectedDevices();
}
...@@ -43,6 +43,11 @@ final Map<Type, Generator> noColorTerminalOverride = <Type, Generator>{ ...@@ -43,6 +43,11 @@ final Map<Type, Generator> noColorTerminalOverride = <Type, Generator>{
Platform: _kNoColorOutputPlatform, Platform: _kNoColorOutputPlatform,
}; };
final Platform macPlatform = FakePlatform(
operatingSystem: 'macos',
environment: <String, String>{'HOME': '/foo/bar'}
);
void main() { void main() {
MockProcessManager mockProcessManager; MockProcessManager mockProcessManager;
MockFlutterVersion mockFlutterVersion; MockFlutterVersion mockFlutterVersion;
...@@ -93,11 +98,14 @@ void main() { ...@@ -93,11 +98,14 @@ void main() {
expect(validator.plistFile, '/path/to/app/Contents/Info.plist'); expect(validator.plistFile, '/path/to/app/Contents/Info.plist');
expect(validator.pluginsPath, pluginsDirectory.path); expect(validator.pluginsPath, pluginsDirectory.path);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
Platform: () => FakePlatform() Platform: () => macPlatform,
..environment = <String, String>{'HOME': '/foo/bar'},
PlistParser: () => mockPlistParser, PlistParser: () => mockPlistParser,
FileSystem: () => fileSystem, FileSystem: () => fileSystem,
ProcessManager: () => mockProcessManager, ProcessManager: () => mockProcessManager,
FileSystemUtils: () => FileSystemUtils(
fileSystem: fileSystem,
platform: macPlatform,
)
}); });
testUsingContext('legacy intellij plugins path checking on mac', () async { testUsingContext('legacy intellij plugins path checking on mac', () async {
...@@ -106,9 +114,14 @@ void main() { ...@@ -106,9 +114,14 @@ void main() {
final IntelliJValidatorOnMac validator = IntelliJValidatorOnMac('Test', 'TestID', '/foo'); final IntelliJValidatorOnMac validator = IntelliJValidatorOnMac('Test', 'TestID', '/foo');
expect(validator.pluginsPath, '/foo/bar/Library/Application Support/TestID2020.10'); expect(validator.pluginsPath, '/foo/bar/Library/Application Support/TestID2020.10');
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
Platform: () => FakePlatform() Platform: () => macPlatform,
..environment = <String, String>{'HOME': '/foo/bar'},
PlistParser: () => mockPlistParser, PlistParser: () => mockPlistParser,
FileSystem: () => fileSystem,
FileSystemUtils: () => FileSystemUtils(
fileSystem: fileSystem,
platform: macPlatform,
),
ProcessManager: () => FakeProcessManager.any(),
}); });
testUsingContext('intellij plugins path checking on mac with override', () async { testUsingContext('intellij plugins path checking on mac with override', () async {
...@@ -118,6 +131,13 @@ void main() { ...@@ -118,6 +131,13 @@ void main() {
expect(validator.pluginsPath, '/path/to/JetBrainsToolboxApp.plugins'); expect(validator.pluginsPath, '/path/to/JetBrainsToolboxApp.plugins');
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
PlistParser: () => mockPlistParser, PlistParser: () => mockPlistParser,
Platform: () => macPlatform,
FileSystem: () => fileSystem,
FileSystemUtils: () => FileSystemUtils(
fileSystem: fileSystem,
platform: macPlatform,
),
ProcessManager: () => FakeProcessManager.any(),
}); });
testUsingContext('vs code validator when both installed', () async { testUsingContext('vs code validator when both installed', () async {
......
...@@ -3,10 +3,8 @@ ...@@ -3,10 +3,8 @@
// found in the LICENSE file. // found in the LICENSE file.
import 'dart:async'; import 'dart:async';
import 'package:file/file.dart'; import 'package:file/file.dart';
import 'package:file/memory.dart'; import 'package:file/memory.dart';
import 'package:args/command_runner.dart';
import 'package:flutter_tools/src/application_package.dart'; import 'package:flutter_tools/src/application_package.dart';
import 'package:flutter_tools/src/artifacts.dart'; import 'package:flutter_tools/src/artifacts.dart';
import 'package:flutter_tools/src/base/common.dart'; import 'package:flutter_tools/src/base/common.dart';
...@@ -15,19 +13,13 @@ import 'package:flutter_tools/src/base/file_system.dart'; ...@@ -15,19 +13,13 @@ import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/io.dart'; import 'package:flutter_tools/src/base/io.dart';
import 'package:flutter_tools/src/base/logger.dart'; import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/user_messages.dart'; import 'package:flutter_tools/src/base/user_messages.dart';
import 'package:flutter_tools/src/base/net.dart';
import 'package:flutter_tools/src/build_info.dart'; import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/cache.dart'; import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/run.dart'; import 'package:flutter_tools/src/commands/run.dart';
import 'package:flutter_tools/src/device.dart'; import 'package:flutter_tools/src/device.dart';
import 'package:flutter_tools/src/features.dart';
import 'package:flutter_tools/src/globals.dart' as globals; import 'package:flutter_tools/src/globals.dart' as globals;
import 'package:flutter_tools/src/project.dart';
import 'package:flutter_tools/src/reporting/reporting.dart'; import 'package:flutter_tools/src/reporting/reporting.dart';
import 'package:flutter_tools/src/resident_runner.dart';
import 'package:flutter_tools/src/runner/flutter_command.dart'; import 'package:flutter_tools/src/runner/flutter_command.dart';
import 'package:flutter_tools/src/version.dart';
import 'package:flutter_tools/src/web/web_runner.dart';
import 'package:mockito/mockito.dart'; import 'package:mockito/mockito.dart';
import '../../src/common.dart'; import '../../src/common.dart';
...@@ -40,15 +32,16 @@ void main() { ...@@ -40,15 +32,16 @@ void main() {
group('run', () { group('run', () {
MockApplicationPackageFactory mockApplicationPackageFactory; MockApplicationPackageFactory mockApplicationPackageFactory;
MockDeviceManager mockDeviceManager; MockDeviceManager mockDeviceManager;
MockFlutterVersion mockStableFlutterVersion; FileSystem fileSystem;
MockFlutterVersion mockUnstableFlutterVersion;
setUpAll(() { setUpAll(() {
Cache.disableLocking(); Cache.disableLocking();
});
setUp(() {
mockApplicationPackageFactory = MockApplicationPackageFactory(); mockApplicationPackageFactory = MockApplicationPackageFactory();
mockDeviceManager = MockDeviceManager(); mockDeviceManager = MockDeviceManager();
mockStableFlutterVersion = MockFlutterVersion(isStable: true); fileSystem = MemoryFileSystem.test();
mockUnstableFlutterVersion = MockFlutterVersion(isStable: false);
}); });
testUsingContext('fails when target not found', () async { testUsingContext('fails when target not found', () async {
...@@ -60,12 +53,16 @@ void main() { ...@@ -60,12 +53,16 @@ void main() {
} on ToolExit catch (e) { } on ToolExit catch (e) {
expect(e.exitCode ?? 1, 1); expect(e.exitCode ?? 1, 1);
} }
}, overrides: <Type, Generator>{
FileSystem: () => fileSystem,
ProcessManager: () => FakeProcessManager.any(),
Logger: () => BufferLogger.test(),
}); });
testUsingContext('does not support "--use-application-binary" and "--fast-start"', () async { testUsingContext('does not support "--use-application-binary" and "--fast-start"', () async {
globals.fs.file(globals.fs.path.join('lib', 'main.dart')).createSync(recursive: true); fileSystem.file('lib/main.dart').createSync(recursive: true);
globals.fs.file('pubspec.yaml').createSync(); fileSystem.file('pubspec.yaml').createSync();
globals.fs.file('.packages').createSync(); fileSystem.file('.packages').createSync();
final RunCommand command = RunCommand(); final RunCommand command = RunCommand();
applyMocksToCommand(command); applyMocksToCommand(command);
...@@ -82,17 +79,18 @@ void main() { ...@@ -82,17 +79,18 @@ void main() {
expect(e.toString(), isNot(contains('--fast-start is not supported with --use-application-binary'))); expect(e.toString(), isNot(contains('--fast-start is not supported with --use-application-binary')));
} }
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FileSystem: () => MemoryFileSystem(), FileSystem: () => fileSystem,
ProcessManager: () => FakeProcessManager.any(), ProcessManager: () => FakeProcessManager.any(),
Logger: () => BufferLogger.test(),
}); });
testUsingContext('Walks upward looking for a pubspec.yaml and succeeds if found', () async { testUsingContext('Walks upward looking for a pubspec.yaml and succeeds if found', () async {
globals.fs.file('pubspec.yaml').createSync(); fileSystem.file('pubspec.yaml').createSync();
globals.fs.file('.packages') fileSystem.file('.packages')
..createSync() .writeAsStringSync('\n');
..writeAsStringSync('Not a valid package'); fileSystem.file('lib/main.dart')
.createSync(recursive: true);
globals.fs.currentDirectory = globals.fs.directory(globals.fs.path.join('a', 'b', 'c')) fileSystem.currentDirectory = fileSystem.directory('a/b/c')
..createSync(recursive: true); ..createSync(recursive: true);
final RunCommand command = RunCommand(); final RunCommand command = RunCommand();
...@@ -100,7 +98,6 @@ void main() { ...@@ -100,7 +98,6 @@ void main() {
try { try {
await createTestCommandRunner(command).run(<String>[ await createTestCommandRunner(command).run(<String>[
'run', 'run',
'--fast-start',
'--no-pub', '--no-pub',
]); ]);
fail('Expect exception'); fail('Expect exception');
...@@ -113,20 +110,22 @@ void main() { ...@@ -113,20 +110,22 @@ void main() {
containsIgnoringWhitespace('Changing current working directory to:'), containsIgnoringWhitespace('Changing current working directory to:'),
); );
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FileSystem: () => MemoryFileSystem(), FileSystem: () => fileSystem,
ProcessManager: () => FakeProcessManager.any(), ProcessManager: () => FakeProcessManager.any(),
Logger: () => BufferLogger.test(),
}); });
testUsingContext('Walks upward looking for a pubspec.yaml and exits if missing', () async { testUsingContext('Walks upward looking for a pubspec.yaml and exits if missing', () async {
globals.fs.currentDirectory = globals.fs.directory(globals.fs.path.join('a', 'b', 'c')) fileSystem.currentDirectory = fileSystem.directory('a/b/c')
..createSync(recursive: true); ..createSync(recursive: true);
fileSystem.file('lib/main.dart')
.createSync(recursive: true);
final RunCommand command = RunCommand(); final RunCommand command = RunCommand();
applyMocksToCommand(command); applyMocksToCommand(command);
try { try {
await createTestCommandRunner(command).run(<String>[ await createTestCommandRunner(command).run(<String>[
'run', 'run',
'--fast-start',
'--no-pub', '--no-pub',
]); ]);
fail('Expect exception'); fail('Expect exception');
...@@ -135,11 +134,11 @@ void main() { ...@@ -135,11 +134,11 @@ void main() {
expect(e.toString(), contains('No pubspec.yaml file found')); expect(e.toString(), contains('No pubspec.yaml file found'));
} }
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FileSystem: () => MemoryFileSystem(), FileSystem: () => fileSystem,
ProcessManager: () => FakeProcessManager.any(), ProcessManager: () => FakeProcessManager.any(),
Logger: () => BufferLogger.test(),
}); });
group('run app', () { group('run app', () {
MemoryFileSystem fs; MemoryFileSystem fs;
MockArtifacts mockArtifacts; MockArtifacts mockArtifacts;
...@@ -148,7 +147,7 @@ void main() { ...@@ -148,7 +147,7 @@ void main() {
MockUsage mockUsage; MockUsage mockUsage;
Directory tempDir; Directory tempDir;
setUpAll(() { setUp(() {
mockArtifacts = MockArtifacts(); mockArtifacts = MockArtifacts();
mockCache = MockCache(); mockCache = MockCache();
mockUsage = MockUsage(); mockUsage = MockUsage();
...@@ -418,18 +417,10 @@ void main() { ...@@ -418,18 +417,10 @@ void main() {
}); });
group('dart-flags option', () { group('dart-flags option', () {
setUpAll(() {
final FakeDevice fakeDevice = FakeDevice();
when(mockDeviceManager.getDevices()).thenAnswer((Invocation invocation) {
return Future<List<Device>>.value(<Device>[fakeDevice]);
});
when(mockDeviceManager.findTargetDevices(any)).thenAnswer(
(Invocation invocation) => Future<List<Device>>.value(<Device>[fakeDevice])
);
});
RunCommand command; RunCommand command;
List<String> args; List<String> args;
MockDeviceManager mockDeviceManager;
setUp(() { setUp(() {
command = TestRunCommand(); command = TestRunCommand();
args = <String> [ args = <String> [
...@@ -438,20 +429,14 @@ void main() { ...@@ -438,20 +429,14 @@ void main() {
'--no-hot', '--no-hot',
'--no-pub', '--no-pub',
]; ];
}); mockDeviceManager = MockDeviceManager();
final FakeDevice fakeDevice = FakeDevice();
testUsingContext('is not available on stable channel', () async { when(mockDeviceManager.getDevices()).thenAnswer((Invocation invocation) {
// Stable branch. return Future<List<Device>>.value(<Device>[fakeDevice]);
try { });
await createTestCommandRunner(command).run(args); when(mockDeviceManager.findTargetDevices(any)).thenAnswer(
fail('Expect exception'); (Invocation invocation) => Future<List<Device>>.value(<Device>[fakeDevice])
// ignore: unused_catch_clause );
} on UsageException catch(e) {
// Not available while on stable branch.
}
}, overrides: <Type, Generator>{
DeviceManager: () => mockDeviceManager,
FlutterVersion: () => mockStableFlutterVersion,
}); });
testUsingContext('is populated in debug mode', () async { testUsingContext('is populated in debug mode', () async {
...@@ -466,7 +451,6 @@ void main() { ...@@ -466,7 +451,6 @@ void main() {
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
ApplicationPackageFactory: () => mockApplicationPackageFactory, ApplicationPackageFactory: () => mockApplicationPackageFactory,
DeviceManager: () => mockDeviceManager, DeviceManager: () => mockDeviceManager,
FlutterVersion: () => mockUnstableFlutterVersion,
}); });
testUsingContext('is populated in profile mode', () async { testUsingContext('is populated in profile mode', () async {
...@@ -483,7 +467,6 @@ void main() { ...@@ -483,7 +467,6 @@ void main() {
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
ApplicationPackageFactory: () => mockApplicationPackageFactory, ApplicationPackageFactory: () => mockApplicationPackageFactory,
DeviceManager: () => mockDeviceManager, DeviceManager: () => mockDeviceManager,
FlutterVersion: () => mockUnstableFlutterVersion,
}); });
testUsingContext('is not populated in release mode', () async { testUsingContext('is not populated in release mode', () async {
...@@ -501,7 +484,6 @@ void main() { ...@@ -501,7 +484,6 @@ void main() {
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
ApplicationPackageFactory: () => mockApplicationPackageFactory, ApplicationPackageFactory: () => mockApplicationPackageFactory,
DeviceManager: () => mockDeviceManager, DeviceManager: () => mockDeviceManager,
FlutterVersion: () => mockUnstableFlutterVersion,
}); });
}); });
...@@ -554,95 +536,6 @@ void main() { ...@@ -554,95 +536,6 @@ void main() {
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
DeviceManager: () => mockDeviceManager, DeviceManager: () => mockDeviceManager,
}); });
group('--dart-define option', () {
MemoryFileSystem fs;
MockProcessManager mockProcessManager;
MockWebRunnerFactory mockWebRunnerFactory;
setUpAll(() {
final FakeDevice fakeDevice = FakeDevice().._targetPlatform = TargetPlatform.web_javascript;
when(mockDeviceManager.getDevices()).thenAnswer(
(Invocation invocation) => Future<List<Device>>.value(<Device>[fakeDevice])
);
when(mockDeviceManager.findTargetDevices(any)).thenAnswer(
(Invocation invocation) => Future<List<Device>>.value(<Device>[fakeDevice])
);
});
RunCommand command;
List<String> args;
setUp(() {
command = TestRunCommand();
args = <String> [
'run',
'--dart-define=FOO=bar',
'--no-hot',
'--no-pub',
];
applyMocksToCommand(command);
fs = MemoryFileSystem();
mockProcessManager = MockProcessManager();
mockWebRunnerFactory = MockWebRunnerFactory();
});
testUsingContext('populates the environment', () async {
final Directory tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_run_test.');
globals.fs.currentDirectory = tempDir;
final Directory libDir = tempDir.childDirectory('lib');
libDir.createSync();
final File mainFile = libDir.childFile('main.dart');
mainFile.writeAsStringSync('void main() {}');
final Directory webDir = tempDir.childDirectory('web');
webDir.createSync();
final File indexFile = libDir.childFile('index.html');
indexFile.writeAsStringSync('<h1>Hello</h1>');
await createTestCommandRunner(command).run(args);
expect(mockWebRunnerFactory._dartDefines, <String>['FOO=bar']);
}, overrides: <Type, Generator>{
FeatureFlags: () => TestFeatureFlags(
isWebEnabled: true,
),
FileSystem: () => fs,
ProcessManager: () => mockProcessManager,
DeviceManager: () => mockDeviceManager,
FlutterVersion: () => mockStableFlutterVersion,
WebRunnerFactory: () => mockWebRunnerFactory,
});
testUsingContext('populates dartDefines in --machine mode', () async {
final Directory tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_run_test.');
globals.fs.currentDirectory = tempDir;
final Directory libDir = tempDir.childDirectory('lib');
libDir.createSync();
final File mainFile = libDir.childFile('main.dart');
mainFile.writeAsStringSync('void main() {}');
final Directory webDir = tempDir.childDirectory('web');
webDir.createSync();
final File indexFile = libDir.childFile('index.html');
indexFile.writeAsStringSync('<h1>Hello</h1>');
when(mockDeviceManager.deviceDiscoverers).thenReturn(<DeviceDiscovery>[]);
args.add('--machine');
await createTestCommandRunner(command).run(args);
expect(mockWebRunnerFactory._dartDefines, <String>['FOO=bar']);
}, overrides: <Type, Generator>{
FeatureFlags: () => TestFeatureFlags(
isWebEnabled: true,
),
FileSystem: () => fs,
ProcessManager: () => mockProcessManager,
DeviceManager: () => mockDeviceManager,
FlutterVersion: () => mockStableFlutterVersion,
WebRunnerFactory: () => mockWebRunnerFactory,
});
});
}); });
} }
...@@ -674,7 +567,7 @@ class FakeDevice extends Fake implements Device { ...@@ -674,7 +567,7 @@ class FakeDevice extends Fake implements Device {
static const int kSuccess = 1; static const int kSuccess = 1;
static const int kFailure = -1; static const int kFailure = -1;
TargetPlatform _targetPlatform = TargetPlatform.ios; final TargetPlatform _targetPlatform = TargetPlatform.ios;
final bool _isLocalEmulator; final bool _isLocalEmulator;
@override @override
...@@ -744,38 +637,3 @@ class FakeDevice extends Fake implements Device { ...@@ -744,38 +637,3 @@ class FakeDevice extends Fake implements Device {
return null; return null;
} }
} }
class MockWebRunnerFactory extends Mock implements WebRunnerFactory {
List<String> _dartDefines;
@override
ResidentRunner createWebRunner(
FlutterDevice device, {
String target,
bool stayResident,
FlutterProject flutterProject,
bool ipv6,
DebuggingOptions debuggingOptions,
UrlTunneller urlTunneller,
}) {
_dartDefines = debuggingOptions.buildInfo.dartDefines;
return MockWebRunner();
}
}
class MockWebRunner extends Mock implements ResidentRunner {
@override
bool get debuggingEnabled => false;
@override
Future<int> run({
Completer<DebugConnectionInfo> connectionInfoCompleter,
Completer<void> appStartedCompleter,
String route,
}) async {
return 0;
}
@override
Future<int> waitForAppToFinish() async => 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment