File size: 13,422 Bytes
8c763fb | 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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | # Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Import-Module HelpersCommon
Describe "Test-ModuleManifest tests" -tags "CI" {
BeforeEach {
$testModulePath = "testdrive:/module/test.psd1"
New-Item -ItemType Directory -Path testdrive:/module > $null
}
AfterEach {
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue testdrive:/module
}
It "module manifest containing paths with backslashes or forwardslashes are resolved correctly" {
New-Item -ItemType Directory -Path testdrive:/module/foo > $null
New-Item -ItemType Directory -Path testdrive:/module/bar > $null
New-Item -ItemType File -Path testdrive:/module/foo/bar.psm1 > $null
New-Item -ItemType File -Path testdrive:/module/foo/bar.ps1 > $null
New-Item -ItemType File -Path testdrive:/module/foo/bar.ps1xml > $null
New-Item -ItemType File -Path testdrive:/module/bar/foo.psm1 > $null
New-Item -ItemType File -Path testdrive:/module/bar/foo.ps1 > $null
New-Item -ItemType File -Path testdrive:/module/bar/foo.ps1xml > $null
$testModulePath = "testdrive:/module/test.psd1"
$fileList = "foo\bar.psm1","bar/foo.psm1"
$scripts = "foo\/bar.ps1","bar/\foo.ps1"
$ps1xml = "foo//bar.ps1xml","bar\\foo.ps1xml"
New-ModuleManifest -NestedModules $fileList -RootModule foo\bar.psm1 -RequiredAssemblies $fileList -Path $testModulePath -TypesToProcess $ps1xml -FormatsToProcess $ps1xml -ScriptsToProcess $scripts -FileList $fileList -ModuleList $fileList
Test-Path $testModulePath | Should -BeTrue
# use -ErrorAction Stop to cause test to fail if Test-ModuleManifest writes to error stream
Test-ModuleManifest -Path $testModulePath -ErrorAction Stop | Should -BeOfType System.Management.Automation.PSModuleInfo
}
It "module manifest containing missing files returns error: <parameter>" -TestCases (
@{parameter = "RequiredAssemblies"; errorId = "Modules_InvalidRequiredAssembliesInModuleManifest"},
@{parameter = "NestedModules"; errorId = "Modules_InvalidNestedModuleinModuleManifest"},
@{parameter = "RequiredModules"; errorId = "Modules_InvalidRequiredModulesinModuleManifest"},
@{parameter = "FileList"; errorId = "Modules_InvalidFilePathinModuleManifest"},
@{parameter = "ModuleList"; errorId = "Modules_InvalidModuleListinModuleManifest"},
@{parameter = "TypesToProcess"; errorId = "Modules_InvalidManifest"},
@{parameter = "FormatsToProcess"; errorId = "Modules_InvalidManifest"},
@{parameter = "RootModule"; errorId = "Modules_InvalidRootModuleInModuleManifest"},
@{parameter = "ScriptsToProcess"; errorId = "Modules_InvalidManifest"}
) {
param ($parameter, $errorId)
New-Item -ItemType Directory -Path testdrive:/module/foo > $null
New-Item -ItemType File -Path testdrive:/module/foo/bar.psm1 > $null
$args = @{$parameter = "doesnotexist.psm1"}
New-ModuleManifest -Path $testModulePath @args
$fullyQualifiedErrorId = "$errorId,Microsoft.PowerShell.Commands.TestModuleManifestCommand"
{ Test-ModuleManifest -Path $testModulePath -ErrorAction Stop } | Should -Throw -ErrorId $fullyQualifiedErrorId
}
It "module manifest containing valid unprocessed rootmodule file type succeeds: <rootModuleValue>" -TestCases (
@{rootModuleValue = "foo.psm1"},
@{rootModuleValue = "foo.dll"},
@{rootModuleValue = "foo.exe"}
) {
param($rootModuleValue)
New-Item -ItemType File -Path testdrive:/module/$rootModuleValue > $null
New-ModuleManifest -Path $testModulePath -RootModule $rootModuleValue
$moduleManifest = Test-ModuleManifest -Path $testModulePath -ErrorAction Stop
$moduleManifest | Should -BeOfType System.Management.Automation.PSModuleInfo
$moduleManifest.RootModule | Should -Be $rootModuleValue
}
It "module manifest containing valid rootmodule without specifying .psm1 extension succeeds" {
$rootModuleFileName = "bar.psm1";
New-Item -ItemType File -Path testdrive:/module/$rootModuleFileName > $null
New-ModuleManifest -Path $testModulePath -RootModule "bar"
$moduleManifest = Test-ModuleManifest -Path $testModulePath -ErrorAction Stop
$moduleManifest | Should -BeOfType System.Management.Automation.PSModuleInfo
$moduleManifest.RootModule | Should -Be "bar"
}
It "module manifest containing valid processed empty rootmodule file type fails: <rootModuleValue>" -TestCases (
@{rootModuleValue = "foo.cdxml"; errorId = "System.Xml.XmlException"} # fails when cmdlet tries to read it as XML
) {
param($rootModuleValue, $errorId)
New-Item -ItemType File -Path testdrive:/module/$rootModuleValue > $null
New-ModuleManifest -Path $testModulePath -RootModule $rootModuleValue
$fullyQualifiedErrorId = "$errorId,Microsoft.PowerShell.Commands.TestModuleManifestCommand"
{ Test-ModuleManifest -Path $testModulePath -ErrorAction Stop } | Should -Throw -ErrorId $fullyQualifiedErrorId
}
It "module manifest containing empty rootmodule succeeds: <rootModuleValue>" -TestCases (
@{rootModuleValue = $null},
@{rootModuleValue = ""}
) {
param($rootModuleValue)
New-ModuleManifest -Path $testModulePath -RootModule $rootModuleValue
$moduleManifest = Test-ModuleManifest -Path $testModulePath -ErrorAction Stop
$moduleManifest | Should -BeOfType System.Management.Automation.PSModuleInfo
$moduleManifest.RootModule | Should -BeNullOrEmpty
}
It "module manifest containing invalid rootmodule returns error: <rootModuleValue>" -TestCases (
@{rootModuleValue = "foo.psd1"; errorId = "Modules_InvalidManifest"}
) {
param($rootModuleValue, $errorId)
New-Item -ItemType File -Path testdrive:/module/$rootModuleValue > $null
New-ModuleManifest -Path $testModulePath -RootModule $rootModuleValue
$fullyQualifiedErrorId = "$errorId,Microsoft.PowerShell.Commands.TestModuleManifestCommand"
{ Test-ModuleManifest -Path $testModulePath -ErrorAction Stop } | Should -Throw -ErrorId $fullyQualifiedErrorId
}
It "module manifest containing non-existing rootmodule returns error: <rootModuleValue>" -TestCases (
@{rootModuleValue = "doesnotexist.psm1"; errorId = "Modules_InvalidRootModuleInModuleManifest"}
) {
param($rootModuleValue, $errorId)
New-ModuleManifest -Path $testModulePath -RootModule $rootModuleValue
$fullyQualifiedErrorId = "$errorId,Microsoft.PowerShell.Commands.TestModuleManifestCommand"
{ Test-ModuleManifest -Path $testModulePath -ErrorAction Stop } | Should -Throw -ErrorId $fullyQualifiedErrorId
}
It "module manifest containing nested module gets returned: <variation>" -TestCases (
@{variation = "no analysis as all exported with no wildcard"; exportValue = "@()"},
@{variation = "analysis as exported with wildcard"; exportValue = "*"}
) {
param($exportValue)
New-Item -ItemType File -Path testdrive:/module/Foo.psm1 > $null
New-ModuleManifest -Path $testModulePath -NestedModules "Foo.psm1" -FunctionsToExport $exportValue -CmdletsToExport $exportValue -VariablesToExport $exportValue -AliasesToExport $exportValue
$module = Test-ModuleManifest -Path $testModulePath
$module.NestedModules | Should -HaveCount 1
$module.NestedModules.Name | Should -BeExactly "Foo"
}
It 'Works for manifest specified as UNC path' -Skip:(!$IsWindows) {
# chance the testdrive path to a UNC path
$testModulePath = '\\localhost\' + "$testDrive\test.psd1".Replace(':', '$')
New-Item -ItemType File -Path testdrive:/foo.psm1 > $null
New-ModuleManifest -Path $testModulePath -RootModule "foo.psm1"
$module = Test-ModuleManifest -Path $testModulePath
$module.RootModule | Should -Be "foo.psm1"
}
}
Describe "Tests for circular references in required modules" -tags "CI" {
function CreateTestModules([string]$RootPath, [string[]]$ModuleNames, [bool]$AddVersion, [bool]$AddGuid, [bool]$AddCircularReference)
{
$RequiredModulesSpecs = @();
foreach($moduleDir in New-Item $ModuleNames -ItemType Directory -Force)
{
if ($lastItem)
{
if ($AddVersion -or $AddGuid) {$RequiredModulesSpecs += $lastItem}
else {$RequiredModulesSpecs += $lastItem.ModuleName}
}
$ModuleVersion = '3.0'
$GUID = New-Guid
New-ModuleManifest ((Join-Path $moduleDir.Name $moduleDir.Name) + ".psd1") -RequiredModules $RequiredModulesSpecs -ModuleVersion $ModuleVersion -Guid $GUID
$lastItem = @{ ModuleName = $moduleDir.Name}
if ($AddVersion) {$lastItem += @{ ModuleVersion = $ModuleVersion}}
if ($AddGuid) {$lastItem += @{ GUID = $GUID}}
}
if ($AddCircularReference)
{
# rewrite first module's manifest to have a reference to the last module, i.e. making a circular reference
if ($AddVersion -or $AddGuid)
{
$firstModuleName = $RequiredModulesSpecs[0].ModuleName
$firstModuleVersion = $RequiredModulesSpecs[0].ModuleVersion
$firstModuleGuid = $RequiredModulesSpecs[0].GUID
$RequiredModulesSpecs = $lastItem
}
else
{
$firstModuleName = $RequiredModulesSpecs[0]
$firstModuleVersion = '3.0' # does not matter - not used in references
$firstModuleGuid = New-Guid # does not matter - not used in references
$RequiredModulesSpecs = $lastItem.ModuleName
}
New-ModuleManifest ((Join-Path $firstModuleName $firstModuleName) + ".psd1") -RequiredModules $RequiredModulesSpecs -ModuleVersion $firstModuleVersion -Guid $firstModuleGuid
}
}
function TestImportModule([bool]$AddVersion, [bool]$AddGuid, [bool]$AddCircularReference)
{
$moduleRootPath = Join-Path $TestDrive 'TestModules'
New-Item $moduleRootPath -ItemType Directory -Force > $null
Push-Location $moduleRootPath
$moduleCount = 6 # this depth was enough to find a bug in cyclic reference detection product code; greater depth will slow tests down
$ModuleNames = 1..$moduleCount | ForEach-Object {"TestModule$_"}
CreateTestModules $moduleRootPath $ModuleNames $AddVersion $AddGuid $AddCircularReference
$newpath = [system.io.path]::PathSeparator + "$moduleRootPath"
$OriginalPSModulePathLength = $env:PSModulePath.Length
$env:PSModulePath += $newpath
$lastModule = $ModuleNames[$moduleCount - 1]
try
{
Import-Module $lastModule -ErrorAction Stop
Get-Module $lastModule | Should -Not -BeNullOrEmpty
}
finally
{
#cleanup
Remove-Module $ModuleNames -Force -ErrorAction SilentlyContinue
$env:PSModulePath = $env:PSModulePath.Substring(0,$OriginalPSModulePathLength)
Pop-Location
Remove-Item $moduleRootPath -Recurse -Force -ErrorAction SilentlyContinue
}
}
It "No circular references and RequiredModules field has only module names" {
TestImportModule $false $false $false
}
It "No circular references and RequiredModules field has module names and versions" {
TestImportModule $true $false $false
}
It "No circular references and RequiredModules field has module names, versions and GUIDs" {
TestImportModule $true $true $false
}
It "Add a circular reference to RequiredModules and verify error" {
{ TestImportModule $false $false $true } | Should -Throw -ErrorId "Modules_InvalidManifest,Microsoft.PowerShell.Commands.ImportModuleCommand"
}
}
Describe "Test-ModuleManifest Performance bug followup" -tags "CI" {
BeforeAll {
$TestModulesPath = [System.IO.Path]::Combine($PSScriptRoot, 'assets', 'testmodulerunspace')
$PSHomeModulesPath = "$PSHOME\Modules"
# Install the Test Module
if (Test-CanWriteToPsHome) {
Copy-Item $TestModulesPath\* $PSHomeModulesPath -Recurse -Force -ErrorAction Stop
}
}
It "Test-ModuleManifest should not load unnessary modules" -Skip:(!(Test-CanWriteToPsHome)) {
$job = Start-Job -Name "job1" -ScriptBlock {Test-ModuleManifest "$using:PSHomeModulesPath\ModuleWithDependencies2\2.0\ModuleWithDependencies2.psd1" -Verbose} | Wait-Job
$verbose = $job.ChildJobs[0].Verbose.ReadAll()
# Before the fix, all modules under $PSHOME will be imported and will be far more than 15 verbose messages. However, we cannot fix the number in case verbose message may vary.
$verbose.Count | Should -BeLessThan 15
}
AfterAll {
#clean up the test modules
if (Test-CanWriteToPsHome) {
Remove-Item $PSHomeModulesPath\ModuleWithDependencies2 -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item $PSHomeModulesPath\NestedRequiredModule1 -Recurse -Force -ErrorAction SilentlyContinue
}
}
}
|