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 @@ -858,6 +858,22 @@ private bool ParseFile(string[] args, ref int i, bool noexitSeen)
// of the script to evaluate. If -file comes before -command, it will
// treat -command as an argument to the script...

bool TryGetBoolValue(string arg, out bool boolValue)
{
if (arg.Equals("$true", StringComparison.OrdinalIgnoreCase) || arg.Equals("true", StringComparison.OrdinalIgnoreCase))
{
boolValue = true;
return true;
}
else if (arg.Equals("$false", StringComparison.OrdinalIgnoreCase) || arg.Equals("false", StringComparison.OrdinalIgnoreCase))
{
boolValue = false;
return true;
}
boolValue = false;
return false;
}

++i;
if (i >= args.Length)
{
Expand Down Expand Up @@ -922,7 +938,6 @@ private bool ParseFile(string[] args, ref int i, bool noexitSeen)

i++;

Regex argPattern = new Regex(@"^.\w+\:", RegexOptions.CultureInvariant);
string pendingParameter = null;

// Accumulate the arguments to this script...
Expand All @@ -939,17 +954,25 @@ private bool ParseFile(string[] args, ref int i, bool noexitSeen)
}
else if (!string.IsNullOrEmpty(arg) && SpecialCharacters.IsDash(arg[0]))
{
Match m = argPattern.Match(arg);
if (m.Success)
int offset = arg.IndexOf(':');
if (offset >= 0)
{
int offset = arg.IndexOf(':');
if (offset == arg.Length - 1)
{
pendingParameter = arg.TrimEnd(':');
}
else
{
_collectedArgs.Add(new CommandParameter(arg.Substring(0, offset), arg.Substring(offset + 1)));
string argValue = arg.Substring(offset + 1);
string argName = arg.Substring(0, offset);
if (TryGetBoolValue(argValue, out bool boolValue))
{
_collectedArgs.Add(new CommandParameter(argName, boolValue));
}
else
{
_collectedArgs.Add(new CommandParameter(argName, argValue));
}
}
}
else
Expand Down
40 changes: 39 additions & 1 deletion test/powershell/Host/ConsoleHost.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,45 @@ Describe "ConsoleHost unit tests" -tags "Feature" {
$observed[1] | Should Be "bar"
}

It "-File should return exit code from script: <Filename>" -TestCases @(
It "-File should be able to pass bool string values as string to parameters: <BoolString>" -TestCases @(
# validates case is preserved
@{BoolString = '$truE'},
@{BoolString = '$falSe'},
@{BoolString = 'trUe'},
@{BoolString = 'faLse'}
) {
param([string]$BoolString)
Set-Content -Path $testdrive/test.ps1 -Value 'param([string]$bool) $bool'
$observed = & $powershell -NoProfile -Nologo -File $testdrive/test.ps1 -Bool $BoolString
$observed | Should Be $BoolString
}

It "-File should be able to pass bool string values as string to positional parameters: <BoolString>" -TestCases @(
# validates case is preserved
@{BoolString = '$tRue'},
@{BoolString = '$falSe'},
@{BoolString = 'tRUe'},
@{BoolString = 'fALse'}
) {
param([string]$BoolString)
Set-Content -Path $testdrive/test.ps1 -Value 'param([string]$bool) $bool'
$observed = & $powershell -NoProfile -Nologo -File $testdrive/test.ps1 $BoolString
$observed | Should BeExactly $BoolString
}

It "-File should be able to pass bool string values as bool to switches: <BoolString>" -TestCases @(
@{BoolString = '$tRue'; BoolValue = 'True'},
@{BoolString = '$faLse'; BoolValue = 'False'},
@{BoolString = 'tRue'; BoolValue = 'True'},
@{BoolString = 'fAlse'; BoolValue = 'False'}
) {
param([string]$BoolString, [string]$BoolValue)
Set-Content -Path $testdrive/test.ps1 -Value 'param([switch]$switch) $switch.IsPresent'
$observed = & $powershell -NoProfile -Nologo -File $testdrive/test.ps1 -switch:$BoolString
$observed | Should Be $BoolValue
}

It "-File should return exit code from script" -TestCases @(
@{Filename = "test.ps1"},
@{Filename = "test"}
) {
Expand Down