[flutter_tools] remove mock SystemClock (#74994)

This commit is contained in:
Jonah Williams
2021-01-29 14:28:08 -08:00
committed by GitHub
parent 8db51f665d
commit e53c48968f
4 changed files with 56 additions and 52 deletions

View File

@@ -154,23 +154,19 @@ void main() {
MemoryFileSystem memoryFileSystem;
MockStdio mockStdio;
TestUsage testUsage;
SystemClock mockClock;
FakeClock fakeClock;
Doctor mockDoctor;
List<int> mockTimes;
setUp(() {
memoryFileSystem = MemoryFileSystem.test();
mockStdio = MockStdio();
testUsage = TestUsage();
mockClock = MockClock();
fakeClock = FakeClock();
mockDoctor = MockDoctor();
when(mockClock.now()).thenAnswer(
(Invocation _) => DateTime.fromMillisecondsSinceEpoch(mockTimes.removeAt(0))
);
});
testUsingContext('flutter commands send timing events', () async {
mockTimes = <int>[1000, 2000];
fakeClock.times = <int>[1000, 2000];
when(mockDoctor.diagnose(
androidLicenses: false,
verbose: false,
@@ -180,28 +176,25 @@ void main() {
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['doctor']);
verify(mockClock.now()).called(2);
expect(testUsage.timings, contains(
const TestTimingEvent(
'flutter', 'doctor', Duration(milliseconds: 1000), label: 'success',
),
));
}, overrides: <Type, Generator>{
SystemClock: () => mockClock,
SystemClock: () => fakeClock,
Doctor: () => mockDoctor,
Usage: () => testUsage,
});
testUsingContext('doctor fail sends warning', () async {
mockTimes = <int>[1000, 2000];
fakeClock.times = <int>[1000, 2000];
when(mockDoctor.diagnose(androidLicenses: false, verbose: false, androidLicenseValidator: anyNamed('androidLicenseValidator')))
.thenAnswer((_) async => false);
final DoctorCommand command = DoctorCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>['doctor']);
verify(mockClock.now()).called(2);
expect(testUsage.timings, contains(
const TestTimingEvent(
@@ -209,7 +202,7 @@ void main() {
),
));
}, overrides: <Type, Generator>{
SystemClock: () => mockClock,
SystemClock: () => fakeClock,
Doctor: () => mockDoctor,
Usage: () => testUsage,
});
@@ -233,7 +226,7 @@ void main() {
testUsingContext('command sends localtime', () async {
const int kMillis = 1000;
mockTimes = <int>[kMillis];
fakeClock.times = <int>[kMillis];
// Since FLUTTER_ANALYTICS_LOG_FILE is set in the environment, analytics
// will be written to a file.
final Usage usage = Usage(
@@ -252,7 +245,7 @@ void main() {
}, overrides: <Type, Generator>{
FileSystem: () => memoryFileSystem,
ProcessManager: () => FakeProcessManager.any(),
SystemClock: () => mockClock,
SystemClock: () => fakeClock,
Platform: () => FakePlatform(
environment: <String, String>{
'FLUTTER_ANALYTICS_LOG_FILE': 'analytics.log',
@@ -263,7 +256,7 @@ void main() {
testUsingContext('event sends localtime', () async {
const int kMillis = 1000;
mockTimes = <int>[kMillis];
fakeClock.times = <int>[kMillis];
// Since FLUTTER_ANALYTICS_LOG_FILE is set in the environment, analytics
// will be written to a file.
final Usage usage = Usage(
@@ -282,7 +275,7 @@ void main() {
}, overrides: <Type, Generator>{
FileSystem: () => memoryFileSystem,
ProcessManager: () => FakeProcessManager.any(),
SystemClock: () => mockClock,
SystemClock: () => fakeClock,
Platform: () => FakePlatform(
environment: <String, String>{
'FLUTTER_ANALYTICS_LOG_FILE': 'analytics.log',
@@ -378,3 +371,12 @@ class FakeFlutterCommand extends FlutterCommand {
class MockDoctor extends Mock implements Doctor {}
class MockFlutterConfig extends Mock implements Config {}
class FakeClock extends Fake implements SystemClock {
List<int> times = <int>[];
@override
DateTime now() {
return DateTime.fromMillisecondsSinceEpoch(times.removeAt(0));
}
}

View File

@@ -37,7 +37,7 @@ void main() {
MockWebProject webProject;
MockWindowsProject windowsProject;
MockLinuxProject linuxProject;
SystemClock mockClock;
FakeSystemClock systemClock;
FlutterVersion mockVersion;
// A Windows-style filesystem. This is not populated by default, so tests
// using it instead of fs must re-run any necessary setup (e.g.,
@@ -112,16 +112,14 @@ void main() {
setUp(() async {
fs = MemoryFileSystem.test();
fsWindows = MemoryFileSystem(style: FileSystemStyle.windows);
mockClock = MockClock();
systemClock = FakeSystemClock()
..currentTime = DateTime(1970, 1, 1);
mockVersion = MockFlutterVersion();
// Add basic properties to the Flutter project and subprojects
setUpProject(fs);
flutterProject.directory.childFile('.packages').createSync(recursive: true);
when(mockClock.now()).thenAnswer(
(Invocation _) => DateTime(1970, 1, 1)
);
when(mockVersion.frameworkVersion).thenAnswer(
(Invocation _) => '1.0.0'
);
@@ -425,9 +423,7 @@ dependencies:
when(iosProject.existsSync()).thenReturn(true);
final DateTime dateCreated = DateTime(1970, 1, 1);
when(mockClock.now()).thenAnswer(
(Invocation _) => dateCreated
);
systemClock.currentTime = dateCreated;
const String version = '1.0.0';
when(mockVersion.frameworkVersion).thenAnswer(
(Invocation _) => version
@@ -516,7 +512,7 @@ dependencies:
}, overrides: <Type, Generator>{
FileSystem: () => fs,
ProcessManager: () => FakeProcessManager.any(),
SystemClock: () => mockClock,
SystemClock: () => systemClock,
FlutterVersion: () => mockVersion
});
@@ -533,7 +529,7 @@ dependencies:
}, overrides: <Type, Generator>{
FileSystem: () => fs,
ProcessManager: () => FakeProcessManager.any(),
SystemClock: () => mockClock,
SystemClock: () => systemClock,
FlutterVersion: () => mockVersion
});
@@ -556,7 +552,7 @@ dependencies:
}, overrides: <Type, Generator>{
FileSystem: () => fs,
ProcessManager: () => FakeProcessManager.any(),
SystemClock: () => mockClock,
SystemClock: () => systemClock,
FlutterVersion: () => mockVersion
});
});
@@ -1410,3 +1406,12 @@ class MockWebProject extends Mock implements WebProject {}
class MockWindowsProject extends Mock implements WindowsProject {}
class MockLinuxProject extends Mock implements LinuxProject {}
class MockOperatingSystemUtils extends Mock implements OperatingSystemUtils {}
class FakeSystemClock extends Fake implements SystemClock {
DateTime currentTime;
@override
DateTime now() {
return currentTime;
}
}

View File

@@ -31,20 +31,16 @@ void main() {
group('Flutter Command', () {
MockitoCache cache;
MockitoUsage usage;
MockClock clock;
FakeClock clock;
MockProcessInfo mockProcessInfo;
List<int> mockTimes;
setUp(() {
Cache.disableLocking();
cache = MockitoCache();
usage = MockitoUsage();
clock = MockClock();
clock = FakeClock();
mockProcessInfo = MockProcessInfo();
when(clock.now()).thenAnswer(
(Invocation _) => DateTime.fromMillisecondsSinceEpoch(mockTimes.removeAt(0))
);
when(mockProcessInfo.maxRss).thenReturn(10);
});
@@ -154,7 +150,7 @@ void main() {
testUsingCommandContext('reports command that results in success', () async {
// Crash if called a third time which is unexpected.
mockTimes = <int>[1000, 2000];
clock.times = <int>[1000, 2000];
final DummyFlutterCommand flutterCommand = DummyFlutterCommand(
commandFunction: () async {
@@ -185,7 +181,7 @@ void main() {
testUsingCommandContext('reports command that results in warning', () async {
// Crash if called a third time which is unexpected.
mockTimes = <int>[1000, 2000];
clock.times = <int>[1000, 2000];
final DummyFlutterCommand flutterCommand = DummyFlutterCommand(
commandFunction: () async {
@@ -216,7 +212,7 @@ void main() {
testUsingCommandContext('reports command that results in failure', () async {
// Crash if called a third time which is unexpected.
mockTimes = <int>[1000, 2000];
clock.times = <int>[1000, 2000];
final DummyFlutterCommand flutterCommand = DummyFlutterCommand(
commandFunction: () async {
@@ -250,7 +246,7 @@ void main() {
testUsingCommandContext('reports command that results in error', () async {
// Crash if called a third time which is unexpected.
mockTimes = <int>[1000, 2000];
clock.times = <int>[1000, 2000];
final DummyFlutterCommand flutterCommand = DummyFlutterCommand(
commandFunction: () async {
@@ -348,7 +344,7 @@ void main() {
testUsingContext('reports command that is killed', () async {
// Crash if called a third time which is unexpected.
mockTimes = <int>[1000, 2000];
clock.times = <int>[1000, 2000];
final Completer<void> completer = Completer<void>();
setExitFunctionForTests((int exitCode) {
@@ -398,7 +394,7 @@ void main() {
});
testUsingContext('command release lock on kill signal', () async {
mockTimes = <int>[1000, 2000];
clock.times = <int>[1000, 2000];
final Completer<void> completer = Completer<void>();
setExitFunctionForTests((int exitCode) {
expect(exitCode, 0);
@@ -437,11 +433,10 @@ void main() {
testUsingCommandContext('report execution timing by default', () async {
// Crash if called a third time which is unexpected.
mockTimes = <int>[1000, 2000];
clock.times = <int>[1000, 2000];
final DummyFlutterCommand flutterCommand = DummyFlutterCommand();
await flutterCommand.run();
verify(clock.now()).called(2);
expect(
verify(usage.sendTiming(
@@ -458,12 +453,11 @@ void main() {
testUsingCommandContext('no timing report without usagePath', () async {
// Crash if called a third time which is unexpected.
mockTimes = <int>[1000, 2000];
clock.times = <int>[1000, 2000];
final DummyFlutterCommand flutterCommand =
DummyFlutterCommand(noUsagePath: true);
await flutterCommand.run();
verify(clock.now()).called(2);
verifyNever(usage.sendTiming(
any, any, any,
label: anyNamed('label')));
@@ -471,7 +465,7 @@ void main() {
testUsingCommandContext('report additional FlutterCommandResult data', () async {
// Crash if called a third time which is unexpected.
mockTimes = <int>[1000, 2000];
clock.times = <int>[1000, 2000];
final FlutterCommandResult commandResult = FlutterCommandResult(
ExitStatus.success,
@@ -484,7 +478,6 @@ void main() {
commandFunction: () async => commandResult
);
await flutterCommand.run();
verify(clock.now()).called(2);
expect(
verify(usage.sendTiming(
captureAny, captureAny, captureAny,
@@ -500,7 +493,7 @@ void main() {
testUsingCommandContext('report failed execution timing too', () async {
// Crash if called a third time which is unexpected.
mockTimes = <int>[1000, 2000];
clock.times = <int>[1000, 2000];
final DummyFlutterCommand flutterCommand = DummyFlutterCommand(
commandFunction: () async {
@@ -513,8 +506,6 @@ void main() {
await flutterCommand.run();
fail('Mock should make this fail');
} on ToolExit {
// Should have still checked time twice.
verify(clock.now()).called(2);
expect(
verify(usage.sendTiming(
@@ -694,3 +685,12 @@ class FakePub extends Fake implements Pub {
bool checkUpToDate = false,
}) async { }
}
class FakeClock extends Fake implements SystemClock {
List<int> times = <int>[];
@override
DateTime now() {
return DateTime.fromMillisecondsSinceEpoch(times.removeAt(0));
}
}

View File

@@ -18,7 +18,6 @@ import 'package:flutter_tools/src/base/process.dart';
import 'package:flutter_tools/src/base/signals.dart';
import 'package:flutter_tools/src/base/template.dart';
import 'package:flutter_tools/src/base/terminal.dart';
import 'package:flutter_tools/src/base/time.dart';
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/isolated/mustache_template.dart';
import 'package:flutter_tools/src/cache.dart';
@@ -421,8 +420,6 @@ class FakeXcodeProjectInterpreter implements XcodeProjectInterpreter {
class MockFlutterVersion extends Mock implements FlutterVersion {}
class MockClock extends Mock implements SystemClock {}
class MockHttpClient extends Mock implements HttpClient {}
class MockCrashReporter extends Mock implements CrashReporter {}