Skip to content

Commit

Permalink
Fix use of Start-Process http://bing.com` (#9793)
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveL-MSFT authored and TravisEz13 committed Jun 4, 2019
1 parent f222a68 commit b4e2423
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 4 deletions.
66 changes: 64 additions & 2 deletions src/System.Management.Automation/engine/CommandSearcher.cs
Expand Up @@ -460,7 +460,7 @@ private CommandInfo GetNextFromPath()
// Try literal path resolution if it is set to run first
if (_commandResolutionOptions.HasFlag(SearchResolutionOptions.ResolveLiteralThenPathPatterns))
{
var path = GetNextLiteralPathThatExists(_commandName, out _);
var path = GetNextLiteralPathThatExistsAndHandleExceptions(_commandName, out _);

if (path != null)
{
Expand All @@ -478,7 +478,7 @@ private CommandInfo GetNextFromPath()
if (!_commandResolutionOptions.HasFlag(SearchResolutionOptions.ResolveLiteralThenPathPatterns) &&
resolvedPaths.Count == 0)
{
string path = GetNextLiteralPathThatExists(_commandName, out _);
string path = GetNextLiteralPathThatExistsAndHandleExceptions(_commandName, out _);

if (path != null)
{
Expand Down Expand Up @@ -1222,6 +1222,68 @@ private string ResolvePSPath(string path)

return result;
}
/// <summary>
/// Gets the next literal path.
/// Filtering to ones that exist for the filesystem.
/// Handles Exceptions
/// </summary>
/// <param name="command">
/// The command to search for.
/// </param>
/// <param name="provider">The provider that the command was found in.</param>
/// <returns>
/// Full path to the command.
/// </returns>
private string GetNextLiteralPathThatExistsAndHandleExceptions(string command, out ProviderInfo provider)
{
try
{
return GetNextLiteralPathThatExists(command, out provider);
}
catch (ItemNotFoundException)
{
CommandDiscovery.discoveryTracer.TraceError(
"The path could not be found: {0}",
_commandName);
}
catch (DriveNotFoundException)
{
// This can be because we think a scope or a url is a drive
// and need to continue searching.
// Although, scope does not work through get-command
CommandDiscovery.discoveryTracer.TraceError(
"A drive could not be found for the path: {0}",
_commandName);
}
catch (ProviderNotFoundException)
{
CommandDiscovery.discoveryTracer.TraceError(
"A provider could not be found for the path: {0}",
_commandName);
}
catch (InvalidOperationException)
{
CommandDiscovery.discoveryTracer.TraceError(
"The path specified a home directory, but the provider home directory was not set. {0}",
_commandName);
}
catch (ProviderInvocationException providerException)
{
CommandDiscovery.discoveryTracer.TraceError(
"The provider associated with the path '{0}' encountered an error: {1}",
_commandName,
providerException.Message);
}
catch (PSNotSupportedException)
{
CommandDiscovery.discoveryTracer.TraceError(
"The provider associated with the path '{0}' does not implement ContainerCmdletProvider",
_commandName);
}

provider = null;
return null;
}

/// <summary>
/// Gets the next literal path.
Expand Down
8 changes: 6 additions & 2 deletions test/powershell/engine/Basic/CommandDiscovery.Tests.ps1
Expand Up @@ -207,8 +207,12 @@ Describe "Command Discovery tests" -Tags "CI" {
}

Context "error cases" {
It 'Get-Command "less `"-PsPage %db?B of %DoesNotExist:`"" should throw Drive not found' {
{Get-Command -Name "less `"-PsPage %db?B of %DoesNotExist:`""} | Should -Throw -ErrorId 'DriveNotFound' -Because "The drive 'DoesNotExist:' should not exist"
It 'Get-Command "less `"-PsPage %db?B of %DoesNotExist:`"" should return nothing' {
Get-Command -Name "less `"-PsPage %db?B of %DoesNotExist:`"" | Should -BeNullOrEmpty
}

It "Should return command not found for commands in the global scope" {
{Get-Command -Name 'global:help' -ErrorAction Stop} | Should -Throw -ErrorId 'CommandNotFoundException'
}
}
}

0 comments on commit b4e2423

Please sign in to comment.