forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-Module.Tests.ps1
More file actions
173 lines (145 loc) · 8.71 KB
/
Copy pathGet-Module.Tests.ps1
File metadata and controls
173 lines (145 loc) · 8.71 KB
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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
Describe "Get-Module -ListAvailable" -Tags "CI" {
BeforeAll {
$originalPSModulePath = $env:PSModulePath
New-Item -ItemType Directory -Path "$testdrive\Modules\Foo\1.1" -Force > $null
New-Item -ItemType Directory -Path "$testdrive\Modules\Foo\2.0" -Force > $null
New-Item -ItemType Directory -Path "$testdrive\Modules\Bar\Download" -Force > $null
New-Item -ItemType Directory -Path "$testdrive\Modules\Zoo\Too" -Force > $null
New-ModuleManifest -Path "$testdrive\Modules\Foo\1.1\Foo.psd1" -ModuleVersion 1.1
New-ModuleManifest -Path "$testdrive\Modules\Foo\2.0\Foo.psd1" -ModuleVersion 2.0
New-ModuleManifest -Path "$testdrive\Modules\Bar\Bar.psd1"
New-ModuleManifest -Path "$testdrive\Modules\Zoo\Zoo.psd1"
New-Item -ItemType File -Path "$testdrive\Modules\Foo\1.1\Foo.psm1" > $null
New-Item -ItemType File -Path "$testdrive\Modules\Foo\2.0\Foo.psm1" > $null
New-Item -ItemType File -Path "$testdrive\Modules\Bar\Bar.psm1" > $null
New-Item -ItemType File -Path "$testdrive\Modules\Bar\Download\Download.psm1" > $null
New-Item -ItemType File -Path "$testdrive\Modules\Zoo\Zoo.psm1" > $null
New-Item -ItemType File -Path "$testdrive\Modules\Zoo\Too\Zoo.psm1" > $null
$fullyQualifiedPathTestCases = @(
# The current behaviour in PowerShell is that version gets ignored when using Get-Module -FullyQualifiedName with a path
@{ ModPath = "$TestDrive/Modules\Foo"; Name = 'Foo'; Version = '2.0'; Count = 2 }
@{ ModPath = "$TestDrive\Modules/Foo\1.1/Foo.psd1"; Name = 'Foo'; Version = '1.1'; Count = 1 }
@{ ModPath = "$TestDrive\Modules/Bar.psd1"; Name = 'Bar'; Version = '0.0'; Count = 1 }
@{ ModPath = "$TestDrive\Modules\Zoo\Too\Zoo.psm1"; Name = 'Zoo'; Version = '0.0'; Count = 1 }
)
$env:PSModulePath = Join-Path $testdrive "Modules"
}
AfterAll {
$env:PSModulePath = $originalPSModulePath
}
It "Get-Module -ListAvailable" {
$modules = Get-Module -ListAvailable
$modules.Count | Should -Be 4
$modules = $modules | Sort-Object -Property Name, Version
$modules.Name -join "," | Should -BeExactly "Bar,Foo,Foo,Zoo"
$modules[1].Version | Should -Be "1.1"
$modules[2].Version | Should -Be '2.0'
}
It "Get-Module <Name> -ListAvailable" {
$modules = Get-Module F* -ListAvailable
$modules.Count | Should -Be 2
$modules = $modules | Sort-Object -Property Version
$modules.Name -join "," | Should -BeExactly "Foo,Foo"
$modules[0].Version | Should -Be "1.1"
$modules[1].Version | Should -Be "2.0"
}
It "Get-Module -ListAvailable -All" {
$modules = Get-Module -ListAvailable -All
$modules.Count | Should -Be 10
$modules = $modules | Sort-Object -Property Name, Path
$modules.Name -join "," | Should -BeExactly "Bar,Bar,Download,Foo,Foo,Foo,Foo,Zoo,Zoo,Zoo"
$modules[0].ModuleType | Should -BeExactly "Manifest"
$modules[1].ModuleType | Should -BeExactly "Script"
$modules[2].ModuleType | Should -BeExactly "Script"
$modules[3].ModuleType | Should -BeExactly "Manifest"
$modules[3].Version | Should -Be "1.1"
$modules[4].ModuleType | Should -BeExactly "Script"
$modules[5].ModuleType | Should -BeExactly "Manifest"
$modules[5].Version | Should -Be "2.0"
$modules[6].ModuleType | Should -BeExactly "Script"
$modules[7].ModuleType | Should -BeExactly "Script"
$modules[7].Path | Should -BeExactly (Resolve-Path "$testdrive\Modules\Zoo\Too\Zoo.psm1").Path
$modules[8].ModuleType | Should -BeExactly "Manifest"
$modules[8].Path | Should -BeExactly (Resolve-Path "$testdrive\Modules\Zoo\Zoo.psd1").Path
$modules[9].ModuleType | Should -BeExactly "Script"
$modules[9].Path | Should -BeExactly (Resolve-Path "$testdrive\Modules\Zoo\Zoo.psm1").Path
}
It "Get-Module <Name> -ListAvailable -All" {
$modules = Get-Module down*, zoo -ListAvailable -All
$modules.Count | Should -Be 4
$modules = $modules | Sort-Object -Property Name, Path
$modules.Name -join "," | Should -BeExactly "Download,Zoo,Zoo,Zoo"
$modules[0].Path | Should -BeExactly (Resolve-Path "$testdrive\Modules\Bar\Download\Download.psm1").Path
$modules[1].Path | Should -BeExactly (Resolve-Path "$testdrive\Modules\Zoo\Too\Zoo.psm1").Path
$modules[2].Path | Should -BeExactly (Resolve-Path "$testdrive\Modules\Zoo\Zoo.psd1").Path
$modules[3].Path | Should -BeExactly (Resolve-Path "$testdrive\Modules\Zoo\Zoo.psm1").Path
}
It "Get-Module <Path> -ListAvailable" {
$modules = Get-Module "$testdrive\Modules\*" -ListAvailable
$modules.Count | Should -Be 4
$modules = $modules | Sort-Object -Property Name, Version
$modules.Name -join "," | Should -BeExactly "Bar,Foo,Foo,Zoo"
$modules[1].Version | Should -Be "1.1"
$modules[2].Version | Should -Be '2.0'
}
It "Get-Module <Path> -ListAvailable -All" {
$modules = Get-Module "$testdrive\Modules\*" -ListAvailable -All
$modules.Count | Should -Be 5
$modules = $modules | Sort-Object -Property Name, Path
$modules.Name -join "," | Should -BeExactly "Bar,Foo,Foo,Zoo,Zoo"
$modules[3].Path | Should -BeExactly (Resolve-Path "$testdrive\Modules\Zoo\Too\Zoo.psm1").Path
}
It "Get-Module -FullyQualifiedName <FullyQualifiedName> -ListAvailable" {
$moduleSpecification = @{ModuleName = "Foo"; ModuleVersion = "2.0"}
$modules = Get-Module -FullyQualifiedName $moduleSpecification -ListAvailable
$modules | Should -HaveCount 1
$modules.Name | Should -BeExactly "Foo"
$modules.Version | Should -BeExactly "2.0"
}
It "Get-Module <Name> -Refresh -ListAvailable" {
$modules = Get-Module -Name 'Zoo' -ListAvailable
$modules | Should -HaveCount 1
$modules.Name | Should -BeExactly "Zoo"
$modules.ExportedFunctions.Count | Should -Be 0 -Because 'No exports were defined'
New-ModuleManifest -Path "$testdrive\Modules\Zoo\Zoo.psd1" -FunctionsToExport 'Test-ZooFunction'
$modules = Get-Module -Name 'Zoo' -ListAvailable -Refresh
$modules | Should -HaveCount 1
$modules.Name | Should -BeExactly "Zoo"
$modules.ExportedFunctions.Count | Should -Be 1 -Because 'We added a new function to export'
}
It "Get-Module respects absolute paths in module specifications: <ModPath>" -TestCases $fullyQualifiedPathTestCases {
param([string]$ModPath, [string]$Name, [string]$Version, [int]$Count)
$modSpec = @{
ModuleName = $ModPath
RequiredVersion = $Version
}
$modules = Get-Module -ListAvailable -FullyQualifiedName $modSpec
$modules | Should -HaveCount $Count
$modules[0].Name | Should -BeExactly $Name
$modules.Version | Should -Contain $Version
}
Context "PSEdition" {
BeforeAll {
New-Item -ItemType Directory -Path "$testdrive\Modules\DesktopOnlyModule" -Force > $null
New-Item -ItemType Directory -Path "$testdrive\Modules\CoreOnlyModule" -Force > $null
New-Item -ItemType Directory -Path "$testdrive\Modules\CoreAndDesktopModule" -Force > $null
New-ModuleManifest -Path "$testdrive\Modules\DesktopOnlyModule\DesktopOnlyModule.psd1" -CompatiblePSEditions Desktop
New-ModuleManifest -Path "$testdrive\Modules\CoreOnlyModule\CoreOnlyModule.psd1" -CompatiblePSEditions Core
New-ModuleManifest -Path "$testdrive\Modules\CoreAndDesktopModule\CoreAndDesktopModule.psd1" -CompatiblePSEditions Core, Desktop
New-Item -ItemType File -Path "$testdrive\Modules\DesktopOnlyModule\DesktopOnlyModule.psm1" > $null
New-Item -ItemType File -Path "$testdrive\Modules\CoreOnlyModule\CoreOnlyModule.psm1" > $null
New-Item -ItemType File -Path "$testdrive\Modules\CoreAndDesktopModule\CoreAndDesktopModule.psm1" > $null
}
It "Get-Module -PSEdition <CompatiblePSEditions> -ListAvailable" -TestCases @(
@{ CompatiblePSEditions = 'Desktop'; ExpectedModule = 'CoreAndDesktopModule', 'DesktopOnlyModule' },
@{ CompatiblePSEditions = 'Core' ; ExpectedModule = 'CoreAndDesktopModule', 'CoreOnlyModule' }
) {
param ($CompatiblePSEditions, $ExpectedModule)
$modules = Get-Module -PSEdition $CompatiblePSEditions -ListAvailable
$modules | Should -HaveCount $ExpectedModule.Count
$modules.Name | Sort-Object | Should -BeExactly $ExpectedModule
}
}
}