mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
Reland handle corrupt config file (#45414)
This commit is contained in:
parent
60869e0a93
commit
c63c576bca
@ -3,15 +3,28 @@
|
|||||||
// 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]) {
|
Config([File configFile, Logger localLogger]) {
|
||||||
|
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,47 +2,43 @@
|
|||||||
// 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;
|
||||||
Directory tempDir;
|
MemoryFileSystem memoryFileSystem;
|
||||||
|
|
||||||
setUp(() {
|
setUp(() {
|
||||||
tempDir = fs.systemTempDirectory.createTempSync('flutter_config_test.');
|
memoryFileSystem = MemoryFileSystem();
|
||||||
final File file = fs.file(fs.path.join(tempDir.path, '.settings'));
|
final File file = memoryFileSystem.file('example');
|
||||||
config = Config(file);
|
config = Config(file);
|
||||||
});
|
});
|
||||||
|
test('Config get set value', () async {
|
||||||
tearDown(() {
|
|
||||||
tryToDelete(tempDir);
|
|
||||||
});
|
|
||||||
|
|
||||||
group('config', () {
|
|
||||||
test('get set value', () async {
|
|
||||||
expect(config.getValue('foo'), null);
|
expect(config.getValue('foo'), null);
|
||||||
config.setValue('foo', 'bar');
|
config.setValue('foo', 'bar');
|
||||||
expect(config.getValue('foo'), 'bar');
|
expect(config.getValue('foo'), 'bar');
|
||||||
expect(config.keys, contains('foo'));
|
expect(config.keys, contains('foo'));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('get set bool value', () async {
|
test('Config get set bool value', () async {
|
||||||
expect(config.getValue('foo'), null);
|
expect(config.getValue('foo'), null);
|
||||||
config.setValue('foo', true);
|
config.setValue('foo', true);
|
||||||
expect(config.getValue('foo'), true);
|
expect(config.getValue('foo'), true);
|
||||||
expect(config.keys, contains('foo'));
|
expect(config.keys, contains('foo'));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('containsKey', () async {
|
test('Config containsKey', () async {
|
||||||
expect(config.containsKey('foo'), false);
|
expect(config.containsKey('foo'), false);
|
||||||
config.setValue('foo', 'bar');
|
config.setValue('foo', 'bar');
|
||||||
expect(config.containsKey('foo'), true);
|
expect(config.containsKey('foo'), true);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('removeValue', () async {
|
test('Config removeValue', () async {
|
||||||
expect(config.getValue('foo'), null);
|
expect(config.getValue('foo'), null);
|
||||||
config.setValue('foo', 'bar');
|
config.setValue('foo', 'bar');
|
||||||
expect(config.getValue('foo'), 'bar');
|
expect(config.getValue('foo'), 'bar');
|
||||||
@ -51,5 +47,14 @@ void main() {
|
|||||||
expect(config.getValue('foo'), null);
|
expect(config.getValue('foo'), null);
|
||||||
expect(config.keys, isNot(contains('foo')));
|
expect(config.keys, isNot(contains('foo')));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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'));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user