Unverified Commit 34674fd5 authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tools] remove mocks from test_compiler test (#81312)

parent a0831048
...@@ -5,7 +5,6 @@ ...@@ -5,7 +5,6 @@
// @dart = 2.8 // @dart = 2.8
import 'package:file/memory.dart'; import 'package:file/memory.dart';
import 'package:file_testing/file_testing.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/logger.dart'; import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart'; import 'package:flutter_tools/src/base/platform.dart';
...@@ -13,8 +12,8 @@ import 'package:flutter_tools/src/build_info.dart'; ...@@ -13,8 +12,8 @@ import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/compile.dart'; import 'package:flutter_tools/src/compile.dart';
import 'package:flutter_tools/src/project.dart'; import 'package:flutter_tools/src/project.dart';
import 'package:flutter_tools/src/test/test_compiler.dart'; import 'package:flutter_tools/src/test/test_compiler.dart';
import 'package:mockito/mockito.dart';
import 'package:package_config/package_config_types.dart'; import 'package:package_config/package_config_types.dart';
import 'package:test/fake.dart';
import '../../src/common.dart'; import '../../src/common.dart';
import '../../src/context.dart'; import '../../src/context.dart';
...@@ -34,36 +33,25 @@ final BuildInfo debugBuild = BuildInfo( ...@@ -34,36 +33,25 @@ final BuildInfo debugBuild = BuildInfo(
); );
void main() { void main() {
MockResidentCompiler residentCompiler; FakeResidentCompiler residentCompiler;
FileSystem fileSystem; FileSystem fileSystem;
setUp(() { setUp(() {
fileSystem = MemoryFileSystem.test(); fileSystem = MemoryFileSystem.test();
fileSystem.file('pubspec.yaml').createSync(); fileSystem.file('pubspec.yaml').createSync();
fileSystem.file('test/foo.dart').createSync(recursive: true); fileSystem.file('test/foo.dart').createSync(recursive: true);
residentCompiler = MockResidentCompiler(); residentCompiler = FakeResidentCompiler(fileSystem);
}); });
testUsingContext('TestCompiler reports a dill file when compile is successful', () async { testUsingContext('TestCompiler reports a dill file when compile is successful', () async {
residentCompiler.compilerOutput = const CompilerOutput('abc.dill', 0, <Uri>[]);
final FakeTestCompiler testCompiler = FakeTestCompiler( final FakeTestCompiler testCompiler = FakeTestCompiler(
debugBuild, debugBuild,
FlutterProject.fromDirectoryTest(fileSystem.currentDirectory), FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
residentCompiler, residentCompiler,
); );
when(residentCompiler.recompile(
any,
<Uri>[Uri.parse('test/foo.dart')],
outputPath: testCompiler.outputDill.path,
packageConfig: anyNamed('packageConfig'),
projectRootPath: anyNamed('projectRootPath'),
fs: anyNamed('fs'),
)).thenAnswer((Invocation invocation) async {
fileSystem.file('abc.dill').createSync();
return const CompilerOutput('abc.dill', 0, <Uri>[]);
});
expect(await testCompiler.compile(Uri.parse('test/foo.dart')), 'test/foo.dart.dill'); expect(await testCompiler.compile(Uri.parse('test/foo.dart')), 'test/foo.dart.dill');
expect(fileSystem.file('test/foo.dart.dill'), exists);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FileSystem: () => fileSystem, FileSystem: () => fileSystem,
Platform: () => linuxPlatform, Platform: () => linuxPlatform,
...@@ -72,26 +60,15 @@ void main() { ...@@ -72,26 +60,15 @@ void main() {
}); });
testUsingContext('TestCompiler reports null when a compile fails', () async { testUsingContext('TestCompiler reports null when a compile fails', () async {
residentCompiler.compilerOutput = const CompilerOutput('abc.dill', 1, <Uri>[]);
final FakeTestCompiler testCompiler = FakeTestCompiler( final FakeTestCompiler testCompiler = FakeTestCompiler(
debugBuild, debugBuild,
FlutterProject.fromDirectoryTest(fileSystem.currentDirectory), FlutterProject.fromDirectoryTest(fileSystem.currentDirectory),
residentCompiler, residentCompiler,
); );
when(residentCompiler.recompile(
any,
<Uri>[Uri.parse('test/foo.dart')],
outputPath: testCompiler.outputDill.path,
packageConfig: anyNamed('packageConfig'),
projectRootPath: anyNamed('projectRootPath'),
fs: anyNamed('fs'),
)).thenAnswer((Invocation invocation) async {
fileSystem.file('abc.dill').createSync();
return const CompilerOutput('abc.dill', 1, <Uri>[]);
});
expect(await testCompiler.compile(Uri.parse('test/foo.dart')), null); expect(await testCompiler.compile(Uri.parse('test/foo.dart')), null);
expect(fileSystem.file('test/foo.dart.dill'), isNot(exists)); expect(residentCompiler.didShutdown, true);
verify(residentCompiler.shutdown()).called(1);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FileSystem: () => fileSystem, FileSystem: () => fileSystem,
Platform: () => linuxPlatform, Platform: () => linuxPlatform,
...@@ -112,7 +89,7 @@ void main() { ...@@ -112,7 +89,7 @@ void main() {
await testCompiler.dispose(); await testCompiler.dispose();
expect(testCompiler.compilerController.isClosed, true); expect(testCompiler.compilerController.isClosed, true);
verify(residentCompiler.shutdown()).called(1); expect(residentCompiler.didShutdown, true);
}, overrides: <Type, Generator>{ }, overrides: <Type, Generator>{
FileSystem: () => fileSystem, FileSystem: () => fileSystem,
Platform: () => linuxPlatform, Platform: () => linuxPlatform,
...@@ -129,7 +106,7 @@ class FakeTestCompiler extends TestCompiler { ...@@ -129,7 +106,7 @@ class FakeTestCompiler extends TestCompiler {
this.residentCompiler, this.residentCompiler,
) : super(buildInfo, flutterProject); ) : super(buildInfo, flutterProject);
final MockResidentCompiler residentCompiler; final FakeResidentCompiler residentCompiler;
@override @override
Future<ResidentCompiler> createCompiler() async { Future<ResidentCompiler> createCompiler() async {
...@@ -137,4 +114,38 @@ class FakeTestCompiler extends TestCompiler { ...@@ -137,4 +114,38 @@ class FakeTestCompiler extends TestCompiler {
} }
} }
class MockResidentCompiler extends Mock implements ResidentCompiler {} class FakeResidentCompiler extends Fake implements ResidentCompiler {
FakeResidentCompiler(this.fileSystem);
final FileSystem fileSystem;
CompilerOutput compilerOutput;
bool didShutdown = false;
@override
Future<CompilerOutput> recompile(
Uri mainUri,
List<Uri> invalidatedFiles, {
String outputPath,
PackageConfig packageConfig,
String projectRootPath,
FileSystem fs,
bool suppressErrors = false,
}) async {
if (compilerOutput != null) {
fileSystem.file(compilerOutput.outputFilename).createSync(recursive: true);
}
return compilerOutput;
}
@override
void accept() { }
@override
void reset() { }
@override
Future<void> shutdown() async {
didShutdown = true;
}
}
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