Unverified Commit 8cf20f3b authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

Dont crash on malformed packages.json (#45786)

parent 0ce9206e
...@@ -224,8 +224,12 @@ class VsCode { ...@@ -224,8 +224,12 @@ class VsCode {
return null; return null;
} }
final String jsonString = fs.file(packageJsonPath).readAsStringSync(); final String jsonString = fs.file(packageJsonPath).readAsStringSync();
try {
final Map<String, dynamic> jsonObject = castStringKeyedMap(json.decode(jsonString)); final Map<String, dynamic> jsonObject = castStringKeyedMap(json.decode(jsonString));
return jsonObject['version'] as String; return jsonObject['version'] as String;
} on FormatException {
return null;
}
} }
} }
......
// 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(),
});
}
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