Skip to content

Commit cc44781

Browse files
SteveL-MSFTdaxian-dbw
authored andcommitted
Ensure NestedModules property gets populated by Test-ModuleManifest (PowerShell#7859)
1 parent 8f4b66a commit cc44781

3 files changed

Lines changed: 89 additions & 36 deletions

File tree

src/System.Management.Automation/engine/Modules/ModuleCmdletBase.cs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,25 @@ internal bool LoadModuleManifestData(ExternalScriptInfo scriptInfo, ManifestProc
13381338
return true;
13391339
}
13401340

1341+
/// <summary>
1342+
/// Helper function to generate fake PSModuleInfo objects from ModuleSpecification objects.
1343+
/// </summary>
1344+
/// <param name="moduleSpecs">Collection of ModuleSpecification objects</param>
1345+
/// <returns>Collection of fake PSModuleInfo objects</returns>
1346+
private IEnumerable<PSModuleInfo> CreateFakeModuleObject(IEnumerable<ModuleSpecification> moduleSpecs)
1347+
{
1348+
foreach (ModuleSpecification moduleSpec in moduleSpecs)
1349+
{
1350+
var fakeModuleInfo = new PSModuleInfo(moduleSpec.Name, Context, null);
1351+
if (moduleSpec.Guid.HasValue)
1352+
{
1353+
fakeModuleInfo.SetGuid(moduleSpec.Guid.Value);
1354+
}
1355+
fakeModuleInfo.SetVersion(moduleSpec.RequiredVersion ?? moduleSpec.Version);
1356+
yield return fakeModuleInfo;
1357+
}
1358+
}
1359+
13411360
private ErrorRecord GetErrorRecordIfUnsupportedRootCdxmlAndNestedModuleScenario(
13421361
Hashtable data,
13431362
string moduleManifestPath,
@@ -1977,16 +1996,8 @@ internal PSModuleInfo LoadModuleManifest(
19771996
}
19781997
else
19791998
{
1980-
PSModuleInfo fakeRequiredModuleInfo = null;
1981-
foreach (ModuleSpecification requiredModule in requiredModules)
1999+
foreach (PSModuleInfo fakeRequiredModuleInfo in CreateFakeModuleObject(requiredModules))
19822000
{
1983-
fakeRequiredModuleInfo = new PSModuleInfo(requiredModule.Name, Context, null);
1984-
if (requiredModule.Guid.HasValue)
1985-
{
1986-
fakeRequiredModuleInfo.SetGuid(requiredModule.Guid.Value);
1987-
}
1988-
fakeRequiredModuleInfo.SetVersion(requiredModule.RequiredVersion ?? requiredModule.Version);
1989-
19902001
requiredModulesSpecifiedInModuleManifest.Add(fakeRequiredModuleInfo);
19912002
}
19922003
}
@@ -2797,6 +2808,12 @@ internal PSModuleInfo LoadModuleManifest(
27972808

27982809
if (!needToAnalyzeScriptModules)
27992810
{
2811+
// Add nested modules to the manifestInfo when no more analysis needs to be done
2812+
foreach (PSModuleInfo fakeNestedModuleInfo in CreateFakeModuleObject(nestedModules))
2813+
{
2814+
manifestInfo.AddNestedModule(fakeNestedModuleInfo);
2815+
}
2816+
28002817
return manifestInfo;
28012818
}
28022819
}
@@ -3428,6 +3445,7 @@ internal PSModuleInfo LoadModuleManifest(
34283445
updated.Add(element);
34293446
}
34303447
}
3448+
34313449
ss.Internal.ExportedVariables.Clear();
34323450
ss.Internal.ExportedVariables.AddRange(updated);
34333451
}

test/powershell/engine/Module/TestModuleManifest.Tests.ps1

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
# Licensed under the MIT License.
33
Describe "Test-ModuleManifest tests" -tags "CI" {
44

5+
BeforeEach {
6+
$testModulePath = "testdrive:/module/test.psd1"
7+
New-Item -ItemType Directory -Path testdrive:/module > $null
8+
}
9+
510
AfterEach {
611
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue testdrive:/module
712
}
813

914
It "module manifest containing paths with backslashes or forwardslashes are resolved correctly" {
1015

11-
New-Item -ItemType Directory -Path testdrive:/module
12-
New-Item -ItemType Directory -Path testdrive:/module/foo
13-
New-Item -ItemType Directory -Path testdrive:/module/bar
14-
New-Item -ItemType File -Path testdrive:/module/foo/bar.psm1
15-
New-Item -ItemType File -Path testdrive:/module/bar/foo.psm1
16+
New-Item -ItemType Directory -Path testdrive:/module/foo > $null
17+
New-Item -ItemType Directory -Path testdrive:/module/bar > $null
18+
New-Item -ItemType File -Path testdrive:/module/foo/bar.psm1 > $null
19+
New-Item -ItemType File -Path testdrive:/module/bar/foo.psm1 > $null
1620
$testModulePath = "testdrive:/module/test.psd1"
1721
$fileList = "foo\bar.psm1","bar/foo.psm1"
1822

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

3943
param ($parameter, $error)
4044

41-
New-Item -ItemType Directory -Path testdrive:/module
42-
New-Item -ItemType Directory -Path testdrive:/module/foo
43-
New-Item -ItemType File -Path testdrive:/module/foo/bar.psm1
44-
$testModulePath = "testdrive:/module/test.psd1"
45+
New-Item -ItemType Directory -Path testdrive:/module/foo > $null
46+
New-Item -ItemType File -Path testdrive:/module/foo/bar.psm1 > $null
4547

4648
$args = @{$parameter = "doesnotexist.psm1"}
4749
New-ModuleManifest -Path $testModulePath @args
@@ -57,10 +59,7 @@ Describe "Test-ModuleManifest tests" -tags "CI" {
5759

5860
param($rootModuleValue)
5961

60-
New-Item -ItemType Directory -Path testdrive:/module
61-
$testModulePath = "testdrive:/module/test.psd1"
62-
63-
New-Item -ItemType File -Path testdrive:/module/$rootModuleValue
62+
New-Item -ItemType File -Path testdrive:/module/$rootModuleValue > $null
6463
New-ModuleManifest -Path $testModulePath -RootModule $rootModuleValue
6564
$moduleManifest = Test-ModuleManifest -Path $testModulePath -ErrorAction Stop
6665
$moduleManifest | Should -BeOfType System.Management.Automation.PSModuleInfo
@@ -74,10 +73,7 @@ Describe "Test-ModuleManifest tests" -tags "CI" {
7473

7574
param($rootModuleValue, $error)
7675

77-
New-Item -ItemType Directory -Path testdrive:/module
78-
$testModulePath = "testdrive:/module/test.psd1"
79-
80-
New-Item -ItemType File -Path testdrive:/module/$rootModuleValue
76+
New-Item -ItemType File -Path testdrive:/module/$rootModuleValue > $null
8177
New-ModuleManifest -Path $testModulePath -RootModule $rootModuleValue
8278
{ Test-ModuleManifest -Path $testModulePath -ErrorAction Stop } | Should -Throw -ErrorId "$error,Microsoft.PowerShell.Commands.TestModuleManifestCommand"
8379
}
@@ -89,9 +85,6 @@ Describe "Test-ModuleManifest tests" -tags "CI" {
8985

9086
param($rootModuleValue)
9187

92-
New-Item -ItemType Directory -Path testdrive:/module
93-
$testModulePath = "testdrive:/module/test.psd1"
94-
9588
New-ModuleManifest -Path $testModulePath -RootModule $rootModuleValue
9689
$moduleManifest = Test-ModuleManifest -Path $testModulePath -ErrorAction Stop
9790
$moduleManifest | Should -BeOfType System.Management.Automation.PSModuleInfo
@@ -104,9 +97,7 @@ Describe "Test-ModuleManifest tests" -tags "CI" {
10497

10598
param($rootModuleValue, $error)
10699

107-
$testModulePath = "testdrive:/module/test.psd1"
108-
New-Item -ItemType Directory -Path testdrive:/module
109-
New-Item -ItemType File -Path testdrive:/module/$rootModuleValue
100+
New-Item -ItemType File -Path testdrive:/module/$rootModuleValue > $null
110101

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

119110
param($rootModuleValue, $error)
120111

121-
$testModulePath = "testdrive:/module/test.psd1"
122-
New-Item -ItemType Directory -Path testdrive:/module
123-
124112
New-ModuleManifest -Path $testModulePath -RootModule $rootModuleValue
125113
{ Test-ModuleManifest -Path $testModulePath -ErrorAction Stop } | Should -Throw -ErrorId "$error,Microsoft.PowerShell.Commands.TestModuleManifestCommand"
126114
}
115+
116+
It "module manifest containing nested module gets returned: <variation>" -TestCases (
117+
@{variation = "no analysis as all exported with no wildcard"; exportValue = "@()"},
118+
@{variation = "analysis as exported with wildcard"; exportValue = "*"}
119+
) {
120+
121+
param($exportValue)
122+
123+
New-Item -ItemType File -Path testdrive:/module/Foo.psm1 > $null
124+
New-ModuleManifest -Path $testModulePath -NestedModules "Foo.psm1" -FunctionsToExport $exportValue -CmdletsToExport $exportValue -VariablesToExport $exportValue -AliasesToExport $exportValue
125+
$module = Test-ModuleManifest -Path $testModulePath
126+
$module.NestedModules | Should -HaveCount 1
127+
$module.NestedModules.Name | Should -BeExactly "Foo"
128+
}
127129
}
128130

129131
Describe "Tests for circular references in required modules" -tags "CI" {
@@ -174,7 +176,7 @@ Describe "Tests for circular references in required modules" -tags "CI" {
174176
function TestImportModule([bool]$AddVersion, [bool]$AddGuid, [bool]$AddCircularReference)
175177
{
176178
$moduleRootPath = Join-Path $TestDrive 'TestModules'
177-
New-Item $moduleRootPath -ItemType Directory -Force
179+
New-Item $moduleRootPath -ItemType Directory -Force > $null
178180
Push-Location $moduleRootPath
179181

180182
$moduleCount = 6 # this depth was enough to find a bug in cyclic reference detection product code; greater depth will slow tests down
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
Describe "Update-ModuleManifest tests" -tags "CI" {
5+
6+
BeforeEach {
7+
$testModulePath = "testdrive:/module/test.psd1"
8+
New-Item -ItemType Directory -Path testdrive:/module > $null
9+
}
10+
11+
AfterEach {
12+
Remove-Item -Recurse -Force -ErrorAction SilentlyContinue testdrive:/module
13+
}
14+
15+
It "Update should not clear out NestedModules: <variation>" -TestCases @(
16+
@{ variation = "export with wildcards"; exportValue = "*" },
17+
@{ variation = "export without wildcards"; exportValue = "@()"}
18+
) {
19+
param($exportValue)
20+
21+
New-Item -ItemType File -Path testdrive:/module/foo.psm1 > $null
22+
New-ModuleManifest -Path $testModulePath -NestedModules foo.psm1 -HelpInfoUri http://foo.com -AliasesToExport $exportValue -CmdletsToExport $exportValue -FunctionsToExport $exportValue -VariablesToExport $exportValue -DscResourcesToExport $exportValue
23+
$module = Test-ModuleManifest -Path $testModulePath
24+
$module.HelpInfoUri | Should -BeExactly "http://foo.com/"
25+
$module.NestedModules | Should -HaveCount 1
26+
$module.NestedModules.Name | Should -BeExactly foo
27+
Update-ModuleManifest -Path $testModulePath -HelpInfoUri https://bar.org
28+
$module = Test-ModuleManifest -Path $testModulePath
29+
$module.HelpInfoUri | Should -BeExactly "https://bar.org/"
30+
$module.NestedModules | Should -HaveCount 1
31+
$module.NestedModules.Name | Should -BeExactly foo
32+
}
33+
}

0 commit comments

Comments
 (0)