forked from firka/flutter
Dont crash on malformed packages.json (#45786)
This commit is contained in:
@@ -224,8 +224,12 @@ class VsCode {
|
||||
return null;
|
||||
}
|
||||
final String jsonString = fs.file(packageJsonPath).readAsStringSync();
|
||||
final Map<String, dynamic> jsonObject = castStringKeyedMap(json.decode(jsonString));
|
||||
return jsonObject['version'] as String;
|
||||
try {
|
||||
final Map<String, dynamic> jsonObject = castStringKeyedMap(json.decode(jsonString));
|
||||
return jsonObject['version'] as String;
|
||||
} on FormatException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
28
packages/flutter_tools/test/general.shard/vscode_test.dart
Normal file
28
packages/flutter_tools/test/general.shard/vscode_test.dart
Normal file
@@ -0,0 +1,28 @@
|
||||
// Copyright 2019 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'package:file/file.dart';
|
||||
import 'package:file/memory.dart';
|
||||
import 'package:flutter_tools/src/base/file_system.dart';
|
||||
import 'package:flutter_tools/src/base/version.dart';
|
||||
import 'package:flutter_tools/src/vscode/vscode.dart';
|
||||
|
||||
import '../src/common.dart';
|
||||
import '../src/context.dart';
|
||||
|
||||
void main() {
|
||||
testUsingContext('VsCode.fromDirectory does not crash when packages.json is malformed', () {
|
||||
// Create invalid JSON file.
|
||||
fs.file(fs.path.join('', 'resources', 'app', 'package.json'))
|
||||
..createSync(recursive: true)
|
||||
..writeAsStringSync('{');
|
||||
|
||||
final VsCode vsCode = VsCode.fromDirectory('', '');
|
||||
|
||||
expect(vsCode.version, Version.unknown);
|
||||
}, overrides: <Type, Generator>{
|
||||
FileSystem: () => MemoryFileSystem(),
|
||||
ProcessManager: () => FakeProcessManager.any(),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user