Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes308downloads
Import-Alias.Tests.ps1119 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Import-Alias DRT Unit Tests" -Tags "CI" {4    $testAliasDirectory = Join-Path -Path $TestDrive -ChildPath ImportAliasTestDirectory5    $aliasFilename      = "aliasFilename"6    $fulltestpath       = Join-Path -Path $testAliasDirectory -ChildPath $aliasFilename7 8    BeforeEach {9        New-Item -Path $testAliasDirectory -ItemType Directory -Force10        Remove-Item alias:abcd* -Force -ErrorAction SilentlyContinue11        Remove-Item alias:ijkl* -Force -ErrorAction SilentlyContinue12        Set-Alias abcd01 efgh0113        Set-Alias abcd02 efgh0214        Set-Alias abcd03 efgh0315        Set-Alias abcd04 efgh0416        Set-Alias ijkl01 mnop0117        Set-Alias ijkl02 mnop0218        Set-Alias ijkl03 mnop0319        Set-Alias ijkl04 mnop0420    }21 22    AfterEach {23        Remove-Item -Path $testAliasDirectory -Recurse -Force -ErrorAction SilentlyContinue24    }25 26    It "Import-Alias Resolve To Multiple will throw PSInvalidOperationException" {27        { Import-Alias * -ErrorAction Stop } | Should -Throw -ErrorId "NotSupported,Microsoft.PowerShell.Commands.ImportAliasCommand"28    }29 30    It "Import-Alias From Exported Alias File Aliases Already Exist should throw SessionStateException" {31        { Export-Alias  $fulltestpath abcd* } | Should -Not -Throw32        { Import-Alias $fulltestpath -ErrorAction Stop } | Should -Throw -ErrorId "AliasAlreadyExists,Microsoft.PowerShell.Commands.ImportAliasCommand"33    }34 35    It "Import-Alias Into Invalid Scope should throw PSArgumentException"{36        { Export-Alias  $fulltestpath abcd* } | Should -Not -Throw37        { Import-Alias $fulltestpath -Scope bogus } | Should -Throw -ErrorId "Argument,Microsoft.PowerShell.Commands.ImportAliasCommand"38    }39 40    It "Import-Alias From Exported Alias File Aliases Already Exist using force should not throw"{41        {Export-Alias  $fulltestpath abcd*} | Should -Not -Throw42        {Import-Alias $fulltestpath  -Force} | Should -Not -Throw43    }44}45 46Describe "Import-Alias" -Tags "CI" {47 48    BeforeAll {49        $newLine = [Environment]::NewLine50 51        $testAliasDirectory = Join-Path -Path $TestDrive -ChildPath ImportAliasTestDirectory52        $aliasFilename = "pesteralias.txt"53        $aliasFilenameMoreThanFourValues = "aliasFileMoreThanFourValues.txt"54        $aliasFilenameLessThanFourValues = "aliasFileLessThanFourValues.txt"55 56        $aliasfile = Join-Path -Path $testAliasDirectory -ChildPath $aliasFilename57        $aliasPathMoreThanFourValues = Join-Path -Path $testAliasDirectory -ChildPath $aliasFileNameMoreThanFourValues58        $aliasPathLessThanFourValues = Join-Path -Path $testAliasDirectory -ChildPath $aliasFileNameLessThanFourValues59 60        $commandToAlias = "echo"61        $alias1 = "pesterecho"62        $alias2    = '"abc""def"'63        $alias3    = '"aaa"'64        $alias4    = '"a,b"'65 66        # create alias file67        New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null68 69        # set header70        $aliasFileContent = '# Alias File' + $newLine71        $aliasFileContent += '# Exported by : alex' + $newLine72        $aliasFileContent += '# Date/Time : Thursday, 12 November 2015 21:55:08' + $newLine73        $aliasFileContent += '# Computer : archvm'74 75        # add various aliases76        $aliasFileContent += $newLine + $alias1 + ',"' + $commandToAlias + '","","None"'77        $aliasFileContent += $newLine + $alias2 + ',"' + $commandToAlias + '","","None"'78        $aliasFileContent += $newLine + $alias3 + ',"' + $commandToAlias + '","","None"'79        $aliasFileContent += $newLine + $alias4 + ',"' + $commandToAlias + '","","None"'80        $aliasFileContent > $aliasfile81 82        # create invalid file with more than four values83        New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null84        $aliasFileContent = $newLine + '"v_1","v_2","v_3","v_4","v_5"'85        $aliasFileContent > $aliasPathMoreThanFourValues86 87        # create invalid file with less than four values88        New-Item -Path $testAliasDirectory -ItemType Directory -Force > $null89        $aliasFileContent = $newLine + '"v_1","v_2","v_3"'90        $aliasFileContent > $aliasPathLessThanFourValues91    }92 93    It "Should be able to import an alias file successfully" {94        { Import-Alias -Path $aliasfile } | Should -Not -Throw95    }96 97    It "Should classify an alias as non existent when it is not imported yet" {98        {Get-Alias -Name invalid_alias -ErrorAction Stop} | Should -Throw -ErrorId 'ItemNotFoundException,Microsoft.PowerShell.Commands.GetAliasCommand'99    }100 101    It "Should be able to parse <aliasToTest>" -TestCases @(102        @{ aliasToTest = 'abc"def' }103        @{ aliasToTest = 'aaa' }104        @{ aliasToTest = 'a,b' }105        ) {106        param($aliasToTest)107        Import-Alias -Path $aliasfile108        ( Get-Alias -Name $aliasToTest ).Definition | Should -BeExactly $commandToAlias109    }110 111    It "Should throw an error when reading more than four values" {112        { Import-Alias -Path $aliasPathMoreThanFourValues } | Should -Throw -ErrorId "ImportAliasFileFormatError,Microsoft.PowerShell.Commands.ImportAliasCommand"113    }114 115    It "Should throw an error when reading less than four values" {116        { Import-Alias -Path $aliasPathLessThanFourValues } | Should -Throw -ErrorId "ImportAliasFileFormatError,Microsoft.PowerShell.Commands.ImportAliasCommand"117    }118}119