From e1c1550ceebda6e51ab982d1a4ea9c3fba151fe6 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 13 Jun 2022 17:11:45 -0700 Subject: [PATCH] Add regression test for accidentally allowing removal of `$psEditor` --- .../Extensions/ExtensionCommandTests.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs b/test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs index ee2163024..1f7998ec5 100644 --- a/test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs +++ b/test/PowerShellEditorServices.Test/Extensions/ExtensionCommandTests.cs @@ -179,5 +179,19 @@ await psesHost.ExecutePSCommandAsync( await Assert.ThrowsAsync( () => extensionCommandService.InvokeCommandAsync("test.scriptblock", editorContext)).ConfigureAwait(true); } + + [Fact] + public async Task CannotRemovePSEditorVariable() + { + ActionPreferenceStopException exception = await Assert.ThrowsAsync( + () => psesHost.ExecutePSCommandAsync( + new PSCommand().AddScript("Remove-Variable psEditor -ErrorAction Stop"), + CancellationToken.None) + ).ConfigureAwait(true); + + Assert.Equal( + "The running command stopped because the preference variable \"ErrorActionPreference\" or common parameter is set to Stop: Cannot remove variable psEditor because it is constant or read-only. If the variable is read-only, try the operation again specifying the Force option.", + exception.Message); + } } }