Merge pull request #1048 from PowerShell/andschwa/skip-extensions
Don't require extensions for script.ps1 etc. on Linux
This commit is contained in:
commit
d833371ed9
|
@ -50,25 +50,33 @@ namespace System.Management.Automation
|
|||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue