Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/System.Management.Automation/engine/CommandPathSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,33 @@ internal CommandPathSearch(
ExecutionContext context,
Collection<string> acceptableCommandNames)
{
string[] commandPatterns;
if (acceptableCommandNames != null)
{
// The name passed in is not a pattern. To minimize enumerating the file system, we
// turn the command name into a pattern and then match against extensions in PATHEXT.
// The old code would enumerate the file system many more times, once per possible extension.
// Porting note: this is wrong on Linux, where we don't depend on extensions
if (Platform.IsWindows)
{
commandName = commandName + ".*";
commandPatterns = new [] { commandName + ".*" };
}
else
{
// Porting note: on non-Windows platforms, we want to always allow just 'commandName'
// as an acceptable command name. However, we also want to allow commands to be
// called with the .ps1 extension, so that 'script.ps1' can be called by 'script'.
commandPatterns = new [] { commandName + ".ps1", commandName };
}
this.postProcessEnumeratedFiles = CheckAgainstAcceptableCommandNames;
this.acceptableCommandNames = acceptableCommandNames;
}
else
{
commandPatterns = new [] { commandName };
postProcessEnumeratedFiles = JustCheckExtensions;
}
Init(new [] { commandName }, lookupPaths, context);

Init(commandPatterns, lookupPaths, context);
this.orderedPathExt = CommandDiscovery.PathExtensionsWithPs1Prepended;
}

Expand Down
21 changes: 21 additions & 0 deletions test/powershell/NativeLinuxCommands.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,24 @@ Describe "NativeLinuxCommands" {
(get-command touch).CommandType | Should Be Application
}
}

Describe "Scripts with extensions" {
BeforeAll {
$data = "Hello World"
Setup -File testScript.ps1 -Content "'$data'"
$originalPath = $env:PATH
$env:PATH += [IO.Path]::PathSeparator + $TestDrive
}

AfterAll {
$env:PATH = $originalPath
}

It "Should run a script with its full name" {
testScript.ps1 | Should Be $data
}

It "Should run a script with its short name" {
testScript | Should Be $data
}
}