From 4884317d0074b6363faa6a110b4ae7392e0d4dd5 Mon Sep 17 00:00:00 2001 From: Sytze Andringa Date: Thu, 11 Apr 2019 19:40:24 +0200 Subject: [PATCH] Added more tests for import-alias by file regarding parsing difficult aliases strings (#9247) --- .../Import-Alias.Tests.ps1 | 153 +++++++++++------- 1 file changed, 94 insertions(+), 59 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 index 1b3959e72..e13442278 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Import-Alias.Tests.ps1 @@ -2,82 +2,117 @@ # Licensed under the MIT License. Describe "Import-Alias DRT Unit Tests" -Tags "CI" { $testAliasDirectory = Join-Path -Path $TestDrive -ChildPath ImportAliasTestDirectory - $testAliases = "TestAliases" - $fulltestpath = Join-Path -Path $testAliasDirectory -ChildPath $testAliases + $aliasFilename = "aliasFilename" + $fulltestpath = Join-Path -Path $testAliasDirectory -ChildPath $aliasFilename BeforeEach { - New-Item -Path $testAliasDirectory -ItemType Directory -Force - remove-item alias:abcd* -force -ErrorAction SilentlyContinue - remove-item alias:ijkl* -force -ErrorAction SilentlyContinue - set-alias abcd01 efgh01 - set-alias abcd02 efgh02 - set-alias abcd03 efgh03 - set-alias abcd04 efgh04 - set-alias ijkl01 mnop01 - set-alias ijkl02 mnop02 - set-alias ijkl03 mnop03 - set-alias ijkl04 mnop04 + New-Item -Path $testAliasDirectory -ItemType Directory -Force + remove-item alias:abcd* -force -ErrorAction SilentlyContinue + remove-item alias:ijkl* -force -ErrorAction SilentlyContinue + set-alias abcd01 efgh01 + set-alias abcd02 efgh02 + set-alias abcd03 efgh03 + set-alias abcd04 efgh04 + set-alias ijkl01 mnop01 + set-alias ijkl02 mnop02 + set-alias ijkl03 mnop03 + set-alias ijkl04 mnop04 } - AfterEach { - Remove-Item -Path $testAliasDirectory -Recurse -Force -ErrorAction SilentlyContinue - } - - It "Import-Alias Resolve To Multiple will throw PSInvalidOperationException" { - { Import-Alias * -ErrorAction Stop } | Should -Throw -ErrorId "NotSupported,Microsoft.PowerShell.Commands.ImportAliasCommand" - } - - It "Import-Alias From Exported Alias File Aliases Already Exist should throw SessionStateException" { - { Export-Alias $fulltestpath abcd* } | Should -Not -Throw - { Import-Alias $fulltestpath -ErrorAction Stop } | Should -Throw -ErrorId "AliasAlreadyExists,Microsoft.PowerShell.Commands.ImportAliasCommand" + AfterEach { + Remove-Item -Path $testAliasDirectory -Recurse -Force -ErrorAction SilentlyContinue } - It "Import-Alias Into Invalid Scope should throw PSArgumentException"{ - { Export-Alias $fulltestpath abcd* } | Should -Not -Throw - { Import-Alias $fulltestpath -scope bogus } | Should -Throw -ErrorId "Argument,Microsoft.PowerShell.Commands.ImportAliasCommand" + It "Import-Alias Resolve To Multiple will throw PSInvalidOperationException" { + { Import-Alias * -ErrorAction Stop } | Should -Throw -ErrorId "NotSupported,Microsoft.PowerShell.Commands.ImportAliasCommand" } - It "Import-Alias From Exported Alias File Aliases Already Exist using force should not throw"{ - {Export-Alias $fulltestpath abcd*} | Should -Not -Throw - {Import-Alias $fulltestpath -Force} | Should -Not -Throw + It "Import-Alias From Exported Alias File Aliases Already Exist should throw SessionStateException" { + { Export-Alias $fulltestpath abcd* } | Should -Not -Throw + { Import-Alias $fulltestpath -ErrorAction Stop } | Should -Throw -ErrorId "AliasAlreadyExists,Microsoft.PowerShell.Commands.ImportAliasCommand" + } + + It "Import-Alias Into Invalid Scope should throw PSArgumentException"{ + { Export-Alias $fulltestpath abcd* } | Should -Not -Throw + { Import-Alias $fulltestpath -scope bogus } | Should -Throw -ErrorId "Argument,Microsoft.PowerShell.Commands.ImportAliasCommand" + } + + It "Import-Alias From Exported Alias File Aliases Already Exist using force should not throw"{ + {Export-Alias $fulltestpath abcd*} | Should -Not -Throw + {Import-Alias $fulltestpath -Force} | Should -Not -Throw } } Describe "Import-Alias" -Tags "CI" { - $newLine=[Environment]::NewLine - $testAliasDirectory = Join-Path -Path $TestDrive -ChildPath ImportAliasTestDirectory - $testAliases = "pesteralias.txt" - $pesteraliasfile = Join-Path -Path $testAliasDirectory -ChildPath $testAliases - BeforeEach { - New-Item -Path $testAliasDirectory -ItemType Directory -Force + BeforeAll { + $newLine = [Environment]::NewLine - $pesteraliascontent ='# Alias File'+$newLine - $pesteraliascontent+='# Exported by : alex'+$newLine - $pesteraliascontent+='# Date/Time : Thursday, 12 November 2015 21:55:08'+$newLine - $pesteraliascontent+='# Computer : archvm'+$newLine+'"pesterecho","echo","","None"' - $pesteraliascontent > $pesteraliasfile - } + $testAliasDirectory = Join-Path -Path $TestDrive -ChildPath ImportAliasTestDirectory + $aliasFilename = "pesteralias.txt" + $aliasFilenameMoreThanFourValues = "aliasFileMoreThanFourValues.txt" + $aliasFilenameLessThanFourValues = "aliasFileLessThanFourValues.txt" - AfterEach { - Remove-Item -Path $testAliasDirectory -Recurse -Force - } + $aliasfile = Join-Path -Path $testAliasDirectory -ChildPath $aliasFilename + $aliasPathMoreThanFourValues = Join-Path -Path $testAliasDirectory -ChildPath $aliasFileNameMoreThanFourValues + $aliasPathLessThanFourValues = Join-Path -Path $testAliasDirectory -ChildPath $aliasFileNameLessThanFourValues - It "Should be able to import an alias file successfully" { - { Import-Alias $pesteraliasfile } | Should -Not -throw - } + $commandToAlias = "echo" + $alias1 = "pesterecho" + $alias2 = '"abc""def"' + $alias3 = '"aaa"' + $alias4 = '"a,b"' - It "Should be able to import file via the Import-Alias alias of ipal" { - { ipal $pesteraliasfile } | Should -Not -throw - } + # create alias file + New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null - It "Should be able to import an alias file and perform imported aliased echo cmd" { - (Import-Alias $pesteraliasfile) - (pesterecho pestertesting) | Should -BeExactly "pestertesting" - } + # set header + $aliasFileContent = '# Alias File' + $newLine + $aliasFileContent += '# Exported by : alex' + $newLine + $aliasFileContent += '# Date/Time : Thursday, 12 November 2015 21:55:08' + $newLine + $aliasFileContent += '# Computer : archvm' - It "Should be able to use ipal alias to import an alias file and perform cmd" { - (ipal $pesteraliasfile) - (pesterecho pestertesting) | Should -BeExactly "pestertesting" - } + # add various aliases + $aliasFileContent += $newLine + $alias1 + ',"' + $commandToAlias + '","","None"' + $aliasFileContent += $newLine + $alias2 + ',"' + $commandToAlias + '","","None"' + $aliasFileContent += $newLine + $alias3 + ',"' + $commandToAlias + '","","None"' + $aliasFileContent += $newLine + $alias4 + ',"' + $commandToAlias + '","","None"' + $aliasFileContent > $aliasfile + + # create invalid file with more than four values + New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null + $aliasFileContent = $newLine + '"v_1","v_2","v_3","v_4","v_5"' + $aliasFileContent > $aliasPathMoreThanFourValues + + # create invalid file with less than four values + New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null + $aliasFileContent = $newLine + '"v_1","v_2","v_3"' + $aliasFileContent > $aliasPathLessThanFourValues + } + + It "Should be able to import an alias file successfully" { + { Import-Alias -Path $aliasfile } | Should -Not -Throw + } + + It "Should classify an alias as non existent when it is not imported yet" { + {Get-Alias -Name invalid_alias -ErrorAction Stop} | Should -Throw -ErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand' + } + + It "Should be able to parse " -TestCases @( + @{ aliasToTest = 'abc"def' } + @{ aliasToTest = 'aaa' } + @{ aliasToTest = 'a,b' } + ) { + param($aliasToTest) + Import-Alias -Path $aliasfile + ( Get-Alias -Name $aliasToTest ).Definition | Should -BeExactly $commandToAlias + } + + It "Should throw an error when reading more than four values" { + { Import-Alias -Path $aliasPathMoreThanFourValues } | Should -Throw -ErrorId "ImportAliasFileFormatError,Microsoft.PowerShell.Commands.ImportAliasCommand" + } + + It "Should throw an error when reading less than four values" { + { Import-Alias -Path $aliasPathLessThanFourValues } | Should -Throw -ErrorId "ImportAliasFileFormatError,Microsoft.PowerShell.Commands.ImportAliasCommand" + } }