Skip to content

Commit

Permalink
Replace MockConfig with TestConfig (#75044)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmagman committed Feb 2, 2021
1 parent d59359f commit 73506f3
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 75 deletions.
16 changes: 11 additions & 5 deletions packages/flutter_tools/lib/src/base/config.dart
Expand Up @@ -4,6 +4,7 @@

// @dart = 2.8

import 'package:file/memory.dart';
import 'package:meta/meta.dart';

import '../convert.dart';
Expand Down Expand Up @@ -38,11 +39,16 @@ class Config {

/// Constructs a new [Config] object from a file called [name] in
/// the given [Directory].
factory Config.test(
String name, {
@required Directory directory,
@required Logger logger,
}) => Config.createForTesting(directory.childFile('.${kConfigDir}_$name'), logger);
///
/// Defaults to [BufferLogger], [MemoryFileSystem], and [name]=test.
factory Config.test({
String name = 'test',
Directory directory,
Logger logger,
}) {
directory ??= MemoryFileSystem.test().directory('/');
return Config.createForTesting(directory.childFile('.${kConfigDir}_$name'), logger ?? BufferLogger.test());
}

/// Test only access to the Config constructor.
@visibleForTesting
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_tools/lib/src/persistent_tool_state.dart
Expand Up @@ -76,7 +76,7 @@ class _DefaultPersistentToolState implements PersistentToolState {
@required Directory directory,
@required Logger logger,
}) : _config = Config.test(
_kFileName,
name: _kFileName,
directory: directory,
logger: logger,
);
Expand Down
22 changes: 8 additions & 14 deletions packages/flutter_tools/test/general.shard/analytics_test.dart
Expand Up @@ -35,12 +35,12 @@ void main() {

group('analytics', () {
Directory tempDir;
MockFlutterConfig mockFlutterConfig;
Config testConfig;

setUp(() {
Cache.flutterRoot = '../..';
tempDir = globals.fs.systemTempDirectory.createTempSync('flutter_tools_analytics_test.');
mockFlutterConfig = MockFlutterConfig();
testConfig = Config.test();
});

tearDown(() {
Expand Down Expand Up @@ -105,8 +105,7 @@ void main() {
});

testUsingContext('Usage records one feature in experiment setting', () async {
when<bool>(mockFlutterConfig.getValue(flutterWebFeature.configSetting) as bool)
.thenReturn(true);
testConfig.setValue(flutterWebFeature.configSetting, true);
final Usage usage = Usage(runningOnBot: true);
usage.sendCommand('test');

Expand All @@ -115,7 +114,7 @@ void main() {
expect(globals.fs.file('test').readAsStringSync(), contains('$featuresKey: enable-web'));
}, overrides: <Type, Generator>{
FlutterVersion: () => FlutterVersion(clock: const SystemClock()),
Config: () => mockFlutterConfig,
Config: () => testConfig,
Platform: () => FakePlatform(environment: <String, String>{
'FLUTTER_ANALYTICS_LOG_FILE': 'test',
}),
Expand All @@ -124,12 +123,9 @@ void main() {
});

testUsingContext('Usage records multiple features in experiment setting', () async {
when<bool>(mockFlutterConfig.getValue(flutterWebFeature.configSetting) as bool)
.thenReturn(true);
when<bool>(mockFlutterConfig.getValue(flutterLinuxDesktopFeature.configSetting) as bool)
.thenReturn(true);
when<bool>(mockFlutterConfig.getValue(flutterMacOSDesktopFeature.configSetting) as bool)
.thenReturn(true);
testConfig.setValue(flutterWebFeature.configSetting, true);
testConfig.setValue(flutterLinuxDesktopFeature.configSetting, true);
testConfig.setValue(flutterMacOSDesktopFeature.configSetting, true);
final Usage usage = Usage(runningOnBot: true);
usage.sendCommand('test');

Expand All @@ -141,7 +137,7 @@ void main() {
);
}, overrides: <Type, Generator>{
FlutterVersion: () => FlutterVersion(clock: const SystemClock()),
Config: () => mockFlutterConfig,
Config: () => testConfig,
Platform: () => FakePlatform(environment: <String, String>{
'FLUTTER_ANALYTICS_LOG_FILE': 'test',
}),
Expand Down Expand Up @@ -370,8 +366,6 @@ 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>[];

Expand Down
Expand Up @@ -9,7 +9,6 @@ import 'package:flutter_tools/src/android/android_sdk.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' show ProcessResult;
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:meta/meta.dart';
import 'package:mockito/mockito.dart';
Expand All @@ -29,11 +28,7 @@ void main() {
setUp(() {
fileSystem = MemoryFileSystem.test();
processManager = MockProcessManager();
config = Config.test(
'test',
directory: fileSystem.currentDirectory,
logger: BufferLogger.test(),
);
config = Config.test();
});

group('android_sdk AndroidSdk', () {
Expand Down
Expand Up @@ -9,7 +9,6 @@ import 'package:flutter_tools/src/android/android_studio_validator.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';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/base/user_messages.dart';
import 'package:flutter_tools/src/doctor.dart';
Expand All @@ -36,11 +35,7 @@ void main() {
});

testWithoutContext('NoAndroidStudioValidator shows Android Studio as "not available" when not available.', () async {
final Config config = Config.test(
'test',
directory: fileSystem.currentDirectory,
logger: BufferLogger.test(),
);
final Config config = Config.test();
final NoAndroidStudioValidator validator = NoAndroidStudioValidator(
config: config,
platform: linuxPlatform,
Expand Down
46 changes: 24 additions & 22 deletions packages/flutter_tools/test/general.shard/features_test.dart
Expand Up @@ -15,20 +15,23 @@ import '../src/common.dart';
void main() {
group('Features', () {
MockFlutterVerion mockFlutterVerion;
MockFlutterConfig mockFlutterConfig;
Config testConfig;
MockPlatform mockPlatform;
FlutterFeatureFlags featureFlags;

setUp(() {
mockFlutterVerion = MockFlutterVerion();
mockFlutterConfig = MockFlutterConfig();
testConfig = Config.test();
mockPlatform = MockPlatform();
when(mockPlatform.environment).thenReturn(<String, String>{});
when<bool>(mockFlutterConfig.getValue(any) as bool).thenReturn(false);

for (final Feature feature in allFeatures) {
testConfig.setValue(feature.configSetting, false);
}

featureFlags = FlutterFeatureFlags(
flutterVersion: mockFlutterVerion,
config: mockFlutterConfig,
config: testConfig,
platform: mockPlatform,
);
});
Expand Down Expand Up @@ -129,7 +132,7 @@ void main() {

testWithoutContext('flutter web enabled with config on master', () {
when(mockFlutterVerion.channel).thenReturn('master');
when<bool>(mockFlutterConfig.getValue('enable-web') as bool).thenReturn(true);
testConfig.setValue('enable-web', true);

expect(featureFlags.isWebEnabled, true);
});
Expand All @@ -149,7 +152,7 @@ void main() {

testWithoutContext('flutter web enabled with config on dev', () {
when(mockFlutterVerion.channel).thenReturn('dev');
when<bool>(mockFlutterConfig.getValue('enable-web') as bool).thenReturn(true);
testConfig.setValue('enable-web', true);

expect(featureFlags.isWebEnabled, true);
});
Expand All @@ -169,7 +172,7 @@ void main() {

testWithoutContext('flutter web enabled with config on beta', () {
when(mockFlutterVerion.channel).thenReturn('beta');
when<bool>(mockFlutterConfig.getValue('enable-web') as bool).thenReturn(true);
testConfig.setValue('enable-web', true);

expect(featureFlags.isWebEnabled, true);
});
Expand All @@ -183,14 +186,14 @@ void main() {

testWithoutContext('flutter web on by default on stable', () {
when(mockFlutterVerion.channel).thenReturn('stable');
when<bool>(mockFlutterConfig.getValue(any) as bool).thenReturn(null);
testConfig.removeValue('enable-web');

expect(featureFlags.isWebEnabled, true);
});

testWithoutContext('flutter web enabled with config on stable', () {
when(mockFlutterVerion.channel).thenReturn('stable');
when<bool>(mockFlutterConfig.getValue('enable-web') as bool).thenReturn(true);
testConfig.setValue('enable-web', true);

expect(featureFlags.isWebEnabled, true);
});
Expand All @@ -212,7 +215,7 @@ void main() {

testWithoutContext('flutter macos desktop enabled with config on master', () {
when(mockFlutterVerion.channel).thenReturn('master');
when<bool>(mockFlutterConfig.getValue('enable-macos-desktop') as bool).thenReturn(true);
testConfig.setValue('enable-macos-desktop', true);

expect(featureFlags.isMacOSEnabled, true);
});
Expand All @@ -232,7 +235,7 @@ void main() {

testWithoutContext('flutter macos desktop enabled with config on dev', () {
when(mockFlutterVerion.channel).thenReturn('dev');
when<bool>(mockFlutterConfig.getValue('enable-macos-desktop') as bool).thenReturn(true);
testConfig.setValue('enable-macos-desktop', true);

expect(featureFlags.isMacOSEnabled, true);
});
Expand All @@ -252,7 +255,7 @@ void main() {

testWithoutContext('flutter macos desktop enabled with config on beta', () {
when(mockFlutterVerion.channel).thenReturn('beta');
when<bool>(mockFlutterConfig.getValue('enable-macos-desktop') as bool).thenReturn(true);
testConfig.setValue('enable-macos-desktop', true);

expect(featureFlags.isMacOSEnabled, true);
});
Expand All @@ -272,7 +275,7 @@ void main() {

testWithoutContext('flutter macos desktop enabled with config on stable', () {
when(mockFlutterVerion.channel).thenReturn('stable');
when<bool>(mockFlutterConfig.getValue('enable-macos-desktop') as bool).thenReturn(true);
testConfig.setValue('enable-macos-desktop', true);

expect(featureFlags.isMacOSEnabled, true);
});
Expand All @@ -293,7 +296,7 @@ void main() {

testWithoutContext('flutter linux desktop enabled with config on master', () {
when(mockFlutterVerion.channel).thenReturn('master');
when<bool>(mockFlutterConfig.getValue('enable-linux-desktop') as bool).thenReturn(true);
testConfig.setValue('enable-linux-desktop', true);

expect(featureFlags.isLinuxEnabled, true);
});
Expand All @@ -313,7 +316,7 @@ void main() {

testWithoutContext('flutter linux desktop enabled with config on dev', () {
when(mockFlutterVerion.channel).thenReturn('dev');
when<bool>(mockFlutterConfig.getValue('enable-linux-desktop') as bool).thenReturn(true);
testConfig.setValue('enable-linux-desktop', true);

expect(featureFlags.isLinuxEnabled, true);
});
Expand All @@ -333,7 +336,7 @@ void main() {

testWithoutContext('flutter linux desktop enabled with config on beta', () {
when(mockFlutterVerion.channel).thenReturn('beta');
when<bool>(mockFlutterConfig.getValue('enable-linux-desktop') as bool).thenReturn(true);
testConfig.setValue('enable-linux-desktop', true);

expect(featureFlags.isLinuxEnabled, true);
});
Expand All @@ -353,7 +356,7 @@ void main() {

testWithoutContext('flutter linux desktop enabled with config on stable', () {
when(mockFlutterVerion.channel).thenReturn('stable');
when<bool>(mockFlutterConfig.getValue('enable-linux-desktop') as bool).thenReturn(true);
testConfig.setValue('enable-linux-desktop', true);

expect(featureFlags.isLinuxEnabled, true);
});
Expand All @@ -374,7 +377,7 @@ void main() {

testWithoutContext('flutter windows desktop enabled with config on master', () {
when(mockFlutterVerion.channel).thenReturn('master');
when<bool>(mockFlutterConfig.getValue('enable-windows-desktop') as bool).thenReturn(true);
testConfig.setValue('enable-windows-desktop', true);

expect(featureFlags.isWindowsEnabled, true);
});
Expand All @@ -394,7 +397,7 @@ void main() {

testWithoutContext('flutter windows desktop enabled with config on dev', () {
when(mockFlutterVerion.channel).thenReturn('dev');
when<bool>(mockFlutterConfig.getValue('enable-windows-desktop') as bool).thenReturn(true);
testConfig.setValue('enable-windows-desktop', true);

expect(featureFlags.isWindowsEnabled, true);
});
Expand All @@ -414,7 +417,7 @@ void main() {

testWithoutContext('flutter windows desktop enabled with config on beta', () {
when(mockFlutterVerion.channel).thenReturn('beta');
when<bool>(mockFlutterConfig.getValue('enable-windows-desktop') as bool).thenReturn(true);
testConfig.setValue('enable-windows-desktop', true);

expect(featureFlags.isWindowsEnabled, true);
});
Expand All @@ -434,7 +437,7 @@ void main() {

testWithoutContext('flutter windows desktop enabled with config on stable', () {
when(mockFlutterVerion.channel).thenReturn('stable');
when<bool>(mockFlutterConfig.getValue('enable-windows-desktop') as bool).thenReturn(true);
testConfig.setValue('enable-windows-desktop', true);

expect(featureFlags.isWindowsEnabled, true);
});
Expand All @@ -449,7 +452,6 @@ void main() {
}

class MockFlutterVerion extends Mock implements FlutterVersion {}
class MockFlutterConfig extends Mock implements Config {}
class MockPlatform extends Mock implements Platform {}

T nonconst<T>(T item) => item;

0 comments on commit 73506f3

Please sign in to comment.