diff --git a/src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs b/src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs index 10de11254..4589b9a44 100644 --- a/src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs +++ b/src/PowerShellEditorServices.Hosting/Commands/StartEditorServicesCommand.cs @@ -301,7 +301,7 @@ private void RemovePSReadLineForStartup() { _logger.Log(PsesLogLevel.Verbose, "Removing PSReadLine"); using SMA.PowerShell pwsh = SMA.PowerShell.Create(RunspaceMode.CurrentRunspace); - bool hasPSReadLine = pwsh.AddCommand(new CmdletInfo("Microsoft.PowerShell.Core\\Get-Module", typeof(GetModuleCommand))) + bool hasPSReadLine = pwsh.AddCommand(new CmdletInfo(@"Microsoft.PowerShell.Core\Get-Module", typeof(GetModuleCommand))) .AddParameter("Name", "PSReadLine") .Invoke() .Count > 0; @@ -310,7 +310,7 @@ private void RemovePSReadLineForStartup() { pwsh.Commands.Clear(); - pwsh.AddCommand(new CmdletInfo("Microsoft.PowerShell.Core\\Remove-Module", typeof(RemoveModuleCommand))) + pwsh.AddCommand(new CmdletInfo(@"Microsoft.PowerShell.Core\Remove-Module", typeof(RemoveModuleCommand))) .AddParameter("Name", "PSReadLine") .AddParameter("ErrorAction", "SilentlyContinue"); diff --git a/src/PowerShellEditorServices/Services/DebugAdapter/Handlers/LaunchAndAttachHandler.cs b/src/PowerShellEditorServices/Services/DebugAdapter/Handlers/LaunchAndAttachHandler.cs index 8b5b4dee6..ac6490c7b 100644 --- a/src/PowerShellEditorServices/Services/DebugAdapter/Handlers/LaunchAndAttachHandler.cs +++ b/src/PowerShellEditorServices/Services/DebugAdapter/Handlers/LaunchAndAttachHandler.cs @@ -378,9 +378,9 @@ await _executionService.ExecutePSCommandAsync( if (request.RunspaceName != null) { PSCommand getRunspaceIdCommand = new PSCommand() - .AddCommand("Microsoft.PowerShell.Utility\\Get-Runspace") + .AddCommand(@"Microsoft.PowerShell.Utility\Get-Runspace") .AddParameter("Name", request.RunspaceName) - .AddCommand("Microsoft.PowerShell.Utility\\Select-Object") + .AddCommand(@"Microsoft.PowerShell.Utility\Select-Object") .AddParameter("ExpandProperty", "Id"); try diff --git a/src/PowerShellEditorServices/Services/PowerShell/Debugging/DscBreakpointCapability.cs b/src/PowerShellEditorServices/Services/PowerShell/Debugging/DscBreakpointCapability.cs index 3bbd166ed..da486ccbc 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Debugging/DscBreakpointCapability.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Debugging/DscBreakpointCapability.cs @@ -92,8 +92,8 @@ public static async Task GetDscCapabilityAsync( if (!isDscInstalled.HasValue) { PSCommand psCommand = new PSCommand() - .AddCommand("Import-Module") - .AddArgument(@"C:\Program Files\DesiredStateConfiguration\1.0.0.0\Modules\PSDesiredStateConfiguration\PSDesiredStateConfiguration.psd1") + .AddCommand(@"Microsoft.PowerShell.Core\Import-Module") + .AddParameter("-Name", "PSDesiredStateConfiguration") .AddParameter("PassThru") .AddParameter("ErrorAction", ActionPreference.Ignore); @@ -101,7 +101,8 @@ public static async Task GetDscCapabilityAsync( await psesHost.ExecutePSCommandAsync( psCommand, CancellationToken.None, - new PowerShellExecutionOptions { ThrowOnError = false }).ConfigureAwait(false); + new PowerShellExecutionOptions { ThrowOnError = false }) + .ConfigureAwait(false); isDscInstalled = dscModule.Count > 0; logger.LogTrace("Side-by-side DSC module found: " + isDscInstalled.Value); diff --git a/src/PowerShellEditorServices/Services/PowerShell/Handlers/GetCommandHandler.cs b/src/PowerShellEditorServices/Services/PowerShell/Handlers/GetCommandHandler.cs index 12df83713..a9f48ce52 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Handlers/GetCommandHandler.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Handlers/GetCommandHandler.cs @@ -42,9 +42,9 @@ public async Task> Handle(GetCommandParams request, Cance // Executes the following: // Get-Command -CommandType Function,Cmdlet,ExternalScript | Sort-Object -Property Name psCommand - .AddCommand("Microsoft.PowerShell.Core\\Get-Command") + .AddCommand(@"Microsoft.PowerShell.Core\Get-Command") .AddParameter("CommandType", new[] { "Function", "Cmdlet", "ExternalScript" }) - .AddCommand("Microsoft.PowerShell.Utility\\Sort-Object") + .AddCommand(@"Microsoft.PowerShell.Utility\Sort-Object") .AddParameter("Property", "Name"); IEnumerable result = await _executionService.ExecutePSCommandAsync(psCommand, cancellationToken).ConfigureAwait(false); diff --git a/src/PowerShellEditorServices/Services/PowerShell/Handlers/PSHostProcessAndRunspaceHandlers.cs b/src/PowerShellEditorServices/Services/PowerShell/Handlers/PSHostProcessAndRunspaceHandlers.cs index fcf58cce3..bff586d44 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Handlers/PSHostProcessAndRunspaceHandlers.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Handlers/PSHostProcessAndRunspaceHandlers.cs @@ -74,11 +74,11 @@ public async Task Handle(GetRunspaceParams request, Cancella rs.Open(); ps.Runspace = rs; // Returns deserialized Runspaces. For simpler code, we use PSObject and rely on dynamic later. - runspaces = ps.AddCommand("Microsoft.PowerShell.Utility\\Get-Runspace").Invoke(); + runspaces = ps.AddCommand(@"Microsoft.PowerShell.Utility\Get-Runspace").Invoke(); } else { - PSCommand psCommand = new PSCommand().AddCommand("Microsoft.PowerShell.Utility\\Get-Runspace"); + PSCommand psCommand = new PSCommand().AddCommand(@"Microsoft.PowerShell.Utility\Get-Runspace"); // returns (not deserialized) Runspaces. For simpler code, we use PSObject and rely on dynamic later. runspaces = await _executionService.ExecutePSCommandAsync(psCommand, cancellationToken).ConfigureAwait(false); } diff --git a/src/PowerShellEditorServices/Services/PowerShell/Utility/CommandHelpers.cs b/src/PowerShellEditorServices/Services/PowerShell/Utility/CommandHelpers.cs index 38a581bec..6532c531a 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Utility/CommandHelpers.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Utility/CommandHelpers.cs @@ -237,7 +237,7 @@ public static async Task GetAliasesAsync( // our PSRL on idle handler. IReadOnlyList aliases = await executionService.ExecutePSCommandAsync( new PSCommand() - .AddCommand("Microsoft.PowerShell.Core\\Get-Command") + .AddCommand(@"Microsoft.PowerShell.Core\Get-Command") .AddParameter("ListImported", true) .AddParameter("CommandType", CommandTypes.Alias), cancellationToken).ConfigureAwait(false); diff --git a/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs b/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs index d7e85f3e4..1032e70f5 100644 --- a/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs +++ b/src/PowerShellEditorServices/Services/PowerShell/Utility/PowerShellExtensions.cs @@ -139,7 +139,7 @@ public static void SetCorrectExecutionPolicy(this PowerShell pwsh, ILogger logge // We want to get the list hierarchy of execution policies // Calling the cmdlet is the simplest way to do that IReadOnlyList policies = pwsh - .AddCommand("Microsoft.PowerShell.Security\\Get-ExecutionPolicy") + .AddCommand(@"Microsoft.PowerShell.Security\Get-ExecutionPolicy") .AddParameter("-List") .InvokeAndClear(); @@ -181,7 +181,7 @@ public static void SetCorrectExecutionPolicy(this PowerShell pwsh, ILogger logge logger.LogTrace("Setting execution policy to {Policy}", policyToSet); try { - pwsh.AddCommand("Microsoft.PowerShell.Security\\Set-ExecutionPolicy") + pwsh.AddCommand(@"Microsoft.PowerShell.Security\Set-ExecutionPolicy") .AddParameter("Scope", ExecutionPolicyScope.Process) .AddParameter("ExecutionPolicy", policyToSet) .AddParameter("Force") @@ -222,7 +222,7 @@ public static void LoadProfiles(this PowerShell pwsh, ProfilePathInfo profilePat public static void ImportModule(this PowerShell pwsh, string moduleNameOrPath) { - pwsh.AddCommand("Microsoft.PowerShell.Core\\Import-Module") + pwsh.AddCommand(@"Microsoft.PowerShell.Core\Import-Module") .AddParameter("-Name", moduleNameOrPath) .InvokeAndClear(); } diff --git a/src/PowerShellEditorServices/Services/Template/TemplateService.cs b/src/PowerShellEditorServices/Services/Template/TemplateService.cs index 4de0de520..e7295ec8c 100644 --- a/src/PowerShellEditorServices/Services/Template/TemplateService.cs +++ b/src/PowerShellEditorServices/Services/Template/TemplateService.cs @@ -74,7 +74,10 @@ public async Task ImportPlasterIfInstalledAsync() IReadOnlyList moduleObject = await _executionService.ExecutePSCommandAsync( - psCommand, CancellationToken.None).ConfigureAwait(false); + psCommand, + CancellationToken.None, + new PowerShellExecutionOptions { ThrowOnError = false }) + .ConfigureAwait(false); isPlasterInstalled = moduleObject.Count > 0; _logger.LogTrace("Plaster installed: " + isPlasterInstalled.Value); @@ -86,13 +89,19 @@ await _executionService.ExecutePSCommandAsync( psCommand = new PSCommand(); psCommand - .AddCommand("Import-Module") + .AddCommand(@"Microsoft.PowerShell.Core\Import-Module") .AddParameter("ModuleInfo", (PSModuleInfo)moduleObject[0].ImmediateBaseObject) - .AddParameter("PassThru"); + .AddParameter("PassThru") + .AddParameter("ErrorAction", ActionPreference.Ignore); - IReadOnlyList importResult = await _executionService.ExecutePSCommandAsync(psCommand, CancellationToken.None).ConfigureAwait(false); + IReadOnlyList plasterModule = + await _executionService.ExecutePSCommandAsync( + psCommand, + CancellationToken.None, + new PowerShellExecutionOptions { ThrowOnError = false }) + .ConfigureAwait(false); - isPlasterLoaded = importResult.Count > 0; + isPlasterLoaded = plasterModule.Count > 0; _logger.LogTrace("Plaster loaded: " + isPlasterLoaded); } } diff --git a/src/PowerShellEditorServices/Services/Workspace/RemoteFileManagerService.cs b/src/PowerShellEditorServices/Services/Workspace/RemoteFileManagerService.cs index 0a7ed4c74..a3e46f4f1 100644 --- a/src/PowerShellEditorServices/Services/Workspace/RemoteFileManagerService.cs +++ b/src/PowerShellEditorServices/Services/Workspace/RemoteFileManagerService.cs @@ -316,7 +316,7 @@ public async Task FetchRemoteFileAsync( { // Load the file contents from the remote machine and create the buffer PSCommand command = new PSCommand() - .AddCommand("Microsoft.PowerShell.Management\\Get-Content") + .AddCommand(@"Microsoft.PowerShell.Management\Get-Content") .AddParameter("Path", remoteFilePath) .AddParameter("Raw"); diff --git a/test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs b/test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs index bfdac2774..294901362 100644 --- a/test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs +++ b/test/PowerShellEditorServices.Test/Debugging/DebugServiceTests.cs @@ -1140,7 +1140,7 @@ await debugService.SetCommandBreakpointsAsync( VariableDetailsBase var = Array.Find(variables, v => v.Name == "$file"); VariableDetailsBase[] childVars = await debugService.GetVariables(var.Id, CancellationToken.None).ConfigureAwait(true); Assert.Contains(childVars, i => i.Name is "PSPath"); - Assert.Contains(childVars, i => i.Name is "PSProvider" && i.ValueString is "Microsoft.PowerShell.Core\\FileSystem"); + Assert.Contains(childVars, i => i.Name is "PSProvider" && i.ValueString is @"Microsoft.PowerShell.Core\FileSystem"); Assert.Contains(childVars, i => i.Name is "Exists" && i.ValueString is "$true"); Assert.Contains(childVars, i => i.Name is "LastAccessTime"); } diff --git a/test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs b/test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs index 430687f93..613202094 100644 --- a/test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs +++ b/test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs @@ -51,7 +51,7 @@ public void Dispose() [Fact] public async Task CanRegisterAndInvokeCommandWithCmdletName() { - string filePath = TestUtilities.NormalizePath("C:\\Temp\\Test.ps1"); + string filePath = TestUtilities.NormalizePath(@"C:\Temp\Test.ps1"); ScriptFile currentFile = new(new Uri(filePath), "This is a test file", new Version("7.0")); EditorContext editorContext = new( editorOperations: null, @@ -87,7 +87,7 @@ await psesHost.ExecutePSCommandAsync( [Fact] public async Task CanRegisterAndInvokeCommandWithScriptBlock() { - string filePath = TestUtilities.NormalizePath("C:\\Temp\\Test.ps1"); + string filePath = TestUtilities.NormalizePath(@"C:\Temp\Test.ps1"); ScriptFile currentFile = new(new Uri(filePath), "This is a test file", new Version("7.0")); EditorContext editorContext = new( editorOperations: null, @@ -149,7 +149,7 @@ await psesHost.ExecutePSCommandAsync( [Fact] public async Task CanUnregisterCommand() { - string filePath = TestUtilities.NormalizePath("C:\\Temp\\Test.ps1"); + string filePath = TestUtilities.NormalizePath(@"C:\Temp\Test.ps1"); ScriptFile currentFile = new(new Uri(filePath), "This is a test file", new Version("7.0")); EditorContext editorContext = new( editorOperations: null, diff --git a/test/PowerShellEditorServices.Test/Session/PathEscapingTests.cs b/test/PowerShellEditorServices.Test/Session/PathEscapingTests.cs index ed591a395..3d4f0d479 100644 --- a/test/PowerShellEditorServices.Test/Session/PathEscapingTests.cs +++ b/test/PowerShellEditorServices.Test/Session/PathEscapingTests.cs @@ -12,17 +12,17 @@ public class PathEscapingTests [Theory] [InlineData("DebugTest.ps1", "DebugTest.ps1")] [InlineData("../../DebugTest.ps1", "../../DebugTest.ps1")] - [InlineData("C:\\Users\\me\\Documents\\DebugTest.ps1", "C:\\Users\\me\\Documents\\DebugTest.ps1")] + [InlineData(@"C:\Users\me\Documents\DebugTest.ps1", @"C:\Users\me\Documents\DebugTest.ps1")] [InlineData("/home/me/Documents/weird&folder/script.ps1", "/home/me/Documents/weird&folder/script.ps1")] [InlineData("./path/with some/spaces", "./path/with some/spaces")] - [InlineData("C:\\path\\with[some]brackets\\file.ps1", "C:\\path\\with`[some`]brackets\\file.ps1")] - [InlineData("C:\\look\\an*\\here.ps1", "C:\\look\\an`*\\here.ps1")] + [InlineData(@"C:\path\with[some]brackets\file.ps1", @"C:\path\with`[some`]brackets\file.ps1")] + [InlineData(@"C:\look\an*\here.ps1", @"C:\look\an`*\here.ps1")] [InlineData("/Users/me/Documents/?here.ps1", "/Users/me/Documents/`?here.ps1")] [InlineData("/Brackets [and s]paces/path.ps1", "/Brackets `[and s`]paces/path.ps1")] [InlineData("/CJK.chars/脚本/hello.ps1", "/CJK.chars/脚本/hello.ps1")] [InlineData("/CJK.chars/脚本/[hello].ps1", "/CJK.chars/脚本/`[hello`].ps1")] - [InlineData("C:\\Animals\\утка\\quack.ps1", "C:\\Animals\\утка\\quack.ps1")] - [InlineData("C:\\&nimals\\утка\\qu*ck?.ps1", "C:\\&nimals\\утка\\qu`*ck`?.ps1")] + [InlineData(@"C:\Animals\утка\quack.ps1", @"C:\Animals\утка\quack.ps1")] + [InlineData(@"C:\&nimals\утка\qu*ck?.ps1", @"C:\&nimals\утка\qu`*ck`?.ps1")] public void CorrectlyWildcardEscapesPathsNoSpaces(string unescapedPath, string escapedPath) { string extensionEscapedPath = PathUtils.WildcardEscapePath(unescapedPath); @@ -33,17 +33,17 @@ public void CorrectlyWildcardEscapesPathsNoSpaces(string unescapedPath, string e [Theory] [InlineData("DebugTest.ps1", "DebugTest.ps1")] [InlineData("../../DebugTest.ps1", "../../DebugTest.ps1")] - [InlineData("C:\\Users\\me\\Documents\\DebugTest.ps1", "C:\\Users\\me\\Documents\\DebugTest.ps1")] + [InlineData(@"C:\Users\me\Documents\DebugTest.ps1", @"C:\Users\me\Documents\DebugTest.ps1")] [InlineData("/home/me/Documents/weird&folder/script.ps1", "/home/me/Documents/weird&folder/script.ps1")] [InlineData("./path/with some/spaces", "./path/with` some/spaces")] - [InlineData("C:\\path\\with[some]brackets\\file.ps1", "C:\\path\\with`[some`]brackets\\file.ps1")] - [InlineData("C:\\look\\an*\\here.ps1", "C:\\look\\an`*\\here.ps1")] + [InlineData(@"C:\path\with[some]brackets\file.ps1", @"C:\path\with`[some`]brackets\file.ps1")] + [InlineData(@"C:\look\an*\here.ps1", @"C:\look\an`*\here.ps1")] [InlineData("/Users/me/Documents/?here.ps1", "/Users/me/Documents/`?here.ps1")] [InlineData("/Brackets [and s]paces/path.ps1", "/Brackets` `[and` s`]paces/path.ps1")] [InlineData("/CJK chars/脚本/hello.ps1", "/CJK` chars/脚本/hello.ps1")] [InlineData("/CJK chars/脚本/[hello].ps1", "/CJK` chars/脚本/`[hello`].ps1")] - [InlineData("C:\\Animal s\\утка\\quack.ps1", "C:\\Animal` s\\утка\\quack.ps1")] - [InlineData("C:\\&nimals\\утка\\qu*ck?.ps1", "C:\\&nimals\\утка\\qu`*ck`?.ps1")] + [InlineData(@"C:\Animal s\утка\quack.ps1", @"C:\Animal` s\утка\quack.ps1")] + [InlineData(@"C:\&nimals\утка\qu*ck?.ps1", @"C:\&nimals\утка\qu`*ck`?.ps1")] public void CorrectlyWildcardEscapesPathsSpaces(string unescapedPath, string escapedPath) { string extensionEscapedPath = PathUtils.WildcardEscapePath(unescapedPath, escapeSpaces: true); diff --git a/test/PowerShellEditorServices.Test/Session/PsesInternalHostTests.cs b/test/PowerShellEditorServices.Test/Session/PsesInternalHostTests.cs index 76f303681..1e141bbc9 100644 --- a/test/PowerShellEditorServices.Test/Session/PsesInternalHostTests.cs +++ b/test/PowerShellEditorServices.Test/Session/PsesInternalHostTests.cs @@ -223,7 +223,7 @@ await psesHost.ExecutePSCommandAsync( [Theory] [InlineData("")] // Regression test for "unset" path. - [InlineData("C:\\Some\\Bad\\Directory")] // Non-existent directory. + [InlineData(@"C:\Some\Bad\Directory")] // Non-existent directory. [InlineData("testhost.dll")] // Existent file. public async Task CanHandleBadInitialWorkingDirectory(string path) { diff --git a/test/PowerShellEditorServices.Test/Session/ScriptFileTests.cs b/test/PowerShellEditorServices.Test/Session/ScriptFileTests.cs index 4839eacfa..11c27c4cf 100644 --- a/test/PowerShellEditorServices.Test/Session/ScriptFileTests.cs +++ b/test/PowerShellEditorServices.Test/Session/ScriptFileTests.cs @@ -654,7 +654,7 @@ public void DocumentUriReturnsCorrectStringForAbsolutePath() scriptFile = new ScriptFile(DocumentUri.FromFileSystemPath(path), emptyStringReader, PowerShellVersion); Assert.Equal("file:///home/NaomiNagata/projects/Rocinate/Proto%3AMole%3Acule.ps1", scriptFile.DocumentUri); - path = "/home/JamesHolden/projects/Rocinate/Proto:Mole\\cule.ps1"; + path = @"/home/JamesHolden/projects/Rocinate/Proto:Mole\cule.ps1"; scriptFile = new ScriptFile(DocumentUri.FromFileSystemPath(path), emptyStringReader, PowerShellVersion); Assert.Equal("file:///home/JamesHolden/projects/Rocinate/Proto%3AMole%5Ccule.ps1", scriptFile.DocumentUri); } @@ -662,12 +662,13 @@ public void DocumentUriReturnsCorrectStringForAbsolutePath() [Trait("Category", "ScriptFile")] [Theory] - [InlineData("C:\\Users\\me\\Documents\\test.ps1", false)] + [InlineData(@"C:\Users\me\Documents\test.ps1", false)] [InlineData("/Users/me/Documents/test.ps1", false)] [InlineData("vscode-notebook-cell:/Users/me/Documents/test.ps1#0001", true)] [InlineData("https://microsoft.com", true)] [InlineData("Untitled:Untitled-1", true)] - [InlineData("'a log statement' > 'c:\\Users\\me\\Documents\\test.txt'\r\n", false)] + [InlineData(@"'a log statement' > 'c:\Users\me\Documents\test.txt' +", false)] public void IsUntitledFileIsCorrect(string path, bool expected) => Assert.Equal(expected, ScriptFile.IsUntitledPath(path)); } }