Unverified Commit 63fc778f authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] remove mocks from dart plugins test (#82413)

parent 6cb91b1c
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
import 'package:args/command_runner.dart'; import 'package:args/command_runner.dart';
import 'package:file/memory.dart'; import 'package:file/memory.dart';
import 'package:flutter_tools/src/android/android_workflow.dart';
import 'package:flutter_tools/src/base/config.dart'; import 'package:flutter_tools/src/base/config.dart';
import 'package:flutter_tools/src/base/file_system.dart'; 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';
...@@ -21,7 +22,7 @@ import 'package:flutter_tools/src/globals_null_migrated.dart' as globals; ...@@ -21,7 +22,7 @@ import 'package:flutter_tools/src/globals_null_migrated.dart' as globals;
import 'package:flutter_tools/src/reporting/reporting.dart'; import 'package:flutter_tools/src/reporting/reporting.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/version.dart';
import 'package:mockito/mockito.dart'; import 'package:test/fake.dart';
import 'package:usage/usage_io.dart'; import 'package:usage/usage_io.dart';
import '../src/common.dart'; import '../src/common.dart';
...@@ -147,28 +148,24 @@ void main() { ...@@ -147,28 +148,24 @@ void main() {
}); });
}); });
group('analytics with mocks', () { group('analytics with fakes', () {
MemoryFileSystem memoryFileSystem; MemoryFileSystem memoryFileSystem;
FakeStdio fakeStdio; FakeStdio fakeStdio;
TestUsage testUsage; TestUsage testUsage;
FakeClock fakeClock; FakeClock fakeClock;
Doctor mockDoctor; FakeDoctor doctor;
setUp(() { setUp(() {
memoryFileSystem = MemoryFileSystem.test(); memoryFileSystem = MemoryFileSystem.test();
fakeStdio = FakeStdio(); fakeStdio = FakeStdio();
testUsage = TestUsage(); testUsage = TestUsage();
fakeClock = FakeClock(); fakeClock = FakeClock();
mockDoctor = MockDoctor(); doctor = FakeDoctor();
}); });
testUsingContext('flutter commands send timing events', () async { testUsingContext('flutter commands send timing events', () async {
fakeClock.times = <int>[1000, 2000]; fakeClock.times = <int>[1000, 2000];
when(mockDoctor.diagnose( doctor.diagnoseSucceeds = true;
androidLicenses: false,
verbose: false,
androidLicenseValidator: anyNamed('androidLicenseValidator')
)).thenAnswer((_) async => true);
final DoctorCommand command = DoctorCommand(); final DoctorCommand command = DoctorCommand();
final CommandRunner<void> runner = createTestCommandRunner(command); final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['doctor']); await runner.run(<String>['doctor']);
...@@ -180,14 +177,13 @@ void main() { ...@@ -180,14 +177,13 @@ void main() {
)); ));
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
SystemClock: () => fakeClock, SystemClock: () => fakeClock,
Doctor: () => mockDoctor, Doctor: () => doctor,
Usage: () => testUsage, Usage: () => testUsage,
}); });
testUsingContext('doctor fail sends warning', () async { testUsingContext('doctor fail sends warning', () async {
fakeClock.times = <int>[1000, 2000]; fakeClock.times = <int>[1000, 2000];
when(mockDoctor.diagnose(androidLicenses: false, verbose: false, androidLicenseValidator: anyNamed('androidLicenseValidator'))) doctor.diagnoseSucceeds = false;
.thenAnswer((_) async => false);
final DoctorCommand command = DoctorCommand(); final DoctorCommand command = DoctorCommand();
final CommandRunner<void> runner = createTestCommandRunner(command); final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['doctor']); await runner.run(<String>['doctor']);
...@@ -200,7 +196,7 @@ void main() { ...@@ -200,7 +196,7 @@ void main() {
)); ));
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
SystemClock: () => fakeClock, SystemClock: () => fakeClock,
Doctor: () => mockDoctor, Doctor: () => doctor,
Usage: () => testUsage, Usage: () => testUsage,
}); });
...@@ -363,7 +359,19 @@ class FakeFlutterCommand extends FlutterCommand { ...@@ -363,7 +359,19 @@ class FakeFlutterCommand extends FlutterCommand {
} }
} }
class MockDoctor extends Mock implements Doctor {} class FakeDoctor extends Fake implements Doctor {
bool diagnoseSucceeds = false;
@override
Future<bool> diagnose({
bool androidLicenses = false,
bool verbose = true,
bool showColor = true,
AndroidLicenseValidator androidLicenseValidator,
}) async {
return diagnoseSucceeds;
}
}
class FakeClock extends Fake implements SystemClock { class FakeClock extends Fake implements SystemClock {
List<int> times = <int>[]; List<int> times = <int>[];
......
...@@ -12,8 +12,8 @@ import 'package:flutter_tools/src/flutter_plugins.dart'; ...@@ -12,8 +12,8 @@ import 'package:flutter_tools/src/flutter_plugins.dart';
import 'package:flutter_tools/src/globals_null_migrated.dart' as globals; import 'package:flutter_tools/src/globals_null_migrated.dart' as globals;
import 'package:flutter_tools/src/plugins.dart'; import 'package:flutter_tools/src/plugins.dart';
import 'package:flutter_tools/src/project.dart'; import 'package:flutter_tools/src/project.dart';
import 'package:mockito/mockito.dart';
import 'package:package_config/package_config.dart'; import 'package:package_config/package_config.dart';
import 'package:test/fake.dart';
import 'package:yaml/yaml.dart'; import 'package:yaml/yaml.dart';
import '../src/common.dart'; import '../src/common.dart';
...@@ -22,23 +22,18 @@ import '../src/context.dart'; ...@@ -22,23 +22,18 @@ import '../src/context.dart';
void main() { void main() {
group('Dart plugin registrant', () { group('Dart plugin registrant', () {
FileSystem fs; FileSystem fs;
MockFlutterProject flutterProject; FakeFlutterProject flutterProject;
MockFlutterManifest flutterManifest; FakeFlutterManifest flutterManifest;
setUp(() async { setUp(() async {
fs = MemoryFileSystem.test(); fs = MemoryFileSystem.test();
final Directory directory = fs.currentDirectory.childDirectory('app');
flutterProject = MockFlutterProject(); flutterManifest = FakeFlutterManifest();
flutterProject = FakeFlutterProject()
flutterManifest = MockFlutterManifest(); ..manifest = flutterManifest
when(flutterManifest.dependencies).thenReturn(<String>{}); ..directory = directory
..flutterPluginsFile = directory.childFile('.flutter-plugins')
when(flutterProject.manifest).thenReturn(flutterManifest); ..flutterPluginsDependenciesFile = directory.childFile('.flutter-plugins-dependencies');
when(flutterProject.directory).thenReturn(fs.systemTempDirectory.childDirectory('app'));
when(flutterProject.flutterPluginsFile).thenReturn(flutterProject.directory.childFile('.flutter-plugins'));
when(flutterProject.flutterPluginsDependenciesFile).thenReturn(flutterProject.directory.childFile('.flutter-plugins-dependencies'));
flutterProject.directory.childFile('.packages').createSync(recursive: true); flutterProject.directory.childFile('.packages').createSync(recursive: true);
}); });
...@@ -564,33 +559,8 @@ void main() { ...@@ -564,33 +559,8 @@ void main() {
}); });
group('generateMainDartWithPluginRegistrant', () { group('generateMainDartWithPluginRegistrant', () {
void createFakeDartPlugins(
FlutterProject flutterProject,
FlutterManifest flutterManifest,
FileSystem fs,
Map<String, String> plugins,
) {
final Directory fakePubCache = fs.systemTempDirectory.childDirectory('cache');
final File packagesFile = flutterProject.directory
.childFile('.packages')
..createSync(recursive: true);
for (final MapEntry<String, String> entry in plugins.entries) {
final String name = fs.path.basename(entry.key);
final Directory pluginDirectory = fakePubCache.childDirectory(name);
packagesFile.writeAsStringSync(
'$name:file://${pluginDirectory.childFile('lib').uri}\n',
mode: FileMode.writeOnlyAppend);
pluginDirectory.childFile('pubspec.yaml')
..createSync(recursive: true)
..writeAsStringSync(entry.value);
}
when(flutterManifest.dependencies).thenReturn(<String>{...plugins.keys});
}
testUsingContext('Generates new entrypoint', () async { testUsingContext('Generates new entrypoint', () async {
when(flutterProject.isModule).thenReturn(false); flutterProject.isModule = true;
createFakeDartPlugins( createFakeDartPlugins(
flutterProject, flutterProject,
...@@ -739,7 +709,7 @@ void main() { ...@@ -739,7 +709,7 @@ void main() {
}); });
testUsingContext('Plugin without platform support throws tool exit', () async { testUsingContext('Plugin without platform support throws tool exit', () async {
when(flutterProject.isModule).thenReturn(false); flutterProject.isModule = false;
createFakeDartPlugins( createFakeDartPlugins(
flutterProject, flutterProject,
...@@ -785,7 +755,7 @@ void main() { ...@@ -785,7 +755,7 @@ void main() {
}); });
testUsingContext('Plugin with platform support without dart plugin class throws tool exit', () async { testUsingContext('Plugin with platform support without dart plugin class throws tool exit', () async {
when(flutterProject.isModule).thenReturn(false); flutterProject.isModule = false;
createFakeDartPlugins( createFakeDartPlugins(
flutterProject, flutterProject,
...@@ -858,8 +828,7 @@ void main() { ...@@ -858,8 +828,7 @@ void main() {
}); });
testUsingContext('Does not create new entrypoint if there are no platform resolutions', () async { testUsingContext('Does not create new entrypoint if there are no platform resolutions', () async {
when(flutterProject.isModule).thenReturn(false); flutterProject.isModule = false;
when(flutterManifest.dependencies).thenReturn(<String>{});
final Directory libDir = flutterProject.directory.childDirectory('lib'); final Directory libDir = flutterProject.directory.childDirectory('lib');
libDir.createSync(recursive: true); libDir.createSync(recursive: true);
...@@ -886,7 +855,7 @@ void main() { ...@@ -886,7 +855,7 @@ void main() {
}); });
testUsingContext('Deletes new entrypoint if there are no platform resolutions', () async { testUsingContext('Deletes new entrypoint if there are no platform resolutions', () async {
when(flutterProject.isModule).thenReturn(false); flutterProject.isModule = false;
createFakeDartPlugins( createFakeDartPlugins(
flutterProject, flutterProject,
...@@ -943,11 +912,74 @@ void main() { ...@@ -943,11 +912,74 @@ void main() {
FileSystem: () => fs, FileSystem: () => fs,
ProcessManager: () => FakeProcessManager.any(), ProcessManager: () => FakeProcessManager.any(),
}); });
}); });
}); });
} }
class MockFlutterManifest extends Mock implements FlutterManifest {} void createFakeDartPlugins(
class MockFlutterProject extends Mock implements FlutterProject {} FakeFlutterProject flutterProject,
FakeFlutterManifest flutterManifest,
FileSystem fs,
Map<String, String> plugins,
) {
final Directory fakePubCache = fs.systemTempDirectory.childDirectory('cache');
final File packagesFile = flutterProject.directory
.childFile('.packages')
..createSync(recursive: true);
for (final MapEntry<String, String> entry in plugins.entries) {
final String name = fs.path.basename(entry.key);
final Directory pluginDirectory = fakePubCache.childDirectory(name);
packagesFile.writeAsStringSync(
'$name:file://${pluginDirectory.childFile('lib').uri}\n',
mode: FileMode.writeOnlyAppend,
);
pluginDirectory.childFile('pubspec.yaml')
..createSync(recursive: true)
..writeAsStringSync(entry.value);
}
flutterManifest.dependencies = plugins.keys.toSet();
}
class FakeFlutterManifest extends Fake implements FlutterManifest {
@override
Set<String> dependencies = <String>{};
}
class FakeFlutterProject extends Fake implements FlutterProject {
@override
bool isModule = false;
@override
FlutterManifest manifest;
@override
Directory directory;
@override
File flutterPluginsFile;
@override
File flutterPluginsDependenciesFile;
@override
IosProject ios;
@override
AndroidProject android;
@override
WebProject web;
@override
MacOSProject macos;
@override
LinuxProject linux;
@override
WindowsProject windows;
@override
WindowsUwpProject windowsUwp;
}
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