Rearrange tests to more closely match code structure (#10043)
This commit is contained in:
@@ -1,28 +0,0 @@
|
||||
// Copyright 2017 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:test/test.dart';
|
||||
import 'package:flutter_tools/src/base/common.dart';
|
||||
|
||||
import '../common.dart';
|
||||
|
||||
void main() {
|
||||
group('throwToolExit', () {
|
||||
test('throws ToolExit', () {
|
||||
expect(() => throwToolExit('message'), throwsToolExit());
|
||||
});
|
||||
|
||||
test('throws ToolExit with exitCode', () {
|
||||
expect(() => throwToolExit('message', exitCode: 42), throwsToolExit(exitCode: 42));
|
||||
});
|
||||
|
||||
test('throws ToolExit with message', () {
|
||||
expect(() => throwToolExit('message'), throwsToolExit(message: 'message'));
|
||||
});
|
||||
|
||||
test('throws ToolExit with message and exit code', () {
|
||||
expect(() => throwToolExit('message', exitCode: 42), throwsToolExit(exitCode: 42, message: 'message'));
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1,107 +0,0 @@
|
||||
// Copyright 2017 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:file/memory.dart';
|
||||
import 'package:flutter_tools/src/base/file_system.dart';
|
||||
import 'package:platform/platform.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../common.dart';
|
||||
import '../context.dart';
|
||||
|
||||
void main() {
|
||||
group('ensureDirectoryExists', () {
|
||||
MemoryFileSystem fs;
|
||||
|
||||
setUp(() {
|
||||
fs = new MemoryFileSystem();
|
||||
});
|
||||
|
||||
testUsingContext('recursively creates a directory if it does not exist', () async {
|
||||
ensureDirectoryExists('foo/bar/baz.flx');
|
||||
expect(fs.isDirectorySync('foo/bar'), true);
|
||||
}, overrides: <Type, Generator>{ FileSystem: () => fs } );
|
||||
|
||||
testUsingContext('throws tool exit on failure to create', () async {
|
||||
fs.file('foo').createSync();
|
||||
expect(() => ensureDirectoryExists('foo/bar.flx'), throwsToolExit());
|
||||
}, overrides: <Type, Generator>{ FileSystem: () => fs } );
|
||||
});
|
||||
|
||||
group('copyDirectorySync', () {
|
||||
/// Test file_systems.copyDirectorySync() using MemoryFileSystem.
|
||||
/// Copies between 2 instances of file systems which is also supported by copyDirectorySync().
|
||||
test('test directory copy', () async {
|
||||
final MemoryFileSystem sourceMemoryFs = new MemoryFileSystem();
|
||||
final String sourcePath = '/some/origin';
|
||||
final Directory sourceDirectory = await sourceMemoryFs.directory(sourcePath).create(recursive: true);
|
||||
sourceMemoryFs.currentDirectory = sourcePath;
|
||||
final File sourceFile1 = sourceMemoryFs.file('some_file.txt')..writeAsStringSync('bleh');
|
||||
final DateTime writeTime = sourceFile1.lastModifiedSync();
|
||||
sourceMemoryFs.file('sub_dir/another_file.txt').createSync(recursive: true);
|
||||
sourceMemoryFs.directory('empty_directory').createSync();
|
||||
|
||||
// Copy to another memory file system instance.
|
||||
final MemoryFileSystem targetMemoryFs = new MemoryFileSystem();
|
||||
final String targetPath = '/some/non-existent/target';
|
||||
final Directory targetDirectory = targetMemoryFs.directory(targetPath);
|
||||
copyDirectorySync(sourceDirectory, targetDirectory);
|
||||
|
||||
expect(targetDirectory.existsSync(), true);
|
||||
targetMemoryFs.currentDirectory = targetPath;
|
||||
expect(targetMemoryFs.directory('empty_directory').existsSync(), true);
|
||||
expect(targetMemoryFs.file('sub_dir/another_file.txt').existsSync(), true);
|
||||
expect(targetMemoryFs.file('some_file.txt').readAsStringSync(), 'bleh');
|
||||
|
||||
// Assert that the copy operation hasn't modified the original file in some way.
|
||||
expect(sourceMemoryFs.file('some_file.txt').lastModifiedSync(), writeTime);
|
||||
// There's still 3 things in the original directory as there were initially.
|
||||
expect(sourceMemoryFs.directory(sourcePath).listSync().length, 3);
|
||||
});
|
||||
});
|
||||
|
||||
group('canonicalizePath', () {
|
||||
test('does not lowercase on Windows', () {
|
||||
String path = 'C:\\Foo\\bAr\\cOOL.dart';
|
||||
expect(canonicalizePath(path), path);
|
||||
// fs.path.canonicalize does lowercase on Windows
|
||||
expect(fs.path.canonicalize(path), isNot(path));
|
||||
|
||||
path = '..\\bar\\.\\\\Foo';
|
||||
final String expected = fs.path.join(fs.currentDirectory.parent.absolute.path, 'bar', 'Foo');
|
||||
expect(canonicalizePath(path), expected);
|
||||
// fs.path.canonicalize should return the same result (modulo casing)
|
||||
expect(fs.path.canonicalize(path), expected.toLowerCase());
|
||||
}, testOn: 'windows');
|
||||
|
||||
test('does not lowercase on posix', () {
|
||||
String path = '/Foo/bAr/cOOL.dart';
|
||||
expect(canonicalizePath(path), path);
|
||||
// fs.path.canonicalize and canonicalizePath should be the same on Posix
|
||||
expect(fs.path.canonicalize(path), path);
|
||||
|
||||
path = '../bar/.//Foo';
|
||||
final String expected = fs.path.join(fs.currentDirectory.parent.absolute.path, 'bar', 'Foo');
|
||||
expect(canonicalizePath(path), expected);
|
||||
}, testOn: 'posix');
|
||||
});
|
||||
|
||||
group('escapePath', () {
|
||||
testUsingContext('on Windows', () {
|
||||
expect(escapePath('C:\\foo\\bar\\cool.dart'), 'C:\\\\foo\\\\bar\\\\cool.dart');
|
||||
expect(escapePath('foo\\bar\\cool.dart'), 'foo\\\\bar\\\\cool.dart');
|
||||
expect(escapePath('C:/foo/bar/cool.dart'), 'C:/foo/bar/cool.dart');
|
||||
}, overrides: <Type, Generator>{
|
||||
Platform: () => new FakePlatform(operatingSystem: 'windows')
|
||||
});
|
||||
|
||||
testUsingContext('on Linux', () {
|
||||
expect(escapePath('/foo/bar/cool.dart'), '/foo/bar/cool.dart');
|
||||
expect(escapePath('foo/bar/cool.dart'), 'foo/bar/cool.dart');
|
||||
expect(escapePath('foo\\cool.dart'), 'foo\\cool.dart');
|
||||
}, overrides: <Type, Generator>{
|
||||
Platform: () => new FakePlatform(operatingSystem: 'linux')
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
// Copyright 2017 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:io' as io;
|
||||
|
||||
import 'package:flutter_tools/src/base/io.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../context.dart';
|
||||
|
||||
void main() {
|
||||
group('ProcessSignal', () {
|
||||
|
||||
testUsingContext('signals are properly delegated', () async {
|
||||
final MockIoProcessSignal mockSignal = new MockIoProcessSignal();
|
||||
final ProcessSignal signalUnderTest = new ProcessSignal(mockSignal);
|
||||
final StreamController<io.ProcessSignal> controller = new StreamController<io.ProcessSignal>();
|
||||
|
||||
when(mockSignal.watch()).thenReturn(controller.stream);
|
||||
controller.add(mockSignal);
|
||||
|
||||
expect(signalUnderTest, await signalUnderTest.watch().first);
|
||||
});
|
||||
|
||||
testUsingContext('toString() works', () async {
|
||||
expect(io.ProcessSignal.SIGINT.toString(), ProcessSignal.SIGINT.toString());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
class MockIoProcessSignal extends Mock implements io.ProcessSignal {}
|
||||
@@ -1,24 +0,0 @@
|
||||
// Copyright 2017 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter_tools/src/base/logger.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
group('AppContext', () {
|
||||
test('error', () async {
|
||||
final BufferLogger mockLogger = new BufferLogger();
|
||||
final VerboseLogger verboseLogger = new VerboseLogger(mockLogger);
|
||||
|
||||
verboseLogger.printStatus('Hey Hey Hey Hey');
|
||||
verboseLogger.printTrace('Oooh, I do I do I do');
|
||||
verboseLogger.printError('Helpless!');
|
||||
|
||||
expect(mockLogger.statusText, matches(r'^\[ (?: {0,2}\+[0-9]{1,3} ms| )\] Hey Hey Hey Hey\n'
|
||||
r'\[ (?: {0,2}\+[0-9]{1,3} ms| )\] Oooh, I do I do I do\n$'));
|
||||
expect(mockLogger.traceText, '');
|
||||
expect(mockLogger.errorText, matches(r'^\[ (?: {0,2}\+[0-9]{1,3} ms| )\] Helpless!\n$'));
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
// Copyright 2017 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter_tools/src/base/io.dart';
|
||||
import 'package:flutter_tools/src/base/file_system.dart';
|
||||
import 'package:flutter_tools/src/base/os.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:process/process.dart';
|
||||
import 'package:platform/platform.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
|
||||
import '../context.dart';
|
||||
|
||||
const String kExecutable = 'foo';
|
||||
const String kPath1 = '/bar/bin/$kExecutable';
|
||||
const String kPath2 = '/another/bin/$kExecutable';
|
||||
|
||||
void main() {
|
||||
ProcessManager mockProcessManager;
|
||||
|
||||
setUp(() {
|
||||
mockProcessManager = new MockProcessManager();
|
||||
});
|
||||
|
||||
group('which on POSIX', () {
|
||||
|
||||
testUsingContext('returns null when executable does not exist', () async {
|
||||
when(mockProcessManager.runSync(<String>['which', kExecutable]))
|
||||
.thenReturn(new ProcessResult(0, 1, null, null));
|
||||
final OperatingSystemUtils utils = new OperatingSystemUtils();
|
||||
expect(utils.which(kExecutable), isNull);
|
||||
}, overrides: <Type, Generator>{
|
||||
ProcessManager: () => mockProcessManager,
|
||||
Platform: () => new FakePlatform(operatingSystem: 'linux')
|
||||
});
|
||||
|
||||
testUsingContext('returns exactly one result', () async {
|
||||
when(mockProcessManager.runSync(<String>['which', 'foo']))
|
||||
.thenReturn(new ProcessResult(0, 0, kPath1, null));
|
||||
final OperatingSystemUtils utils = new OperatingSystemUtils();
|
||||
expect(utils.which(kExecutable).path, kPath1);
|
||||
}, overrides: <Type, Generator>{
|
||||
ProcessManager: () => mockProcessManager,
|
||||
Platform: () => new FakePlatform(operatingSystem: 'linux')
|
||||
});
|
||||
|
||||
testUsingContext('returns all results for whichAll', () async {
|
||||
when(mockProcessManager.runSync(<String>['which', '-a', kExecutable]))
|
||||
.thenReturn(new ProcessResult(0, 0, '$kPath1\n$kPath2', null));
|
||||
final OperatingSystemUtils utils = new OperatingSystemUtils();
|
||||
final List<File> result = utils.whichAll(kExecutable);
|
||||
expect(result, hasLength(2));
|
||||
expect(result[0].path, kPath1);
|
||||
expect(result[1].path, kPath2);
|
||||
}, overrides: <Type, Generator>{
|
||||
ProcessManager: () => mockProcessManager,
|
||||
Platform: () => new FakePlatform(operatingSystem: 'linux')
|
||||
});
|
||||
});
|
||||
|
||||
group('which on Windows', () {
|
||||
|
||||
testUsingContext('returns null when executable does not exist', () async {
|
||||
when(mockProcessManager.runSync(<String>['where', kExecutable]))
|
||||
.thenReturn(new ProcessResult(0, 1, null, null));
|
||||
final OperatingSystemUtils utils = new OperatingSystemUtils();
|
||||
expect(utils.which(kExecutable), isNull);
|
||||
}, overrides: <Type, Generator>{
|
||||
ProcessManager: () => mockProcessManager,
|
||||
Platform: () => new FakePlatform(operatingSystem: 'windows')
|
||||
});
|
||||
|
||||
testUsingContext('returns exactly one result', () async {
|
||||
when(mockProcessManager.runSync(<String>['where', 'foo']))
|
||||
.thenReturn(new ProcessResult(0, 0, '$kPath1\n$kPath2', null));
|
||||
final OperatingSystemUtils utils = new OperatingSystemUtils();
|
||||
expect(utils.which(kExecutable).path, kPath1);
|
||||
}, overrides: <Type, Generator>{
|
||||
ProcessManager: () => mockProcessManager,
|
||||
Platform: () => new FakePlatform(operatingSystem: 'windows')
|
||||
});
|
||||
|
||||
testUsingContext('returns all results for whichAll', () async {
|
||||
when(mockProcessManager.runSync(<String>['where', kExecutable]))
|
||||
.thenReturn(new ProcessResult(0, 0, '$kPath1\n$kPath2', null));
|
||||
final OperatingSystemUtils utils = new OperatingSystemUtils();
|
||||
final List<File> result = utils.whichAll(kExecutable);
|
||||
expect(result, hasLength(2));
|
||||
expect(result[0].path, kPath1);
|
||||
expect(result[1].path, kPath2);
|
||||
}, overrides: <Type, Generator>{
|
||||
ProcessManager: () => mockProcessManager,
|
||||
Platform: () => new FakePlatform(operatingSystem: 'windows')
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
class MockProcessManager extends Mock implements ProcessManager {}
|
||||
@@ -1,41 +0,0 @@
|
||||
// Copyright 2017 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter_tools/src/base/process.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
void main() {
|
||||
group('shutdownHooks', () {
|
||||
test('runInExpectedOrder', () async {
|
||||
int i = 1;
|
||||
int serializeRecording1;
|
||||
int serializeRecording2;
|
||||
int postProcessRecording;
|
||||
int cleanup;
|
||||
|
||||
addShutdownHook(() async {
|
||||
serializeRecording1 = i++;
|
||||
}, ShutdownStage.SERIALIZE_RECORDING);
|
||||
|
||||
addShutdownHook(() async {
|
||||
cleanup = i++;
|
||||
}, ShutdownStage.CLEANUP);
|
||||
|
||||
addShutdownHook(() async {
|
||||
postProcessRecording = i++;
|
||||
}, ShutdownStage.POST_PROCESS_RECORDING);
|
||||
|
||||
addShutdownHook(() async {
|
||||
serializeRecording2 = i++;
|
||||
}, ShutdownStage.SERIALIZE_RECORDING);
|
||||
|
||||
await runShutdownHooks();
|
||||
|
||||
expect(serializeRecording1, lessThanOrEqualTo(2));
|
||||
expect(serializeRecording2, lessThanOrEqualTo(2));
|
||||
expect(postProcessRecording, 3);
|
||||
expect(cleanup, 4);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1,67 +0,0 @@
|
||||
// Copyright 2016 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:file/file.dart';
|
||||
import 'package:file/memory.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:test/test.dart';
|
||||
import 'package:platform/platform.dart';
|
||||
|
||||
import 'package:flutter_tools/src/cache.dart';
|
||||
|
||||
import 'context.dart';
|
||||
|
||||
void main() {
|
||||
group('$Cache.checkLockAcquired', () {
|
||||
setUp(() {
|
||||
Cache.enableLocking();
|
||||
});
|
||||
|
||||
tearDown(() {
|
||||
// Restore locking to prevent potential side-effects in
|
||||
// tests outside this group (this option is globally shared).
|
||||
Cache.enableLocking();
|
||||
});
|
||||
|
||||
test('should throw when locking is not acquired', () {
|
||||
expect(() => Cache.checkLockAcquired(), throwsStateError);
|
||||
});
|
||||
|
||||
test('should not throw when locking is disabled', () {
|
||||
Cache.disableLocking();
|
||||
Cache.checkLockAcquired();
|
||||
});
|
||||
|
||||
testUsingContext('should not throw when lock is acquired', () async {
|
||||
await Cache.lock();
|
||||
Cache.checkLockAcquired();
|
||||
}, overrides: <Type, Generator>{
|
||||
FileSystem: () => new MockFileSystem(),
|
||||
});
|
||||
|
||||
testUsingContext('should not throw when FLUTTER_ALREADY_LOCKED is set', () async {
|
||||
Cache.checkLockAcquired();
|
||||
}, overrides: <Type, Generator>{
|
||||
Platform: () => new FakePlatform()..environment = <String, String>{'FLUTTER_ALREADY_LOCKED': 'true'},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
class MockFileSystem extends MemoryFileSystem {
|
||||
@override
|
||||
File file(dynamic path) {
|
||||
return new MockFile();
|
||||
}
|
||||
}
|
||||
|
||||
class MockFile extends Mock implements File {
|
||||
@override
|
||||
Future<RandomAccessFile> open({FileMode mode: FileMode.READ}) async {
|
||||
return new MockRandomAccessFile();
|
||||
}
|
||||
}
|
||||
|
||||
class MockRandomAccessFile extends Mock implements RandomAccessFile {}
|
||||
@@ -1,106 +0,0 @@
|
||||
// Copyright 2017 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:io' show ProcessResult;
|
||||
|
||||
import 'package:file/file.dart';
|
||||
import 'package:flutter_tools/src/base/file_system.dart';
|
||||
import 'package:flutter_tools/src/ios/devices.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:platform/platform.dart';
|
||||
import 'package:process/process.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../context.dart';
|
||||
|
||||
class MockProcessManager extends Mock implements ProcessManager {}
|
||||
class MockFile extends Mock implements File {}
|
||||
|
||||
void main() {
|
||||
final FakePlatform osx = new FakePlatform.fromPlatform(const LocalPlatform());
|
||||
osx.operatingSystem = 'macos';
|
||||
|
||||
group('test screenshot', () {
|
||||
MockProcessManager mockProcessManager;
|
||||
MockFile mockOutputFile;
|
||||
IOSDevice iosDeviceUnderTest;
|
||||
|
||||
setUp(() {
|
||||
mockProcessManager = new MockProcessManager();
|
||||
mockOutputFile = new MockFile();
|
||||
});
|
||||
|
||||
testUsingContext(
|
||||
'screenshot without ideviceinstaller error',
|
||||
() async {
|
||||
when(mockOutputFile.path).thenReturn(fs.path.join('some', 'test', 'path', 'image.png'));
|
||||
// Let everything else return exit code 0 so process.dart doesn't crash.
|
||||
// The matcher order is important.
|
||||
when(
|
||||
mockProcessManager.run(any, environment: null, workingDirectory: null)
|
||||
).thenReturn(
|
||||
new Future<ProcessResult>.value(new ProcessResult(2, 0, '', ''))
|
||||
);
|
||||
// Let `which idevicescreenshot` fail with exit code 1.
|
||||
when(
|
||||
mockProcessManager.runSync(
|
||||
<String>['which', 'idevicescreenshot'], environment: null, workingDirectory: null)
|
||||
).thenReturn(
|
||||
new ProcessResult(1, 1, '', '')
|
||||
);
|
||||
|
||||
iosDeviceUnderTest = new IOSDevice('1234');
|
||||
await iosDeviceUnderTest.takeScreenshot(mockOutputFile);
|
||||
verify(mockProcessManager.runSync(
|
||||
<String>['which', 'idevicescreenshot'], environment: null, workingDirectory: null));
|
||||
verifyNever(mockProcessManager.run(
|
||||
<String>['idevicescreenshot', fs.path.join('some', 'test', 'path', 'image.png')],
|
||||
environment: null,
|
||||
workingDirectory: null
|
||||
));
|
||||
expect(testLogger.errorText, contains('brew install ideviceinstaller'));
|
||||
},
|
||||
overrides: <Type, Generator>{
|
||||
ProcessManager: () => mockProcessManager,
|
||||
Platform: () => osx,
|
||||
}
|
||||
);
|
||||
|
||||
testUsingContext(
|
||||
'screenshot with ideviceinstaller gets command',
|
||||
() async {
|
||||
when(mockOutputFile.path).thenReturn(fs.path.join('some', 'test', 'path', 'image.png'));
|
||||
// Let everything else return exit code 0.
|
||||
// The matcher order is important.
|
||||
when(
|
||||
mockProcessManager.run(any, environment: null, workingDirectory: null)
|
||||
).thenReturn(
|
||||
new Future<ProcessResult>.value(new ProcessResult(4, 0, '', ''))
|
||||
);
|
||||
// Let there be idevicescreenshot in the PATH.
|
||||
when(
|
||||
mockProcessManager.runSync(
|
||||
<String>['which', 'idevicescreenshot'], environment: null, workingDirectory: null)
|
||||
).thenReturn(
|
||||
new ProcessResult(3, 0, fs.path.join('some', 'path', 'to', 'iscreenshot'), '')
|
||||
);
|
||||
|
||||
iosDeviceUnderTest = new IOSDevice('1234');
|
||||
await iosDeviceUnderTest.takeScreenshot(mockOutputFile);
|
||||
verify(mockProcessManager.runSync(
|
||||
<String>['which', 'idevicescreenshot'], environment: null, workingDirectory: null));
|
||||
verify(mockProcessManager.run(
|
||||
<String>[
|
||||
fs.path.join('some', 'path', 'to', 'iscreenshot'),
|
||||
fs.path.join('some', 'test', 'path', 'image.png')
|
||||
],
|
||||
environment: null,
|
||||
workingDirectory: null
|
||||
));
|
||||
},
|
||||
overrides: <Type, Generator>{ProcessManager: () => mockProcessManager}
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -1,226 +0,0 @@
|
||||
// Copyright 2017 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:file/memory.dart';
|
||||
import 'package:flutter_tools/src/base/common.dart';
|
||||
import 'package:flutter_tools/src/base/file_system.dart';
|
||||
import 'package:flutter_tools/src/base/io.dart';
|
||||
import 'package:flutter_tools/src/doctor.dart';
|
||||
import 'package:flutter_tools/src/ios/ios_workflow.dart';
|
||||
import 'package:flutter_tools/src/ios/mac.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:process/process.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../context.dart';
|
||||
|
||||
void main() {
|
||||
group('iOS Workflow validation', () {
|
||||
MockXcode xcode;
|
||||
MockProcessManager processManager;
|
||||
FileSystem fs;
|
||||
|
||||
setUp(() {
|
||||
xcode = new MockXcode();
|
||||
processManager = new MockProcessManager();
|
||||
fs = new MemoryFileSystem();
|
||||
});
|
||||
|
||||
testUsingContext('Emit missing status when nothing is installed', () async {
|
||||
when(xcode.isInstalled).thenReturn(false);
|
||||
when(xcode.xcodeSelectPath).thenReturn(null);
|
||||
final IOSWorkflowTestTarget workflow = new IOSWorkflowTestTarget()
|
||||
..hasPythonSixModule = false
|
||||
..hasHomebrew = false
|
||||
..hasIosDeploy = false;
|
||||
final ValidationResult result = await workflow.validate();
|
||||
expect(result.type, ValidationType.missing);
|
||||
}, overrides: <Type, Generator>{ Xcode: () => xcode });
|
||||
|
||||
testUsingContext('Emits partial status when Xcode is not installed', () async {
|
||||
when(xcode.isInstalled).thenReturn(false);
|
||||
when(xcode.xcodeSelectPath).thenReturn(null);
|
||||
final IOSWorkflowTestTarget workflow = new IOSWorkflowTestTarget();
|
||||
final ValidationResult result = await workflow.validate();
|
||||
expect(result.type, ValidationType.partial);
|
||||
}, overrides: <Type, Generator>{ Xcode: () => xcode });
|
||||
|
||||
testUsingContext('Emits partial status when Xcode is partially installed', () async {
|
||||
when(xcode.isInstalled).thenReturn(false);
|
||||
when(xcode.xcodeSelectPath).thenReturn('/Library/Developer/CommandLineTools');
|
||||
final IOSWorkflowTestTarget workflow = new IOSWorkflowTestTarget();
|
||||
final ValidationResult result = await workflow.validate();
|
||||
expect(result.type, ValidationType.partial);
|
||||
}, overrides: <Type, Generator>{ Xcode: () => xcode });
|
||||
|
||||
testUsingContext('Emits partial status when Xcode version too low', () async {
|
||||
when(xcode.isInstalled).thenReturn(true);
|
||||
when(xcode.xcodeVersionText)
|
||||
.thenReturn('Xcode 7.0.1\nBuild version 7C1002\n');
|
||||
when(xcode.isInstalledAndMeetsVersionCheck).thenReturn(false);
|
||||
when(xcode.eulaSigned).thenReturn(true);
|
||||
final IOSWorkflowTestTarget workflow = new IOSWorkflowTestTarget();
|
||||
final ValidationResult result = await workflow.validate();
|
||||
expect(result.type, ValidationType.partial);
|
||||
}, overrides: <Type, Generator>{ Xcode: () => xcode });
|
||||
|
||||
testUsingContext('Emits partial status when Xcode EULA not signed', () async {
|
||||
when(xcode.isInstalled).thenReturn(true);
|
||||
when(xcode.xcodeVersionText)
|
||||
.thenReturn('Xcode 8.2.1\nBuild version 8C1002\n');
|
||||
when(xcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
|
||||
when(xcode.eulaSigned).thenReturn(false);
|
||||
final IOSWorkflowTestTarget workflow = new IOSWorkflowTestTarget();
|
||||
final ValidationResult result = await workflow.validate();
|
||||
expect(result.type, ValidationType.partial);
|
||||
}, overrides: <Type, Generator>{ Xcode: () => xcode });
|
||||
|
||||
testUsingContext('Emits partial status when python six not installed', () async {
|
||||
when(xcode.isInstalled).thenReturn(true);
|
||||
when(xcode.xcodeVersionText)
|
||||
.thenReturn('Xcode 8.2.1\nBuild version 8C1002\n');
|
||||
when(xcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
|
||||
when(xcode.eulaSigned).thenReturn(true);
|
||||
final IOSWorkflowTestTarget workflow = new IOSWorkflowTestTarget()
|
||||
..hasPythonSixModule = false;
|
||||
final ValidationResult result = await workflow.validate();
|
||||
expect(result.type, ValidationType.partial);
|
||||
}, overrides: <Type, Generator>{ Xcode: () => xcode });
|
||||
|
||||
testUsingContext('Emits partial status when homebrew not installed', () async {
|
||||
when(xcode.isInstalled).thenReturn(true);
|
||||
when(xcode.xcodeVersionText)
|
||||
.thenReturn('Xcode 8.2.1\nBuild version 8C1002\n');
|
||||
when(xcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
|
||||
when(xcode.eulaSigned).thenReturn(true);
|
||||
final IOSWorkflowTestTarget workflow = new IOSWorkflowTestTarget()
|
||||
..hasHomebrew = false;
|
||||
final ValidationResult result = await workflow.validate();
|
||||
expect(result.type, ValidationType.partial);
|
||||
}, overrides: <Type, Generator>{ Xcode: () => xcode });
|
||||
|
||||
testUsingContext('Emits partial status when ios-deploy is not installed', () async {
|
||||
when(xcode.isInstalled).thenReturn(true);
|
||||
when(xcode.xcodeVersionText)
|
||||
.thenReturn('Xcode 8.2.1\nBuild version 8C1002\n');
|
||||
when(xcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
|
||||
when(xcode.eulaSigned).thenReturn(true);
|
||||
final IOSWorkflowTestTarget workflow = new IOSWorkflowTestTarget()
|
||||
..hasIosDeploy = false;
|
||||
final ValidationResult result = await workflow.validate();
|
||||
expect(result.type, ValidationType.partial);
|
||||
}, overrides: <Type, Generator>{ Xcode: () => xcode });
|
||||
|
||||
testUsingContext('Emits partial status when ios-deploy version is too low', () async {
|
||||
when(xcode.isInstalled).thenReturn(true);
|
||||
when(xcode.xcodeVersionText)
|
||||
.thenReturn('Xcode 8.2.1\nBuild version 8C1002\n');
|
||||
when(xcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
|
||||
when(xcode.eulaSigned).thenReturn(true);
|
||||
final IOSWorkflowTestTarget workflow = new IOSWorkflowTestTarget()
|
||||
..iosDeployVersionText = '1.8.0';
|
||||
final ValidationResult result = await workflow.validate();
|
||||
expect(result.type, ValidationType.partial);
|
||||
}, overrides: <Type, Generator>{ Xcode: () => xcode });
|
||||
|
||||
testUsingContext('Emits partial status when CocoaPods is not installed', () async {
|
||||
when(xcode.isInstalled).thenReturn(true);
|
||||
when(xcode.xcodeVersionText)
|
||||
.thenReturn('Xcode 8.2.1\nBuild version 8C1002\n');
|
||||
when(xcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
|
||||
when(xcode.eulaSigned).thenReturn(true);
|
||||
final IOSWorkflowTestTarget workflow = new IOSWorkflowTestTarget()
|
||||
..hasCocoaPods = false;
|
||||
final ValidationResult result = await workflow.validate();
|
||||
expect(result.type, ValidationType.partial);
|
||||
}, overrides: <Type, Generator>{ Xcode: () => xcode });
|
||||
|
||||
testUsingContext('Emits partial status when CocoaPods version is too low', () async {
|
||||
when(xcode.isInstalled).thenReturn(true);
|
||||
when(xcode.xcodeVersionText)
|
||||
.thenReturn('Xcode 8.2.1\nBuild version 8C1002\n');
|
||||
when(xcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
|
||||
when(xcode.eulaSigned).thenReturn(true);
|
||||
final IOSWorkflowTestTarget workflow = new IOSWorkflowTestTarget()
|
||||
..cocoaPodsVersionText = '0.39.0';
|
||||
final ValidationResult result = await workflow.validate();
|
||||
expect(result.type, ValidationType.partial);
|
||||
}, overrides: <Type, Generator>{ Xcode: () => xcode });
|
||||
|
||||
testUsingContext('Emits partial status when CocoaPods is not initialized', () async {
|
||||
when(xcode.isInstalled).thenReturn(true);
|
||||
when(xcode.xcodeVersionText)
|
||||
.thenReturn('Xcode 8.2.1\nBuild version 8C1002\n');
|
||||
when(xcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
|
||||
when(xcode.eulaSigned).thenReturn(true);
|
||||
|
||||
when(processManager.runSync(argThat(contains('idevice_id'))))
|
||||
.thenReturn(exitsHappy);
|
||||
when(processManager.run(argThat(contains('idevice_id')), workingDirectory: any, environment: any))
|
||||
.thenReturn(exitsHappy);
|
||||
|
||||
final ValidationResult result = await new IOSWorkflowTestTarget().validate();
|
||||
expect(result.type, ValidationType.partial);
|
||||
}, overrides: <Type, Generator>{
|
||||
FileSystem: () => fs,
|
||||
Xcode: () => xcode,
|
||||
ProcessManager: () => processManager,
|
||||
});
|
||||
|
||||
testUsingContext('Succeeds when all checks pass', () async {
|
||||
when(xcode.isInstalled).thenReturn(true);
|
||||
when(xcode.xcodeVersionText)
|
||||
.thenReturn('Xcode 8.2.1\nBuild version 8C1002\n');
|
||||
when(xcode.isInstalledAndMeetsVersionCheck).thenReturn(true);
|
||||
when(xcode.eulaSigned).thenReturn(true);
|
||||
|
||||
when(processManager.runSync(argThat(contains('idevice_id'))))
|
||||
.thenReturn(exitsHappy);
|
||||
when(processManager.run(argThat(contains('idevice_id')), workingDirectory: any, environment: any))
|
||||
.thenReturn(exitsHappy);
|
||||
|
||||
ensureDirectoryExists(fs.path.join(homeDirPath, '.cocoapods', 'repos', 'master', 'README.md'));
|
||||
|
||||
final ValidationResult result = await new IOSWorkflowTestTarget().validate();
|
||||
expect(result.type, ValidationType.installed);
|
||||
}, overrides: <Type, Generator>{
|
||||
FileSystem: () => fs,
|
||||
Xcode: () => xcode,
|
||||
ProcessManager: () => processManager,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
final ProcessResult exitsHappy = new ProcessResult(
|
||||
1, // pid
|
||||
0, // exitCode
|
||||
'', // stdout
|
||||
'', // stderr
|
||||
);
|
||||
|
||||
class MockXcode extends Mock implements Xcode {}
|
||||
class MockProcessManager extends Mock implements ProcessManager {}
|
||||
|
||||
class IOSWorkflowTestTarget extends IOSWorkflow {
|
||||
@override
|
||||
bool hasPythonSixModule = true;
|
||||
|
||||
@override
|
||||
bool hasHomebrew = true;
|
||||
|
||||
@override
|
||||
bool hasIosDeploy = true;
|
||||
|
||||
@override
|
||||
String iosDeployVersionText = '1.9.0';
|
||||
|
||||
@override
|
||||
bool get hasIDeviceInstaller => true;
|
||||
|
||||
@override
|
||||
bool hasCocoaPods = true;
|
||||
|
||||
@override
|
||||
String cocoaPodsVersionText = '1.2.0';
|
||||
}
|
||||
@@ -1,180 +0,0 @@
|
||||
import 'dart:async';
|
||||
import 'dart:io' show ProcessResult;
|
||||
|
||||
import 'package:file/file.dart';
|
||||
import 'package:flutter_tools/src/base/file_system.dart';
|
||||
import 'package:flutter_tools/src/ios/mac.dart';
|
||||
import 'package:flutter_tools/src/ios/simulators.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:platform/platform.dart';
|
||||
import 'package:process/process.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../context.dart';
|
||||
|
||||
class MockXcode extends Mock implements Xcode {}
|
||||
class MockFile extends Mock implements File {}
|
||||
class MockProcessManager extends Mock implements ProcessManager {}
|
||||
|
||||
void main() {
|
||||
final FakePlatform osx = new FakePlatform.fromPlatform(const LocalPlatform());
|
||||
osx.operatingSystem = 'macos';
|
||||
|
||||
group('compareIosVersions', () {
|
||||
test('compares correctly', () {
|
||||
// This list must be sorted in ascending preference order
|
||||
final List<String> testList = <String>[
|
||||
'8', '8.0', '8.1', '8.2',
|
||||
'9', '9.0', '9.1', '9.2',
|
||||
'10', '10.0', '10.1',
|
||||
];
|
||||
|
||||
for (int i = 0; i < testList.length; i++) {
|
||||
expect(compareIosVersions(testList[i], testList[i]), 0);
|
||||
}
|
||||
|
||||
for (int i = 0; i < testList.length - 1; i++) {
|
||||
for (int j = i + 1; j < testList.length; j++) {
|
||||
expect(compareIosVersions(testList[i], testList[j]), lessThan(0));
|
||||
expect(compareIosVersions(testList[j], testList[i]), greaterThan(0));
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
group('compareIphoneVersions', () {
|
||||
test('compares correctly', () {
|
||||
// This list must be sorted in ascending preference order
|
||||
final List<String> testList = <String>[
|
||||
'com.apple.CoreSimulator.SimDeviceType.iPhone-4s',
|
||||
'com.apple.CoreSimulator.SimDeviceType.iPhone-5',
|
||||
'com.apple.CoreSimulator.SimDeviceType.iPhone-5s',
|
||||
'com.apple.CoreSimulator.SimDeviceType.iPhone-6strange',
|
||||
'com.apple.CoreSimulator.SimDeviceType.iPhone-6-Plus',
|
||||
'com.apple.CoreSimulator.SimDeviceType.iPhone-6',
|
||||
'com.apple.CoreSimulator.SimDeviceType.iPhone-6s-Plus',
|
||||
'com.apple.CoreSimulator.SimDeviceType.iPhone-6s',
|
||||
];
|
||||
|
||||
for (int i = 0; i < testList.length; i++) {
|
||||
expect(compareIphoneVersions(testList[i], testList[i]), 0);
|
||||
}
|
||||
|
||||
for (int i = 0; i < testList.length - 1; i++) {
|
||||
for (int j = i + 1; j < testList.length; j++) {
|
||||
expect(compareIphoneVersions(testList[i], testList[j]), lessThan(0));
|
||||
expect(compareIphoneVersions(testList[j], testList[i]), greaterThan(0));
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
group('IOSSimulator.isSupported', () {
|
||||
testUsingContext('Apple TV is unsupported', () {
|
||||
expect(new IOSSimulator('x', name: 'Apple TV').isSupported(), false);
|
||||
}, overrides: <Type, Generator>{
|
||||
Platform: () => osx,
|
||||
});
|
||||
|
||||
testUsingContext('Apple Watch is unsupported', () {
|
||||
expect(new IOSSimulator('x', name: 'Apple Watch').isSupported(), false);
|
||||
}, overrides: <Type, Generator>{
|
||||
Platform: () => osx,
|
||||
});
|
||||
|
||||
testUsingContext('iPad 2 is unsupported', () {
|
||||
expect(new IOSSimulator('x', name: 'iPad 2').isSupported(), false);
|
||||
}, overrides: <Type, Generator>{
|
||||
Platform: () => osx,
|
||||
});
|
||||
|
||||
testUsingContext('iPad Retina is unsupported', () {
|
||||
expect(new IOSSimulator('x', name: 'iPad Retina').isSupported(), false);
|
||||
}, overrides: <Type, Generator>{
|
||||
Platform: () => osx,
|
||||
});
|
||||
|
||||
testUsingContext('iPhone 5 is unsupported', () {
|
||||
expect(new IOSSimulator('x', name: 'iPhone 5').isSupported(), false);
|
||||
}, overrides: <Type, Generator>{
|
||||
Platform: () => osx,
|
||||
});
|
||||
|
||||
testUsingContext('iPhone 5s is supported', () {
|
||||
expect(new IOSSimulator('x', name: 'iPhone 5s').isSupported(), true);
|
||||
}, overrides: <Type, Generator>{
|
||||
Platform: () => osx,
|
||||
});
|
||||
|
||||
testUsingContext('iPhone SE is supported', () {
|
||||
expect(new IOSSimulator('x', name: 'iPhone SE').isSupported(), true);
|
||||
}, overrides: <Type, Generator>{
|
||||
Platform: () => osx,
|
||||
});
|
||||
|
||||
testUsingContext('iPhone 7 Plus is supported', () {
|
||||
expect(new IOSSimulator('x', name: 'iPhone 7 Plus').isSupported(), true);
|
||||
}, overrides: <Type, Generator>{
|
||||
Platform: () => osx,
|
||||
});
|
||||
});
|
||||
|
||||
group('Simulator screenshot', () {
|
||||
MockXcode mockXcode;
|
||||
MockProcessManager mockProcessManager;
|
||||
IOSSimulator deviceUnderTest;
|
||||
|
||||
setUp(() {
|
||||
mockXcode = new MockXcode();
|
||||
mockProcessManager = new MockProcessManager();
|
||||
// Let everything else return exit code 0 so process.dart doesn't crash.
|
||||
when(
|
||||
mockProcessManager.run(any, environment: null, workingDirectory: null)
|
||||
).thenReturn(
|
||||
new Future<ProcessResult>.value(new ProcessResult(2, 0, '', ''))
|
||||
);
|
||||
// Doesn't matter what the device is.
|
||||
deviceUnderTest = new IOSSimulator('x', name: 'iPhone SE');
|
||||
});
|
||||
|
||||
testUsingContext(
|
||||
'old Xcode doesn\'t support screenshot',
|
||||
() {
|
||||
when(mockXcode.xcodeMajorVersion).thenReturn(7);
|
||||
when(mockXcode.xcodeMinorVersion).thenReturn(1);
|
||||
expect(deviceUnderTest.supportsScreenshot, false);
|
||||
},
|
||||
overrides: <Type, Generator>{Xcode: () => mockXcode}
|
||||
);
|
||||
|
||||
testUsingContext(
|
||||
'Xcode 8.2+ supports screenshots',
|
||||
() async {
|
||||
when(mockXcode.xcodeMajorVersion).thenReturn(8);
|
||||
when(mockXcode.xcodeMinorVersion).thenReturn(2);
|
||||
expect(deviceUnderTest.supportsScreenshot, true);
|
||||
final MockFile mockFile = new MockFile();
|
||||
when(mockFile.path).thenReturn(fs.path.join('some', 'path', 'to', 'screenshot.png'));
|
||||
await deviceUnderTest.takeScreenshot(mockFile);
|
||||
verify(mockProcessManager.run(
|
||||
<String>[
|
||||
'/usr/bin/xcrun',
|
||||
'simctl',
|
||||
'io',
|
||||
'booted',
|
||||
'screenshot',
|
||||
fs.path.join('some', 'path', 'to', 'screenshot.png'),
|
||||
],
|
||||
environment: null,
|
||||
workingDirectory: null
|
||||
));
|
||||
},
|
||||
overrides: <Type, Generator>{
|
||||
ProcessManager: () => mockProcessManager,
|
||||
// Test a real one. Screenshot doesn't require instance states.
|
||||
SimControl: () => new SimControl(),
|
||||
Xcode: () => mockXcode,
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
// Copyright 2017 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:flutter_tools/src/version.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../common.dart';
|
||||
import '../context.dart';
|
||||
import 'flutter_command_test.dart';
|
||||
|
||||
void main() {
|
||||
group('FlutterCommandRunner', () {
|
||||
testUsingContext('checks that Flutter installation is up-to-date', () async {
|
||||
final MockFlutterVersion version = FlutterVersion.instance;
|
||||
bool versionChecked = false;
|
||||
when(version.checkFlutterVersionFreshness()).thenAnswer((_) async {
|
||||
versionChecked = true;
|
||||
});
|
||||
|
||||
await createTestCommandRunner(new DummyFlutterCommand(shouldUpdateCache: false))
|
||||
.run(<String>['dummy']);
|
||||
|
||||
expect(versionChecked, isTrue);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -1,180 +0,0 @@
|
||||
// Copyright 2017 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_tools/src/cache.dart';
|
||||
import 'package:flutter_tools/src/usage.dart';
|
||||
import 'package:flutter_tools/src/base/common.dart';
|
||||
import 'package:flutter_tools/src/runner/flutter_command.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:quiver/time.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../context.dart';
|
||||
|
||||
void main() {
|
||||
|
||||
group('Flutter Command', () {
|
||||
|
||||
MockCache cache;
|
||||
MockClock clock;
|
||||
MockUsage usage;
|
||||
List<int> mockTimes;
|
||||
|
||||
setUp(() {
|
||||
cache = new MockCache();
|
||||
clock = new MockClock();
|
||||
usage = new MockUsage();
|
||||
when(usage.isFirstRun).thenReturn(false);
|
||||
when(clock.now()).thenAnswer(
|
||||
(Invocation _) => new DateTime.fromMillisecondsSinceEpoch(mockTimes.removeAt(0))
|
||||
);
|
||||
});
|
||||
|
||||
testUsingContext('honors shouldUpdateCache false', () async {
|
||||
final DummyFlutterCommand flutterCommand = new DummyFlutterCommand(shouldUpdateCache: false);
|
||||
await flutterCommand.run();
|
||||
verifyZeroInteractions(cache);
|
||||
},
|
||||
overrides: <Type, Generator>{
|
||||
Cache: () => cache,
|
||||
});
|
||||
|
||||
testUsingContext('honors shouldUpdateCache true', () async {
|
||||
final DummyFlutterCommand flutterCommand = new DummyFlutterCommand(shouldUpdateCache: true);
|
||||
await flutterCommand.run();
|
||||
verify(cache.updateAll()).called(1);
|
||||
},
|
||||
overrides: <Type, Generator>{
|
||||
Cache: () => cache,
|
||||
});
|
||||
|
||||
testUsingContext('report execution timing by default', () async {
|
||||
// Crash if called a third time which is unexpected.
|
||||
mockTimes = <int>[1000, 2000];
|
||||
|
||||
final DummyFlutterCommand flutterCommand = new DummyFlutterCommand();
|
||||
await flutterCommand.run();
|
||||
verify(clock.now()).called(2);
|
||||
|
||||
expect(
|
||||
verify(usage.sendTiming(captureAny, captureAny, captureAny, label: captureAny)).captured,
|
||||
<dynamic>['flutter', 'dummy', const Duration(milliseconds: 1000), null]
|
||||
);
|
||||
},
|
||||
overrides: <Type, Generator>{
|
||||
Clock: () => clock,
|
||||
Usage: () => usage,
|
||||
});
|
||||
|
||||
testUsingContext('no timing report without usagePath', () async {
|
||||
// Crash if called a third time which is unexpected.
|
||||
mockTimes = <int>[1000, 2000];
|
||||
|
||||
final DummyFlutterCommand flutterCommand =
|
||||
new DummyFlutterCommand(noUsagePath: true);
|
||||
await flutterCommand.run();
|
||||
verify(clock.now()).called(2);
|
||||
verifyNever(usage.sendTiming(captureAny, captureAny, captureAny, label: captureAny));
|
||||
},
|
||||
overrides: <Type, Generator>{
|
||||
Clock: () => clock,
|
||||
Usage: () => usage,
|
||||
});
|
||||
|
||||
testUsingContext('report additional FlutterCommandResult data', () async {
|
||||
// Crash if called a third time which is unexpected.
|
||||
mockTimes = <int>[1000, 2000];
|
||||
|
||||
final FlutterCommandResult commandResult = new FlutterCommandResult(
|
||||
ExitStatus.success,
|
||||
// nulls should be cleaned up.
|
||||
analyticsParameters: <String> ['blah1', 'blah2', null, 'blah3'],
|
||||
endTimeOverride: new DateTime.fromMillisecondsSinceEpoch(1500)
|
||||
);
|
||||
|
||||
final DummyFlutterCommand flutterCommand = new DummyFlutterCommand(
|
||||
commandFunction: () async => commandResult
|
||||
);
|
||||
await flutterCommand.run();
|
||||
verify(clock.now()).called(2);
|
||||
expect(
|
||||
verify(usage.sendTiming(captureAny, captureAny, captureAny, label: captureAny)).captured,
|
||||
<dynamic>[
|
||||
'flutter',
|
||||
'dummy',
|
||||
const Duration(milliseconds: 500), // FlutterCommandResult's end time used instead.
|
||||
'success-blah1-blah2-blah3',
|
||||
],
|
||||
);
|
||||
},
|
||||
overrides: <Type, Generator>{
|
||||
Clock: () => clock,
|
||||
Usage: () => usage,
|
||||
});
|
||||
|
||||
testUsingContext('report failed execution timing too', () async {
|
||||
// Crash if called a third time which is unexpected.
|
||||
mockTimes = <int>[1000, 2000];
|
||||
|
||||
final DummyFlutterCommand flutterCommand =
|
||||
new DummyFlutterCommand(commandFunction: () async { throwToolExit('fail'); });
|
||||
|
||||
try {
|
||||
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(captureAny, captureAny, captureAny, label: captureAny)).captured,
|
||||
<dynamic>['flutter', 'dummy', const Duration(milliseconds: 1000), 'fail']
|
||||
);
|
||||
}
|
||||
},
|
||||
overrides: <Type, Generator>{
|
||||
Clock: () => clock,
|
||||
Usage: () => usage,
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
typedef Future<FlutterCommandResult> CommandFunction();
|
||||
|
||||
class DummyFlutterCommand extends FlutterCommand {
|
||||
|
||||
DummyFlutterCommand({
|
||||
this.shouldUpdateCache : false,
|
||||
this.noUsagePath : false,
|
||||
this.commandFunction,
|
||||
});
|
||||
|
||||
final bool noUsagePath;
|
||||
final CommandFunction commandFunction;
|
||||
|
||||
@override
|
||||
final bool shouldUpdateCache;
|
||||
|
||||
@override
|
||||
String get description => 'does nothing';
|
||||
|
||||
@override
|
||||
Future<String> get usagePath => noUsagePath ? null : super.usagePath;
|
||||
|
||||
@override
|
||||
String get name => 'dummy';
|
||||
|
||||
@override
|
||||
Future<FlutterCommandResult> runCommand() async {
|
||||
return commandFunction == null ? null : commandFunction();
|
||||
}
|
||||
}
|
||||
|
||||
class MockCache extends Mock implements Cache {}
|
||||
|
||||
class MockUsage extends Mock implements Usage {}
|
||||
@@ -1,325 +0,0 @@
|
||||
// Copyright 2017 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:process/process.dart';
|
||||
import 'package:quiver/time.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import 'package:flutter_tools/src/base/context.dart';
|
||||
import 'package:flutter_tools/src/base/io.dart';
|
||||
import 'package:flutter_tools/src/base/logger.dart';
|
||||
import 'package:flutter_tools/src/cache.dart';
|
||||
import 'package:flutter_tools/src/version.dart';
|
||||
|
||||
import 'context.dart';
|
||||
|
||||
const JsonEncoder _kPrettyJsonEncoder = const JsonEncoder.withIndent(' ');
|
||||
final Clock _testClock = new Clock.fixed(new DateTime(2015, 1, 1));
|
||||
final DateTime _upToDateVersion = _testClock.agoBy(FlutterVersion.kVersionAgeConsideredUpToDate ~/ 2);
|
||||
final DateTime _outOfDateVersion = _testClock.agoBy(FlutterVersion.kVersionAgeConsideredUpToDate * 2);
|
||||
final DateTime _stampUpToDate = _testClock.agoBy(FlutterVersion.kCheckAgeConsideredUpToDate ~/ 2);
|
||||
final DateTime _stampOutOfDate = _testClock.agoBy(FlutterVersion.kCheckAgeConsideredUpToDate * 2);
|
||||
|
||||
void main() {
|
||||
group('$FlutterVersion', () {
|
||||
setUpAll(() {
|
||||
Cache.disableLocking();
|
||||
FlutterVersion.kPauseToLetUserReadTheMessage = Duration.ZERO;
|
||||
});
|
||||
|
||||
testFlutterVersion('prints nothing when Flutter installation looks fresh', () async {
|
||||
fakeData(localCommitDate: _upToDateVersion);
|
||||
await FlutterVersion.instance.checkFlutterVersionFreshness();
|
||||
_expectVersionMessage('');
|
||||
});
|
||||
|
||||
testFlutterVersion('prints nothing when Flutter installation looks out-of-date by is actually up-to-date', () async {
|
||||
final FlutterVersion version = FlutterVersion.instance;
|
||||
|
||||
fakeData(
|
||||
localCommitDate: _outOfDateVersion,
|
||||
stamp: new VersionCheckStamp(
|
||||
lastTimeVersionWasChecked: _stampOutOfDate,
|
||||
lastKnownRemoteVersion: _outOfDateVersion,
|
||||
),
|
||||
remoteCommitDate: _outOfDateVersion,
|
||||
expectSetStamp: true,
|
||||
expectServerPing: true,
|
||||
);
|
||||
|
||||
await version.checkFlutterVersionFreshness();
|
||||
_expectVersionMessage('');
|
||||
});
|
||||
|
||||
testFlutterVersion('does not ping server when version stamp is up-to-date', () async {
|
||||
final FlutterVersion version = FlutterVersion.instance;
|
||||
|
||||
fakeData(
|
||||
localCommitDate: _outOfDateVersion,
|
||||
stamp: new VersionCheckStamp(
|
||||
lastTimeVersionWasChecked: _stampUpToDate,
|
||||
lastKnownRemoteVersion: _upToDateVersion,
|
||||
),
|
||||
expectSetStamp: true,
|
||||
);
|
||||
|
||||
await version.checkFlutterVersionFreshness();
|
||||
_expectVersionMessage(FlutterVersion.versionOutOfDateMessage(_testClock.now().difference(_outOfDateVersion)));
|
||||
});
|
||||
|
||||
testFlutterVersion('does not print warning if printed recently', () async {
|
||||
final FlutterVersion version = FlutterVersion.instance;
|
||||
|
||||
fakeData(
|
||||
localCommitDate: _outOfDateVersion,
|
||||
stamp: new VersionCheckStamp(
|
||||
lastTimeVersionWasChecked: _stampUpToDate,
|
||||
lastKnownRemoteVersion: _upToDateVersion,
|
||||
),
|
||||
expectSetStamp: true,
|
||||
);
|
||||
|
||||
await version.checkFlutterVersionFreshness();
|
||||
_expectVersionMessage(FlutterVersion.versionOutOfDateMessage(_testClock.now().difference(_outOfDateVersion)));
|
||||
expect((await VersionCheckStamp.load()).lastTimeWarningWasPrinted, _testClock.now());
|
||||
|
||||
await version.checkFlutterVersionFreshness();
|
||||
_expectVersionMessage('');
|
||||
});
|
||||
|
||||
testFlutterVersion('pings server when version stamp is missing then does not', () async {
|
||||
final FlutterVersion version = FlutterVersion.instance;
|
||||
|
||||
fakeData(
|
||||
localCommitDate: _outOfDateVersion,
|
||||
remoteCommitDate: _upToDateVersion,
|
||||
expectSetStamp: true,
|
||||
expectServerPing: true,
|
||||
);
|
||||
|
||||
await version.checkFlutterVersionFreshness();
|
||||
_expectVersionMessage(FlutterVersion.versionOutOfDateMessage(_testClock.now().difference(_outOfDateVersion)));
|
||||
|
||||
// Immediate subsequent check is not expected to ping the server.
|
||||
fakeData(
|
||||
localCommitDate: _outOfDateVersion,
|
||||
stamp: await VersionCheckStamp.load(),
|
||||
);
|
||||
await version.checkFlutterVersionFreshness();
|
||||
_expectVersionMessage('');
|
||||
});
|
||||
|
||||
testFlutterVersion('pings server when version stamp is out-of-date', () async {
|
||||
final FlutterVersion version = FlutterVersion.instance;
|
||||
|
||||
fakeData(
|
||||
localCommitDate: _outOfDateVersion,
|
||||
stamp: new VersionCheckStamp(
|
||||
lastTimeVersionWasChecked: _stampOutOfDate,
|
||||
lastKnownRemoteVersion: _testClock.ago(days: 2),
|
||||
),
|
||||
remoteCommitDate: _upToDateVersion,
|
||||
expectSetStamp: true,
|
||||
expectServerPing: true,
|
||||
);
|
||||
|
||||
await version.checkFlutterVersionFreshness();
|
||||
_expectVersionMessage(FlutterVersion.versionOutOfDateMessage(_testClock.now().difference(_outOfDateVersion)));
|
||||
});
|
||||
|
||||
testFlutterVersion('ignores network issues', () async {
|
||||
final FlutterVersion version = FlutterVersion.instance;
|
||||
|
||||
fakeData(
|
||||
localCommitDate: _outOfDateVersion,
|
||||
errorOnFetch: true,
|
||||
expectServerPing: true,
|
||||
);
|
||||
|
||||
await version.checkFlutterVersionFreshness();
|
||||
_expectVersionMessage('');
|
||||
});
|
||||
});
|
||||
|
||||
group('$VersionCheckStamp', () {
|
||||
void _expectDefault(VersionCheckStamp stamp) {
|
||||
expect(stamp.lastKnownRemoteVersion, isNull);
|
||||
expect(stamp.lastTimeVersionWasChecked, isNull);
|
||||
expect(stamp.lastTimeWarningWasPrinted, isNull);
|
||||
}
|
||||
|
||||
testFlutterVersion('loads blank when stamp file missing', () async {
|
||||
fakeData();
|
||||
_expectDefault(await VersionCheckStamp.load());
|
||||
});
|
||||
|
||||
testFlutterVersion('loads blank when stamp file is malformed JSON', () async {
|
||||
fakeData(stampJson: '<');
|
||||
_expectDefault(await VersionCheckStamp.load());
|
||||
});
|
||||
|
||||
testFlutterVersion('loads blank when stamp file is well-formed but invalid JSON', () async {
|
||||
fakeData(stampJson: '[]');
|
||||
_expectDefault(await VersionCheckStamp.load());
|
||||
});
|
||||
|
||||
testFlutterVersion('loads valid JSON', () async {
|
||||
fakeData(stampJson: '''
|
||||
{
|
||||
"lastKnownRemoteVersion": "${_testClock.ago(days: 1)}",
|
||||
"lastTimeVersionWasChecked": "${_testClock.ago(days: 2)}",
|
||||
"lastTimeWarningWasPrinted": "${_testClock.now()}"
|
||||
}
|
||||
''');
|
||||
|
||||
final VersionCheckStamp stamp = await VersionCheckStamp.load();
|
||||
expect(stamp.lastKnownRemoteVersion, _testClock.ago(days: 1));
|
||||
expect(stamp.lastTimeVersionWasChecked, _testClock.ago(days: 2));
|
||||
expect(stamp.lastTimeWarningWasPrinted, _testClock.now());
|
||||
});
|
||||
|
||||
testFlutterVersion('stores version stamp', () async {
|
||||
fakeData(expectSetStamp: true);
|
||||
|
||||
_expectDefault(await VersionCheckStamp.load());
|
||||
|
||||
final VersionCheckStamp stamp = new VersionCheckStamp(
|
||||
lastKnownRemoteVersion: _testClock.ago(days: 1),
|
||||
lastTimeVersionWasChecked: _testClock.ago(days: 2),
|
||||
lastTimeWarningWasPrinted: _testClock.now(),
|
||||
);
|
||||
await stamp.store();
|
||||
|
||||
final VersionCheckStamp storedStamp = await VersionCheckStamp.load();
|
||||
expect(storedStamp.lastKnownRemoteVersion, _testClock.ago(days: 1));
|
||||
expect(storedStamp.lastTimeVersionWasChecked, _testClock.ago(days: 2));
|
||||
expect(storedStamp.lastTimeWarningWasPrinted, _testClock.now());
|
||||
});
|
||||
|
||||
testFlutterVersion('overwrites individual fields', () async {
|
||||
fakeData(expectSetStamp: true);
|
||||
|
||||
_expectDefault(await VersionCheckStamp.load());
|
||||
|
||||
final VersionCheckStamp stamp = new VersionCheckStamp(
|
||||
lastKnownRemoteVersion: _testClock.ago(days: 10),
|
||||
lastTimeVersionWasChecked: _testClock.ago(days: 9),
|
||||
lastTimeWarningWasPrinted: _testClock.ago(days: 8),
|
||||
);
|
||||
await stamp.store(
|
||||
newKnownRemoteVersion: _testClock.ago(days: 1),
|
||||
newTimeVersionWasChecked: _testClock.ago(days: 2),
|
||||
newTimeWarningWasPrinted: _testClock.now(),
|
||||
);
|
||||
|
||||
final VersionCheckStamp storedStamp = await VersionCheckStamp.load();
|
||||
expect(storedStamp.lastKnownRemoteVersion, _testClock.ago(days: 1));
|
||||
expect(storedStamp.lastTimeVersionWasChecked, _testClock.ago(days: 2));
|
||||
expect(storedStamp.lastTimeWarningWasPrinted, _testClock.now());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void _expectVersionMessage(String message) {
|
||||
final BufferLogger logger = context[Logger];
|
||||
expect(logger.statusText.trim(), message.trim());
|
||||
logger.clear();
|
||||
}
|
||||
|
||||
void testFlutterVersion(String description, dynamic testMethod()) {
|
||||
testUsingContext(
|
||||
description,
|
||||
testMethod,
|
||||
overrides: <Type, Generator>{
|
||||
FlutterVersion: () => new FlutterVersion(_testClock),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void fakeData({
|
||||
DateTime localCommitDate,
|
||||
DateTime remoteCommitDate,
|
||||
VersionCheckStamp stamp,
|
||||
String stampJson,
|
||||
bool errorOnFetch: false,
|
||||
bool expectSetStamp: false,
|
||||
bool expectServerPing: false,
|
||||
}) {
|
||||
final MockProcessManager pm = new MockProcessManager();
|
||||
context.setVariable(ProcessManager, pm);
|
||||
|
||||
final MockCache cache = new MockCache();
|
||||
context.setVariable(Cache, cache);
|
||||
|
||||
ProcessResult success(String standardOutput) {
|
||||
return new ProcessResult(1, 0, standardOutput, '');
|
||||
}
|
||||
|
||||
ProcessResult failure(int exitCode) {
|
||||
return new ProcessResult(1, exitCode, '', 'error');
|
||||
}
|
||||
|
||||
when(cache.getStampFor(any)).thenAnswer((Invocation invocation) {
|
||||
expect(invocation.positionalArguments.single, VersionCheckStamp.kFlutterVersionCheckStampFile);
|
||||
|
||||
if (stampJson != null)
|
||||
return stampJson;
|
||||
|
||||
if (stamp != null)
|
||||
return JSON.encode(stamp.toJson());
|
||||
|
||||
return null;
|
||||
});
|
||||
|
||||
when(cache.setStampFor(any, any)).thenAnswer((Invocation invocation) {
|
||||
expect(invocation.positionalArguments.first, VersionCheckStamp.kFlutterVersionCheckStampFile);
|
||||
|
||||
if (expectSetStamp) {
|
||||
stamp = VersionCheckStamp.fromJson(JSON.decode(invocation.positionalArguments[1]));
|
||||
return null;
|
||||
}
|
||||
|
||||
throw new StateError('Unexpected call to Cache.setStampFor(${invocation.positionalArguments}, ${invocation.namedArguments})');
|
||||
});
|
||||
|
||||
final Answering syncAnswer = (Invocation invocation) {
|
||||
bool argsAre(String a1, [String a2, String a3, String a4, String a5, String a6, String a7, String a8]) {
|
||||
const ListEquality<String> equality = const ListEquality<String>();
|
||||
final List<String> args = invocation.positionalArguments.single;
|
||||
final List<String> expectedArgs =
|
||||
<String>[a1, a2, a3, a4, a5, a6, a7, a8]
|
||||
.where((String arg) => arg != null)
|
||||
.toList();
|
||||
return equality.equals(args, expectedArgs);
|
||||
}
|
||||
|
||||
if (argsAre('git', 'log', '-n', '1', '--pretty=format:%ad', '--date=iso')) {
|
||||
return success(localCommitDate.toString());
|
||||
} else if (argsAre('git', 'remote')) {
|
||||
return success('');
|
||||
} else if (argsAre('git', 'remote', 'add', '__flutter_version_check__', 'https://github.com/flutter/flutter.git')) {
|
||||
return success('');
|
||||
} else if (argsAre('git', 'fetch', '__flutter_version_check__', 'master')) {
|
||||
if (!expectServerPing)
|
||||
fail('Did not expect server ping');
|
||||
return errorOnFetch ? failure(128) : success('');
|
||||
} else if (remoteCommitDate != null && argsAre('git', 'log', '__flutter_version_check__/master', '-n', '1', '--pretty=format:%ad', '--date=iso')) {
|
||||
return success(remoteCommitDate.toString());
|
||||
}
|
||||
|
||||
throw new StateError('Unexpected call to ProcessManager.run(${invocation.positionalArguments}, ${invocation.namedArguments})');
|
||||
};
|
||||
|
||||
when(pm.runSync(any, workingDirectory: any)).thenAnswer(syncAnswer);
|
||||
when(pm.run(any, workingDirectory: any)).thenAnswer((Invocation invocation) async {
|
||||
return syncAnswer(invocation);
|
||||
});
|
||||
}
|
||||
|
||||
class MockProcessManager extends Mock implements ProcessManager {}
|
||||
class MockCache extends Mock implements Cache {}
|
||||
Reference in New Issue
Block a user