From edf685dc9c339a68aec38c6e7503b0202b67c525 Mon Sep 17 00:00:00 2001 From: Noutlaw1 Date: Thu, 26 Jan 2017 17:17:40 -0500 Subject: [PATCH] Added alias (Path) to the FilePath parameter of the Out-File cmdlet. (#2945) * Added parameter alias to the Out-File cmdlet, allowing -FilePath to be used as -Path. Relevant issue: https://github.com/PowerShell/PowerShell/issues/2923 * Test to confirm the alias I added to the Out-File cmdlet's -FilePath parameter works. * Changed test for parameter alias as per feedback. * Updating based on feedback. Made formatting changes: Added space between brackets and start of cmdlet. Moved end bracket to before the pipe and Should Not Throw statement. Removed -InputObject parameter for the test, as it was not needed. * Added BeforeAll to the first part of the file. Replaced tabs with spaces. #2945 * Fixing indentation within the test file --- .../FormatAndOutput/out-file/Out-File.cs | 1 + .../Out-File.Tests.ps1 | 133 +++++++++--------- 2 files changed, 70 insertions(+), 64 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/out-file/Out-File.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/out-file/Out-File.cs index 653738e36a..b8fd1cc09b 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/out-file/Out-File.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/FormatAndOutput/out-file/Out-File.cs @@ -38,6 +38,7 @@ namespace Microsoft.PowerShell.Commands /// /// mandatory file name to write to /// + [Alias("Path")] [Parameter(Mandatory = true, Position = 0, ParameterSetName = "ByPath")] public string FilePath { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Out-File.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Out-File.Tests.ps1 index 67202abca2..e11f884133 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Out-File.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Out-File.Tests.ps1 @@ -18,113 +18,118 @@ Describe "Out-File DRT Unit Tests" -Tags "CI" { } Describe "Out-File" -Tags "CI" { - $expectedContent = "some test text" - $inObject = New-Object psobject -Property @{text=$expectedContent} - $testfile = Join-Path -Path $TestDrive -ChildPath outfileTest.txt + BeforeAll { + $expectedContent = "some test text" + $inObject = New-Object psobject -Property @{text=$expectedContent} + $testfile = Join-Path -Path $TestDrive -ChildPath outfileTest.txt + } AfterEach { - Remove-Item -Path $testfile -Force + Remove-Item -Path $testfile -Force } It "Should be able to be called without error" { - { Out-File -FilePath $testfile } | Should Not Throw + { Out-File -FilePath $testfile } | Should Not Throw } It "Should be able to accept string input via piping" { - { $expectedContent | Out-File -FilePath $testfile } | Should Not Throw + { $expectedContent | Out-File -FilePath $testfile } | Should Not Throw - $actual = Get-Content $testfile + $actual = Get-Content $testfile - $actual | Should Be $expectedContent + $actual | Should Be $expectedContent } It "Should be able to accept string input via the InputObject switch" { - { Out-File -FilePath $testfile -InputObject $expectedContent } | Should Not Throw + { Out-File -FilePath $testfile -InputObject $expectedContent } | Should Not Throw - $actual = Get-Content $testfile + $actual = Get-Content $testfile - $actual | Should Be $expectedContent + $actual | Should Be $expectedContent } It "Should be able to accept object input" { - { $inObject | Out-File -FilePath $testfile } | Should Not Throw + { $inObject | Out-File -FilePath $testfile } | Should Not Throw - { Out-File -FilePath $testfile -InputObject $inObject } | Should Not Throw + { Out-File -FilePath $testfile -InputObject $inObject } | Should Not Throw } It "Should not overwrite when the noclobber switch is used" { - Out-File -FilePath $testfile -InputObject $inObject + Out-File -FilePath $testfile -InputObject $inObject - { Out-File -FilePath $testfile -InputObject $inObject -NoClobber -ErrorAction SilentlyContinue } | Should Throw "already exists." - { Out-File -FilePath $testfile -InputObject $inObject -NoOverWrite -ErrorAction SilentlyContinue } | Should Throw "already exists." + { Out-File -FilePath $testfile -InputObject $inObject -NoClobber -ErrorAction SilentlyContinue } | Should Throw "already exists." + { Out-File -FilePath $testfile -InputObject $inObject -NoOverWrite -ErrorAction SilentlyContinue } | Should Throw "already exists." - $actual = Get-Content $testfile + $actual = Get-Content $testfile - $actual[0] | Should Be "" - $actual[1] | Should Match "text" - $actual[2] | Should Match "----" - $actual[3] | Should Match "some test text" + $actual[0] | Should Be "" + $actual[1] | Should Match "text" + $actual[2] | Should Match "----" + $actual[3] | Should Match "some test text" } It "Should Append a new line when the append switch is used" { - { Out-File -FilePath $testfile -InputObject $inObject } | Should Not Throw - { Out-File -FilePath $testfile -InputObject $inObject -Append } | Should Not Throw + { Out-File -FilePath $testfile -InputObject $inObject } | Should Not Throw + { Out-File -FilePath $testfile -InputObject $inObject -Append } | Should Not Throw - $actual = Get-Content $testfile - - $actual[0] | Should Be "" - $actual[1] | Should Match "text" - $actual[2] | Should Match "----" - $actual[3] | Should Match "some test text" - $actual[4] | Should Be "" - $actual[5] | Should Be "" - $actual[6] | Should Be "" - $actual[7] | Should Match "text" - $actual[8] | Should Match "----" - $actual[9] | Should Match "some test text" - $actual[10] | Should Be "" - $actual[11] | Should Be "" + $actual = Get-Content $testfile + $actual[0] | Should Be "" + $actual[1] | Should Match "text" + $actual[2] | Should Match "----" + $actual[3] | Should Match "some test text" + $actual[4] | Should Be "" + $actual[5] | Should Be "" + $actual[6] | Should Be "" + $actual[7] | Should Match "text" + $actual[8] | Should Match "----" + $actual[9] | Should Match "some test text" + $actual[10] | Should Be "" + $actual[11] | Should Be "" } It "Should limit each line to the specified number of characters when the width switch is used on objects" { - Out-File -FilePath $testfile -Width 10 -InputObject $inObject + Out-File -FilePath $testfile -Width 10 -InputObject $inObject - $actual = Get-Content $testfile - - $actual[0] | Should Be "" - $actual[1] | Should Be "text " - $actual[2] | Should Be "---- " - $actual[3] | Should Be "some te..." + $actual = Get-Content $testfile + $actual[0] | Should Be "" + $actual[1] | Should Be "text " + $actual[2] | Should Be "---- " + $actual[3] | Should Be "some te..." } It "Should allow the cmdlet to overwrite an existing read-only file" { - # create a read-only text file - { Out-File -FilePath $testfile -InputObject $inObject } | Should Not Throw - Set-ItemProperty -Path $testfile -Name IsReadOnly -Value $true + # create a read-only text file + { Out-File -FilePath $testfile -InputObject $inObject } | Should Not Throw + Set-ItemProperty -Path $testfile -Name IsReadOnly -Value $true - # write information to the RO file - { Out-File -FilePath $testfile -InputObject $inObject -Append -Force } | Should Not Throw + # write information to the RO file + { Out-File -FilePath $testfile -InputObject $inObject -Append -Force } | Should Not Throw - $actual = Get-Content $testfile + $actual = Get-Content $testfile - $actual[0] | Should Be "" - $actual[1] | Should Match "text" - $actual[2] | Should Match "----" - $actual[3] | Should Match "some test text" - $actual[4] | Should Be "" - $actual[5] | Should Be "" - $actual[6] | Should Be "" - $actual[7] | Should Match "text" - $actual[8] | Should Match "----" - $actual[9] | Should Match "some test text" - $actual[10] | Should Be "" - $actual[11] | Should Be "" + $actual[0] | Should Be "" + $actual[1] | Should Match "text" + $actual[2] | Should Match "----" + $actual[3] | Should Match "some test text" + $actual[4] | Should Be "" + $actual[5] | Should Be "" + $actual[6] | Should Be "" + $actual[7] | Should Match "text" + $actual[8] | Should Match "----" + $actual[9] | Should Match "some test text" + $actual[10] | Should Be "" + $actual[11] | Should Be "" - # reset to not read only so it can be deleted - Set-ItemProperty -Path $testfile -Name IsReadOnly -Value $false + # reset to not read only so it can be deleted + Set-ItemProperty -Path $testfile -Name IsReadOnly -Value $false + } + + It "Should be able to use the 'Path' alias for the 'FilePath' parameter" { + { Out-File -Path $testfile } | Should Not Throw } } +