forked from firka/flutter
Make it possible to override the FLUTTER_TEST env variable (#34301)
* Make it possible to override the FLUTTER_TEST env variable without unsetting it. * Switch to using platform instead of Platform. * Document the bindings, and introduce tests that initialize multiple WidgetsBindings with different environments. * Add tests for the flutter platform test. * Add license headers * Fix lints * Remove trailing whitespace * Respond to Jonahs comments * Respond to Ians comments * Mock out the HttpServer in flutter_platform_test * Mock out the HttpServer in flutter_platform_test * Explain why we mock out the HttpServer in flutter_platform_test
This commit is contained in:
@@ -4,9 +4,12 @@
|
||||
|
||||
import 'package:flutter_tools/src/base/common.dart';
|
||||
import 'package:flutter_tools/src/base/io.dart';
|
||||
import 'package:flutter_tools/src/base/platform.dart';
|
||||
import 'package:flutter_tools/src/test/flutter_platform.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:process/process.dart';
|
||||
import 'package:test_core/backend.dart';
|
||||
|
||||
import 'src/common.dart';
|
||||
@@ -15,15 +18,70 @@ import 'src/context.dart';
|
||||
void main() {
|
||||
group('FlutterPlatform', () {
|
||||
testUsingContext('ensureConfiguration throws an error if an explicitObservatoryPort is specified and more than one test file', () async {
|
||||
final FlutterPlatform flutterPlatfrom = FlutterPlatform(shellPath: '/', explicitObservatoryPort: 1234);
|
||||
flutterPlatfrom.loadChannel('test1.dart', MockPlatform());
|
||||
expect(() => flutterPlatfrom.loadChannel('test2.dart', MockPlatform()), throwsA(isA<ToolExit>()));
|
||||
final FlutterPlatform flutterPlatform = FlutterPlatform(shellPath: '/', explicitObservatoryPort: 1234);
|
||||
flutterPlatform.loadChannel('test1.dart', MockSuitePlatform());
|
||||
expect(() => flutterPlatform.loadChannel('test2.dart', MockSuitePlatform()), throwsA(isA<ToolExit>()));
|
||||
});
|
||||
|
||||
testUsingContext('ensureConfiguration throws an error if a precompiled entrypoint is specified and more that one test file', () {
|
||||
final FlutterPlatform flutterPlatfrom = FlutterPlatform(shellPath: '/', precompiledDillPath: 'example.dill');
|
||||
flutterPlatfrom.loadChannel('test1.dart', MockPlatform());
|
||||
expect(() => flutterPlatfrom.loadChannel('test2.dart', MockPlatform()), throwsA(isA<ToolExit>()));
|
||||
final FlutterPlatform flutterPlatform = FlutterPlatform(shellPath: '/', precompiledDillPath: 'example.dill');
|
||||
flutterPlatform.loadChannel('test1.dart', MockSuitePlatform());
|
||||
expect(() => flutterPlatform.loadChannel('test2.dart', MockSuitePlatform()), throwsA(isA<ToolExit>()));
|
||||
});
|
||||
|
||||
group('The FLUTTER_TEST environment variable is passed to the test process', () {
|
||||
MockPlatform mockPlatform;
|
||||
MockProcessManager mockProcessManager;
|
||||
FlutterPlatform flutterPlatform;
|
||||
final Map<Type, Generator> contextOverrides = <Type, Generator>{
|
||||
Platform: () => mockPlatform,
|
||||
ProcessManager: () => mockProcessManager,
|
||||
};
|
||||
|
||||
setUp(() {
|
||||
mockPlatform = MockPlatform();
|
||||
mockProcessManager = MockProcessManager();
|
||||
flutterPlatform = TestFlutterPlatform();
|
||||
});
|
||||
|
||||
Future<Map<String, String>> captureEnvironment() async {
|
||||
flutterPlatform.loadChannel('test1.dart', MockSuitePlatform());
|
||||
await untilCalled(mockProcessManager.start(any, environment: anyNamed('environment')));
|
||||
final VerificationResult toVerify = verify(mockProcessManager.start(any, environment: captureAnyNamed('environment')));
|
||||
expect(toVerify.captured, hasLength(1));
|
||||
expect(toVerify.captured.first, isInstanceOf<Map<String, String>>());
|
||||
return toVerify.captured.first;
|
||||
}
|
||||
|
||||
testUsingContext('as true when not originally set', () async {
|
||||
when(mockPlatform.environment).thenReturn(<String, String>{});
|
||||
final Map<String, String> capturedEnvironment = await captureEnvironment();
|
||||
expect(capturedEnvironment['FLUTTER_TEST'], 'true');
|
||||
}, overrides: contextOverrides);
|
||||
|
||||
testUsingContext('as true when set to true', () async {
|
||||
when(mockPlatform.environment).thenReturn(<String, String>{'FLUTTER_TEST': 'true'});
|
||||
final Map<String, String> capturedEnvironment = await captureEnvironment();
|
||||
expect(capturedEnvironment['FLUTTER_TEST'], 'true');
|
||||
}, overrides: contextOverrides);
|
||||
|
||||
testUsingContext('as false when set to false', () async {
|
||||
when(mockPlatform.environment).thenReturn(<String, String>{'FLUTTER_TEST': 'false'});
|
||||
final Map<String, String> capturedEnvironment = await captureEnvironment();
|
||||
expect(capturedEnvironment['FLUTTER_TEST'], 'false');
|
||||
}, overrides: contextOverrides);
|
||||
|
||||
testUsingContext('unchanged when set', () async {
|
||||
when(mockPlatform.environment).thenReturn(<String, String>{'FLUTTER_TEST': 'neither true nor false'});
|
||||
final Map<String, String> capturedEnvironment = await captureEnvironment();
|
||||
expect(capturedEnvironment['FLUTTER_TEST'], 'neither true nor false');
|
||||
}, overrides: contextOverrides);
|
||||
|
||||
testUsingContext('as null when set to null', () async {
|
||||
when(mockPlatform.environment).thenReturn(<String, String>{'FLUTTER_TEST': null});
|
||||
final Map<String, String> capturedEnvironment = await captureEnvironment();
|
||||
expect(capturedEnvironment['FLUTTER_TEST'], null);
|
||||
}, overrides: contextOverrides);
|
||||
});
|
||||
|
||||
testUsingContext('installHook creates a FlutterPlatform', () {
|
||||
@@ -80,4 +138,30 @@ void main() {
|
||||
});
|
||||
}
|
||||
|
||||
class MockPlatform extends Mock implements SuitePlatform {}
|
||||
class MockSuitePlatform extends Mock implements SuitePlatform {}
|
||||
|
||||
class MockProcessManager extends Mock implements ProcessManager {}
|
||||
|
||||
class MockPlatform extends Mock implements Platform {}
|
||||
|
||||
class MockHttpServer extends Mock implements HttpServer {}
|
||||
|
||||
// A FlutterPlatform with enough fields set to load and start a test.
|
||||
//
|
||||
// Uses a mock HttpServer. We don't want to bind random ports in our CI hosts.
|
||||
class TestFlutterPlatform extends FlutterPlatform {
|
||||
TestFlutterPlatform() : super(
|
||||
shellPath: '/',
|
||||
precompiledDillPath: 'example.dill',
|
||||
host: InternetAddress.loopbackIPv6,
|
||||
port: 0,
|
||||
updateGoldens: false,
|
||||
startPaused: false,
|
||||
enableObservatory: false,
|
||||
buildTestAssets: false,
|
||||
);
|
||||
|
||||
@override
|
||||
@protected
|
||||
Future<HttpServer> bind(InternetAddress host, int port) async => MockHttpServer();
|
||||
}
|
||||
Reference in New Issue
Block a user