diff --git a/packages/flutter_tools/lib/src/vscode/vscode.dart b/packages/flutter_tools/lib/src/vscode/vscode.dart index ce08e70c34..89ba0a1c34 100644 --- a/packages/flutter_tools/lib/src/vscode/vscode.dart +++ b/packages/flutter_tools/lib/src/vscode/vscode.dart @@ -3,6 +3,7 @@ // found in the LICENSE file. import '../base/file_system.dart'; +import '../base/platform.dart'; import '../base/utils.dart'; import '../base/version.dart'; import '../convert.dart'; @@ -86,15 +87,18 @@ class VsCode { Iterable get validationMessages => _validationMessages; - static List allInstalled() { - if (globals.platform.isMacOS) { - return _installedMacOS(); + static List allInstalled( + FileSystem fileSystem, + Platform platform, + ) { + if (platform.isMacOS) { + return _installedMacOS(fileSystem); } - if (globals.platform.isWindows) { - return _installedWindows(); + if (platform.isWindows) { + return _installedWindows(fileSystem, platform); } - if (globals.platform.isLinux) { - return _installedLinux(); + if (platform.isLinux) { + return _installedLinux(fileSystem); } // VS Code isn't supported on the other platforms. return []; @@ -108,14 +112,14 @@ class VsCode { // macOS Extensions: // $HOME/.vscode/extensions // $HOME/.vscode-insiders/extensions - static List _installedMacOS() { + static List _installedMacOS(FileSystem fileSystem) { return _findInstalled(<_VsCodeInstallLocation>[ _VsCodeInstallLocation( - globals.fs.path.join('/Applications', 'Visual Studio Code.app', 'Contents'), + fileSystem.path.join('/Applications', 'Visual Studio Code.app', 'Contents'), '.vscode', ), _VsCodeInstallLocation( - globals.fs.path.join( + fileSystem.path.join( globals.fsUtils.homeDirPath, 'Applications', 'Visual Studio Code.app', @@ -124,12 +128,12 @@ class VsCode { '.vscode', ), _VsCodeInstallLocation( - globals.fs.path.join('/Applications', 'Visual Studio Code - Insiders.app', 'Contents'), + fileSystem.path.join('/Applications', 'Visual Studio Code - Insiders.app', 'Contents'), '.vscode-insiders', isInsiders: true, ), _VsCodeInstallLocation( - globals.fs.path.join( + fileSystem.path.join( globals.fsUtils.homeDirPath, 'Applications', 'Visual Studio Code - Insiders.app', @@ -138,7 +142,7 @@ class VsCode { '.vscode-insiders', isInsiders: true, ), - ]); + ], fileSystem); } // Windows: @@ -153,51 +157,56 @@ class VsCode { // Windows Extensions: // $HOME/.vscode/extensions // $HOME/.vscode-insiders/extensions - static List _installedWindows() { - final String progFiles86 = globals.platform.environment['programfiles(x86)']; - final String progFiles = globals.platform.environment['programfiles']; - final String localAppData = globals.platform.environment['localappdata']; + static List _installedWindows( + FileSystem fileSystem, + Platform platform, + ) { + final String progFiles86 = platform.environment['programfiles(x86)']; + final String progFiles = platform.environment['programfiles']; + final String localAppData = platform.environment['localappdata']; - final List<_VsCodeInstallLocation> searchLocations = - <_VsCodeInstallLocation>[]; - - if (localAppData != null) { - searchLocations.add(_VsCodeInstallLocation( - globals.fs.path.join(localAppData, r'Programs\Microsoft VS Code'), - '.vscode', - )); - } - searchLocations.add(_VsCodeInstallLocation( - globals.fs.path.join(progFiles86, 'Microsoft VS Code'), - '.vscode', - edition: '32-bit edition', - )); - searchLocations.add(_VsCodeInstallLocation( - globals.fs.path.join(progFiles, 'Microsoft VS Code'), - '.vscode', - edition: '64-bit edition', - )); - if (localAppData != null) { - searchLocations.add(_VsCodeInstallLocation( - globals.fs.path.join(localAppData, r'Programs\Microsoft VS Code Insiders'), - '.vscode-insiders', - isInsiders: true, - )); - } - searchLocations.add(_VsCodeInstallLocation( - globals.fs.path.join(progFiles86, 'Microsoft VS Code Insiders'), - '.vscode-insiders', - edition: '32-bit edition', - isInsiders: true, - )); - searchLocations.add(_VsCodeInstallLocation( - globals.fs.path.join(progFiles, 'Microsoft VS Code Insiders'), - '.vscode-insiders', - edition: '64-bit edition', - isInsiders: true, - )); - - return _findInstalled(searchLocations); + final List<_VsCodeInstallLocation> searchLocations = <_VsCodeInstallLocation>[ + if (localAppData != null) + _VsCodeInstallLocation( + fileSystem.path.join(localAppData, r'Programs\Microsoft VS Code'), + '.vscode', + ), + if (progFiles86 != null) + ...<_VsCodeInstallLocation>[ + _VsCodeInstallLocation( + fileSystem.path.join(progFiles86, 'Microsoft VS Code'), + '.vscode', + edition: '32-bit edition', + ), + _VsCodeInstallLocation( + fileSystem.path.join(progFiles86, 'Microsoft VS Code Insiders'), + '.vscode-insiders', + edition: '32-bit edition', + isInsiders: true, + ), + ], + if (progFiles != null) + ...<_VsCodeInstallLocation>[ + _VsCodeInstallLocation( + fileSystem.path.join(progFiles, 'Microsoft VS Code'), + '.vscode', + edition: '64-bit edition', + ), + _VsCodeInstallLocation( + fileSystem.path.join(progFiles, 'Microsoft VS Code Insiders'), + '.vscode-insiders', + edition: '64-bit edition', + isInsiders: true, + ), + ], + if (localAppData != null) + _VsCodeInstallLocation( + fileSystem.path.join(localAppData, r'Programs\Microsoft VS Code Insiders'), + '.vscode-insiders', + isInsiders: true, + ), + ]; + return _findInstalled(searchLocations, fileSystem); } // Linux: @@ -206,7 +215,7 @@ class VsCode { // Linux Extensions: // $HOME/.vscode/extensions // $HOME/.vscode-insiders/extensions - static List _installedLinux() { + static List _installedLinux(FileSystem fileSystem) { return _findInstalled(<_VsCodeInstallLocation>[ const _VsCodeInstallLocation('/usr/share/code', '.vscode'), const _VsCodeInstallLocation( @@ -214,10 +223,13 @@ class VsCode { '.vscode-insiders', isInsiders: true, ), - ]); + ], fileSystem); } - static List _findInstalled(List<_VsCodeInstallLocation> allLocations) { + static List _findInstalled( + List<_VsCodeInstallLocation> allLocations, + FileSystem fileSystem, + ) { final Iterable<_VsCodeInstallLocation> searchLocations = _includeInsiders ? allLocations @@ -226,8 +238,8 @@ class VsCode { final List results = []; for (final _VsCodeInstallLocation searchLocation in searchLocations) { - if (globals.fs.isDirectorySync(searchLocation.installPath)) { - final String extensionDirectory = globals.fs.path.join( + if (fileSystem.isDirectorySync(searchLocation.installPath)) { + final String extensionDirectory = fileSystem.path.join( globals.fsUtils.homeDirPath, searchLocation.extensionsFolder, 'extensions', diff --git a/packages/flutter_tools/lib/src/vscode/vscode_validator.dart b/packages/flutter_tools/lib/src/vscode/vscode_validator.dart index 6fb8409f2a..aa9736ce68 100644 --- a/packages/flutter_tools/lib/src/vscode/vscode_validator.dart +++ b/packages/flutter_tools/lib/src/vscode/vscode_validator.dart @@ -7,6 +7,7 @@ import 'dart:async'; import '../base/user_messages.dart'; import '../base/version.dart'; import '../doctor.dart'; +import '../globals.dart' as globals; import 'vscode.dart'; class VsCodeValidator extends DoctorValidator { @@ -16,7 +17,7 @@ class VsCodeValidator extends DoctorValidator { static Iterable get installedValidators { return VsCode - .allInstalled() + .allInstalled(globals.fs, globals.platform) .map((VsCode vsCode) => VsCodeValidator(vsCode)); } diff --git a/packages/flutter_tools/test/general.shard/vscode_test.dart b/packages/flutter_tools/test/general.shard/vscode/vscode_test.dart similarity index 94% rename from packages/flutter_tools/test/general.shard/vscode_test.dart rename to packages/flutter_tools/test/general.shard/vscode/vscode_test.dart index 8e58859579..25c0e5e4fd 100644 --- a/packages/flutter_tools/test/general.shard/vscode_test.dart +++ b/packages/flutter_tools/test/general.shard/vscode/vscode_test.dart @@ -10,8 +10,8 @@ import 'package:flutter_tools/src/base/version.dart'; import 'package:flutter_tools/src/globals.dart' as globals; import 'package:flutter_tools/src/vscode/vscode.dart'; -import '../src/common.dart'; -import '../src/context.dart'; +import '../../src/common.dart'; +import '../../src/context.dart'; void main() { testUsingContext('VsCode.fromDirectory does not crash when packages.json is malformed', () { diff --git a/packages/flutter_tools/test/general.shard/vscode/vscode_validator_test.dart b/packages/flutter_tools/test/general.shard/vscode/vscode_validator_test.dart new file mode 100644 index 0000000000..78cdcb5fee --- /dev/null +++ b/packages/flutter_tools/test/general.shard/vscode/vscode_validator_test.dart @@ -0,0 +1,22 @@ +// Copyright 2014 The Flutter 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/platform.dart'; +import 'package:flutter_tools/src/vscode/vscode.dart'; + +import '../../src/common.dart'; + +void main() { + testWithoutContext('VsCode search locations on windows supports an empty environment', () { + final FileSystem fileSystem = MemoryFileSystem.test(style: FileSystemStyle.windows); + final Platform platform = FakePlatform( + operatingSystem: 'windows', + environment: {}, + ); + + expect(VsCode.allInstalled(fileSystem, platform), isEmpty); + }); +}