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

This commit is contained in:
xster 2019-11-22 10:56:16 -08:00 committed by GitHub
parent 6640c8b9b4
commit 942707591d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 53 deletions

View file

@ -3,28 +3,15 @@
// found in the LICENSE file.
import '../convert.dart';
import '../globals.dart';
import 'context.dart';
import 'file_system.dart';
import 'logger.dart';
import 'utils.dart';
class Config {
Config([File configFile, Logger localLogger]) {
final Logger loggerInstance = localLogger ?? logger;
Config([File configFile]) {
_configFile = configFile ?? fs.file(fs.path.join(userHomePath(), '.flutter_settings'));
if (_configFile.existsSync()) {
try {
_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();
}
_values = castStringKeyedMap(json.decode(_configFile.readAsStringSync()));
}
}

View file

@ -2,59 +2,54 @@
// 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/config.dart';
import 'package:flutter_tools/src/base/file_system.dart';
import 'package:flutter_tools/src/base/logger.dart';
import '../src/common.dart';
void main() {
Config config;
MemoryFileSystem memoryFileSystem;
Directory tempDir;
setUp(() {
memoryFileSystem = MemoryFileSystem();
final File file = memoryFileSystem.file('example');
tempDir = fs.systemTempDirectory.createTempSync('flutter_config_test.');
final File file = fs.file(fs.path.join(tempDir.path, '.settings'));
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'));
tearDown(() {
tryToDelete(tempDir);
});
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'));
});
group('config', () {
test('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 containsKey', () async {
expect(config.containsKey('foo'), false);
config.setValue('foo', 'bar');
expect(config.containsKey('foo'), true);
});
test('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 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')));
});
test('containsKey', () async {
expect(config.containsKey('foo'), false);
config.setValue('foo', 'bar');
expect(config.containsKey('foo'), true);
});
test('Config parse error', () {
final BufferLogger bufferLogger =BufferLogger();
final File file = memoryFileSystem.file('example')
..writeAsStringSync('{"hello":"bar');
config = Config(file, bufferLogger);
expect(file.existsSync(), false);
expect(bufferLogger.errorText, contains('Failed to decode preferences'));
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')));
});
});
}