"Fossies" - the Fresh Open Source Software Archive 
Member "PowerShell-7.2.5/test/powershell/Modules/Microsoft.PowerShell.Utility/alias.tests.ps1" (21 Jun 2022, 4763 Bytes) of package /linux/misc/PowerShell-7.2.5.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Microsoft PowerShell source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 # Copyright (c) Microsoft Corporation.
2 # Licensed under the MIT License.
3 Describe "Alias tests" -Tags "CI" {
4
5 BeforeAll {
6 $testPath = Join-Path testdrive:\ ("testAlias\.test")
7 New-Item -ItemType Directory -Path $testPath -Force | Out-Null
8
9 class TestData
10 {
11 [string] $testName
12 [string] $testFile
13 [string] $expectedError
14
15 TestData($name, $file, $errorId)
16 {
17 $this.testName = $name
18 $this.testFile = $file
19 $this.expectedError = $errorId
20 }
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 try
43 {
44 Export-Alias -LiteralPath $test.testFile -ErrorAction SilentlyContinue
45 }
46 catch
47 {
48 $exportAliasError = $_
49 }
50
51 if($null -eq $test.expectedError)
52 {
53 Test-Path -LiteralPath $test.testFile | Should -BeTrue
54 }
55 else
56 {
57 $exportAliasError.FullyqualifiedErrorId | Should -Be $test.expectedError
58 }
59 }
60
61 AfterEach {
62 Remove-Item -LiteralPath $test.testFile -Force -ErrorAction SilentlyContinue
63 }
64 }
65
66 It "when file exists with NoClobber" {
67 Export-Alias -LiteralPath $csvFile
68 { 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 $testPath
75 }
76
77 It "with a CSV file" {
78 Export-Alias "alias.csv"
79 Test-Path -LiteralPath (Join-Path $testPath "alias.csv") | Should -BeTrue
80 }
81
82 It "with NoClobber" {
83 $path = Export-Alias alias.csv
84
85 { Export-Alias alias.csv -NoClobber } | Should -Throw -ErrorId "NoClobber,Microsoft.PowerShell.Commands.ExportAliasCommand"
86 }
87
88 AfterEach {
89 Pop-Location
90 }
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.expectedError
111 }
112 }
113
114 It "can be done from a CSV file" {
115
116 # alias file definition content
117 $aliasDefinition = @'
118 "myuh","update-help","","ReadOnly, AllScope"
119 '@
120
121 $aliasFile = Join-Path $testPath "alias.csv"
122 $aliasDefinition | Out-File -LiteralPath $aliasFile
123
124 Import-Alias -LiteralPath $aliasFile
125
126 # Verify that the alias was imported
127 $definedAlias = Get-Alias myuh
128
129 $definedAlias | Should -Not -BeNullOrEmpty
130 $definedAlias.Name | Should -BeExactly "myuh"
131 $definedAlias.Definition | Should -Be "update-help"
132 }
133 }
134 }