Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,25 @@ internal bool LoadModuleManifestData(ExternalScriptInfo scriptInfo, ManifestProc
return true;
}

/// <summary>
/// Helper function to generate fake PSModuleInfo objects from ModuleSpecification objects.
/// </summary>
/// <param name="moduleSpecs">Collection of ModuleSpecification objects</param>
/// <returns>Collection of fake PSModuleInfo objects</returns>
private IEnumerable<PSModuleInfo> CreateFakeModuleObject(IEnumerable<ModuleSpecification> moduleSpecs)
{
foreach (ModuleSpecification moduleSpec in moduleSpecs)
{
var fakeModuleInfo = new PSModuleInfo(moduleSpec.Name, Context, null);
if (moduleSpec.Guid.HasValue)
{
fakeModuleInfo.SetGuid(moduleSpec.Guid.Value);
}
fakeModuleInfo.SetVersion(moduleSpec.RequiredVersion ?? moduleSpec.Version);
yield return fakeModuleInfo;
}
}

private ErrorRecord GetErrorRecordIfUnsupportedRootCdxmlAndNestedModuleScenario(
Hashtable data,
string moduleManifestPath,
Expand Down Expand Up @@ -1977,16 +1996,8 @@ internal PSModuleInfo LoadModuleManifest(
}
else
{
PSModuleInfo fakeRequiredModuleInfo = null;
foreach (ModuleSpecification requiredModule in requiredModules)
foreach (PSModuleInfo fakeRequiredModuleInfo in CreateFakeModuleObject(requiredModules))
{
fakeRequiredModuleInfo = new PSModuleInfo(requiredModule.Name, Context, null);
if (requiredModule.Guid.HasValue)
{
fakeRequiredModuleInfo.SetGuid(requiredModule.Guid.Value);
}
fakeRequiredModuleInfo.SetVersion(requiredModule.RequiredVersion ?? requiredModule.Version);

requiredModulesSpecifiedInModuleManifest.Add(fakeRequiredModuleInfo);
}
}
Expand Down Expand Up @@ -2797,6 +2808,12 @@ internal PSModuleInfo LoadModuleManifest(

if (!needToAnalyzeScriptModules)
{
// Add nested modules to the manifestInfo when no more analysis needs to be done
foreach (PSModuleInfo fakeNestedModuleInfo in CreateFakeModuleObject(nestedModules))
{
manifestInfo.AddNestedModule(fakeNestedModuleInfo);
}

return manifestInfo;
}
}
Expand Down Expand Up @@ -3428,6 +3445,7 @@ internal PSModuleInfo LoadModuleManifest(
updated.Add(element);
}
}

ss.Internal.ExportedVariables.Clear();
ss.Internal.ExportedVariables.AddRange(updated);
}
Expand Down
56 changes: 29 additions & 27 deletions test/powershell/engine/Module/TestModuleManifest.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@
# Licensed under the MIT License.
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
New-Item -ItemType Directory -Path testdrive:/module/foo
New-Item -ItemType Directory -Path testdrive:/module/bar
New-Item -ItemType File -Path testdrive:/module/foo/bar.psm1
New-Item -ItemType File -Path testdrive:/module/bar/foo.psm1
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/bar/foo.psm1 > $null
$testModulePath = "testdrive:/module/test.psd1"
$fileList = "foo\bar.psm1","bar/foo.psm1"

Expand All @@ -38,10 +42,8 @@ Describe "Test-ModuleManifest tests" -tags "CI" {

param ($parameter, $error)

New-Item -ItemType Directory -Path testdrive:/module
New-Item -ItemType Directory -Path testdrive:/module/foo
New-Item -ItemType File -Path testdrive:/module/foo/bar.psm1
$testModulePath = "testdrive:/module/test.psd1"
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
Expand All @@ -57,10 +59,7 @@ Describe "Test-ModuleManifest tests" -tags "CI" {

param($rootModuleValue)

New-Item -ItemType Directory -Path testdrive:/module
$testModulePath = "testdrive:/module/test.psd1"

New-Item -ItemType File -Path testdrive:/module/$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
Expand All @@ -74,10 +73,7 @@ Describe "Test-ModuleManifest tests" -tags "CI" {

param($rootModuleValue, $error)

New-Item -ItemType Directory -Path testdrive:/module
$testModulePath = "testdrive:/module/test.psd1"

New-Item -ItemType File -Path testdrive:/module/$rootModuleValue
New-Item -ItemType File -Path testdrive:/module/$rootModuleValue > $null
New-ModuleManifest -Path $testModulePath -RootModule $rootModuleValue
{ Test-ModuleManifest -Path $testModulePath -ErrorAction Stop } | Should -Throw -ErrorId "$error,Microsoft.PowerShell.Commands.TestModuleManifestCommand"
}
Expand All @@ -89,9 +85,6 @@ Describe "Test-ModuleManifest tests" -tags "CI" {

param($rootModuleValue)

New-Item -ItemType Directory -Path testdrive:/module
$testModulePath = "testdrive:/module/test.psd1"

New-ModuleManifest -Path $testModulePath -RootModule $rootModuleValue
$moduleManifest = Test-ModuleManifest -Path $testModulePath -ErrorAction Stop
$moduleManifest | Should -BeOfType System.Management.Automation.PSModuleInfo
Expand All @@ -104,9 +97,7 @@ Describe "Test-ModuleManifest tests" -tags "CI" {

param($rootModuleValue, $error)

$testModulePath = "testdrive:/module/test.psd1"
New-Item -ItemType Directory -Path testdrive:/module
New-Item -ItemType File -Path testdrive:/module/$rootModuleValue
New-Item -ItemType File -Path testdrive:/module/$rootModuleValue > $null

New-ModuleManifest -Path $testModulePath -RootModule $rootModuleValue
{ Test-ModuleManifest -Path $testModulePath -ErrorAction Stop } | Should -Throw -ErrorId "$error,Microsoft.PowerShell.Commands.TestModuleManifestCommand"
Expand All @@ -118,12 +109,23 @@ Describe "Test-ModuleManifest tests" -tags "CI" {

param($rootModuleValue, $error)

$testModulePath = "testdrive:/module/test.psd1"
New-Item -ItemType Directory -Path testdrive:/module

New-ModuleManifest -Path $testModulePath -RootModule $rootModuleValue
{ Test-ModuleManifest -Path $testModulePath -ErrorAction Stop } | Should -Throw -ErrorId "$error,Microsoft.PowerShell.Commands.TestModuleManifestCommand"
}

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"
}
}

Describe "Tests for circular references in required modules" -tags "CI" {
Expand Down Expand Up @@ -174,7 +176,7 @@ Describe "Tests for circular references in required modules" -tags "CI" {
function TestImportModule([bool]$AddVersion, [bool]$AddGuid, [bool]$AddCircularReference)
{
$moduleRootPath = Join-Path $TestDrive 'TestModules'
New-Item $moduleRootPath -ItemType Directory -Force
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
Expand Down
33 changes: 33 additions & 0 deletions test/powershell/engine/Module/UpdateModuleManifest.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

Describe "Update-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 "Update should not clear out NestedModules: <variation>" -TestCases @(
@{ variation = "export with wildcards"; exportValue = "*" },
@{ variation = "export without wildcards"; exportValue = "@()"}
) {
param($exportValue)

New-Item -ItemType File -Path testdrive:/module/foo.psm1 > $null
New-ModuleManifest -Path $testModulePath -NestedModules foo.psm1 -HelpInfoUri http://foo.com -AliasesToExport $exportValue -CmdletsToExport $exportValue -FunctionsToExport $exportValue -VariablesToExport $exportValue -DscResourcesToExport $exportValue
$module = Test-ModuleManifest -Path $testModulePath
$module.HelpInfoUri | Should -BeExactly "http://foo.com/"
$module.NestedModules | Should -HaveCount 1
$module.NestedModules.Name | Should -BeExactly foo
Update-ModuleManifest -Path $testModulePath -HelpInfoUri https://bar.org
$module = Test-ModuleManifest -Path $testModulePath
$module.HelpInfoUri | Should -BeExactly "https://bar.org/"
$module.NestedModules | Should -HaveCount 1
$module.NestedModules.Name | Should -BeExactly foo
}
}