From cf6876e71639145eff189658e939a43338cc92f6 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 1 Apr 2021 13:03:19 -0700 Subject: [PATCH] Allow `Set-Clipboard` to accept empty string (#14579) --- .../commands/management/Clipboard.cs | 31 ++++++++++--------- .../Clipboard.Tests.ps1 | 10 ++++++ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Clipboard.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Clipboard.cs index 5ce64350fb..45e2a183b3 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Clipboard.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Clipboard.cs @@ -19,7 +19,8 @@ namespace Microsoft.PowerShell.Commands.Internal private static string StartProcess( string tool, string args, - string stdin = "") + string stdin = "", + bool readStdout = true) { ProcessStartInfo startInfo = new(); startInfo.UseShellExecute = false; @@ -28,7 +29,7 @@ namespace Microsoft.PowerShell.Commands.Internal startInfo.RedirectStandardError = true; startInfo.FileName = tool; startInfo.Arguments = args; - string stdout; + string stdout = string.Empty; using (Process process = new()) { @@ -43,15 +44,15 @@ namespace Microsoft.PowerShell.Commands.Internal return string.Empty; } - if (!string.IsNullOrEmpty(stdin)) + process.StandardInput.Write(stdin); + process.StandardInput.Close(); + + if (readStdout) { - process.StandardInput.Write(stdin); - process.StandardInput.Close(); + stdout = process.StandardOutput.ReadToEnd(); } - stdout = process.StandardOutput.ReadToEnd(); process.WaitForExit(250); - _clipboardSupported = process.ExitCode == 0; } @@ -93,11 +94,6 @@ namespace Microsoft.PowerShell.Commands.Internal public static void SetText(string text) { - if (string.IsNullOrEmpty(text)) - { - return; - } - if (_clipboardSupported == false) { _internalClipboard = text; @@ -114,7 +110,14 @@ namespace Microsoft.PowerShell.Commands.Internal else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { tool = "xclip"; - args = "-selection clipboard -in"; + if (string.IsNullOrEmpty(text)) + { + args = "-selection clipboard /dev/null"; + } + else + { + args = "-selection clipboard -in"; + } } else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) { @@ -126,7 +129,7 @@ namespace Microsoft.PowerShell.Commands.Internal return; } - StartProcess(tool, args, text); + StartProcess(tool, args, text, readStdout: false); if (_clipboardSupported == false) { _internalClipboard = text; diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1 index d28394c9f2..8cfc07b875 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1 @@ -41,5 +41,15 @@ Describe 'Clipboard cmdlet tests' -Tag CI { 'world' | Set-Clipboard -Append Get-Clipboard -Raw | Should -BeExactly "hello$([Environment]::NewLine)world" } + + It 'Set-Clipboard accepts string' -TestCases @( + @{ value = 'empty'; text = "" } + @{ value = 'null' ; text = $null } + ){ + param ($text) + + $text | Set-Clipboard + Get-Clipboard -Raw | Should -BeNullOrEmpty + } } }