From 08ff5fb5d8b4f83bd0e94d972aa11e3427920cfb Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 7 Jan 2021 13:47:34 -0800 Subject: [PATCH 1/6] Allow `Set-Clipboard` to accept empty string --- .../commands/management/Clipboard.cs | 12 ++---------- .../Clipboard.Tests.ps1 | 5 +++++ 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Clipboard.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Clipboard.cs index 5ce64350fbd..d7a3d6f87e5 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Clipboard.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Clipboard.cs @@ -43,11 +43,8 @@ private static string StartProcess( return string.Empty; } - if (!string.IsNullOrEmpty(stdin)) - { - process.StandardInput.Write(stdin); - process.StandardInput.Close(); - } + process.StandardInput.Write(stdin); + process.StandardInput.Close(); stdout = process.StandardOutput.ReadToEnd(); process.WaitForExit(250); @@ -93,11 +90,6 @@ public static string GetText() public static void SetText(string text) { - if (string.IsNullOrEmpty(text)) - { - return; - } - 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 d28394c9f26..3321c3515ab 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1 @@ -41,5 +41,10 @@ Describe 'Clipboard cmdlet tests' -Tag CI { 'world' | Set-Clipboard -Append Get-Clipboard -Raw | Should -BeExactly "hello$([Environment]::NewLine)world" } + + It 'Set-Clipboard accepts empty string' { + '' | Set-Clipboard + Get-Clipboard -Raw | Should -BeNullOrEmpty + } } } From 6bd9d3cf30890ae5f36b38c0dddfe19f0a2972ce Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 7 Jan 2021 14:36:44 -0800 Subject: [PATCH 2/6] Fix to work on Linux with xclip --- .../commands/management/Clipboard.cs | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Clipboard.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Clipboard.cs index d7a3d6f87e5..45e2a183b3e 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 @@ internal static class Clipboard 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 @@ private static string StartProcess( startInfo.RedirectStandardError = true; startInfo.FileName = tool; startInfo.Arguments = args; - string stdout; + string stdout = string.Empty; using (Process process = new()) { @@ -46,9 +47,12 @@ private static string StartProcess( process.StandardInput.Write(stdin); process.StandardInput.Close(); - stdout = process.StandardOutput.ReadToEnd(); - process.WaitForExit(250); + if (readStdout) + { + stdout = process.StandardOutput.ReadToEnd(); + } + process.WaitForExit(250); _clipboardSupported = process.ExitCode == 0; } @@ -106,7 +110,14 @@ public static void SetText(string text) 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)) { @@ -118,7 +129,7 @@ public static void SetText(string text) return; } - StartProcess(tool, args, text); + StartProcess(tool, args, text, readStdout: false); if (_clipboardSupported == false) { _internalClipboard = text; From 9eee940587d3c9d79280b79b30793e3cb2c935c4 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 4 Feb 2021 14:50:20 -0800 Subject: [PATCH 3/6] Update src/Microsoft.PowerShell.Commands.Management/commands/management/Clipboard.cs Co-authored-by: Ilya --- .../commands/management/Clipboard.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Clipboard.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Clipboard.cs index 45e2a183b3e..96e95a81f15 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Clipboard.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Clipboard.cs @@ -19,7 +19,7 @@ internal static class Clipboard private static string StartProcess( string tool, string args, - string stdin = "", + string stdin = string.Empty, bool readStdout = true) { ProcessStartInfo startInfo = new(); From 3d1f8162e6eeb5bae0846d514e5d168b13b589bf Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 4 Feb 2021 16:09:32 -0800 Subject: [PATCH 4/6] Fix build break --- .../commands/management/Clipboard.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Clipboard.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Clipboard.cs index 96e95a81f15..45e2a183b3e 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Clipboard.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Clipboard.cs @@ -19,7 +19,7 @@ internal static class Clipboard private static string StartProcess( string tool, string args, - string stdin = string.Empty, + string stdin = "", bool readStdout = true) { ProcessStartInfo startInfo = new(); From 23160221fd181b30f388151bd49c49d7ba0f6e1c Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Thu, 4 Feb 2021 16:12:28 -0800 Subject: [PATCH 5/6] Add $null test --- .../Microsoft.PowerShell.Management/Clipboard.Tests.ps1 | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1 index 3321c3515ab..5ca85b36490 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1 @@ -42,8 +42,13 @@ Describe 'Clipboard cmdlet tests' -Tag CI { Get-Clipboard -Raw | Should -BeExactly "hello$([Environment]::NewLine)world" } - It 'Set-Clipboard accepts empty string' { - '' | Set-Clipboard + It 'Set-Clipboard accepts string' -TestCases @( + @{ value = 'empty'; input = "" } + @{ value = 'null' ; input = $null } + ){ + param ($input) + + $input | Set-Clipboard Get-Clipboard -Raw | Should -BeNullOrEmpty } } From 7ab3102e045bfaeb2d93aaed4936123b202e7750 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 8 Mar 2021 21:52:37 -0800 Subject: [PATCH 6/6] replace $input with $text --- .../Microsoft.PowerShell.Management/Clipboard.Tests.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1 index 5ca85b36490..8cfc07b8755 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1 @@ -43,12 +43,12 @@ Describe 'Clipboard cmdlet tests' -Tag CI { } It 'Set-Clipboard accepts string' -TestCases @( - @{ value = 'empty'; input = "" } - @{ value = 'null' ; input = $null } + @{ value = 'empty'; text = "" } + @{ value = 'null' ; text = $null } ){ - param ($input) + param ($text) - $input | Set-Clipboard + $text | Set-Clipboard Get-Clipboard -Raw | Should -BeNullOrEmpty } }