Unverified Commit 94270759 authored by xster's avatar xster Committed by GitHub

Revert "catch parse error from corrupt config (#45319)" (#45412)

parent 6640c8b9
...@@ -3,28 +3,15 @@ ...@@ -3,28 +3,15 @@
// found in the LICENSE file. // found in the LICENSE file.
import '../convert.dart'; import '../convert.dart';
import '../globals.dart';
import 'context.dart'; import 'context.dart';
import 'file_system.dart'; import 'file_system.dart';
import 'logger.dart';
import 'utils.dart'; import 'utils.dart';
class Config { class Config {
Config([File configFile, Logger localLogger]) { Config([File configFile]) {
final Logger loggerInstance = localLogger ?? logger;
_configFile = configFile ?? fs.file(fs.path.join(userHomePath(), '.flutter_settings')); _configFile = configFile ?? fs.file(fs.path.join(userHomePath(), '.flutter_settings'));
if (_configFile.existsSync()) { if (_configFile.existsSync()) {
try { _values = castStringKeyedMap(json.decode(_configFile.readAsStringSync()));
_values = castStringKeyedMap(json.decode(_configFile.readAsStringSync()));
} on FormatException {
loggerInstance
..printError('Failed to decode preferences in ${_configFile.path}.')
..printError(
'You may need to reapply any previously saved configuration '
'with the "flutter config" command.',
);
_configFile.deleteSync();
}
} }
} }
......
...@@ -2,59 +2,54 @@ ...@@ -2,59 +2,54 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/config.dart'; import 'package:flutter_tools/src/base/config.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 '../src/common.dart'; import '../src/common.dart';
void main() { void main() {
Config config; Config config;
MemoryFileSystem memoryFileSystem; Directory tempDir;
setUp(() { setUp(() {
memoryFileSystem = MemoryFileSystem(); tempDir = fs.systemTempDirectory.createTempSync('flutter_config_test.');
final File file = memoryFileSystem.file('example'); final File file = fs.file(fs.path.join(tempDir.path, '.settings'));
config = Config(file); config = Config(file);
}); });
test('Config get set value', () async {
expect(config.getValue('foo'), null);
config.setValue('foo', 'bar');
expect(config.getValue('foo'), 'bar');
expect(config.keys, contains('foo'));
});
test('Config get set bool value', () async {
expect(config.getValue('foo'), null);
config.setValue('foo', true);
expect(config.getValue('foo'), true);
expect(config.keys, contains('foo'));
});
test('Config containsKey', () async { tearDown(() {
expect(config.containsKey('foo'), false); tryToDelete(tempDir);
config.setValue('foo', 'bar');
expect(config.containsKey('foo'), true);
}); });
test('Config removeValue', () async { group('config', () {
expect(config.getValue('foo'), null); test('get set value', () async {
config.setValue('foo', 'bar'); expect(config.getValue('foo'), null);
expect(config.getValue('foo'), 'bar'); config.setValue('foo', 'bar');
expect(config.keys, contains('foo')); expect(config.getValue('foo'), 'bar');
config.removeValue('foo'); expect(config.keys, contains('foo'));
expect(config.getValue('foo'), null); });
expect(config.keys, isNot(contains('foo')));
}); test('get set bool value', () async {
expect(config.getValue('foo'), null);
test('Config parse error', () { config.setValue('foo', true);
final BufferLogger bufferLogger =BufferLogger(); expect(config.getValue('foo'), true);
final File file = memoryFileSystem.file('example') expect(config.keys, contains('foo'));
..writeAsStringSync('{"hello":"bar'); });
config = Config(file, bufferLogger);
test('containsKey', () async {
expect(file.existsSync(), false); expect(config.containsKey('foo'), false);
expect(bufferLogger.errorText, contains('Failed to decode preferences')); config.setValue('foo', 'bar');
expect(config.containsKey('foo'), true);
});
test('removeValue', () async {
expect(config.getValue('foo'), null);
config.setValue('foo', 'bar');
expect(config.getValue('foo'), 'bar');
expect(config.keys, contains('foo'));
config.removeValue('foo');
expect(config.getValue('foo'), null);
expect(config.keys, isNot(contains('foo')));
});
}); });
} }
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