Skip to content

Commit d677511

Browse files
committed
Add Alias Exporting GetCommandAlias
Add integration tests
1 parent 7d9b2ad commit d677511

9 files changed

Lines changed: 153 additions & 4 deletions

File tree

Source/ModuleBuilder.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
# Always define FunctionsToExport as an empty @() which will be replaced on build
3939
FunctionsToExport = @()
40+
AliasesToExport = @()
4041

4142
# ID used to uniquely identify this module
4243
GUID = '4775ad56-8f64-432f-8da7-87ddf7a34653'

Source/Private/GetCommandAlias.ps1

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
function GetCommandAlias {
2+
[CmdletBinding()]
3+
param(
4+
# Path to the PSM1 file to amend
5+
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)]
6+
[System.Management.Automation.Language.Ast]$AST
7+
)
8+
begin {
9+
$Result = [Ordered]@{}
10+
}
11+
process {
12+
foreach($function in $AST.FindAll(
13+
{ $Args[0] -is [System.Management.Automation.Language.FunctionDefinitionAst] },
14+
$false )
15+
) {
16+
$Result[$function.Name] = $function.Body.ParamBlock.Attributes.Where{
17+
$_.TypeName.Name -eq "Alias" }.PositionalArguments.Value
18+
}
19+
}
20+
end {
21+
$Result
22+
}
23+
}

Source/Public/Build-Module.ps1

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ function Build-Module {
102102
[AllowEmptyString()]
103103
[string[]]$PublicFilter = "Public\*.ps1",
104104

105+
# A switch that allows you to disable the update of the AliasesToExport
106+
# By default, (if PublicFilter is not empty, and this is not set)
107+
# Build-Module updates the module manifest FunctionsToExport and AliasesToExport
108+
# with the combination of all the values in [Alias()] attributes on public functions in the module
109+
[switch]$IgnoreAliasAttribute,
110+
105111
# File encoding for output RootModule (defaults to UTF8)
106112
# Converted to System.Text.Encoding for PowerShell 6 (and something else for PowerShell 5)
107113
[ValidateSet("UTF8","UTF7","ASCII","Unicode","UTF32")]
@@ -211,6 +217,13 @@ function Build-Module {
211217

212218
$ParseResult = ConvertToAst $RootModule
213219
$ParseResult | MoveUsingStatements -Encoding "$($ModuleInfo.Encoding)"
220+
221+
if ($PublicFunctions -and -not $ModuleInfo.IgnoreAliasAttribute) {
222+
if (($AliasesToExport = ($ParseResult | GetCommandAlias)[$PublicFunctions] | Select-Object -Unique)) {
223+
Update-Metadata -Path $OutputManifest -PropertyName AliasesToExport -Value $AliasesToExport
224+
}
225+
}
226+
214227
try {
215228
if ($Version) {
216229
Write-Verbose "Update Manifest at $OutputManifest with version: $Version"
Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
#requires -Module ModuleBuilder
22

33
Describe "Build-Module With Source1" {
4-
Write-Host "Current Modules`n$(Get-Module | Out-String)"
4+
Write-Host "Current Modules: `n$(Get-Module | Out-String)" -ForegroundColor DarkGreen -BackgroundColor Yellow
55
Context "When we call Build-Module" {
66
$Output = Build-Module $PSScriptRoot\Source1\build.psd1 -Passthru
7+
$Module = [IO.Path]::ChangeExtension($Output.Path, "psm1")
78

89
It "Should not put the module's DefaultCommandPrefix into the psm1 as code. Duh!" {
9-
[IO.Path]::ChangeExtension($Output.Path, "psm1") |
10-
Should -Not -FileContentMatch '^Source$'
10+
$Module | Should -Not -FileContentMatch '^Source$'
11+
}
12+
13+
$Metadata = Import-Metadata $Output.Path
14+
15+
It "Should update FunctionsToExport in the manifest" {
16+
$Metadata.FunctionsToExport | Should -Be @("Get-Finale", "Get-Source")
17+
}
18+
19+
It "Should update AliasesToExport in the manifest" {
20+
$Metadata.AliasesToExport | Should -Be @("GS")
21+
}
22+
23+
It "Should de-dupe and move using statements to the top of the file" {
24+
Select-String -Pattern "^using" -Path $Module | ForEach-Object LineNumber | Should -Be 1
25+
}
26+
27+
It "Will comment out the original using statements in their original positions" {
28+
(Select-String -Pattern "^#\s*using" -Path $Module).Count | Should -Be 2
1129
}
1230
}
1331
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using module ModuleBuilder
2+
3+
function Get-Finale {
4+
[CmdletBinding()]
5+
[Alias("gs")]
6+
param()
7+
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
using module ModuleBuilder
2+
13
function Get-Source {
24
[CmdletBinding()]
5+
[Alias("gs")]
36
param()
4-
}
7+
}

Tests/Integration/Source1/Source1.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
# Always define FunctionsToExport as an empty @() which will be replaced on build
4141
FunctionsToExport = @()
42+
AliasesToExport = @()
4243

4344
# ID used to uniquely identify this module
4445
GUID = 'a264e183-e0f7-4219-bc80-c30d14e0e98e'
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
Describe "GetCommandAlias" {
2+
Import-Module ModuleBuilder -DisableNameChecking -Verbose:$False
3+
4+
Context "Mandatory Parameter" {
5+
$CommandInfo = InModuleScope ModuleBuilder { Get-Command GetCommandAlias }
6+
7+
It 'has a mandatory AST parameter' {
8+
$AST = $CommandInfo.Parameters['AST']
9+
$AST | Should -Not -BeNullOrEmpty
10+
$AST.ParameterType | Should -Be ([System.Management.Automation.Language.Ast])
11+
$AST.Attributes.Where{ $_ -is [Parameter] }.Mandatory | Should -Be $true
12+
}
13+
14+
}
15+
16+
Context "Parsing Code" {
17+
It "returns a hashtable of command names to aliases" {
18+
$Result = InModuleScope ModuleBuilder {
19+
GetCommandAlias -Ast {
20+
function Test-Alias {
21+
[Alias("Foo","Bar","Alias")]
22+
param()
23+
}
24+
}.Ast
25+
}
26+
27+
$Result["Test-Alias"] | Should -Be @("Foo", "Bar", "Alias")
28+
}
29+
}
30+
31+
Context "Parsing Code" {
32+
It "Parses only top-level functions, and returns them in order" {
33+
$Result = InModuleScope ModuleBuilder {
34+
GetCommandAlias -Ast {
35+
function Test-Alias {
36+
[Alias("TA", "TAlias")]
37+
param()
38+
}
39+
40+
function TestAlias {
41+
[Alias("T")]
42+
param()
43+
44+
function Test-Negative {
45+
[Alias("TN")]
46+
param()
47+
}
48+
}
49+
}.Ast
50+
}
51+
52+
$Result.Keys | Should -Be "Test-Alias", "TestAlias"
53+
$Result["Test-Alias"] | Should -Be "TA","TAlias"
54+
$Result["TestAlias"] | Should -Be "T"
55+
}
56+
}
57+
}

Tests/Public/Build-Module.Tests.ps1

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@ Describe "Build-Module" {
6161
New-Item -ItemType Directory -Path TestDrive:\1.0.0\ -Force
6262

6363
Mock SetModuleContent -ModuleName ModuleBuilder {}
64+
Mock ConvertToAst -ModuleName ModuleBuilder {
65+
[PSCustomObject]@{
66+
PSTypeName = "PoshCode.ModuleBuilder.ParseResults"
67+
ParseErrors = $null
68+
Tokens = $null
69+
AST = { }.AST
70+
}
71+
}
72+
Mock GetCommandAlias -ModuleName ModuleBuilder { @{'Get-MyInfo' = @('GMI') } }
73+
Mock MoveUsingStatements -ModuleName ModuleBuilder {}
6474
Mock Update-Metadata -ModuleName ModuleBuilder {}
6575
Mock InitializeBuild -ModuleName ModuleBuilder {
6676
# These are actually all the values that we need
@@ -111,6 +121,16 @@ Describe "Build-Module" {
111121
}
112122
}
113123

124+
It "Should call ConvertToAst to parse the module" {
125+
Assert-MockCalled ConvertToAst -ModuleName ModuleBuilder
126+
}
127+
128+
It "Should call MoveUsingStatements to move the using statements, just in case" {
129+
Assert-MockCalled MoveUsingStatements -ModuleName ModuleBuilder -Parameter {
130+
$AST.Extent.Text -eq "{ }"
131+
}
132+
}
133+
114134
It "Should call SetModuleContent to combine the source files" {
115135
Assert-MockCalled SetModuleContent -ModuleName ModuleBuilder
116136
}
@@ -120,6 +140,12 @@ Describe "Build-Module" {
120140
$PropertyName -eq "FunctionsToExport"
121141
}
122142
}
143+
144+
It "Should call Update-Metadata to set the AliasesToExport" {
145+
Assert-MockCalled Update-Metadata -ModuleName ModuleBuilder -Parameter {
146+
$PropertyName -eq "AliasesToExport"
147+
}
148+
}
123149
}
124150

125151
Context "When run without 'Clean' in the target" {

0 commit comments

Comments
 (0)