[flutter_tools] remove mocks from dart plugins test (#82413)
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
|
||||
import 'package:args/command_runner.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/file_system.dart';
|
||||
import 'package:flutter_tools/src/base/io.dart';
|
||||
@@ -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/runner/flutter_command.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 '../src/common.dart';
|
||||
@@ -147,28 +148,24 @@ void main() {
|
||||
});
|
||||
});
|
||||
|
||||
group('analytics with mocks', () {
|
||||
group('analytics with fakes', () {
|
||||
MemoryFileSystem memoryFileSystem;
|
||||
FakeStdio fakeStdio;
|
||||
TestUsage testUsage;
|
||||
FakeClock fakeClock;
|
||||
Doctor mockDoctor;
|
||||
FakeDoctor doctor;
|
||||
|
||||
setUp(() {
|
||||
memoryFileSystem = MemoryFileSystem.test();
|
||||
fakeStdio = FakeStdio();
|
||||
testUsage = TestUsage();
|
||||
fakeClock = FakeClock();
|
||||
mockDoctor = MockDoctor();
|
||||
doctor = FakeDoctor();
|
||||
});
|
||||
|
||||
testUsingContext('flutter commands send timing events', () async {
|
||||
fakeClock.times = <int>[1000, 2000];
|
||||
when(mockDoctor.diagnose(
|
||||
androidLicenses: false,
|
||||
verbose: false,
|
||||
androidLicenseValidator: anyNamed('androidLicenseValidator')
|
||||
)).thenAnswer((_) async => true);
|
||||
doctor.diagnoseSucceeds = true;
|
||||
final DoctorCommand command = DoctorCommand();
|
||||
final CommandRunner<void> runner = createTestCommandRunner(command);
|
||||
await runner.run(<String>['doctor']);
|
||||
@@ -180,14 +177,13 @@ void main() {
|
||||
));
|
||||
}, overrides: <Type, Generator>{
|
||||
SystemClock: () => fakeClock,
|
||||
Doctor: () => mockDoctor,
|
||||
Doctor: () => doctor,
|
||||
Usage: () => testUsage,
|
||||
});
|
||||
|
||||
testUsingContext('doctor fail sends warning', () async {
|
||||
fakeClock.times = <int>[1000, 2000];
|
||||
when(mockDoctor.diagnose(androidLicenses: false, verbose: false, androidLicenseValidator: anyNamed('androidLicenseValidator')))
|
||||
.thenAnswer((_) async => false);
|
||||
doctor.diagnoseSucceeds = false;
|
||||
final DoctorCommand command = DoctorCommand();
|
||||
final CommandRunner<void> runner = createTestCommandRunner(command);
|
||||
await runner.run(<String>['doctor']);
|
||||
@@ -200,7 +196,7 @@ void main() {
|
||||
));
|
||||
}, overrides: <Type, Generator>{
|
||||
SystemClock: () => fakeClock,
|
||||
Doctor: () => mockDoctor,
|
||||
Doctor: () => doctor,
|
||||
Usage: () => testUsage,
|
||||
});
|
||||
|
||||
@@ -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 {
|
||||
List<int> times = <int>[];
|
||||
|
||||
@@ -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/plugins.dart';
|
||||
import 'package:flutter_tools/src/project.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:package_config/package_config.dart';
|
||||
import 'package:test/fake.dart';
|
||||
import 'package:yaml/yaml.dart';
|
||||
|
||||
import '../src/common.dart';
|
||||
@@ -22,23 +22,18 @@ import '../src/context.dart';
|
||||
void main() {
|
||||
group('Dart plugin registrant', () {
|
||||
FileSystem fs;
|
||||
MockFlutterProject flutterProject;
|
||||
MockFlutterManifest flutterManifest;
|
||||
FakeFlutterProject flutterProject;
|
||||
FakeFlutterManifest flutterManifest;
|
||||
|
||||
setUp(() async {
|
||||
fs = MemoryFileSystem.test();
|
||||
|
||||
flutterProject = MockFlutterProject();
|
||||
|
||||
flutterManifest = MockFlutterManifest();
|
||||
when(flutterManifest.dependencies).thenReturn(<String>{});
|
||||
|
||||
when(flutterProject.manifest).thenReturn(flutterManifest);
|
||||
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'));
|
||||
|
||||
final Directory directory = fs.currentDirectory.childDirectory('app');
|
||||
flutterManifest = FakeFlutterManifest();
|
||||
flutterProject = FakeFlutterProject()
|
||||
..manifest = flutterManifest
|
||||
..directory = directory
|
||||
..flutterPluginsFile = directory.childFile('.flutter-plugins')
|
||||
..flutterPluginsDependenciesFile = directory.childFile('.flutter-plugins-dependencies');
|
||||
flutterProject.directory.childFile('.packages').createSync(recursive: true);
|
||||
});
|
||||
|
||||
@@ -564,33 +559,8 @@ void main() {
|
||||
});
|
||||
|
||||
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 {
|
||||
when(flutterProject.isModule).thenReturn(false);
|
||||
flutterProject.isModule = true;
|
||||
|
||||
createFakeDartPlugins(
|
||||
flutterProject,
|
||||
@@ -739,7 +709,7 @@ void main() {
|
||||
});
|
||||
|
||||
testUsingContext('Plugin without platform support throws tool exit', () async {
|
||||
when(flutterProject.isModule).thenReturn(false);
|
||||
flutterProject.isModule = false;
|
||||
|
||||
createFakeDartPlugins(
|
||||
flutterProject,
|
||||
@@ -785,7 +755,7 @@ void main() {
|
||||
});
|
||||
|
||||
testUsingContext('Plugin with platform support without dart plugin class throws tool exit', () async {
|
||||
when(flutterProject.isModule).thenReturn(false);
|
||||
flutterProject.isModule = false;
|
||||
|
||||
createFakeDartPlugins(
|
||||
flutterProject,
|
||||
@@ -858,8 +828,7 @@ void main() {
|
||||
});
|
||||
|
||||
testUsingContext('Does not create new entrypoint if there are no platform resolutions', () async {
|
||||
when(flutterProject.isModule).thenReturn(false);
|
||||
when(flutterManifest.dependencies).thenReturn(<String>{});
|
||||
flutterProject.isModule = false;
|
||||
|
||||
final Directory libDir = flutterProject.directory.childDirectory('lib');
|
||||
libDir.createSync(recursive: true);
|
||||
@@ -886,7 +855,7 @@ void main() {
|
||||
});
|
||||
|
||||
testUsingContext('Deletes new entrypoint if there are no platform resolutions', () async {
|
||||
when(flutterProject.isModule).thenReturn(false);
|
||||
flutterProject.isModule = false;
|
||||
|
||||
createFakeDartPlugins(
|
||||
flutterProject,
|
||||
@@ -943,11 +912,74 @@ void main() {
|
||||
FileSystem: () => fs,
|
||||
ProcessManager: () => FakeProcessManager.any(),
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
class MockFlutterManifest extends Mock implements FlutterManifest {}
|
||||
class MockFlutterProject extends Mock implements FlutterProject {}
|
||||
void createFakeDartPlugins(
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user