forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImport-Module.Tests.ps1
More file actions
90 lines (74 loc) · 3.67 KB
/
Copy pathImport-Module.Tests.ps1
File metadata and controls
90 lines (74 loc) · 3.67 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
Describe "Import-Module" -Tags "CI" {
$moduleName = "Microsoft.PowerShell.Security"
BeforeEach {
Remove-Module -Name $moduleName -Force
(Get-Module -Name $moduleName).Name | Should BeNullOrEmpty
}
AfterEach {
Import-Module -Name $moduleName -Force
(Get-Module -Name $moduleName).Name | Should Be $moduleName
}
It "should be able to add a module with using Name switch" {
{ Import-Module -Name $moduleName } | Should Not Throw
(Get-Module -Name $moduleName).Name | Should Be $moduleName
}
It "should be able to add a module with using ModuleInfo switch" {
$a = Get-Module -ListAvailable $moduleName
{ Import-Module -ModuleInfo $a } | Should Not Throw
(Get-Module -Name $moduleName).Name | Should Be $moduleName
}
}
Describe "Import-Module with ScriptsToProcess" -Tags "CI" {
BeforeAll {
$moduleRootPath = Join-Path $TestDrive 'TestModules'
New-Item $moduleRootPath -ItemType Directory -Force | Out-Null
Push-Location $moduleRootPath
"1 | Out-File out.txt -Append -NoNewline" | Out-File script1.ps1
"2 | Out-File out.txt -Append -NoNewline" | Out-File script2.ps1
New-ModuleManifest module1.psd1 -ScriptsToProcess script1.ps1
New-ModuleManifest module2.psd1 -ScriptsToProcess script2.ps1 -NestedModules module1.psd1
}
AfterAll {
Pop-Location
}
BeforeEach {
New-Item out.txt -ItemType File -Force | Out-Null
}
AfterEach {
$m = @('module1','module2','script1','script2')
remove-module $m -Force -ErrorAction SilentlyContinue
Remove-Item out.txt -Force -ErrorAction SilentlyContinue
}
$testCases = @(
@{ TestNameSuffix = 'for top-level module'; ipmoParms = @{'Name'='.\module1.psd1'}; Expected = '1' }
@{ TestNameSuffix = 'for top-level and nested module'; ipmoParms = @{'Name'='.\module2.psd1'}; Expected = '21' }
@{ TestNameSuffix = 'for top-level module when -Version is specified'; ipmoParms = @{'Name'='.\module1.psd1'; 'Version'='1.0'}; Expected = '1' }
@{ TestNameSuffix = 'for top-level and nested module when -Version is specified'; ipmoParms = @{'Name'='.\module2.psd1'; 'Version'='1.0'}; Expected = '21' }
)
It "Verify ScriptsToProcess are executed <TestNameSuffix>" -TestCases $testCases {
param($TestNameSuffix,$ipmoParms,$Expected)
Import-Module @ipmoParms
Get-Content out.txt | Should Be $Expected
}
}
Describe "Import-Module for Binary Modules in GAC" -Tags 'CI' {
Context "Modules are not loaded from GAC" {
BeforeAll {
[System.Management.Automation.PowerShellAssemblyLoadContextTestHooks]::SetTestHook('AllowGACLoading', $false)
}
AfterAll {
[System.Management.Automation.PowerShellAssemblyLoadContextTestHooks]::SetTestHook('AllowGACLoading', $true)
}
It "Load PSScheduledJob from Windows Powershell Modules folder should fail" -Skip:(-not $IsWindows) {
$modulePath = Join-Path $env:windir "System32/WindowsPowershell/v1.0/Modules/PSScheduledJob"
{ Import-Module $modulePath -ErrorAction SilentlyContinue } | ShouldBeErrorId 'FormatXmlUpdateException,Microsoft.PowerShell.Commands.ImportModuleCommand'
}
}
Context "Modules are loaded from GAC" {
It "Load PSScheduledJob from Windows Powershell Modules folder" -Skip:(-not $IsWindows) {
$modulePath = Join-Path $env:windir "System32/WindowsPowershell/v1.0/Modules/PSScheduledJob"
Import-Module $modulePath
(Get-Command New-JobTrigger).Name | Should Be 'New-JobTrigger'
}
}
}