Unverified Commit 5d7b4019 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] remove mocks from downgrade and devices test (#82579)

parent aa9b2d91
...@@ -35,7 +35,7 @@ class DowngradeCommand extends FlutterCommand { ...@@ -35,7 +35,7 @@ class DowngradeCommand extends FlutterCommand {
Logger logger, Logger logger,
ProcessManager processManager, ProcessManager processManager,
FlutterVersion flutterVersion, FlutterVersion flutterVersion,
AnsiTerminal terminal, Terminal terminal,
Stdio stdio, Stdio stdio,
FileSystem fileSystem, FileSystem fileSystem,
}) : _terminal = terminal, }) : _terminal = terminal,
...@@ -61,7 +61,7 @@ class DowngradeCommand extends FlutterCommand { ...@@ -61,7 +61,7 @@ class DowngradeCommand extends FlutterCommand {
); );
} }
AnsiTerminal _terminal; Terminal _terminal;
FlutterVersion _flutterVersion; FlutterVersion _flutterVersion;
PersistentToolState _persistentToolState; PersistentToolState _persistentToolState;
ProcessUtils _processUtils; ProcessUtils _processUtils;
......
...@@ -12,7 +12,7 @@ import 'package:flutter_tools/src/base/terminal.dart'; ...@@ -12,7 +12,7 @@ import 'package:flutter_tools/src/base/terminal.dart';
import 'package:flutter_tools/src/cache.dart'; import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/downgrade.dart'; import 'package:flutter_tools/src/commands/downgrade.dart';
import 'package:flutter_tools/src/persistent_tool_state.dart'; import 'package:flutter_tools/src/persistent_tool_state.dart';
import 'package:mockito/mockito.dart'; import 'package:test/fake.dart';
import '../../src/common.dart'; import '../../src/common.dart';
import '../../src/context.dart'; import '../../src/context.dart';
...@@ -22,9 +22,9 @@ import '../../src/test_flutter_command_runner.dart'; ...@@ -22,9 +22,9 @@ import '../../src/test_flutter_command_runner.dart';
void main() { void main() {
FileSystem fileSystem; FileSystem fileSystem;
BufferLogger bufferLogger; BufferLogger bufferLogger;
AnsiTerminal terminal; FakeTerminal terminal;
ProcessManager processManager; ProcessManager processManager;
MockStdio mockStdio; FakeStdio stdio;
setUpAll(() { setUpAll(() {
Cache.disableLocking(); Cache.disableLocking();
...@@ -35,9 +35,9 @@ void main() { ...@@ -35,9 +35,9 @@ void main() {
}); });
setUp(() { setUp(() {
mockStdio = MockStdio(); stdio = FakeStdio();
processManager = FakeProcessManager.any(); processManager = FakeProcessManager.any();
terminal = MockTerminal(); terminal = FakeTerminal();
fileSystem = MemoryFileSystem.test(); fileSystem = MemoryFileSystem.test();
bufferLogger = BufferLogger.test(terminal: terminal); bufferLogger = BufferLogger.test(terminal: terminal);
}); });
...@@ -50,7 +50,7 @@ void main() { ...@@ -50,7 +50,7 @@ void main() {
persistentToolState: PersistentToolState.test(directory: fileSystem.currentDirectory, logger: bufferLogger), persistentToolState: PersistentToolState.test(directory: fileSystem.currentDirectory, logger: bufferLogger),
processManager: processManager, processManager: processManager,
terminal: terminal, terminal: terminal,
stdio: mockStdio, stdio: stdio,
flutterVersion: fakeFlutterVersion, flutterVersion: fakeFlutterVersion,
logger: bufferLogger, logger: bufferLogger,
); );
...@@ -75,7 +75,7 @@ void main() { ...@@ -75,7 +75,7 @@ void main() {
) )
]), ]),
terminal: terminal, terminal: terminal,
stdio: mockStdio, stdio: stdio,
flutterVersion: fakeFlutterVersion, flutterVersion: fakeFlutterVersion,
logger: bufferLogger, logger: bufferLogger,
); );
...@@ -103,7 +103,7 @@ void main() { ...@@ -103,7 +103,7 @@ void main() {
) )
]), ]),
terminal: terminal, terminal: terminal,
stdio: mockStdio, stdio: stdio,
flutterVersion: fakeFlutterVersion, flutterVersion: fakeFlutterVersion,
logger: bufferLogger, logger: bufferLogger,
); );
...@@ -114,67 +114,49 @@ void main() { ...@@ -114,67 +114,49 @@ void main() {
testUsingContext('Downgrade prompts for user input when terminal is attached - y', () async { testUsingContext('Downgrade prompts for user input when terminal is attached - y', () async {
final FakeFlutterVersion fakeFlutterVersion = FakeFlutterVersion(channel: 'master'); final FakeFlutterVersion fakeFlutterVersion = FakeFlutterVersion(channel: 'master');
when(mockStdio.hasTerminal).thenReturn(true); stdio.hasTerminal = true;
fileSystem.currentDirectory.childFile('.flutter_tool_state') fileSystem.currentDirectory.childFile('.flutter_tool_state')
.writeAsStringSync('{"last-active-master-version":"g6b00b5e88"}'); .writeAsStringSync('{"last-active-master-version":"g6b00b5e88"}');
final DowngradeCommand command = DowngradeCommand( final DowngradeCommand command = DowngradeCommand(
persistentToolState: PersistentToolState.test(directory: fileSystem.currentDirectory, logger: bufferLogger), persistentToolState: PersistentToolState.test(directory: fileSystem.currentDirectory, logger: bufferLogger),
processManager: processManager, processManager: processManager,
terminal: terminal, terminal: terminal,
stdio: mockStdio, stdio: stdio,
flutterVersion: fakeFlutterVersion, flutterVersion: fakeFlutterVersion,
logger: bufferLogger, logger: bufferLogger,
); );
when(terminal.promptForCharInput( terminal.addPrompt(const <String>['y', 'n'], 'y');
const <String>['y', 'n'],
prompt: anyNamed('prompt'),
logger: anyNamed('logger'),
)).thenAnswer((Invocation invocation) async => 'y');
await createTestCommandRunner(command).run(const <String>['downgrade']); await createTestCommandRunner(command).run(const <String>['downgrade']);
verify(terminal.promptForCharInput(
const <String>['y', 'n'],
prompt: anyNamed('prompt'),
logger: anyNamed('logger'),
)).called(1);
expect(bufferLogger.statusText, contains('Success')); expect(bufferLogger.statusText, contains('Success'));
}); });
testUsingContext('Downgrade prompts for user input when terminal is attached - n', () async { testUsingContext('Downgrade prompts for user input when terminal is attached - n', () async {
final FakeFlutterVersion fakeFlutterVersion = FakeFlutterVersion(channel: 'master'); final FakeFlutterVersion fakeFlutterVersion = FakeFlutterVersion(channel: 'master');
when(mockStdio.hasTerminal).thenReturn(true); stdio.hasTerminal = true;
fileSystem.currentDirectory.childFile('.flutter_tool_state') fileSystem.currentDirectory.childFile('.flutter_tool_state')
.writeAsStringSync('{"last-active-master-version":"g6b00b5e88"}'); .writeAsStringSync('{"last-active-master-version":"g6b00b5e88"}');
final DowngradeCommand command = DowngradeCommand( final DowngradeCommand command = DowngradeCommand(
persistentToolState: PersistentToolState.test(directory: fileSystem.currentDirectory, logger: bufferLogger), persistentToolState: PersistentToolState.test(directory: fileSystem.currentDirectory, logger: bufferLogger),
processManager: processManager, processManager: processManager,
terminal: terminal, terminal: terminal,
stdio: mockStdio, stdio: stdio,
flutterVersion: fakeFlutterVersion, flutterVersion: fakeFlutterVersion,
logger: bufferLogger, logger: bufferLogger,
); );
when(terminal.promptForCharInput( terminal.addPrompt(const <String>['y', 'n'], 'n');
const <String>['y', 'n'],
prompt: anyNamed('prompt'),
logger: anyNamed('logger'),
)).thenAnswer((Invocation invocation) async => 'n');
await createTestCommandRunner(command).run(const <String>['downgrade']); await createTestCommandRunner(command).run(const <String>['downgrade']);
verify(terminal.promptForCharInput(
const <String>['y', 'n'],
prompt: anyNamed('prompt'),
logger: anyNamed('logger'),
)).called(1);
expect(bufferLogger.statusText, isNot(contains('Success'))); expect(bufferLogger.statusText, isNot(contains('Success')));
}); });
testUsingContext('Downgrade does not prompt when there is no terminal', () async { testUsingContext('Downgrade does not prompt when there is no terminal', () async {
final FakeFlutterVersion fakeFlutterVersion = FakeFlutterVersion(channel: 'master'); final FakeFlutterVersion fakeFlutterVersion = FakeFlutterVersion(channel: 'master');
when(mockStdio.hasTerminal).thenReturn(false); stdio.hasTerminal = false;
fileSystem.currentDirectory.childFile('.flutter_tool_state') fileSystem.currentDirectory.childFile('.flutter_tool_state')
.writeAsStringSync('{"last-active-master-version":"g6b00b5e88"}'); .writeAsStringSync('{"last-active-master-version":"g6b00b5e88"}');
final DowngradeCommand command = DowngradeCommand( final DowngradeCommand command = DowngradeCommand(
...@@ -184,24 +166,19 @@ void main() { ...@@ -184,24 +166,19 @@ void main() {
), ),
processManager: processManager, processManager: processManager,
terminal: terminal, terminal: terminal,
stdio: mockStdio, stdio: stdio,
flutterVersion: fakeFlutterVersion, flutterVersion: fakeFlutterVersion,
logger: bufferLogger, logger: bufferLogger,
); );
await createTestCommandRunner(command).run(const <String>['downgrade']); await createTestCommandRunner(command).run(const <String>['downgrade']);
verifyNever(terminal.promptForCharInput(
const <String>['y', 'n'],
prompt: anyNamed('prompt'),
logger: anyNamed('logger'),
));
expect(bufferLogger.statusText, contains('Success')); expect(bufferLogger.statusText, contains('Success'));
}); });
testUsingContext('Downgrade performs correct git commands', () async { testUsingContext('Downgrade performs correct git commands', () async {
final FakeFlutterVersion fakeFlutterVersion = FakeFlutterVersion(channel: 'master'); final FakeFlutterVersion fakeFlutterVersion = FakeFlutterVersion(channel: 'master');
when(mockStdio.hasTerminal).thenReturn(false); stdio.hasTerminal = false;
fileSystem.currentDirectory.childFile('.flutter_tool_state') fileSystem.currentDirectory.childFile('.flutter_tool_state')
.writeAsStringSync('{"last-active-master-version":"g6b00b5e88"}'); .writeAsStringSync('{"last-active-master-version":"g6b00b5e88"}');
final DowngradeCommand command = DowngradeCommand( final DowngradeCommand command = DowngradeCommand(
...@@ -228,7 +205,7 @@ void main() { ...@@ -228,7 +205,7 @@ void main() {
), ),
]), ]),
terminal: terminal, terminal: terminal,
stdio: mockStdio, stdio: stdio,
flutterVersion: fakeFlutterVersion, flutterVersion: fakeFlutterVersion,
logger: bufferLogger, logger: bufferLogger,
); );
...@@ -239,5 +216,26 @@ void main() { ...@@ -239,5 +216,26 @@ void main() {
}); });
} }
class MockTerminal extends Mock implements AnsiTerminal {} class FakeTerminal extends Fake implements Terminal {
class MockStdio extends Mock implements Stdio {} @override
bool usesTerminalUi = false;
void addPrompt(List<String> characters, String selected) {
_characters = characters;
_selected = selected;
}
List<String> _characters;
String _selected;
@override
Future<String> promptForCharInput(List<String> acceptedCharacters, {Logger logger, String prompt, int defaultChoiceIndex, bool displayAcceptedCharacters = true}) async {
expect(acceptedCharacters, _characters);
return _selected;
}
}
class FakeStdio extends Fake implements Stdio {
@override
bool hasTerminal = true;
}
...@@ -13,21 +13,27 @@ import 'package:flutter_tools/src/cache.dart'; ...@@ -13,21 +13,27 @@ import 'package:flutter_tools/src/cache.dart';
import 'package:flutter_tools/src/commands/devices.dart'; import 'package:flutter_tools/src/commands/devices.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/features.dart';
import 'package:flutter_tools/src/globals.dart' as globals;
import 'package:flutter_tools/src/web/web_device.dart'; import 'package:flutter_tools/src/web/web_device.dart';
import 'package:mockito/mockito.dart'; import 'package:test/fake.dart';
import '../../src/context.dart'; import '../../src/context.dart';
import '../../src/fakes.dart'; import '../../src/fakes.dart';
import '../../src/test_flutter_command_runner.dart'; import '../../src/test_flutter_command_runner.dart';
void main() { void main() {
FakeDeviceManager deviceManager;
BufferLogger logger;
setUpAll(() { setUpAll(() {
Cache.disableLocking(); Cache.disableLocking();
}); });
setUp(() {
deviceManager = FakeDeviceManager();
logger = BufferLogger.test();
});
testUsingContext('devices can display no connected devices with the --machine flag', () async { testUsingContext('devices can display no connected devices with the --machine flag', () async {
final BufferLogger logger = context.get<Logger>() as BufferLogger;
final DevicesCommand command = DevicesCommand(); final DevicesCommand command = DevicesCommand();
final CommandRunner<void> runner = createTestCommandRunner(command); final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['devices', '--machine']); await runner.run(<String>['devices', '--machine']);
...@@ -38,16 +44,13 @@ void main() { ...@@ -38,16 +44,13 @@ void main() {
); );
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FeatureFlags: () => TestFeatureFlags(isWebEnabled: false), FeatureFlags: () => TestFeatureFlags(isWebEnabled: false),
Logger: () => logger,
}); });
testUsingContext('devices can display via the --machine flag', () async { testUsingContext('devices can display via the --machine flag', () async {
when(globals.deviceManager.refreshAllConnectedDevices()).thenAnswer((Invocation invocation) async { deviceManager.devices = <Device>[
return <Device>[ WebServerDevice(logger: logger),
WebServerDevice(logger: BufferLogger.test()), ];
];
});
final BufferLogger logger = context.get<Logger>() as BufferLogger;
final DevicesCommand command = DevicesCommand(); final DevicesCommand command = DevicesCommand();
final CommandRunner<void> runner = createTestCommandRunner(command); final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['devices', '--machine']); await runner.run(<String>['devices', '--machine']);
...@@ -76,8 +79,19 @@ void main() { ...@@ -76,8 +79,19 @@ void main() {
); );
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FeatureFlags: () => TestFeatureFlags(isWebEnabled: true), FeatureFlags: () => TestFeatureFlags(isWebEnabled: true),
DeviceManager: () => MockDeviceManager(), DeviceManager: () => deviceManager,
Logger: () => logger,
}); });
} }
class MockDeviceManager extends Mock implements DeviceManager {} class FakeDeviceManager extends Fake implements DeviceManager {
List<Device> devices = <Device>[];
@override
String specifiedDeviceId;
@override
Future<List<Device>> refreshAllConnectedDevices({Duration timeout}) async {
return devices;
}
}
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