diff --git a/packages/flutter_tools/lib/src/windows/visual_studio.dart b/packages/flutter_tools/lib/src/windows/visual_studio.dart index 527866ec5a..6a48b94516 100644 --- a/packages/flutter_tools/lib/src/windows/visual_studio.dart +++ b/packages/flutter_tools/lib/src/windows/visual_studio.dart @@ -208,6 +208,8 @@ class VisualStudio { // Thrown if vswhere doesnt' exist; ignore and return null below. } on ProcessException { // Ignored, return null below. + } on FormatException { + // may be thrown if invalid JSON is returned. } return null; } diff --git a/packages/flutter_tools/test/general.shard/windows/visual_studio_test.dart b/packages/flutter_tools/test/general.shard/windows/visual_studio_test.dart index 5a9b7e4d4e..bd9af7bcee 100644 --- a/packages/flutter_tools/test/general.shard/windows/visual_studio_test.dart +++ b/packages/flutter_tools/test/general.shard/windows/visual_studio_test.dart @@ -71,6 +71,7 @@ void main() { List requiredComponents, List additionalArguments, Map response, + String responseOverride, ]) { fs.file(vswherePath).createSync(recursive: true); fs.file(vcvarsPath).createSync(recursive: true); @@ -78,7 +79,7 @@ void main() { final MockProcessResult result = MockProcessResult(); when(result.exitCode).thenReturn(0); - final String finalResponse = + final String finalResponse = responseOverride ?? json.encode(>[response]); when(result.stdout).thenReturn(finalResponse); when(result.stderr).thenReturn(''); @@ -116,10 +117,15 @@ void main() { // Sets whether or not a vswhere query searching for 'all' and 'prerelease' // versions will return an installation. - void setMockAnyVisualStudioInstallation(Mapresponse) { + void setMockAnyVisualStudioInstallation(Map response) { setMockVswhereResponse(null, ['-prerelease', '-all'], response); } + // Set a pre-encoded query result. + void setMockEncodedAnyVisualStudioInstallation(String response) { + setMockVswhereResponse(null, ['-prerelease', '-all'], null, response); + } + group('Visual Studio', () { VisualStudio visualStudio; @@ -393,6 +399,19 @@ void main() { Platform: () => windowsPlatform, }); + testUsingContext('vcvarsPath returns null when VS is present but when vswhere returns invalid JSON', () { + setMockCompatibleVisualStudioInstallation(null); + setMockPrereleaseVisualStudioInstallation(null); + setMockEncodedAnyVisualStudioInstallation('{'); + + visualStudio = VisualStudio(); + expect(visualStudio.vcvarsPath, isNull); + }, overrides: { + FileSystem: () => memoryFilesystem, + ProcessManager: () => mockProcessManager, + Platform: () => windowsPlatform, + }); + testUsingContext('Everything returns good values when VS is present with all components', () { setMockCompatibleVisualStudioInstallation(_defaultResponse); setMockPrereleaseVisualStudioInstallation(null);