Skip to content

Commit c63c576

Browse files
Reland handle corrupt config file (flutter#45414)
1 parent 60869e0 commit c63c576

File tree

2 files changed

+55
-37
lines changed

2 files changed

+55
-37
lines changed

packages/flutter_tools/lib/src/base/config.dart

+15-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,28 @@
33
// found in the LICENSE file.
44

55
import '../convert.dart';
6+
import '../globals.dart';
67
import 'context.dart';
78
import 'file_system.dart';
9+
import 'logger.dart';
810
import 'utils.dart';
911

1012
class Config {
11-
Config([File configFile]) {
13+
Config([File configFile, Logger localLogger]) {
14+
final Logger loggerInstance = localLogger ?? logger;
1215
_configFile = configFile ?? fs.file(fs.path.join(userHomePath(), '.flutter_settings'));
1316
if (_configFile.existsSync()) {
14-
_values = castStringKeyedMap(json.decode(_configFile.readAsStringSync()));
17+
try {
18+
_values = castStringKeyedMap(json.decode(_configFile.readAsStringSync()));
19+
} on FormatException {
20+
loggerInstance
21+
..printError('Failed to decode preferences in ${_configFile.path}.')
22+
..printError(
23+
'You may need to reapply any previously saved configuration '
24+
'with the "flutter config" command.',
25+
);
26+
_configFile.deleteSync();
27+
}
1528
}
1629
}
1730

packages/flutter_tools/test/general.shard/config_test.dart

+40-35
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,59 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'package:file/memory.dart';
56
import 'package:flutter_tools/src/base/config.dart';
67
import 'package:flutter_tools/src/base/file_system.dart';
8+
import 'package:flutter_tools/src/base/logger.dart';
79

810
import '../src/common.dart';
911

1012
void main() {
1113
Config config;
12-
Directory tempDir;
14+
MemoryFileSystem memoryFileSystem;
1315

1416
setUp(() {
15-
tempDir = fs.systemTempDirectory.createTempSync('flutter_config_test.');
16-
final File file = fs.file(fs.path.join(tempDir.path, '.settings'));
17+
memoryFileSystem = MemoryFileSystem();
18+
final File file = memoryFileSystem.file('example');
1719
config = Config(file);
1820
});
21+
test('Config get set value', () async {
22+
expect(config.getValue('foo'), null);
23+
config.setValue('foo', 'bar');
24+
expect(config.getValue('foo'), 'bar');
25+
expect(config.keys, contains('foo'));
26+
});
27+
28+
test('Config get set bool value', () async {
29+
expect(config.getValue('foo'), null);
30+
config.setValue('foo', true);
31+
expect(config.getValue('foo'), true);
32+
expect(config.keys, contains('foo'));
33+
});
1934

20-
tearDown(() {
21-
tryToDelete(tempDir);
35+
test('Config containsKey', () async {
36+
expect(config.containsKey('foo'), false);
37+
config.setValue('foo', 'bar');
38+
expect(config.containsKey('foo'), true);
2239
});
2340

24-
group('config', () {
25-
test('get set value', () async {
26-
expect(config.getValue('foo'), null);
27-
config.setValue('foo', 'bar');
28-
expect(config.getValue('foo'), 'bar');
29-
expect(config.keys, contains('foo'));
30-
});
31-
32-
test('get set bool value', () async {
33-
expect(config.getValue('foo'), null);
34-
config.setValue('foo', true);
35-
expect(config.getValue('foo'), true);
36-
expect(config.keys, contains('foo'));
37-
});
38-
39-
test('containsKey', () async {
40-
expect(config.containsKey('foo'), false);
41-
config.setValue('foo', 'bar');
42-
expect(config.containsKey('foo'), true);
43-
});
44-
45-
test('removeValue', () async {
46-
expect(config.getValue('foo'), null);
47-
config.setValue('foo', 'bar');
48-
expect(config.getValue('foo'), 'bar');
49-
expect(config.keys, contains('foo'));
50-
config.removeValue('foo');
51-
expect(config.getValue('foo'), null);
52-
expect(config.keys, isNot(contains('foo')));
53-
});
41+
test('Config removeValue', () async {
42+
expect(config.getValue('foo'), null);
43+
config.setValue('foo', 'bar');
44+
expect(config.getValue('foo'), 'bar');
45+
expect(config.keys, contains('foo'));
46+
config.removeValue('foo');
47+
expect(config.getValue('foo'), null);
48+
expect(config.keys, isNot(contains('foo')));
49+
});
50+
51+
test('Config parse error', () {
52+
final BufferLogger bufferLogger =BufferLogger();
53+
final File file = memoryFileSystem.file('example')
54+
..writeAsStringSync('{"hello":"bar');
55+
config = Config(file, bufferLogger);
56+
57+
expect(file.existsSync(), false);
58+
expect(bufferLogger.errorText, contains('Failed to decode preferences'));
5459
});
5560
}

0 commit comments

Comments
 (0)