Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes308downloads
alias.tests.ps1135 linesDownload Raw Back to Microsoft.PowerShell.Utility
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Alias tests" -Tags "CI" {4 5    BeforeAll {6        $testPath = Join-Path testdrive:\ ("testAlias\.test")7        New-Item -ItemType Directory -Path $testPath -Force | Out-Null8 9        class TestData10        {11            [string] $testName12            [string] $testFile13            [string] $expectedError14 15            TestData($name, $file, $errorId)16            {17                $this.testName = $name18                $this.testFile = $file19                $this.expectedError = $errorId20            }21        }22    }23 24    Context "Export-Alias literal path" {25        BeforeAll {26            $csvFile = Join-Path $testPath "alias.csv"27            $ps1File = Join-Path $testPath "alias.ps1"28 29            $testCases = @()30            $testCases += [TestData]::new("CSV", $csvFile, [NullString]::Value)31            $testCases += [TestData]::new("PS1", $ps1File, [NullString]::Value)32            $testCases += [TestData]::new("Empty string", "", "ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.ExportAliasCommand")33            $testCases += [TestData]::new("Null", [NullString]::Value, "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ExportAliasCommand")34            $testCases += [TestData]::new("Non filesystem provider", 'env:\alias.ps1', "ReadWriteFileNotFileSystemProvider,Microsoft.PowerShell.Commands.ExportAliasCommand")35        }36 37        $testCases | ForEach-Object {38 39            It "for $($_.testName)" {40 41                $test = $_42                try43                {44                    Export-Alias -LiteralPath $test.testFile -ErrorAction SilentlyContinue45                }46                catch47                {48                    $exportAliasError = $_49                }50 51                if($null -eq $test.expectedError)52                {53                   Test-Path -LiteralPath $test.testFile | Should -BeTrue54                }55                else56                {57                    $exportAliasError.FullyqualifiedErrorId | Should -Be $test.expectedError58                }59            }60 61            AfterEach {62                Remove-Item -LiteralPath $test.testFile -Force -ErrorAction SilentlyContinue63            }64        }65 66        It "when file exists with NoClobber" {67            Export-Alias -LiteralPath $csvFile68            { Export-Alias -LiteralPath $csvFile -NoClobber } | Should -Throw -ErrorId "NoClobber,Microsoft.PowerShell.Commands.ExportAliasCommand"69        }70    }71 72    Context "Export-All inside a literal path" {73        BeforeEach {74            Push-Location -LiteralPath $testPath75        }76 77        It "with a CSV file" {78            Export-Alias "alias.csv"79            Test-Path -LiteralPath (Join-Path $testPath "alias.csv") | Should -BeTrue80        }81 82        It "with NoClobber" {83            $path = Export-Alias alias.csv84 85            { Export-Alias alias.csv -NoClobber } | Should -Throw -ErrorId "NoClobber,Microsoft.PowerShell.Commands.ExportAliasCommand"86        }87 88        AfterEach {89            Pop-Location90        }91    }92 93    Context "Import-Alias literal path" {94 95        BeforeAll {96            $csvFile = Join-Path $testPath "alias.csv"97            $ps1File = Join-Path $testPath "alias.ps1"98 99            $testCases = @()100            $testCases += [TestData]::new("Empty string", "", "ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.ImportAliasCommand")101            $testCases += [TestData]::new("Null", [NullString]::Value, "ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.ImportAliasCommand")102            $testCases += [TestData]::new("Non filesystem provider", 'env:\alias.ps1', "NotSupported,Microsoft.PowerShell.Commands.ImportAliasCommand")103        }104 105        $testCases | ForEach-Object {106 107            It "for $($_.testName)" {108                $test = $_109 110                { Import-Alias -LiteralPath $test.testFile -ErrorAction SilentlyContinue } | Should -Throw -ErrorId $test.expectedError111            }112        }113 114        It "can be done from a CSV file" {115 116            # alias file definition content117            $aliasDefinition = @'118            "myuh","update-help","","ReadOnly, AllScope"119'@120 121            $aliasFile = Join-Path $testPath "alias.csv"122            $aliasDefinition | Out-File -LiteralPath $aliasFile123 124            Import-Alias -LiteralPath $aliasFile125 126            # Verify that the alias was imported127            $definedAlias = Get-Alias myuh128 129            $definedAlias | Should -Not -BeNullOrEmpty130            $definedAlias.Name | Should -BeExactly "myuh"131            $definedAlias.Definition | Should -Be "update-help"132        }133    }134}135