Skip to content

Commit 434281d

Browse files
committed
Add initial (insufficient) tests for Build-Module
1 parent d6bbe10 commit 434281d

File tree

2 files changed

+134
-20
lines changed

2 files changed

+134
-20
lines changed

Source/Public/Build-Module.ps1

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ function Build-Module {
3535
param(
3636
# The path to the module folder, manifest or build.psd1
3737
[Parameter(Position = 0, ValueFromPipelineByPropertyName)]
38-
[ValidateScript( {
39-
if (Test-Path $_) {
40-
$true
41-
} else {
42-
throw "Source must point to a valid module"
43-
}
44-
} )]
38+
[ValidateScript({
39+
if (Test-Path $_) {
40+
$true
41+
} else {
42+
throw "Source must point to a valid module"
43+
}
44+
})]
4545
[Alias("ModuleManifest", "Path")]
4646
[string]$SourcePath = $(Get-Location -PSProvider FileSystem),
4747

@@ -78,25 +78,18 @@ function Build-Module {
7878
# The prefix is either the path to a file (relative to the module folder) or text to put at the top of the file.
7979
# If the value of prefix resolves to a file, that file will be read in, otherwise, the value will be used.
8080
# The default is nothing. See examples for more details.
81-
$Prefix,
81+
[string]$Prefix,
8282

8383
# The Suffix is either the path to a file (relative to the module folder) or text to put at the bottom of the file.
8484
# If the value of Suffix resolves to a file, that file will be read in, otherwise, the value will be used.
8585
# The default is nothing. See examples for more details.
8686
[Alias("ExportModuleMember","Postfix")]
87-
$Suffix,
87+
[string]$Suffix,
8888

8989
# Controls whether or not there is a build or cleanup performed
9090
[ValidateSet("Clean", "Build", "CleanBuild")]
9191
[string]$Target = "CleanBuild",
9292

93-
# Your CultureInfo is, by default, the current UICulture and is used to determine the language of the ReadMe.
94-
[Globalization.CultureInfo]$Culture = $(Get-UICulture),
95-
96-
# The Readme or About is the path to a file (relative to the module folder) to include as the about_Module.help.txt
97-
[Alias("AboutPath")]
98-
$ReadMe,
99-
10093
# Output the ModuleInfo of the "built" module
10194
[switch]$Passthru
10295
)
@@ -138,7 +131,7 @@ function Build-Module {
138131
return # Skip the build
139132
}
140133
}
141-
$null = mkdir $OutputDirectory -Force
134+
$null = New-Item -ItemType Directory -Path $OutputDirectory -Force
142135

143136
# Note that this requires that the module manifest be in the "root" of the source directories
144137
Set-Location $ModuleInfo.ModuleBase
@@ -151,8 +144,6 @@ function Build-Module {
151144
Copy-Item -Path $ModuleInfo.CopyDirectories -Recurse -Destination $OutputDirectory -Force
152145
}
153146

154-
$ModuleInfo | CopyReadMe
155-
156147
Write-Verbose "Combine scripts to $RootModule"
157148

158149
# SilentlyContinue because there don't *HAVE* to be functions at all
@@ -179,7 +170,7 @@ function Build-Module {
179170
Get-Module $OutputManifest -ListAvailable
180171
}
181172
} finally {
182-
Pop-Location -StackName Optimize-Module -ErrorAction SilentlyContinue
173+
Pop-Location -StackName Build-Module -ErrorAction SilentlyContinue
183174
}
184175
}
185176
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
Describe "Build-Module" {
2+
3+
Context "Parameter Binding" {
4+
5+
$Parameters = (Get-Command Build-Module).Parameters
6+
7+
It "has an optional string parameter for the SourcePath" {
8+
$parameters.ContainsKey("SourcePath") | Should -Be $true
9+
$parameters["SourcePath"].ParameterType | Should -Be ([string])
10+
$parameters["SourcePath"].Attributes.Where{$_ -is [Parameter]}.Mandatory | Should -Be $false
11+
}
12+
13+
It "has an optional string parameter for the OutputDirectory" {
14+
$parameters.ContainsKey("OutputDirectory") | Should -Be $true
15+
$parameters["OutputDirectory"].ParameterType | Should -Be ([string])
16+
$parameters["OutputDirectory"].Attributes.Where{$_ -is [Parameter]}.Mandatory | Should -Be $false
17+
}
18+
19+
It "has an optional parameter for setting the Version"{
20+
$parameters.ContainsKey("Version") | Should -Be $true
21+
$parameters["Version"].ParameterType | Should -Be ([version])
22+
$parameters["Version"].Attributes.Where{$_ -is [Parameter]}.Mandatory | Should -Be $false
23+
}
24+
25+
It "has an optional parameter for setting the Encoding"{
26+
$parameters.ContainsKey("Encoding") | Should -Be $true
27+
# Note that in PS Core, we can't use encoding types for parameters
28+
$parameters["Encoding"].ParameterType | Should -Be ([string])
29+
$parameters["Encoding"].Attributes.Where{$_ -is [Parameter]}.Mandatory | Should -Be $false
30+
}
31+
32+
It "has an optional string parameter for a Prefix"{
33+
$parameters.ContainsKey("Prefix") | Should -Be $true
34+
$parameters["Prefix"].ParameterType | Should -Be ([string])
35+
$parameters["Prefix"].Attributes.Where{$_ -is [Parameter]}.Mandatory | Should -Be $false
36+
}
37+
38+
It "has an optional string parameter for a Suffix" {
39+
$parameters.ContainsKey("Suffix") | Should -Be $true
40+
$parameters["Suffix"].ParameterType | Should -Be ([string])
41+
$parameters["Suffix"].Attributes.Where{$_ -is [Parameter]}.Mandatory | Should -Be $false
42+
}
43+
44+
It "supports setting the Target to Clean, Build or both" {
45+
$parameters.ContainsKey("Target") | Should -Be $true
46+
47+
# Techincally we could implement this a few other ways ...
48+
$parameters["Target"].ParameterType | Should -Be ([string])
49+
$parameters["Target"].Attributes.Where{$_ -is [ValidateSet]}.ValidValues | Should -Be "Clean", "Build", "CleanBuild"
50+
}
51+
52+
It "has an Passthru switch parameter" {
53+
$parameters.ContainsKey("Passthru") | Should -Be $true
54+
$parameters["Passthru"].ParameterType | Should -Be ([switch])
55+
}
56+
}
57+
58+
Context "When run without parameters" {
59+
Push-Location TestDrive:\ -StackName BuildModuleTest
60+
New-Item -ItemType Directory -Path TestDrive:\MyModule\ -Force
61+
New-Item -ItemType Directory -Path TestDrive:\1.0.0\ -Force
62+
63+
Mock SetModuleContent -ModuleName ModuleBuilder {}
64+
Mock Update-Metadata -ModuleName ModuleBuilder {}
65+
Mock InitializeBuild -ModuleName ModuleBuilder {
66+
# These are actually all the values that we need
67+
@{
68+
OutputDirectory = "TestDrive:\1.0.0"
69+
Name = "MyModule"
70+
ModuleBase = "TestDrive:\MyModule\"
71+
CopyDirectories = @()
72+
Encoding = "UTF8"
73+
PublicFilter = "Public\*.ps1"
74+
}
75+
}
76+
77+
Mock Test-Path {$True} -Parameter {$Path -eq "TestDrive:\1.0.0"} -ModuleName ModuleBuilder
78+
Mock Remove-Item {} -Parameter {$Path -eq "TestDrive:\1.0.0"} -ModuleName ModuleBuilder
79+
Mock Set-Location {} -ModuleName ModuleBuilder
80+
Mock Copy-Item {} -ModuleName ModuleBuilder
81+
82+
Mock Get-ChildItem {
83+
[IO.FileInfo]$(Join-Path $(Convert-Path "TestDrive:\") "MyModule\Public\Get-MyInfo.ps1")
84+
} -ModuleName ModuleBuilder
85+
86+
Mock New-Item {} -Parameter {
87+
$Path -eq "TestDrive:\1.0.0" -and
88+
$ItemType -eq "Directory" -and
89+
$Force -eq $true
90+
} -ModuleName ModuleBuilder
91+
92+
try {
93+
Build-Module
94+
} finally {
95+
Pop-Location -StackName BuildModuleTest
96+
}
97+
98+
# NOTE: We're not just clearing output, but the whole folder
99+
It "Should remove the output folder if it exists" {
100+
Assert-MockCalled Remove-Item -ModuleName ModuleBuilder
101+
}
102+
103+
It "Should always (re)create the OutputDirectory" {
104+
Assert-MockCalled New-Item -ModuleName ModuleBuilder
105+
}
106+
107+
It "Should run in the module source folder" {
108+
Assert-MockCalled Set-Location -ModuleName ModuleBuilder -Parameter {
109+
$Path -eq "TestDrive:\MyModule\"
110+
}
111+
}
112+
113+
It "Should call SetModuleContent to combine the source files" {
114+
Assert-MockCalled SetModuleContent -ModuleName ModuleBuilder
115+
}
116+
117+
It "Should call Update-Metadata to set the FunctionsToExport" {
118+
Assert-MockCalled Update-Metadata -ModuleName ModuleBuilder -Parameter {
119+
$PropertyName -eq "FunctionsToExport"
120+
}
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)