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