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