-
-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathAboutModules.Koans.ps1
213 lines (179 loc) · 6.89 KB
/
AboutModules.Koans.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using module PSKoans
[Koan(Position = 212)]
param()
<#
Modules
PowerShell is built from various types of modules, which come in three flavours:
- Script modules
- Manifest modules
- Binary modules
Manifest modules are the least common, consisting only of a single .psd1 file;
their primary code is often stored in a core library of PowerShell and is not
considered part of 'the module' itself, merely exposed by it.
Binary modules are compiled from C# using the PowerShell libraries and tend to
be in .dll formats with other files providing metadata.
Script modules commonly consist of .psm1, .psd1, .ps1 and other files, and are
one of the more common module types.
#>
Describe 'Get-Module' {
<#
Get-Module is used to find out which modules are presently loaded into
the session, or to search available modules on the computer.
#>
It 'returns a list of modules in the current session' {
<#
By default, Get-Module returns modules currently in use.
To get a list of all modules on the system, you can use the -ListAvailable switch.
#>
$Modules = Get-Module | Sort-Object -Property Name -Unique
$FirstThreeModules = $Modules | Select-Object -First 3
$VersionOfThirdModule = $FirstThreeModules[2].Version
$TypeOfLastModule = $Modules |
Select-Object -Last 1 |
ForEach-Object -MemberName ModuleType
@('____', '____', '____') | Should -Be $FirstThreeModules.Name
'____' | Should -Be $VersionOfThirdModule
'____' | Should -Be $TypeOfLastModule
}
It 'can filter by module name' {
$Pester = Get-Module -Name 'Pester'
$PesterCommands = $Pester.ExportedCommands.Values.Name
@('____', '____', '____') | Should -BeIn $PesterCommands
}
It 'can list nested modules' {
$AllModules = Get-Module -All
$Axioms = $AllModules | Where-Object Name -eq 'Axiom'
$AxiomsCommands = $Axioms.ExportedCommands.Values.Name
$Commands = @('____', '____', '____')
$Commands | Should -BeIn $AxiomsCommands
<#
Despite us being able to 'see' this module, we cannot use its commands;
it is only available within the scope of the Pester module itself,
since it's a nested module within Pester.
#>
{
if ($Commands[1] -in $Axioms.ExportedCommands.Values.Name) {
& $Commands[1]
}
} | Should -Throw
}
}
Describe 'Find-Module' {
<#
Find-Module is very similar to Get-Module, except that it searches all
registered PSRepositories in order to find a module available for
install.
#>
BeforeAll {
<#
This will effectively produce similar output to the Find-Module cmdlet
while only searching the local modules instead of online ones. 'Mock'
is a Pester command that will be covered later.
#>
Mock Find-Module {
Get-Module -ListAvailable -Name $Name |
Select-Object -Property Version, Name, @{
Name = 'Repository'
Expression = {'PSGallery'}
}, Description
}
$Module = Find-Module -Name 'Pester' | Select-Object -First 1
}
It 'finds modules that can be installed' {
'____' | Should -Be $Module.Name
}
It 'lists the latest version of the module' {
'____' | Should -Be $Module.Version
}
It 'indicates which repository stores the module' {
<#
Unless an additional repository has been configured, all modules
from Find-Module will come from the PowerShell Gallery.
#>
'____' | Should -Be $Module.Repository
}
It 'gives a brief description of the module' {
'____' | Should -Match $Module.Description
}
}
Describe 'New-Module' {
<#
New-Module is rarely used in practice, but is capable of dynamically generating
a module in-memory without needing a file on disk.
#>
BeforeAll {
$Module = New-Module -Name 'PSKoans_TestModule' -ScriptBlock {}
}
It 'creates a dynamic module object' {
$Module | Should -Not -BeNullOrEmpty
}
It 'has many properties with $null defaults' {
<#
RootModule is usually the script file or .dll that contains the main portion of the module's code.
For a module like this, that property is meaningless -- there is no such file.
#>
$____ | Should -Be $Module.RootModule
}
It 'uses an existing filesystem location as the ModuleBase' {
<#
In most cases, the ModuleBase for a dynamically generated module will be the current location.
This may not be accurate in all cases, depending on scoping.
#>
'____' | Should -Be $Module.ModuleBase
}
AfterAll {
$Module | Remove-Module
}
}
Describe 'Import-Module' {
<#
Import-Module is an auxiliary cmdlet that imports the requested module
into the current session. It is capable of both importing
#>
Context 'Importing Installed Modules' {
BeforeAll {
$Module = New-Module -Name 'PSKoans_ImportModuleTest' { }
}
It 'does not produce output' {
Import-Module $Module | Should -BeNull
}
It 'imports the module into the current session' {
$ImportedModule = Get-Module -Name 'PSKoans_ImportModuleTest'
$ImportedModule.ExportedCommands.Keys | Should -BeNull
'____' | Should -Be $ImportedModule.Name
}
}
Context 'Importing Module From File' {
BeforeAll {
$ModuleScript = {
function Test-ModuleFunction {
'____' # Fill in the correct value here
}
Export-ModuleMember 'Test-ModuleFunction'
}
$ModuleScript.ToString() | Set-Content -Path 'TestDrive:\TestModule.psm1'
}
It 'does not produce output' {
Import-Module 'TestDrive:\TestModule.psm1' | Should -BeNullOrEmpty
}
It 'imports the module into the current session' {
$ImportedModule = Get-Module -Name '____'
$ImportedModule | Should -Not -BeNullOrEmpty
}
It 'makes the exported commands from the module available for use' {
$ImportedModule = Get-Module -Name '____'
@('____') | Should -Be $ImportedModule.ExportedCommands.Values.Name
Test-ModuleFunction | Should -BeExactly 'A successful test.'
}
}
AfterAll {
Remove-Module -Name 'TestModule', 'PSKoans_ImportModuleTest'
}
}
<#
Other *-Module Cmdlets
Check out the other module cmdlets with:
Get-Help *-Module
Get-Command -Noun Module
You'll need Install-Module to install new ones, at least!
#>