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 @@ -387,7 +387,7 @@ private static string GenerateRowField(string val, int width, int alignment, Dis
string currentValue = s;
int currentValueDisplayLength = dc.Length(currentValue);

if (addPadding && currentValueDisplayLength < width)
if (currentValueDisplayLength < width)
{
// the string is shorter than the width of the column
// need to pad with with blanks to reach the desired width
Expand All @@ -406,14 +406,21 @@ private static string GenerateRowField(string val, int width, int alignment, Dis
int padLeft = padCount / 2;
int padRight = padCount - padLeft;

s = StringUtil.Padding(padLeft) + s + StringUtil.Padding(padRight);
s = StringUtil.Padding(padLeft) + s;
if (addPadding)
{
s += StringUtil.Padding(padRight);
}
}
break;

default:
{
// left align is the default
s += StringUtil.Padding(padCount);
if (addPadding)
{
// left align is the default
s += StringUtil.Padding(padCount);
}
}
break;
}
Expand Down Expand Up @@ -497,8 +504,6 @@ private static string GenerateRowField(string val, int width, int alignment, Dis
{
return s;
}
// we have to pad
System.Diagnostics.Debug.Assert(finalValueDisplayLength == width - 1, "padding is not correct");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[This is not a blocking comment]
@SteveL-MSFT I just noticed that in the switch (alignment) block below, addPadding is checked only when padding for left alignment, not when padding for center alignment. This is not in the same pattern as in the switch (alignment) block in if (currentValueDisplayLength < width). Just want to bring it up so you can take a another look to see if it's intended.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, let me check this. I'll add an extra test for truncation.

switch (alignment)
{
case TextAlignment.Right:
Expand All @@ -509,7 +514,10 @@ private static string GenerateRowField(string val, int width, int alignment, Dis

case TextAlignment.Center:
{
s += " ";
if (addPadding)
{
s += " ";
}
}
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,150 @@ Describe "Format-Table DRT Unit Tests" -Tags "CI" {
$out = [pscustomobject]@{a=1;b=2} | Format-Table -HideTableHeaders | Out-String
$out.Replace([System.Environment]::NewLine, "") | Should BeExactly "1 2"
}

It "Format-Table should have correct alignment" {
$ps1xml = @"
<Configuration>
<ViewDefinitions>
<View>
<Name>Test.Format</Name>
<ViewSelectedBy>
<TypeName>Test.Format</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Left</Label>
<Alignment>left</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Center</Label>
<Alignment>center</Alignment>
</TableColumnHeader>
<TableColumnHeader>
<Label>Right</Label>
<Alignment>right</Alignment>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Left</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Center</PropertyName>
</TableColumnItem>
<TableColumnItem>
<PropertyName>Right</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
"@

$ps1xmlPath = Join-Path -Path $TestDrive -ChildPath "test.format.ps1xml"
Set-Content -Path $ps1xmlPath -Value $ps1xml
# run in own runspace so not affect global sessionstate
$ps = [powershell]::Create()
$ps.AddScript( {
param($ps1xmlPath)
Update-FormatData -AppendPath $ps1xmlPath
$a = [PSCustomObject]@{Left=1;Center=2;Right=3}
$a.PSObject.TypeNames.Insert(0,"Test.Format")
$a | Out-String
} ).AddArgument($ps1xmlPath) | Out-Null
$output = $ps.Invoke()

$expectedTable = @"

Left Center Right
---- ------ -----
1 2 3



"@
$output.Replace("`n","").Replace("`r","") | Should BeExactly $expectedTable.Replace("`n","").Replace("`r","")
}

It "Format-Table should not have trailing whitespace if there is truncation: <view>" -TestCases @(
# `u{2B758} is a double-byte Japanese character
@{view="Test.Format.Left" ; object=[pscustomobject]@{Left="123`u{2B758}"} ; expected="Left----1..." },
@{view="Test.Format.Center"; object=[pscustomobject]@{Center="12345`u{2B758}"}; expected="Center------123..."}
) {
param($view, $object, $expected)

$ps1xml = @"
<Configuration>
<ViewDefinitions>
<View>
<Name>Test.Format.Left</Name>
<ViewSelectedBy>
<TypeName>Test.Format</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Left</Label>
<Alignment>left</Alignment>
<Width>4</Width>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Left</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
<View>
<Name>Test.Format.Center</Name>
<ViewSelectedBy>
<TypeName>Test.Format</TypeName>
</ViewSelectedBy>
<TableControl>
<TableHeaders>
<TableColumnHeader>
<Label>Center</Label>
<Alignment>center</Alignment>
<Width>6</Width>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>Center</PropertyName>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
"@

$ps1xmlPath = Join-Path -Path $TestDrive -ChildPath "test.format.ps1xml"
Set-Content -Path $ps1xmlPath -Value $ps1xml
# run in own runspace so not affect global sessionstate
$ps = [powershell]::Create()
$ps.AddScript( {
param($ps1xmlPath,$view,$object)
Update-FormatData -AppendPath $ps1xmlPath
$object.PSObject.TypeNames.Insert(0,"Test.Format")
$object | Format-Table -View $view | Out-String
} ).AddArgument($ps1xmlPath).AddArgument($view).AddArgument($object) | Out-Null
$output = $ps.Invoke()
$output.Replace("`n","").Replace("`r","") | Should BeExactly $expected
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ try
# GET CERTIFICATE
#

$tempName = "$env:TEMP\signedscript_$(Get-Random).ps1"
$tempName = "TESTDRIVE:\signedscript_$(Get-Random).ps1"
"123456" > $tempName
$cert = $null
foreach ($thisCertificate in (Get-ChildItem cert:\ -rec -codesigning))
Expand Down Expand Up @@ -72,17 +72,17 @@ try
}

#
# Set process scope execution policy to 'AllSigned'
# Create a remote session
#

$oldExecutionPolicy = Get-ExecutionPolicy -Scope Process
Set-ExecutionPolicy AllSigned -Scope Process
$session = New-RemoteSession

#
# Create a remote session
# Set process scope execution policy to 'AllSigned'
#

$session = New-RemoteSession
$oldExecutionPolicy = Get-ExecutionPolicy -Scope Process
Set-ExecutionPolicy AllSigned -Scope Process
}

AfterAll {
Expand Down Expand Up @@ -510,8 +510,9 @@ try

BeforeAll {
if ($skipTest) { return }
# remote into same powershell instance
$samesession = New-RemoteSession -ConfigurationName $endpointName
$session = New-RemoteSession

function CreateTempPs1xmlFile
{
do {
Expand Down Expand Up @@ -626,6 +627,7 @@ try
AfterAll {
if ($skipTest) { return }
if ($null -ne $session) { Remove-PSSession $session -ErrorAction SilentlyContinue }
if ($null -ne $samesession) { Remove-PSSession $samesession -ErrorAction SilentlyContinue }
if ($null -ne $formatFile) { Remove-Item $formatFile -Force -ErrorAction SilentlyContinue }
if ($null -ne $typeFile) { Remove-Item $typeFile -Force -ErrorAction SilentlyContinue }
}
Expand All @@ -638,16 +640,16 @@ try
$originalLocalFormatting = & $formattingScript

# Original local and remote formatting should be equal (sanity check)
$originalRemoteFormatting = Invoke-Command $session $formattingScript
$originalRemoteFormatting = Invoke-Command $samesession $formattingScript
$originalLocalFormatting | Should Be $originalRemoteFormatting

Invoke-Command $session { param($file) Update-FormatData $file } -ArgumentList $formatFile
Invoke-Command $samesession { param($file) Update-FormatData $file } -ArgumentList $formatFile

# Original remote and modified remote formatting should not be equal (sanity check)
$modifiedRemoteFormatting = Invoke-Command $session $formattingScript
$modifiedRemoteFormatting = Invoke-Command $samesession $formattingScript
$originalRemoteFormatting | Should Not Be $modifiedRemoteFormatting

$module = Import-PSSession -Session $session -CommandName @() -FormatTypeName * -AllowClobber
$module = Import-PSSession -Session $samesession -CommandName @() -FormatTypeName * -AllowClobber
}

AfterAll {
Expand Down Expand Up @@ -2010,7 +2012,7 @@ try
BeforeAll {
# Skip tests for CoreCLR for now
# Skip tests if .NET 2.0 and PS 2.0 are not installed on the machine
$skipThisTest = $skipTest -or $IsCoreCLR -or
$skipThisTest = $skipTest -or $IsCoreCLR -or
(! (Test-Path 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v2.0.50727')) -or
(! (Test-Path 'HKLM:\SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine'))

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
Describe "Set-Date for admin" -Tag @('CI', 'RequireAdminOnWindows', 'RequireSudoOnUnix') {
It "Set-Date should be able to set the date in an elevated context" {
It "Set-Date should be able to set the date in an elevated context" -Pending {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't mark these as pending... Pending on linux at most.

{ Get-Date | Set-Date } | Should Not Throw
}

It "Set-Date should be able to set the date with -Date parameter" {
It "Set-Date should be able to set the date with -Date parameter" -Pending {
$target = Get-Date
$expected = $target
Set-Date -Date $target | Should Be $expected
Expand Down