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
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,14 @@ static object ConvertToBoolIfPossible(string arg)
showHelp: true);
return false;
}
#if !UNIX
// Only do the .ps1 extension check on Windows since shebang is not supported
if (!_file.EndsWith(".ps1", StringComparison.OrdinalIgnoreCase))
{
SetCommandLineError(string.Format(CultureInfo.CurrentCulture, CommandLineParameterParserStrings.InvalidFileArgumentExtension, args[i]));
return false;
}
#endif

i++;

Expand Down
4 changes: 3 additions & 1 deletion src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1857,13 +1857,15 @@ private void DoRunspaceInitialization(bool skipProfiles, string initialCommand,

Pipeline tempPipeline = exec.CreatePipeline();
Command c;
#if UNIX
// if file doesn't have .ps1 extension, we read the contents and treat it as a script to support shebang with no .ps1 extension usage
if (!Path.GetExtension(filePath).Equals(".ps1", StringComparison.OrdinalIgnoreCase))
if (!filePath.EndsWith(".ps1", StringComparison.OrdinalIgnoreCase))
{
string script = File.ReadAllText(filePath);
c = new Command(script, isScript: true, useLocalScope: false);
}
else
#endif
{
c = new Command(filePath, false, false);
}
Expand Down
32 changes: 20 additions & 12 deletions test/powershell/Host/ConsoleHost.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,32 @@ Describe "ConsoleHost unit tests" -tags "Feature" {
}

It "-File should be default parameter" {
Set-Content -Path $testdrive/test -Value "'hello'"
$observed = & $powershell -NoProfile $testdrive/test
Set-Content -Path $testdrive/test.ps1 -Value "'hello'"
$observed = & $powershell -NoProfile $testdrive/test.ps1
$observed | Should -Be "hello"
}

It "-File accepts scripts with and without .ps1 extension: <Filename>" -TestCases @(
@{Filename="test.ps1"},
@{Filename="test"}
) {
param($Filename)
It "-File accepts scripts with .ps1 extension" {
$Filename = 'test.ps1'
Set-Content -Path $testdrive/$Filename -Value "'hello'"
$observed = & $powershell -NoProfile -File $testdrive/$Filename
$observed | Should -Be "hello"
}

It "-File accepts scripts without .ps1 extension to support shebang" -Skip:($IsWindows) {
$Filename = 'test.xxx'
Set-Content -Path $testdrive/$Filename -Value "'hello'"
$observed = & $powershell -NoProfile -File $testdrive/$Filename
$observed | Should -Be "hello"
}

It "-File should fail for script without .ps1 extension" -Skip:(!$IsWindows) {
$Filename = 'test.xxx'
Set-Content -Path $testdrive/$Filename -Value "'hello'"
& $powershell -NoProfile -File $testdrive/$Filename > $null
$LASTEXITCODE | Should -Be 64
}

It "-File should pass additional arguments to script" {
Set-Content -Path $testdrive/script.ps1 -Value 'foreach($arg in $args){$arg}'
$observed = & $powershell -NoProfile $testdrive/script.ps1 foo bar
Expand Down Expand Up @@ -208,11 +219,8 @@ Describe "ConsoleHost unit tests" -tags "Feature" {
$observed | Should -Be $BoolValue
}

It "-File '<filename>' should return exit code from script" -TestCases @(
@{Filename = "test.ps1"},
@{Filename = "test"}
) {
param($Filename)
It "-File should return exit code from script" {
$Filename = 'test.ps1'
Set-Content -Path $testdrive/$Filename -Value 'exit 123'
& $powershell $testdrive/$Filename
$LASTEXITCODE | Should -Be 123
Expand Down
65 changes: 45 additions & 20 deletions test/xUnit/csharp/test_CommandLineParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,27 @@ public static void TestDefaultParameterIsFileName_Not_Exist(params string[] comm
[Fact]
public static void TestDefaultParameterIsFileName_Exist()
{
var fileName = System.IO.Path.GetTempFileName();
var tempFile = System.IO.Path.GetTempFileName();
var tempPs1 = tempFile + ".ps1";
File.Move(tempFile, tempPs1);

var cpp = new CommandLineParameterParser();

cpp.Parse(new string[] { fileName });
cpp.Parse(new string[] { tempPs1 });

Assert.False(cpp.AbortStartup);
Assert.False(cpp.NoExit);
Assert.False(cpp.ShowShortHelp);
Assert.False(cpp.ShowBanner);
Assert.Equal(CommandLineParameterParser.NormalizeFilePath(fileName), cpp.File);
Assert.Null(cpp.ErrorMessage);
try
{
Assert.False(cpp.AbortStartup);
Assert.False(cpp.NoExit);
Assert.False(cpp.ShowShortHelp);
Assert.False(cpp.ShowBanner);
Assert.Equal(CommandLineParameterParser.NormalizeFilePath(tempPs1), cpp.File);
Assert.Null(cpp.ErrorMessage);
}
finally
{
File.Delete(tempPs1);
}
}

[Theory]
Expand Down Expand Up @@ -1212,7 +1221,16 @@ public static void TestParameter_WorkingDirectory_RemoveTrailingCharacter(params

public class TestDataLastFile : IEnumerable<object[]>
{
private readonly string _fileName = Path.GetTempFileName();
private static string _fileName
{
get
{
var tempFile = Path.GetTempFileName();
var tempPs1 = tempFile + ".ps1";
File.Move(tempFile, tempPs1);
return tempPs1;
}
}

public IEnumerator<object[]> GetEnumerator()
{
Expand All @@ -1230,21 +1248,28 @@ public static void TestParameter_LastParameterIsFileName_Exist(params string[] c

cpp.Parse(commandLine);

Assert.False(cpp.AbortStartup);
Assert.False(cpp.NoExit);
Assert.False(cpp.ShowShortHelp);
Assert.False(cpp.ShowBanner);
if (Platform.IsWindows)
try
{
Assert.True(cpp.StaMode);
Assert.False(cpp.AbortStartup);
Assert.False(cpp.NoExit);
Assert.False(cpp.ShowShortHelp);
Assert.False(cpp.ShowBanner);
if (Platform.IsWindows)
{
Assert.True(cpp.StaMode);
}
else
{
Assert.False(cpp.StaMode);
}

Assert.Equal(CommandLineParameterParser.NormalizeFilePath(commandLine[commandLine.Length - 1]), cpp.File);
Assert.Null(cpp.ErrorMessage);
}
else
finally
{
Assert.False(cpp.StaMode);
File.Delete(cpp.File);
}

Assert.Equal(CommandLineParameterParser.NormalizeFilePath(commandLine[commandLine.Length - 1]), cpp.File);
Assert.Null(cpp.ErrorMessage);
}
}
}