From eec3b334cc5e1cfd69fff291bac2b6984577f184 Mon Sep 17 00:00:00 2001 From: Alex Jordan Date: Tue, 21 Jun 2016 12:52:40 -0700 Subject: [PATCH 1/9] Adding logic for invoke-item for non-Windows OSes --- .../namespaces/FileSystemProvider.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 8f691de069..e10a175945 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -1331,7 +1331,22 @@ namespace Microsoft.PowerShell.Commands if (ShouldProcess(resource, action)) { - System.Diagnostics.Process.Start(path); + System.Diagnostics.Process invokeProcess = new System.Diagnostics.Process(); + invokeProcess.StartInfo.UseShellExecute = true; + + if (Platform.IsWindows) + { + System.Diagnostics.Process.Start(path); + } else if (Platform.IsOSX) { + invokeProcess.StartInfo.FileName = "open"; + invokeProcess.StartInfo.Arguments = path; + } else if (Platform.IsLinux) { + invokeProcess.StartInfo.FileName = "xdg-open"; + invokeProcess.StartInfo.Arguments = path; + } + invokeProcess.Start(); + + //System.Diagnostics.Process.Start(path); } } // InvokeDefaultAction @@ -7784,7 +7799,7 @@ namespace Microsoft.PowerShell.Commands private bool wait; /// - /// When the –Raw switch is present, we don’t do any breaks on newlines, + /// When the �Raw switch is present, we don�t do any breaks on newlines, /// and only emit one object to the pipeline: all of the content. /// [Parameter] From 61a6713f4cc1e5c77143af14c0ad046c3c24dca4 Mon Sep 17 00:00:00 2001 From: Alex Jordan Date: Mon, 27 Jun 2016 10:06:37 -0700 Subject: [PATCH 2/9] Invoke-Item tests added --- test/powershell/Invoke-Item.Tests.ps1 | 46 +++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 test/powershell/Invoke-Item.Tests.ps1 diff --git a/test/powershell/Invoke-Item.Tests.ps1 b/test/powershell/Invoke-Item.Tests.ps1 new file mode 100644 index 0000000000..685af663eb --- /dev/null +++ b/test/powershell/Invoke-Item.Tests.ps1 @@ -0,0 +1,46 @@ +function Clean-State +{ + if (Test-Path $FullyQualifiedLink) + { + Remove-Item $FullyQualifiedLink -Force + } + + if (Test-Path $FullyQualifiedFile) + { + Remove-Item $FullyQualifiedFile -Force + } + + if (Test-Path $FullyQualifiedFolder) + { + Remove-Item $FullyQualifiedFolder -Force + } +} + +Describe "New-Item" { + $tmpDirectory = $TestDrive + $testfile = "testfile.txt" + $testfolder = "newDirectory" + $testlink = "testlink" + $FullyQualifiedFile = Join-Path -Path $tmpDirectory -ChildPath $testfile + $FullyQualifiedFolder = Join-Path -Path $tmpDirectory -ChildPath $testfolder + $FullyQualifiedLink = Join-Path -Path $tmpDirectory -ChildPath $testlink + + BeforeEach { + Clean-State + } + + It "Should call the function without error" { + { + New-Item -Name $testfile -Path $tmpDirectory -ItemType file } | Should Not Throw + } + + It "Should invoke a text file without error" { + New-Item -Name $testfile -Path $tmpDirectory -ItemType file + Test-Path $FullyQualifiedFile | Should Be $true + $fileInfo = Get-ChildItem $FullyQualifiedFile + $fileInfo.Target | Should Be $null + $fileInfo.LinkType | Should Be $null + Invoke-Item $FullyQualifiedFile | Should Not throw + } + +} \ No newline at end of file From 65f31c06bb3a1ada8e705ca3106b141eb1c83bc2 Mon Sep 17 00:00:00 2001 From: Alex Jordan Date: Mon, 27 Jun 2016 15:41:40 -0700 Subject: [PATCH 3/9] Adding Invoke-Item tests --- test/powershell/Invoke-Item.Tests.ps1 | 92 +++++++++++++++++---------- 1 file changed, 60 insertions(+), 32 deletions(-) diff --git a/test/powershell/Invoke-Item.Tests.ps1 b/test/powershell/Invoke-Item.Tests.ps1 index 685af663eb..05e1013cd4 100644 --- a/test/powershell/Invoke-Item.Tests.ps1 +++ b/test/powershell/Invoke-Item.Tests.ps1 @@ -1,22 +1,7 @@ -function Clean-State -{ - if (Test-Path $FullyQualifiedLink) - { - Remove-Item $FullyQualifiedLink -Force - } +using namespace System.Diagnostics - if (Test-Path $FullyQualifiedFile) - { - Remove-Item $FullyQualifiedFile -Force - } +Describe "Invoke-Item" { - if (Test-Path $FullyQualifiedFolder) - { - Remove-Item $FullyQualifiedFolder -Force - } -} - -Describe "New-Item" { $tmpDirectory = $TestDrive $testfile = "testfile.txt" $testfolder = "newDirectory" @@ -24,23 +9,66 @@ Describe "New-Item" { $FullyQualifiedFile = Join-Path -Path $tmpDirectory -ChildPath $testfile $FullyQualifiedFolder = Join-Path -Path $tmpDirectory -ChildPath $testfolder $FullyQualifiedLink = Join-Path -Path $tmpDirectory -ChildPath $testlink - - BeforeEach { - Clean-State + + function NewProcessStartInfo([string]$CommandLine, [switch]$RedirectStdIn) + { + return [ProcessStartInfo]@{ + FileName = $powershell + Arguments = $CommandLine + RedirectStandardInput = $RedirectStdIn + RedirectStandardOutput = $true + RedirectStandardError = $true + UseShellExecute = $false + } } - It "Should call the function without error" { - { - New-Item -Name $testfile -Path $tmpDirectory -ItemType file } | Should Not Throw + function RunPowerShell([ProcessStartInfo]$debugfn) + { + $process = [Process]::Start($debugfn) + return $process + } + + function EnsureChildHasExited([Process]$process, [int]$WaitTimeInMS = 15000) + { + $process.WaitForExit($WaitTimeInMS) + + if (!$process.HasExited) + { + $process.HasExited | Should Be $true + $process.Kill() + } + } + + function Clean-State + { + if (Test-Path $FullyQualifiedLink) + { + Remove-Item $FullyQualifiedLink -Force + } + + if (Test-Path $FullyQualifiedFile) + { + Remove-Item $FullyQualifiedFile -Force + } + + if (Test-Path $FullyQualifiedFolder) + { + Remove-Item $FullyQualifiedFolder -Force + } + } + + BeforeAll { + $powershell = Join-Path -Path $PsHome -ChildPath "powershell" + } + + It "Should call the function without error" { + { New-Item -Name $testfile -Path $tmpDirectory -ItemType file } | Should Not Throw } It "Should invoke a text file without error" { - New-Item -Name $testfile -Path $tmpDirectory -ItemType file - Test-Path $FullyQualifiedFile | Should Be $true - $fileInfo = Get-ChildItem $FullyQualifiedFile - $fileInfo.Target | Should Be $null - $fileInfo.LinkType | Should Be $null - Invoke-Item $FullyQualifiedFile | Should Not throw - } - -} \ No newline at end of file + $debugfn = NewProcessStartInfo "-noprofile ""``Invoke-Item $FullyQualifiedFile`n" -RedirectStdIn + $process = RunPowerShell $debugfn + EnsureChildHasExited $process + $process.ExitCode | Should Be 0 + } +} From fd7a16778cff5dd4d5dfa1b98bdce93b7a7f9218 Mon Sep 17 00:00:00 2001 From: Alex Jordan Date: Mon, 27 Jun 2016 16:12:51 -0700 Subject: [PATCH 4/9] Fixed unknown char in comments Cosmetic change. --- .../namespaces/FileSystemProvider.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index e10a175945..c7a69fd540 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -7799,7 +7799,7 @@ namespace Microsoft.PowerShell.Commands private bool wait; /// - /// When the �Raw switch is present, we don�t do any breaks on newlines, + /// When the Raw switch is present, we don't do any breaks on newlines, /// and only emit one object to the pipeline: all of the content. /// [Parameter] From 7cac07f31cd7fa5af62aaf7626597bff81c1e98a Mon Sep 17 00:00:00 2001 From: Alex Jordan Date: Wed, 29 Jun 2016 12:32:46 -0700 Subject: [PATCH 5/9] Adding pending to invokeitem tests due to windows bug --- test/powershell/Invoke-Item.Tests.ps1 | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/powershell/Invoke-Item.Tests.ps1 b/test/powershell/Invoke-Item.Tests.ps1 index 05e1013cd4..9562284c4b 100644 --- a/test/powershell/Invoke-Item.Tests.ps1 +++ b/test/powershell/Invoke-Item.Tests.ps1 @@ -61,11 +61,13 @@ Describe "Invoke-Item" { $powershell = Join-Path -Path $PsHome -ChildPath "powershell" } - It "Should call the function without error" { +#Both tests are pending due to a bug in Invoke-Item on Windows. Fixed for Linux + + It "Should call the function without error" -Pending { { New-Item -Name $testfile -Path $tmpDirectory -ItemType file } | Should Not Throw } - It "Should invoke a text file without error" { + It "Should invoke a text file without error" -Pending { $debugfn = NewProcessStartInfo "-noprofile ""``Invoke-Item $FullyQualifiedFile`n" -RedirectStdIn $process = RunPowerShell $debugfn EnsureChildHasExited $process From 7bc1bffdcadf75719f0a48ac3c77d92223afa79e Mon Sep 17 00:00:00 2001 From: Alex Jordan Date: Wed, 29 Jun 2016 13:56:46 -0700 Subject: [PATCH 6/9] Adding isWindows to invoke item test and new test location --- .../Invoke-Item.Tests.ps1 | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 new file mode 100644 index 0000000000..efc9b7917c --- /dev/null +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Invoke-Item.Tests.ps1 @@ -0,0 +1,76 @@ +using namespace System.Diagnostics + +Describe "Invoke-Item" { + + $tmpDirectory = $TestDrive + $testfile = "testfile.txt" + $testfolder = "newDirectory" + $testlink = "testlink" + $FullyQualifiedFile = Join-Path -Path $tmpDirectory -ChildPath $testfile + $FullyQualifiedFolder = Join-Path -Path $tmpDirectory -ChildPath $testfolder + $FullyQualifiedLink = Join-Path -Path $tmpDirectory -ChildPath $testlink + + function NewProcessStartInfo([string]$CommandLine, [switch]$RedirectStdIn) + { + return [ProcessStartInfo]@{ + FileName = $powershell + Arguments = $CommandLine + RedirectStandardInput = $RedirectStdIn + RedirectStandardOutput = $true + RedirectStandardError = $true + UseShellExecute = $false + } + } + + function RunPowerShell([ProcessStartInfo]$debugfn) + { + $process = [Process]::Start($debugfn) + return $process + } + + function EnsureChildHasExited([Process]$process, [int]$WaitTimeInMS = 15000) + { + $process.WaitForExit($WaitTimeInMS) + + if (!$process.HasExited) + { + $process.HasExited | Should Be $true + $process.Kill() + } + } + + function Clean-State + { + if (Test-Path $FullyQualifiedLink) + { + Remove-Item $FullyQualifiedLink -Force + } + + if (Test-Path $FullyQualifiedFile) + { + Remove-Item $FullyQualifiedFile -Force + } + + if (Test-Path $FullyQualifiedFolder) + { + Remove-Item $FullyQualifiedFolder -Force + } + } + + BeforeAll { + $powershell = Join-Path -Path $PsHome -ChildPath "powershell" + } + +#Both tests are pending due to a bug in Invoke-Item on Windows. Fixed for Linux + + It "Should call the function without error" -Pending:$IsWindows { + { New-Item -Name $testfile -Path $tmpDirectory -ItemType file } | Should Not Throw + } + + It "Should invoke a text file without error" -Pending:$IsWindows { + $debugfn = NewProcessStartInfo "-noprofile ""``Invoke-Item $FullyQualifiedFile`n" -RedirectStdIn + $process = RunPowerShell $debugfn + EnsureChildHasExited $process + $process.ExitCode | Should Be 0 + } +} From e580941667329312e73163281f46c873c55855d2 Mon Sep 17 00:00:00 2001 From: Alex Jordan Date: Tue, 5 Jul 2016 16:05:58 -0700 Subject: [PATCH 7/9] UseShellExecute moved to OS guards for Invoke-Item --- .../namespaces/FileSystemProvider.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index c7a69fd540..b1f4abac39 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -1331,22 +1331,25 @@ namespace Microsoft.PowerShell.Commands if (ShouldProcess(resource, action)) { - System.Diagnostics.Process invokeProcess = new System.Diagnostics.Process(); - invokeProcess.StartInfo.UseShellExecute = true; - if (Platform.IsWindows) { System.Diagnostics.Process.Start(path); - } else if (Platform.IsOSX) { + } + else if (Platform.IsOSX) { + System.Diagnostics.Process invokeProcess = new System.Diagnostics.Process(); + invokeProcess.StartInfo.UseShellExecute = true; invokeProcess.StartInfo.FileName = "open"; invokeProcess.StartInfo.Arguments = path; - } else if (Platform.IsLinux) { + } + else if (Platform.IsLinux) { + System.Diagnostics.Process invokeProcess = new System.Diagnostics.Process(); + invokeProcess.StartInfo.UseShellExecute = true; invokeProcess.StartInfo.FileName = "xdg-open"; invokeProcess.StartInfo.Arguments = path; } + invokeProcess.Start(); - //System.Diagnostics.Process.Start(path); } } // InvokeDefaultAction From 946280b4f347379b97a86478c0536bf8bffbb860 Mon Sep 17 00:00:00 2001 From: Alex Jordan Date: Tue, 5 Jul 2016 18:05:56 -0700 Subject: [PATCH 8/9] Removing extra invokeitem test --- test/powershell/Invoke-Item.Tests.ps1 | 76 --------------------------- 1 file changed, 76 deletions(-) delete mode 100644 test/powershell/Invoke-Item.Tests.ps1 diff --git a/test/powershell/Invoke-Item.Tests.ps1 b/test/powershell/Invoke-Item.Tests.ps1 deleted file mode 100644 index 9562284c4b..0000000000 --- a/test/powershell/Invoke-Item.Tests.ps1 +++ /dev/null @@ -1,76 +0,0 @@ -using namespace System.Diagnostics - -Describe "Invoke-Item" { - - $tmpDirectory = $TestDrive - $testfile = "testfile.txt" - $testfolder = "newDirectory" - $testlink = "testlink" - $FullyQualifiedFile = Join-Path -Path $tmpDirectory -ChildPath $testfile - $FullyQualifiedFolder = Join-Path -Path $tmpDirectory -ChildPath $testfolder - $FullyQualifiedLink = Join-Path -Path $tmpDirectory -ChildPath $testlink - - function NewProcessStartInfo([string]$CommandLine, [switch]$RedirectStdIn) - { - return [ProcessStartInfo]@{ - FileName = $powershell - Arguments = $CommandLine - RedirectStandardInput = $RedirectStdIn - RedirectStandardOutput = $true - RedirectStandardError = $true - UseShellExecute = $false - } - } - - function RunPowerShell([ProcessStartInfo]$debugfn) - { - $process = [Process]::Start($debugfn) - return $process - } - - function EnsureChildHasExited([Process]$process, [int]$WaitTimeInMS = 15000) - { - $process.WaitForExit($WaitTimeInMS) - - if (!$process.HasExited) - { - $process.HasExited | Should Be $true - $process.Kill() - } - } - - function Clean-State - { - if (Test-Path $FullyQualifiedLink) - { - Remove-Item $FullyQualifiedLink -Force - } - - if (Test-Path $FullyQualifiedFile) - { - Remove-Item $FullyQualifiedFile -Force - } - - if (Test-Path $FullyQualifiedFolder) - { - Remove-Item $FullyQualifiedFolder -Force - } - } - - BeforeAll { - $powershell = Join-Path -Path $PsHome -ChildPath "powershell" - } - -#Both tests are pending due to a bug in Invoke-Item on Windows. Fixed for Linux - - It "Should call the function without error" -Pending { - { New-Item -Name $testfile -Path $tmpDirectory -ItemType file } | Should Not Throw - } - - It "Should invoke a text file without error" -Pending { - $debugfn = NewProcessStartInfo "-noprofile ""``Invoke-Item $FullyQualifiedFile`n" -RedirectStdIn - $process = RunPowerShell $debugfn - EnsureChildHasExited $process - $process.ExitCode | Should Be 0 - } -} From aefeb80108ae2499b8dc98b90366588f5b14f1b4 Mon Sep 17 00:00:00 2001 From: Alex Jordan Date: Tue, 5 Jul 2016 18:20:02 -0700 Subject: [PATCH 9/9] Fixed scope issue in invokeitem --- .../namespaces/FileSystemProvider.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index b1f4abac39..2c83a4ea18 100644 --- a/src/System.Management.Automation/namespaces/FileSystemProvider.cs +++ b/src/System.Management.Automation/namespaces/FileSystemProvider.cs @@ -1331,25 +1331,22 @@ namespace Microsoft.PowerShell.Commands if (ShouldProcess(resource, action)) { + System.Diagnostics.Process invokeProcess = new System.Diagnostics.Process(); + if (Platform.IsWindows) { System.Diagnostics.Process.Start(path); } else if (Platform.IsOSX) { - System.Diagnostics.Process invokeProcess = new System.Diagnostics.Process(); - invokeProcess.StartInfo.UseShellExecute = true; invokeProcess.StartInfo.FileName = "open"; invokeProcess.StartInfo.Arguments = path; } else if (Platform.IsLinux) { - System.Diagnostics.Process invokeProcess = new System.Diagnostics.Process(); - invokeProcess.StartInfo.UseShellExecute = true; invokeProcess.StartInfo.FileName = "xdg-open"; invokeProcess.StartInfo.Arguments = path; } invokeProcess.Start(); - } } // InvokeDefaultAction