diff --git a/src/System.Management.Automation/namespaces/FileSystemProvider.cs b/src/System.Management.Automation/namespaces/FileSystemProvider.cs index 8f691de069..2c83a4ea18 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(); + + 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(); } } // 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] 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 + } +}