|
| 1 | +Import-Module "../HelpUsers/johlju/SqlServerDsc/output/RequiredModules/Pester" |
| 2 | + |
| 3 | +Remove-Module -Name 'ModuleBuilder' -Force -ErrorAction 'SilentlyContinue' |
| 4 | + |
| 5 | +New-Module -Name 'ModuleBuilder' -ScriptBlock { |
| 6 | + . $PSScriptRoot\..\..\Source\Private\GetCommandAlias.ps1 |
| 7 | +} | Import-Module |
| 8 | + |
| 9 | +Describe "GetCommandAlias" { |
| 10 | + |
| 11 | + Context "Mandatory Parameter" { |
| 12 | + $CommandInfo = InModuleScope ModuleBuilder { Get-Command GetCommandAlias } |
| 13 | + |
| 14 | + It 'has a mandatory AST parameter' { |
| 15 | + $AST = $CommandInfo.Parameters['AST'] |
| 16 | + $AST | Should -Not -BeNullOrEmpty |
| 17 | + $AST.ParameterType | Should -Be ([System.Management.Automation.Language.Ast]) |
| 18 | + $AST.Attributes.Where{ $_ -is [Parameter] }.Mandatory | Should -Be $true |
| 19 | + } |
| 20 | + |
| 21 | + } |
| 22 | + |
| 23 | + Context "Parsing Code" { |
| 24 | + It "returns a hashtable of command names to aliases" { |
| 25 | + $Result = InModuleScope ModuleBuilder { |
| 26 | + GetCommandAlias -Ast { |
| 27 | + function Test-Alias { |
| 28 | + [Alias("Foo", "Bar", "Alias")] |
| 29 | + param() |
| 30 | + } |
| 31 | + }.Ast |
| 32 | + } |
| 33 | + |
| 34 | + $Result["Test-Alias"] | Should -Be @("Foo", "Bar", "Alias") |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + Context "Parsing Code" { |
| 39 | + It "Parses only top-level functions, and returns them in order" { |
| 40 | + $Result = InModuleScope ModuleBuilder { |
| 41 | + GetCommandAlias -Ast { |
| 42 | + function Test-Alias { |
| 43 | + [Alias("TA", "TAlias")] |
| 44 | + param() |
| 45 | + } |
| 46 | + |
| 47 | + function TestAlias { |
| 48 | + [Alias("T")] |
| 49 | + param() |
| 50 | + |
| 51 | + function Test-Negative { |
| 52 | + [Alias("TN")] |
| 53 | + param() |
| 54 | + } |
| 55 | + } |
| 56 | + }.Ast |
| 57 | + } |
| 58 | + |
| 59 | + $Result.Keys | Should -Be "Test-Alias", "TestAlias" |
| 60 | + $Result["Test-Alias"] | Should -Be "TA", "TAlias" |
| 61 | + $Result["TestAlias"] | Should -Be "T" |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + Context "Parsing Code For New-Alias" { |
| 66 | + BeforeAll { |
| 67 | + # Must write a mock module script file and parse it to replicate real conditions |
| 68 | + " |
| 69 | + New-Alias -Name 'Alias1' -Value 'Write-Verbose' |
| 70 | + New-Alias -Value 'Write-Verbose' -Name 'Alias2' |
| 71 | + New-Alias 'Alias3' 'Write-Verbose' |
| 72 | + " | Out-File -FilePath "$TestDrive/MockBuiltModule.psm1" -Encoding ascii -Force |
| 73 | + } |
| 74 | + |
| 75 | + It "returns a hashtable with correct aliases" { |
| 76 | + $Result = InModuleScope ModuleBuilder { |
| 77 | + $ParseErrors, $Tokens = $null |
| 78 | + $mockAST = [System.Management.Automation.Language.Parser]::ParseFile("$TestDrive/MockBuiltModule.psm1", [ref]$Tokens, [ref]$ParseErrors) |
| 79 | + |
| 80 | + GetCommandAlias -Ast $mockAST |
| 81 | + } |
| 82 | + |
| 83 | + $Result.Count | Should -Be 3 |
| 84 | + $Result['Alias1'] | Should -Be 'Alias1' |
| 85 | + $Result['Alias2'] | Should -Be 'Alias2' |
| 86 | + $Result['Alias3'] | Should -Be 'Alias3' |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + Context "Parsing Code For Set-Alias" { |
| 91 | + BeforeAll { |
| 92 | + # Must write a mock module script file and parse it to replicate real conditions |
| 93 | + " |
| 94 | + Set-Alias -Name 'Alias1' -Value 'Write-Verbose' |
| 95 | + Set-Alias -Value 'Write-Verbose' -Name 'Alias2' |
| 96 | + Set-Alias 'Alias3' 'Write-Verbose' |
| 97 | + " | Out-File -FilePath "$TestDrive/MockBuiltModule.psm1" -Encoding ascii -Force |
| 98 | + } |
| 99 | + |
| 100 | + It "returns a hashtable with correct aliases" { |
| 101 | + $Result = InModuleScope ModuleBuilder { |
| 102 | + $ParseErrors, $Tokens = $null |
| 103 | + $mockAST = [System.Management.Automation.Language.Parser]::ParseFile("$TestDrive/MockBuiltModule.psm1", [ref]$Tokens, [ref]$ParseErrors) |
| 104 | + |
| 105 | + GetCommandAlias -Ast $mockAST |
| 106 | + } |
| 107 | + |
| 108 | + $Result.Count | Should -Be 3 |
| 109 | + $Result['Alias1'] | Should -Be 'Alias1' |
| 110 | + $Result['Alias2'] | Should -Be 'Alias2' |
| 111 | + $Result['Alias3'] | Should -Be 'Alias3' |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + Context "Parsing Code For New-Alias" { |
| 116 | + BeforeAll { |
| 117 | + # Must write a mock module script file and parse it to replicate real conditions |
| 118 | + " |
| 119 | + function Get-Something { |
| 120 | + param() |
| 121 | +
|
| 122 | + New-Alias -Name 'Alias1' -Value 'Write-Verbose' |
| 123 | + Set-Alias -Name 'Alias2' -Value 'Write-Verbose' |
| 124 | + } |
| 125 | +
|
| 126 | + New-Alias -Name 'Alias3' -Value 'Write-Verbose' |
| 127 | + " | Out-File -FilePath "$TestDrive/MockBuiltModule.psm1" -Encoding ascii -Force |
| 128 | + } |
| 129 | + |
| 130 | + It "returns a hashtable with just the aliases at script-level" { |
| 131 | + $Result = InModuleScope ModuleBuilder { |
| 132 | + $ParseErrors, $Tokens = $null |
| 133 | + $mockAST = [System.Management.Automation.Language.Parser]::ParseFile("$TestDrive/MockBuiltModule.psm1", [ref]$Tokens, [ref]$ParseErrors) |
| 134 | + |
| 135 | + GetCommandAlias -Ast $mockAST |
| 136 | + } |
| 137 | + |
| 138 | + $Result.Count | Should -Be 2 |
| 139 | + $Result['Get-Something'] | Should -BeNullOrEmpty # Does not return any alias for this function |
| 140 | + $Result['Alias3'] | Should -Be 'Alias3' |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + Context "Parsing Code For *-Alias Using Global Scope" { |
| 145 | + BeforeAll { |
| 146 | + # Must write a mock module script file and parse it to replicate real conditions |
| 147 | + " |
| 148 | + Set-Alias -Name 'Alias1' -Value 'Write-Verbose' -Scope Global |
| 149 | + Set-Alias -Name 'Alias2' -Value 'Write-Verbose' -Scope 'Global' |
| 150 | + Set-Alias -Value 'Write-Verbose' -Scope Global -Name 'Alias3' |
| 151 | + Set-Alias -Scope Global -Value 'Write-Verbose' -Name 'Alias4' |
| 152 | + Set-Alias 'Alias5' 'Write-Verbose' -Scope Global |
| 153 | + Set-Alias 'Alias6' -Scope Global 'Write-Verbose' |
| 154 | + Set-Alias -Scope Global 'Alias7' 'Write-Verbose' |
| 155 | +
|
| 156 | + New-Alias -Name 'Alias8' -Value 'Write-Verbose' -Scope Global |
| 157 | + New-Alias -Name 'Alias9' -Value 'Write-Verbose' -Scope 'Global' |
| 158 | + New-Alias -Value 'Write-Verbose' -Scope Global -Name 'Alias10' |
| 159 | + New-Alias -Scope Global -Value 'Write-Verbose' -Name 'Alias11' |
| 160 | + New-Alias 'Alias12' 'Write-Verbose' -Scope Global |
| 161 | + New-Alias 'Alias13' -Scope Global 'Write-Verbose' |
| 162 | + New-Alias -Scope Global 'Alias14' 'Write-Verbose' |
| 163 | +
|
| 164 | + New-Alias 'Alias15' 'Write-Verbose' |
| 165 | + " | Out-File -FilePath "$TestDrive/MockBuiltModule.psm1" -Encoding ascii -Force |
| 166 | + } |
| 167 | + |
| 168 | + It "returns a hashtable with correct aliases" { |
| 169 | + $Result = InModuleScope ModuleBuilder { |
| 170 | + $ParseErrors, $Tokens = $null |
| 171 | + $mockAST = [System.Management.Automation.Language.Parser]::ParseFile("$TestDrive/MockBuiltModule.psm1", [ref]$Tokens, [ref]$ParseErrors) |
| 172 | + |
| 173 | + GetCommandAlias -Ast $mockAST |
| 174 | + } |
| 175 | + |
| 176 | + $Result.Count | Should -Be 1 |
| 177 | + $Result['Alias15'] | Should -Be 'Alias15' |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + Context "Parsing Code For Remove-Alias" { |
| 182 | + BeforeAll { |
| 183 | + # Must write a mock module script file and parse it to replicate real conditions |
| 184 | + " |
| 185 | + New-Alias -Name 'Alias1' -Value 'Write-Verbose' |
| 186 | + New-Alias -Name 'Alias2' -Value 'Write-Verbose' |
| 187 | + Remove-Alias -Name 'Alias2' |
| 188 | + " | Out-File -FilePath "$TestDrive/MockBuiltModule.psm1" -Encoding ascii -Force |
| 189 | + |
| 190 | + Mock -CommandName Write-Warning -ModuleName 'ModuleBuilder' |
| 191 | + |
| 192 | + } |
| 193 | + |
| 194 | + It "returns a hashtable with correct aliases" { |
| 195 | + $Result = InModuleScope ModuleBuilder { |
| 196 | + $ParseErrors, $Tokens = $null |
| 197 | + $mockAST = [System.Management.Automation.Language.Parser]::ParseFile("$TestDrive/MockBuiltModule.psm1", [ref]$Tokens, [ref]$ParseErrors) |
| 198 | + |
| 199 | + GetCommandAlias -Ast $mockAST |
| 200 | + } |
| 201 | + |
| 202 | + $Result.Count | Should -Be 1 |
| 203 | + $Result['Alias1'] | Should -Be 'Alias1' |
| 204 | + |
| 205 | + Assert-MockCalled -CommandName Write-Warning -Exactly -Times 1 -Scope It -ModuleName 'ModuleBuilder' |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments