Skip to content
Closed
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
422 changes: 144 additions & 278 deletions src/System.Management.Automation/engine/Modules/ModuleIntrinsics.cs

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/System.Management.Automation/engine/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ internal static bool IsValidPSEditionValue(string editionValue)

/// <summary>
/// The subdirectory of module paths
/// e.g. ~\Documents\WindowsPowerShell\Modules and %ProgramFiles%\WindowsPowerShell\Modules.
/// e.g. ~\Documents\PowerShell\Modules and %ProgramFiles%\PowerShell\Modules.
/// </summary>
internal static string ModuleDirectory = Path.Combine(ProductNameForDirectory, "Modules");

Expand Down Expand Up @@ -2065,6 +2065,7 @@ public static class InternalTestHooks
// A location to test PSEdition compatibility functionality for Windows PowerShell modules with
// since we can't manipulate the System32 directory in a test
internal static string TestWindowsPowerShellPSHomeLocation;
internal static object[] TestPowerShellPSModulePaths;

internal static bool ShowMarkdownOutputBypass;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1907,8 +1907,7 @@ private string GetRoleCapabilityPath(string roleCapability)
}

// Go through each directory in the module path
string[] modulePaths = ModuleIntrinsics.GetModulePath().Split(Utils.Separators.PathSeparator);
foreach (string path in modulePaths)
foreach (string path in ModuleIntrinsics.GetModulePath())
{
try
{
Expand Down
2 changes: 1 addition & 1 deletion test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Describe "TabCompletion" -Tags CI {
It 'Should complete abbreviated function' {
$res = (TabExpansion2 -inputScript 'pschrl' -cursorColumn 'pschr'.Length).CompletionMatches.CompletionText
$res.Count | Should -BeGreaterOrEqual 1
$res | Should -BeExactly 'PSConsoleHostReadLine'
$res[0] | Should -BeExactly 'PSConsoleHostReadLine'
}

It 'Should complete native exe' -Skip:(!$IsWindows) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ function New-EditionCompatibleModule
$psm1Name = "$ModuleName.psm1"
$psm1Path = Join-Path $modulePath $psm1Name

New-Item -Path $modulePath -ItemType Directory
New-Item -Path $modulePath -ItemType Directory > $null

New-Item -Path $psm1Path -Value "function Test-$ModuleName { `$true }"
New-Item -Path $psm1Path -Value "function Test-$ModuleName { `$true }" > $null

if ($CompatiblePSEditions)
{
Expand Down Expand Up @@ -86,7 +86,7 @@ function New-TestNestedModule
$nestedModules = [System.Collections.ArrayList]::new()

# Create script module
New-Item -Path (Join-Path $ModuleBase $ScriptModuleFileName) -Value $ScriptModuleContent
New-Item -Path (Join-Path $ModuleBase $ScriptModuleFileName) -Value $ScriptModuleContent > $null
$nestedModules.Add($ScriptModuleFilename)

if ($BinaryModuleFilename -and $BinaryModuleDllPath)
Expand All @@ -99,7 +99,7 @@ function New-TestNestedModule
# Create the root module if there is one
if ($UseRootModule)
{
New-Item -Path (Join-Path $ModuleBase $RootModuleFilename) -Value $RootModuleContent
New-Item -Path (Join-Path $ModuleBase $RootModuleFilename) -Value $RootModuleContent > $null
}

# Create the manifest command
Expand Down Expand Up @@ -412,7 +412,7 @@ Describe "Get-Module nested module behaviour with Edition checking" -Tag "Featur

foreach ($basePath in $compatiblePath,$incompatiblePath)
{
New-Item -Path $basePath -ItemType Directory
New-Item -Path $basePath -ItemType Directory > $null
}
}

Expand All @@ -432,11 +432,13 @@ Describe "Get-Module nested module behaviour with Edition checking" -Tag "Featur
$containingDir = Join-Path $TestDrive $compatibilityDir $guid
$moduleName = "CpseTestModule"
$moduleBase = Join-Path $containingDir $moduleName
New-Item -Path $moduleBase -ItemType Directory
New-Item -Path $moduleBase -ItemType Directory > $null
Add-ModulePath $containingDir
}

AfterEach {
Get-Module $moduleName | Remove-Module -Force
Remove-Item -Recurse -Path $moduleBase -Force
Restore-ModulePath
}

Expand Down Expand Up @@ -494,7 +496,11 @@ Describe "Get-Module nested module behaviour with Edition checking" -Tag "Featur
return
}

$modules.Count | Should -Be 1
# In $env:PSModulePath we have 2 paths to the module:
# - direct path to the module
# - "system" path to a directory on one level above
# so we will get the module path twice (2 instead of 1).
$modules.Count | Should -Be 2
$modules[0].Name | Should -Be $moduleName
}

Expand Down Expand Up @@ -538,13 +544,17 @@ Describe "Get-Module nested module behaviour with Edition checking" -Tag "Featur
Get-Module -ListAvailable -All | Where-Object { $_.Path.Contains($guid) }
}

# In $env:PSModulePath we have 2 paths to the module:
# - direct path to the module
# - "system" path to a directory on one level above
# so we will get the module path twice (8 instead of 4, 6 instead of 3).
if ($UseRootModule)
{
$modules.Count | Should -Be 4
$modules.Count | Should -Be 8
}
else
{
$modules.Count | Should -Be 3
$modules.Count | Should -Be 6
}

$names = $modules.Name
Expand All @@ -562,11 +572,13 @@ Describe "Get-Module nested module behaviour with Edition checking" -Tag "Featur
$containingDir = Join-Path $TestDrive $compatibilityDir $guid
$moduleName = "CpseTestModule"
$moduleBase = Join-Path $containingDir $moduleName
New-Item -Path $moduleBase -ItemType Directory
New-Item -Path $moduleBase -ItemType Directory > $null
Add-ModulePath $containingDir
}

AfterEach {
Get-Module $moduleName | Remove-Module -Force
Remove-Item -Recurse -Path $moduleBase -Force
Restore-ModulePath
}

Expand Down Expand Up @@ -708,7 +720,7 @@ Describe "Import-Module nested module behaviour with Edition checking" -Tag "Fea

foreach ($basePath in $compatiblePath,$incompatiblePath)
{
New-Item -Path $basePath -ItemType Directory
New-Item -Path $basePath -ItemType Directory > $null
}
}

Expand All @@ -728,12 +740,13 @@ Describe "Import-Module nested module behaviour with Edition checking" -Tag "Fea
$containingDir = Join-Path $TestDrive $compatibilityDir $guid
$moduleName = "CpseTestModule"
$moduleBase = Join-Path $containingDir $moduleName
New-Item -Path $moduleBase -ItemType Directory
New-Item -Path $moduleBase -ItemType Directory > $null
Add-ModulePath $containingDir
}

AfterEach {
Get-Module $moduleName | Remove-Module -Force
Remove-Item -Recurse -Path $moduleBase -Force
Restore-ModulePath
}

Expand Down Expand Up @@ -818,12 +831,13 @@ Describe "Import-Module nested module behaviour with Edition checking" -Tag "Fea
$containingDir = Join-Path $TestDrive $compatibilityDir $guid
$moduleName = "CpseTestModule"
$moduleBase = Join-Path $containingDir $moduleName
New-Item -Path $moduleBase -ItemType Directory
New-Item -Path $moduleBase -ItemType Directory > $null
Add-ModulePath $containingDir
}

AfterEach {
Get-Module $moduleName | Remove-Module -Force
Remove-Item -Recurse -Path $moduleBase -Force
Restore-ModulePath
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ Describe "Get-Module -ListAvailable" -Tags "CI" {
}
)

$env:PSModulePath = Join-Path $testdrive "Modules"
$testModulePath = Join-Path $testdrive "Modules"
$env:PSModulePath = $testModulePath + [System.IO.Path]::PathSeparator + $env:PSModulePath
}

AfterAll {
Expand All @@ -71,8 +72,8 @@ Describe "Get-Module -ListAvailable" -Tags "CI" {

It "Get-Module -ListAvailable" {
$modules = Get-Module -ListAvailable
$modules = $modules | Where-Object { $_.Path.StartsWith($testModulePath) } | Sort-Object -Property Name, Version
$modules.Count | Should -Be 5
$modules = $modules | Sort-Object -Property Name, Version
$modules.Name -join "," | Should -BeExactly "Az,Bar,Foo,Foo,Zoo"
$modules[0].Version | Should -Be "1.1"
$modules[1].Version | Should -Be "0.0.1"
Expand All @@ -82,6 +83,7 @@ Describe "Get-Module -ListAvailable" -Tags "CI" {

It "Get-Module <Name> -ListAvailable" {
$modules = Get-Module F* -ListAvailable
$modules = $modules | Where-Object { $_.Path.StartsWith($testModulePath) } | Sort-Object -Property Name, Version
$modules.Count | Should -Be 2
$modules = $modules | Sort-Object -Property Version
$modules.Name -join "," | Should -BeExactly "Foo,Foo"
Expand All @@ -91,6 +93,8 @@ Describe "Get-Module -ListAvailable" -Tags "CI" {

It "Get-Module -ListAvailable -All" {
$modules = Get-Module -ListAvailable -All
$modules = $modules | Where-Object { $_.Path.StartsWith($testModulePath) } | Sort-Object -Property Name, Version
$modules = $modules | Where-Object { $_.Path.StartsWith($testModulePath) } | Sort-Object -Property Name, Version
$modules.Count | Should -Be 12
$modules = $modules | Sort-Object -Property Name, Path
$modules.Name -join "," | Should -BeExactly "Az,Az,Bar,Bar,Download,Foo,Foo,Foo,Foo,Zoo,Zoo,Zoo"
Expand All @@ -116,6 +120,7 @@ Describe "Get-Module -ListAvailable" -Tags "CI" {

It "Get-Module <Name> -ListAvailable -All" {
$modules = Get-Module down*, zoo -ListAvailable -All
$modules = $modules | Where-Object { $_.Path.StartsWith($testModulePath) } | Sort-Object -Property Name, Version
$modules.Count | Should -Be 4
$modules = $modules | Sort-Object -Property Name, Path
$modules.Name -join "," | Should -BeExactly "Download,Zoo,Zoo,Zoo"
Expand All @@ -128,6 +133,7 @@ Describe "Get-Module -ListAvailable" -Tags "CI" {

It "Get-Module <Path> -ListAvailable" {
$modules = Get-Module "$testdrive\Modules\*" -ListAvailable
$modules = $modules | Where-Object { $_.Path.StartsWith($testModulePath) } | Sort-Object -Property Name, Version
$modules.Count | Should -Be 5
$modules = $modules | Sort-Object -Property Name, Version
$modules.Name -join "," | Should -BeExactly "Az,Bar,Foo,Foo,Zoo"
Expand All @@ -137,6 +143,7 @@ Describe "Get-Module -ListAvailable" -Tags "CI" {

It "Get-Module <Path> -ListAvailable -All" {
$modules = Get-Module "$testdrive\Modules\*" -ListAvailable -All
$modules = $modules | Where-Object { $_.Path.StartsWith($testModulePath) } | Sort-Object -Property Name, Version
$modules.Count | Should -Be 6
$modules = $modules | Sort-Object -Property Name, Path
$modules.Name -join "," | Should -BeExactly "Az,Bar,Foo,Foo,Zoo,Zoo"
Expand All @@ -155,6 +162,7 @@ Describe "Get-Module -ListAvailable" -Tags "CI" {

$moduleSpecification = @{ModuleName = $name ; ModuleVersion = $ModuleVersion}
$modules = Get-Module -FullyQualifiedName $moduleSpecification -ListAvailable
$modules = $modules | Where-Object { $_.Path.StartsWith($testModulePath) } | Sort-Object -Property Name, Version
$modules | Should -HaveCount 1
$modules.Name | Should -BeExactly $ExpectedName
$modules.Version | Should -BeExactly $ModuleVersion
Expand All @@ -179,6 +187,7 @@ Describe "Get-Module -ListAvailable" -Tags "CI" {

It "Get-Module <Name> -Refresh -ListAvailable" {
$modules = Get-Module -Name 'Zoo' -ListAvailable
$modules = $modules | Where-Object { $_.Path.StartsWith($testModulePath) } | Sort-Object -Property Name, Version
$modules | Should -HaveCount 1
$modules.Name | Should -BeExactly "Zoo"
$modules.ExportedFunctions.Count | Should -Be 0 -Because 'No exports were defined'
Expand All @@ -200,6 +209,7 @@ Describe "Get-Module -ListAvailable" -Tags "CI" {
}

$modules = Get-Module -ListAvailable -FullyQualifiedName $modSpec
$modules = $modules | Where-Object { $_.Path.StartsWith($testModulePath) } | Sort-Object -Property Name, Version
$modules | Should -HaveCount $Count
$modules[0].Name | Should -BeExactly $Name
$modules.Version | Should -Contain $Version
Expand Down Expand Up @@ -227,6 +237,7 @@ Describe "Get-Module -ListAvailable" -Tags "CI" {
) {
param ($CompatiblePSEditions, $ExpectedModule)
$modules = Get-Module -PSEdition $CompatiblePSEditions -ListAvailable
$modules = $modules | Where-Object { $_.Path.StartsWith($testModulePath) } | Sort-Object -Property Name, Version
$modules | Should -HaveCount $ExpectedModule.Count
$modules.Name | Sort-Object | Should -BeExactly $ExpectedModule
}
Expand Down Expand Up @@ -274,7 +285,7 @@ Describe "Get-Module -ListAvailable" -Tags "CI" {

It "'Get-Module -ListAvailable' should not load the module assembly" {
## $fullName should be null and thus the result should just be the module's name.
$result = pwsh -noprofile -c "`$env:PSModulePath = '$tempModulePath'; `$module = Get-Module -ListAvailable; `$fullName = [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object Location -eq $assemblyPath | Foreach-Object FullName; `$module.Name + `$fullName"
$result = pwsh -noprofile -c "`$env:PSModulePath = '$tempModulePath'; `$module = Get-Module -ListAvailable | Where-Object { `$_.Path.StartsWith('$tempModulePath') }; `$fullName = [System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object Location -eq $assemblyPath | Foreach-Object FullName; `$module.Name + `$fullName"
$result | Should -BeExactly "MyModuelTest"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Describe "Import-Module" -Tags "CI" {
$moduleName = "Microsoft.PowerShell.Security"
BeforeAll {
$originalPSModulePath = $env:PSModulePath

# Remove system paths because PS 6 modify $env:PSModulePath
$env:PSModulePath = ""
New-Item -ItemType Directory -Path "$testdrive\Modules\TestModule\1.1" -Force > $null
New-Item -ItemType Directory -Path "$testdrive\Modules\TestModule\2.0" -Force > $null
$env:PSModulePath += [System.IO.Path]::PathSeparator + "$testdrive\Modules"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,18 @@ Describe "Remove-Module : module contains nested modules" -Tags "CI" {
}

Describe "Remove-Module core module on module path by name" -Tags "CI" {
$moduleName = "Microsoft.PowerShell.Security"
BeforeAll {
$originalPSModulePath = $env:PSModulePath

# Remove system paths because PS 6 modify $env:PSModulePath
$env:PSModulePath = ""
$moduleName = "Microsoft.PowerShell.Security"
Remove-Module -Name $moduleName -Force -ErrorAction SilentlyContinue
}

AfterAll {
$env:PSModulePath = $originalPSModulePath
}

BeforeEach {
Import-Module -Name $moduleName -Force
Expand All @@ -413,10 +424,6 @@ Describe "Remove-Module core module on module path by name" -Tags "CI" {
{ Remove-Module -ModuleInfo $a } | Should -Not -Throw
(Get-Module -Name $moduleName).Name | Should -BeNullOrEmpty
}

AfterEach {
Import-Module -Name $moduleName -Force
}
}

Describe "Remove-Module custom module with FullyQualifiedName" -Tags "Feature" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Describe "DSC MOF Compilation" -tags "CI" {

BeforeAll {
$IsAlpine = (Get-PlatformInfo) -eq "alpine"
$env:PSModulePath = ""
Import-Module PSDesiredStateConfiguration
$dscModule = Get-Module PSDesiredStateConfiguration
$baseSchemaPath = Join-Path $dscModule.ModuleBase 'Configuration'
Expand Down
Loading