Add 'doctor' support for Windows (#33872)
Moves the logic for finding vcvars64.bat to a new VisualStudio class that encapsulates finding, and providing information about, VisualStudio installations. Adds a validator for it, and runs it for Windows workflows in doctor.
This commit is contained in:
@@ -9,6 +9,7 @@ import 'package:flutter_tools/src/base/io.dart';
|
||||
import 'package:flutter_tools/src/base/platform.dart';
|
||||
import 'package:flutter_tools/src/cache.dart';
|
||||
import 'package:flutter_tools/src/commands/build.dart';
|
||||
import 'package:flutter_tools/src/windows/visual_studio.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:process/process.dart';
|
||||
import 'package:xml/xml.dart' as xml;
|
||||
@@ -25,6 +26,7 @@ void main() {
|
||||
final MockPlatform windowsPlatform = MockPlatform()
|
||||
..environment['PROGRAMFILES(X86)'] = r'C:\Program Files (x86)\';
|
||||
final MockPlatform notWindowsPlatform = MockPlatform();
|
||||
final MockVisualStudio mockVisualStudio = MockVisualStudio();
|
||||
const String solutionPath = r'C:\windows\Runner.sln';
|
||||
const String visualStudioPath = r'C:\Program Files (x86)\Microsoft Visual Studio\2017\Community';
|
||||
const String vcvarsPath = visualStudioPath + r'\VC\Auxiliary\Build\vcvars64.bat';
|
||||
@@ -41,25 +43,6 @@ void main() {
|
||||
when(windowsPlatform.isWindows).thenReturn(true);
|
||||
when(notWindowsPlatform.isWindows).thenReturn(false);
|
||||
|
||||
// Sets up the mock environment so that lookup of vcvars64.bat will succeed.
|
||||
void enableVcvarsMocking() {
|
||||
const String vswherePath = r'C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe';
|
||||
fs.file(vswherePath).createSync(recursive: true);
|
||||
fs.file(vcvarsPath).createSync(recursive: true);
|
||||
|
||||
final MockProcessResult result = MockProcessResult();
|
||||
when(result.exitCode).thenReturn(0);
|
||||
when<String>(result.stdout).thenReturn(visualStudioPath);
|
||||
when(mockProcessManager.run(<String>[
|
||||
vswherePath,
|
||||
'-latest',
|
||||
'-requires', 'Microsoft.VisualStudio.Workload.NativeDesktop',
|
||||
'-property', 'installationPath',
|
||||
])).thenAnswer((Invocation invocation) async {
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
testUsingContext('Windows build fails when there is no vcvars64.bat', () async {
|
||||
final BuildCommand command = BuildCommand();
|
||||
applyMocksToCommand(command);
|
||||
@@ -70,25 +53,27 @@ void main() {
|
||||
}, overrides: <Type, Generator>{
|
||||
Platform: () => windowsPlatform,
|
||||
FileSystem: () => memoryFilesystem,
|
||||
VisualStudio: () => mockVisualStudio,
|
||||
});
|
||||
|
||||
testUsingContext('Windows build fails when there is no windows project', () async {
|
||||
final BuildCommand command = BuildCommand();
|
||||
applyMocksToCommand(command);
|
||||
enableVcvarsMocking();
|
||||
when(mockVisualStudio.vcvarsPath).thenReturn(vcvarsPath);
|
||||
expect(createTestCommandRunner(command).run(
|
||||
const <String>['build', 'windows']
|
||||
), throwsA(isInstanceOf<ToolExit>()));
|
||||
}, overrides: <Type, Generator>{
|
||||
Platform: () => windowsPlatform,
|
||||
FileSystem: () => memoryFilesystem,
|
||||
VisualStudio: () => mockVisualStudio,
|
||||
});
|
||||
|
||||
testUsingContext('Windows build fails on non windows platform', () async {
|
||||
final BuildCommand command = BuildCommand();
|
||||
applyMocksToCommand(command);
|
||||
fs.file(solutionPath).createSync(recursive: true);
|
||||
enableVcvarsMocking();
|
||||
when(mockVisualStudio.vcvarsPath).thenReturn(vcvarsPath);
|
||||
fs.file('pubspec.yaml').createSync();
|
||||
fs.file('.packages').createSync();
|
||||
|
||||
@@ -98,13 +83,14 @@ void main() {
|
||||
}, overrides: <Type, Generator>{
|
||||
Platform: () => notWindowsPlatform,
|
||||
FileSystem: () => memoryFilesystem,
|
||||
VisualStudio: () => mockVisualStudio,
|
||||
});
|
||||
|
||||
testUsingContext('Windows build invokes msbuild and writes generated files', () async {
|
||||
final BuildCommand command = BuildCommand();
|
||||
applyMocksToCommand(command);
|
||||
fs.file(solutionPath).createSync(recursive: true);
|
||||
enableVcvarsMocking();
|
||||
when(mockVisualStudio.vcvarsPath).thenReturn(vcvarsPath);
|
||||
fs.file('pubspec.yaml').createSync();
|
||||
fs.file('.packages').createSync();
|
||||
|
||||
@@ -132,15 +118,16 @@ void main() {
|
||||
FileSystem: () => memoryFilesystem,
|
||||
ProcessManager: () => mockProcessManager,
|
||||
Platform: () => windowsPlatform,
|
||||
VisualStudio: () => mockVisualStudio,
|
||||
});
|
||||
}
|
||||
|
||||
class MockProcessManager extends Mock implements ProcessManager {}
|
||||
class MockProcess extends Mock implements Process {}
|
||||
class MockProcessResult extends Mock implements ProcessResult {}
|
||||
class MockPlatform extends Mock implements Platform {
|
||||
@override
|
||||
Map<String, String> environment = <String, String>{
|
||||
'FLUTTER_ROOT': r'C:\',
|
||||
};
|
||||
}
|
||||
class MockVisualStudio extends Mock implements VisualStudio {}
|
||||
|
||||
236
packages/flutter_tools/test/windows/visual_studio_test.dart
Normal file
236
packages/flutter_tools/test/windows/visual_studio_test.dart
Normal file
@@ -0,0 +1,236 @@
|
||||
// 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/memory.dart';
|
||||
import 'package:flutter_tools/src/base/file_system.dart';
|
||||
import 'package:flutter_tools/src/base/io.dart' show ProcessException, ProcessResult;
|
||||
import 'package:flutter_tools/src/base/platform.dart';
|
||||
import 'package:flutter_tools/src/convert.dart';
|
||||
import 'package:flutter_tools/src/windows/visual_studio.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
import 'package:process/process.dart';
|
||||
|
||||
import '../src/common.dart';
|
||||
import '../src/context.dart';
|
||||
|
||||
class MockPlatform extends Mock implements Platform {
|
||||
@override
|
||||
Map<String, String> environment = <String, String>{};
|
||||
}
|
||||
class MockProcessManager extends Mock implements ProcessManager {}
|
||||
class MockProcessResult extends Mock implements ProcessResult {}
|
||||
|
||||
void main() {
|
||||
const String programFilesPath = r'C:\Program Files (x86)';
|
||||
const String visualStudioPath = programFilesPath + r'\Microsoft Visual Studio\2017\Community';
|
||||
const String vcvarsPath = visualStudioPath + r'\VC\Auxiliary\Build\vcvars64.bat';
|
||||
const String vswherePath = programFilesPath + r'\Microsoft Visual Studio\Installer\vswhere.exe';
|
||||
|
||||
final MockPlatform windowsPlatform = MockPlatform()
|
||||
..environment['PROGRAMFILES(X86)'] = r'C:\Program Files (x86)\';
|
||||
MockProcessManager mockProcessManager;
|
||||
final MemoryFileSystem memoryFilesystem = MemoryFileSystem(style: FileSystemStyle.windows);
|
||||
|
||||
// Sets up the mock environment so that searching for Visual Studio with
|
||||
// exactly the given required components will provide a result. By default it
|
||||
// return a preset installation, but the response can be overridden.
|
||||
void setMockVswhereResponse([List<String> requiredComponents, String response]) {
|
||||
fs.file(vswherePath).createSync(recursive: true);
|
||||
fs.file(vcvarsPath).createSync(recursive: true);
|
||||
|
||||
final MockProcessResult result = MockProcessResult();
|
||||
when(result.exitCode).thenReturn(0);
|
||||
when<String>(result.stdout).thenReturn(response == null
|
||||
? json.encode(<Map<String, dynamic>>[
|
||||
<String, dynamic>{
|
||||
'installationPath': visualStudioPath,
|
||||
'displayName': 'Visual Studio Community 2017',
|
||||
'installationVersion': '15.9.28307.665',
|
||||
'catalog': <String, String>{
|
||||
'productDisplayVersion': '15.9.12',
|
||||
},
|
||||
}
|
||||
])
|
||||
: response);
|
||||
|
||||
final List<String> requirementArguments = requiredComponents == null
|
||||
? <String>[]
|
||||
: <String>['-requires', ...requiredComponents];
|
||||
when(mockProcessManager.runSync(<String>[
|
||||
vswherePath,
|
||||
'-format', 'json',
|
||||
'-utf8',
|
||||
'-latest',
|
||||
...?requirementArguments,
|
||||
])).thenAnswer((Invocation invocation) {
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
// Sets whether or not a vswhere query without components will return an
|
||||
// installation.
|
||||
void setMockIncompleteVisualStudioExists(bool exists) {
|
||||
setMockVswhereResponse(null, exists ? null : '[]');
|
||||
}
|
||||
|
||||
// Sets whether or not a vswhere query with the required components will
|
||||
// return an installation.
|
||||
void setMockCompatibleVisualStudioExists(bool exists) {
|
||||
setMockVswhereResponse(<String>[
|
||||
'Microsoft.Component.MSBuild',
|
||||
'Microsoft.VisualStudio.Component.VC.Tools.x86.x64',
|
||||
'Microsoft.VisualStudio.Component.Windows10SDK.17763',
|
||||
], exists ? null : '[]');
|
||||
}
|
||||
|
||||
group('Visual Studio', () {
|
||||
VisualStudio visualStudio;
|
||||
|
||||
setUp(() {
|
||||
mockProcessManager = MockProcessManager();
|
||||
});
|
||||
|
||||
testUsingContext('isInstalled returns false when vswhere is missing', () {
|
||||
when(mockProcessManager.runSync(any))
|
||||
.thenThrow(const ProcessException('vswhere', <String>[]));
|
||||
|
||||
visualStudio = VisualStudio();
|
||||
expect(visualStudio.isInstalled, false);
|
||||
}, overrides: <Type, Generator>{
|
||||
FileSystem: () => memoryFilesystem,
|
||||
Platform: () => windowsPlatform,
|
||||
ProcessManager: () => mockProcessManager,
|
||||
});
|
||||
|
||||
testUsingContext('vcvarsPath returns null when vswhere is missing', () {
|
||||
when(mockProcessManager.runSync(any))
|
||||
.thenThrow(const ProcessException('vswhere', <String>[]));
|
||||
|
||||
visualStudio = VisualStudio();
|
||||
expect(visualStudio.vcvarsPath, isNull);
|
||||
}, overrides: <Type, Generator>{
|
||||
FileSystem: () => memoryFilesystem,
|
||||
Platform: () => windowsPlatform,
|
||||
ProcessManager: () => mockProcessManager,
|
||||
});
|
||||
|
||||
testUsingContext('isInstalled returns false when vswhere returns non-zero', () {
|
||||
when(mockProcessManager.runSync(any))
|
||||
.thenThrow(const ProcessException('vswhere', <String>[]));
|
||||
final MockProcessResult result = MockProcessResult();
|
||||
when(result.exitCode).thenReturn(1);
|
||||
when(mockProcessManager.runSync(any)).thenAnswer((Invocation invocation) {
|
||||
return result;
|
||||
});
|
||||
|
||||
visualStudio = VisualStudio();
|
||||
expect(visualStudio.isInstalled, false);
|
||||
}, overrides: <Type, Generator>{
|
||||
FileSystem: () => memoryFilesystem,
|
||||
Platform: () => windowsPlatform,
|
||||
ProcessManager: () => mockProcessManager,
|
||||
});
|
||||
|
||||
testUsingContext('isInstalled returns true when VS is present but missing components', () {
|
||||
setMockIncompleteVisualStudioExists(true);
|
||||
setMockCompatibleVisualStudioExists(false);
|
||||
|
||||
visualStudio = VisualStudio();
|
||||
expect(visualStudio.isInstalled, true);
|
||||
}, overrides: <Type, Generator>{
|
||||
FileSystem: () => memoryFilesystem,
|
||||
Platform: () => windowsPlatform,
|
||||
ProcessManager: () => mockProcessManager,
|
||||
});
|
||||
|
||||
testUsingContext('hasNecessaryComponents returns false when VS is present but missing components', () {
|
||||
setMockIncompleteVisualStudioExists(true);
|
||||
setMockCompatibleVisualStudioExists(false);
|
||||
|
||||
visualStudio = VisualStudio();
|
||||
expect(visualStudio.hasNecessaryComponents, false);
|
||||
}, overrides: <Type, Generator>{
|
||||
FileSystem: () => memoryFilesystem,
|
||||
Platform: () => windowsPlatform,
|
||||
ProcessManager: () => mockProcessManager,
|
||||
});
|
||||
|
||||
testUsingContext('vcvarsPath returns null when VS is present but missing components', () {
|
||||
setMockIncompleteVisualStudioExists(true);
|
||||
setMockCompatibleVisualStudioExists(false);
|
||||
|
||||
visualStudio = VisualStudio();
|
||||
expect(visualStudio.vcvarsPath, isNull);
|
||||
}, overrides: <Type, Generator>{
|
||||
FileSystem: () => memoryFilesystem,
|
||||
Platform: () => windowsPlatform,
|
||||
ProcessManager: () => mockProcessManager,
|
||||
});
|
||||
|
||||
testUsingContext('VS metadata is available when VS is present, even if missing components', () {
|
||||
setMockIncompleteVisualStudioExists(true);
|
||||
setMockCompatibleVisualStudioExists(false);
|
||||
|
||||
visualStudio = VisualStudio();
|
||||
expect(visualStudio.displayName, equals('Visual Studio Community 2017'));
|
||||
expect(visualStudio.displayVersion, equals('15.9.12'));
|
||||
expect(visualStudio.installLocation, equals(visualStudioPath));
|
||||
expect(visualStudio.fullVersion, equals('15.9.28307.665'));
|
||||
}, overrides: <Type, Generator>{
|
||||
FileSystem: () => memoryFilesystem,
|
||||
Platform: () => windowsPlatform,
|
||||
ProcessManager: () => mockProcessManager,
|
||||
});
|
||||
|
||||
|
||||
testUsingContext('isInstalled returns true when VS is present but missing components', () {
|
||||
setMockIncompleteVisualStudioExists(true);
|
||||
setMockCompatibleVisualStudioExists(false);
|
||||
|
||||
visualStudio = VisualStudio();
|
||||
expect(visualStudio.isInstalled, true);
|
||||
}, overrides: <Type, Generator>{
|
||||
FileSystem: () => memoryFilesystem,
|
||||
Platform: () => windowsPlatform,
|
||||
ProcessManager: () => mockProcessManager,
|
||||
});
|
||||
|
||||
testUsingContext('Everything returns good values when VS is present with all components', () {
|
||||
setMockCompatibleVisualStudioExists(true);
|
||||
|
||||
visualStudio = VisualStudio();
|
||||
expect(visualStudio.isInstalled, true);
|
||||
expect(visualStudio.hasNecessaryComponents, true);
|
||||
expect(visualStudio.vcvarsPath, equals(vcvarsPath));
|
||||
}, overrides: <Type, Generator>{
|
||||
FileSystem: () => memoryFilesystem,
|
||||
Platform: () => windowsPlatform,
|
||||
ProcessManager: () => mockProcessManager,
|
||||
});
|
||||
|
||||
testUsingContext('Metadata is for compatible version when latest is missing components', () {
|
||||
setMockCompatibleVisualStudioExists(true);
|
||||
// Return a different version for queries without the required packages.
|
||||
final String incompleteVersionResponse = json.encode(<Map<String, dynamic>>[
|
||||
<String, dynamic>{
|
||||
'installationPath': visualStudioPath,
|
||||
'displayName': 'Visual Studio Community 2019',
|
||||
'installationVersion': '16.1.1.1',
|
||||
'catalog': <String, String>{
|
||||
'productDisplayVersion': '16.1',
|
||||
},
|
||||
}
|
||||
]);
|
||||
setMockVswhereResponse(null, incompleteVersionResponse);
|
||||
|
||||
visualStudio = VisualStudio();
|
||||
expect(visualStudio.displayName, equals('Visual Studio Community 2017'));
|
||||
expect(visualStudio.displayVersion, equals('15.9.12'));
|
||||
}, overrides: <Type, Generator>{
|
||||
FileSystem: () => memoryFilesystem,
|
||||
Platform: () => windowsPlatform,
|
||||
ProcessManager: () => mockProcessManager,
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
// 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:flutter_tools/src/doctor.dart';
|
||||
import 'package:flutter_tools/src/windows/visual_studio.dart';
|
||||
import 'package:flutter_tools/src/windows/visual_studio_validator.dart';
|
||||
import 'package:mockito/mockito.dart';
|
||||
|
||||
import '../src/common.dart';
|
||||
import '../src/context.dart';
|
||||
|
||||
class MockVisualStudio extends Mock implements VisualStudio {}
|
||||
|
||||
void main() {
|
||||
group('Visual Studio validation', () {
|
||||
MockVisualStudio mockVisualStudio;
|
||||
|
||||
setUp(() {
|
||||
mockVisualStudio = MockVisualStudio();
|
||||
});
|
||||
|
||||
testUsingContext('Emits missing status when Visual Studio is not installed', () async {
|
||||
when(visualStudio.isInstalled).thenReturn(false);
|
||||
const VisualStudioValidator validator = VisualStudioValidator();
|
||||
final ValidationResult result = await validator.validate();
|
||||
expect(result.type, ValidationType.missing);
|
||||
}, overrides: <Type, Generator>{
|
||||
VisualStudio: () => mockVisualStudio,
|
||||
});
|
||||
|
||||
testUsingContext('Emits partial status when Visual Studio is installed without necessary components', () async {
|
||||
when(visualStudio.isInstalled).thenReturn(true);
|
||||
when(visualStudio.hasNecessaryComponents).thenReturn(false);
|
||||
when(visualStudio.workloadDescription).thenReturn('Desktop development');
|
||||
when(visualStudio.necessaryComponentDescriptions(any)).thenReturn(<String>['A', 'B']);
|
||||
when(visualStudio.fullVersion).thenReturn('15.1');
|
||||
const VisualStudioValidator validator = VisualStudioValidator();
|
||||
final ValidationResult result = await validator.validate();
|
||||
expect(result.type, ValidationType.partial);
|
||||
}, overrides: <Type, Generator>{
|
||||
VisualStudio: () => mockVisualStudio,
|
||||
});
|
||||
|
||||
testUsingContext('Emits installed status when Visual Studio is installed with necessary components', () async {
|
||||
when(visualStudio.isInstalled).thenReturn(true);
|
||||
when(visualStudio.hasNecessaryComponents).thenReturn(true);
|
||||
const VisualStudioValidator validator = VisualStudioValidator();
|
||||
final ValidationResult result = await validator.validate();
|
||||
expect(result.type, ValidationType.installed);
|
||||
}, overrides: <Type, Generator>{
|
||||
VisualStudio: () => mockVisualStudio,
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user