Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Get-Module.Tests.ps1380 linesDownload Raw Back to Microsoft.PowerShell.Core
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Describe "Get-Module -ListAvailable" -Tags "CI" {5 6    BeforeAll {7        $originalPSModulePath = $env:PSModulePath8 9        New-Item -ItemType Directory -Path "$testdrive\Modules\Foo\1.1" -Force > $null10        New-Item -ItemType Directory -Path "$testdrive\Modules\Foo\2.0" -Force > $null11        New-Item -ItemType Directory -Path "$testdrive\Modules\Bar\Download" -Force > $null12        New-Item -ItemType Directory -Path "$testdrive\Modules\Zoo\Too" -Force > $null13        New-Item -ItemType Directory -Path "$testdrive\Modules\Az" -Force > $null14 15        New-ModuleManifest -Path "$testdrive\Modules\Foo\1.1\Foo.psd1" -ModuleVersion 1.116        New-ModuleManifest -Path "$testdrive\Modules\Foo\2.0\Foo.psd1" -ModuleVersion 2.017        New-ModuleManifest -Path "$testdrive\Modules\Bar\Bar.psd1"18        New-ModuleManifest -Path "$testdrive\Modules\Zoo\Zoo.psd1"19        New-ModuleManifest -Path "$testdrive\Modules\Az\Az.psd1" -ModuleVersion 1.120 21        New-Item -ItemType File -Path "$testdrive\Modules\Foo\1.1\Foo.psm1" > $null22        New-Item -ItemType File -Path "$testdrive\Modules\Foo\2.0\Foo.psm1" > $null23        New-Item -ItemType File -Path "$testdrive\Modules\Bar\Bar.psm1" > $null24        New-Item -ItemType File -Path "$testdrive\Modules\Bar\Download\Download.psm1" > $null25        New-Item -ItemType File -Path "$testdrive\Modules\Zoo\Zoo.psm1" > $null26        New-Item -ItemType File -Path "$testdrive\Modules\Zoo\Too\Zoo.psm1" > $null27        New-Item -ItemType File -Path "$testdrive\Modules\Az\Az.psm1" > $null28 29        $fullyQualifiedPathTestCases = @(30            @{ ModPath = "$TestDrive/Modules\Foo"; Name = 'Foo'; Version = '2.0'; Count = 1 }31            @{ ModPath = "$TestDrive\Modules/Foo\1.1/Foo.psd1"; Name = 'Foo'; Version = '1.1'; Count = 1 }32            @{ ModPath = "$TestDrive\Modules/Bar.psd1"; Name = 'Bar'; Version = '0.0'; Count = 1 }33            @{ ModPath = "$TestDrive\Modules\Zoo\Too\Zoo.psm1"; Name = 'Zoo'; Version = '0.0'; Count = 1 }34        )35 36        $listModuleNameTestCases = @(37            @{38                Name = 'Foo'39                TestCaseName = 'Match case'40                ExpectedName = 'Foo'41                ModuleVersion = '2.0'42            }43            @{44                Name = 'foo'45                TestCaseName = 'Mismatched case'46                ExpectedName = 'Foo'47                ModuleVersion = '2.0'48            }49        )50        $loadedModuleNameTestCases = @(51            @{52                Name = 'Microsoft.PowerShell.Managemen*'53                TestCaseName = 'Wildcard'54                ExpectedName = 'Microsoft.PowerShell.Management'55                ModuleVersion = '7.0.0.0'56            }57            @{58                Name = 'microsoft.powershell.managemen*'59                TestCaseName = 'Mismatched case'60                ExpectedName = 'Microsoft.PowerShell.Management'61                ModuleVersion = '7.0.0.0'62            }63        )64 65        $env:PSModulePath = Join-Path $testdrive "Modules"66    }67 68    AfterAll {69        $env:PSModulePath = $originalPSModulePath70    }71 72    It "Get-Module -ListAvailable" {73        $modules = Get-Module -ListAvailable74        $modules.Count | Should -Be 575        $modules = $modules | Sort-Object -Property Name, Version76        $modules.Name -join "," | Should -BeExactly "Az,Bar,Foo,Foo,Zoo"77        $modules[0].Version | Should -Be "1.1"78        $modules[1].Version | Should -Be "0.0.1"79        $modules[2].Version | Should -Be '1.1'80        $modules[3].Version | Should -Be '2.0'81    }82 83    It "Get-Module <Name> -ListAvailable" {84        $modules = Get-Module F* -ListAvailable85        $modules.Count | Should -Be 286        $modules = $modules | Sort-Object -Property Version87        $modules.Name -join "," | Should -BeExactly "Foo,Foo"88        $modules[0].Version | Should -Be "1.1"89        $modules[1].Version | Should -Be "2.0"90    }91 92    It "Get-Module -ListAvailable -All" {93        $modules = Get-Module -ListAvailable -All94        $modules.Count | Should -Be 1295        $modules = $modules | Sort-Object -Property Name, Path96        $modules.Name -join "," | Should -BeExactly "Az,Az,Bar,Bar,Download,Foo,Foo,Foo,Foo,Zoo,Zoo,Zoo"97 98        $modules[0].ModuleType | Should -BeExactly "Manifest"99        $modules[1].ModuleType | Should -BeExactly "Script"100        $modules[2].ModuleType | Should -BeExactly "Manifest"101        $modules[3].ModuleType | Should -BeExactly "Script"102        $modules[4].ModuleType | Should -BeExactly "Script"103        $modules[5].ModuleType | Should -BeExactly "Manifest"104        $modules[5].Version | Should -Be "1.1"105        $modules[6].ModuleType | Should -BeExactly "Script"106        $modules[7].ModuleType | Should -BeExactly "Manifest"107        $modules[7].Version | Should -Be "2.0"108        $modules[8].ModuleType | Should -BeExactly "Script"109        $modules[9].ModuleType | Should -BeExactly "Script"110        $modules[9].Path | Should -BeExactly (Resolve-Path "$testdrive\Modules\Zoo\Too\Zoo.psm1").Path111        $modules[10].ModuleType | Should -BeExactly "Manifest"112        $modules[10].Path | Should -BeExactly (Resolve-Path "$testdrive\Modules\Zoo\Zoo.psd1").Path113        $modules[11].ModuleType | Should -BeExactly "Script"114        $modules[11].Path | Should -BeExactly (Resolve-Path "$testdrive\Modules\Zoo\Zoo.psm1").Path115    }116 117    It "Get-Module <Name> -ListAvailable -All" {118        $modules = Get-Module down*, zoo -ListAvailable -All119        $modules.Count | Should -Be 4120        $modules = $modules | Sort-Object -Property Name, Path121        $modules.Name -join "," | Should -BeExactly "Download,Zoo,Zoo,Zoo"122 123        $modules[0].Path | Should -BeExactly (Resolve-Path "$testdrive\Modules\Bar\Download\Download.psm1").Path124        $modules[1].Path | Should -BeExactly (Resolve-Path "$testdrive\Modules\Zoo\Too\Zoo.psm1").Path125        $modules[2].Path | Should -BeExactly (Resolve-Path "$testdrive\Modules\Zoo\Zoo.psd1").Path126        $modules[3].Path | Should -BeExactly (Resolve-Path "$testdrive\Modules\Zoo\Zoo.psm1").Path127    }128 129    It "Get-Module <Path> -ListAvailable" {130        $modules = Get-Module "$testdrive\Modules\*" -ListAvailable131        $modules.Count | Should -Be 5132        $modules = $modules | Sort-Object -Property Name, Version133        $modules.Name -join "," | Should -BeExactly "Az,Bar,Foo,Foo,Zoo"134        $modules[2].Version | Should -Be "1.1"135        $modules[3].Version | Should -Be '2.0'136    }137 138    It "Get-Module <Path> -ListAvailable -All" {139        $modules = Get-Module "$testdrive\Modules\*" -ListAvailable -All140        $modules.Count | Should -Be 6141        $modules = $modules | Sort-Object -Property Name, Path142        $modules.Name -join "," | Should -BeExactly "Az,Bar,Foo,Foo,Zoo,Zoo"143        $modules[4].Path | Should -BeExactly (Resolve-Path "$testdrive\Modules\Zoo\Too\Zoo.psm1").Path144    }145 146    It "Get-Module -FullyQualifiedName @{ModuleName = '<Name>' ; ModuleVersion = '<ModuleVersion>'} -ListAvailable - <TestCaseName>" -TestCases $listModuleNameTestCases {147        param(148            [Parameter(Mandatory = $true)]149            $Name,150            $TestCaseName,151            [Parameter(Mandatory = $true)]152            $ExpectedName,153            $ModuleVersion154        )155 156        $moduleSpecification  = @{ModuleName = $name ; ModuleVersion = $ModuleVersion}157        $modules = Get-Module -FullyQualifiedName $moduleSpecification -ListAvailable158        $modules | Should -HaveCount 1159        $modules.Name | Should -BeExactly $ExpectedName160        $modules.Version | Should -BeExactly $ModuleVersion161    }162 163    It "Get-Module -FullyQualifiedName @{ModuleName = '<Name>' ; ModuleVersion = '<ModuleVersion>'} - <TestCaseName>" -TestCases $loadedModuleNameTestCases {164        param(165            [Parameter(Mandatory = $true)]166            $Name,167            $TestCaseName,168            [Parameter(Mandatory = $true)]169            $ExpectedName,170            $ModuleVersion171        )172 173        $moduleSpecification  = @{ModuleName = $name ; ModuleVersion = $ModuleVersion}174        $modules = Get-Module -FullyQualifiedName $moduleSpecification175        $modules | Should -HaveCount 1176        $modules.Name | Should -BeExactly $ExpectedName177        $modules.Version | Should -BeExactly $ModuleVersion178    }179 180    It "Get-Module <Name> -Refresh -ListAvailable" {181        $modules = Get-Module -Name 'Zoo' -ListAvailable182        $modules | Should -HaveCount 1183        $modules.Name | Should -BeExactly "Zoo"184        $modules.ExportedFunctions.Count | Should -Be 0 -Because 'No exports were defined'185 186        New-ModuleManifest -Path "$testdrive\Modules\Zoo\Zoo.psd1" -FunctionsToExport 'Test-ZooFunction'187 188        $modules = Get-Module -Name 'Zoo' -ListAvailable -Refresh189        $modules | Should -HaveCount 1190        $modules.Name | Should -BeExactly "Zoo"191        $modules.ExportedFunctions.Count | Should -Be 1 -Because 'We added a new function to export'192    }193 194    It "Get-Module respects absolute paths in module specifications: <ModPath>" -TestCases $fullyQualifiedPathTestCases {195        param([string]$ModPath, [string]$Name, [string]$Version, [int]$Count)196 197        $modSpec = @{198            ModuleName = $ModPath199            RequiredVersion = $Version200        }201 202        $modules = Get-Module -ListAvailable -FullyQualifiedName $modSpec203        $modules | Should -HaveCount $Count204        $modules[0].Name | Should -BeExactly $Name205        $modules.Version | Should -Contain $Version206    }207 208    Context "PSEdition" {209 210        BeforeAll {211            New-Item -ItemType Directory -Path "$testdrive\Modules\DesktopOnlyModule" -Force > $null212            New-Item -ItemType Directory -Path "$testdrive\Modules\CoreOnlyModule" -Force > $null213            New-Item -ItemType Directory -Path "$testdrive\Modules\CoreAndDesktopModule" -Force > $null214 215            New-ModuleManifest -Path "$testdrive\Modules\DesktopOnlyModule\DesktopOnlyModule.psd1" -CompatiblePSEditions Desktop216            New-ModuleManifest -Path "$testdrive\Modules\CoreOnlyModule\CoreOnlyModule.psd1" -CompatiblePSEditions Core217            New-ModuleManifest -Path "$testdrive\Modules\CoreAndDesktopModule\CoreAndDesktopModule.psd1" -CompatiblePSEditions Core, Desktop218 219            New-Item -ItemType File -Path "$testdrive\Modules\DesktopOnlyModule\DesktopOnlyModule.psm1" > $null220            New-Item -ItemType File -Path "$testdrive\Modules\CoreOnlyModule\CoreOnlyModule.psm1" > $null221            New-Item -ItemType File -Path "$testdrive\Modules\CoreAndDesktopModule\CoreAndDesktopModule.psm1" > $null222        }223 224        It "Get-Module -PSEdition <CompatiblePSEditions> -ListAvailable" -TestCases @(225            @{ CompatiblePSEditions = 'Desktop'; ExpectedModule = 'CoreAndDesktopModule', 'DesktopOnlyModule' },226            @{ CompatiblePSEditions = 'Core'   ; ExpectedModule = 'CoreAndDesktopModule', 'CoreOnlyModule' }227        ) {228            param ($CompatiblePSEditions, $ExpectedModule)229            $modules = Get-Module -PSEdition $CompatiblePSEditions -ListAvailable230            $modules | Should -HaveCount $ExpectedModule.Count231            $modules.Name | Sort-Object | Should -BeExactly $ExpectedModule232        }233    }234 235    Context "Module analysis shouldn't load assembly" {236        BeforeAll {237            $tempModulePath = Join-Path $TestDrive "TempModules"238            $testModuleDir = Join-Path $tempModulePath "MyModuelTest"239            $moduleManifest = Join-Path $testModuleDir "MyModuelTest.psd1"240            $assemblyPath = Join-Path $testModuleDir "MyModuelTestCommandAssembly.dll"241 242            $null = New-Item $testModuleDir -ItemType Directory -ErrorAction SilentlyContinue243            if (-not (Test-Path $moduleManifest))244            {245                Set-Content $moduleManifest -Value @'246    @{247        RootModule = 'MyModuelTestCommandAssembly.dll'248        ModuleVersion = '0.0.1'249        GUID = '5776ed43-1607-4e64-be76-acacdf8e9c8c'250        FunctionsToExport = @()251        CmdletsToExport = @("Get-Test")252        AliasesToExport = @()253    }254'@255            }256 257            $code = @'258    using System.Management.Automation;259 260    [Cmdlet("Get", "Test")]261    public class MyModuelTestCommand : PSCmdlet262    {263        protected override void ProcessRecord()264        {265            WriteObject("BLAH");266        }267    }268'@269            if (-not (Test-Path $assemblyPath))270            {271                Add-Type -TypeDefinition $code -OutputAssembly $assemblyPath272            }273        }274 275        It "'Get-Module -ListAvailable' should not load the module assembly" {276            ## $fullName should be null and thus the result should just be the module's name.277            $result = & "$PSHOME/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"278            $result | Should -BeExactly "MyModuelTest"279        }280    }281}282 283Describe 'Get-Module -ListAvailable with path' -Tags "CI" {284    BeforeAll {285        $moduleName = 'Banana'286        $modulePath = Join-Path $TestDrive $moduleName287        $v1 = '1.2.3'288        $v2 = '4.8.3'289        $v1DirPath = Join-Path $modulePath $v1290        $v2DirPath = Join-Path $modulePath $v2291        $manifestV1Path = Join-Path $v1DirPath "$moduleName.psd1"292        $manifestV2Path = Join-Path $v2DirPath "$moduleName.psd1"293 294        New-Item -ItemType Directory $modulePath295        New-Item -ItemType Directory -Path $v1DirPath296        New-Item -ItemType Directory -Path $v2DirPath297        New-ModuleManifest -Path $manifestV1Path -ModuleVersion $v1298        New-ModuleManifest -Path $manifestV2Path -ModuleVersion $v2299    }300 301    It "Gets all versions by path" {302        $modules = Get-Module -ListAvailable $modulePath | Sort-Object -Property Version303 304        $modules | Should -HaveCount 2305        $modules[0].Name | Should -BeExactly $moduleName306        $modules[0].Path | Should -BeExactly $manifestV1Path307        $modules[0].Version | Should -Be $v1308        $modules[1].Name | Should -BeExactly $moduleName309        $modules[1].Path | Should -BeExactly $manifestV2Path310        $modules[1].Version | Should -Be $v2311    }312 313    It "Gets all versions by FullyQualifiedName with path with lower version" {314        $modules = Get-Module -ListAvailable -FullyQualifiedName @{ ModuleName = $modulePath; ModuleVersion = '0.0' } | Sort-Object -Property Version315 316        $modules | Should -HaveCount 2317        $modules[0].Name | Should -BeExactly $moduleName318        $modules[0].Path | Should -BeExactly $manifestV1Path319        $modules[0].Version | Should -Be $v1320        $modules[1].Name | Should -BeExactly $moduleName321        $modules[1].Path | Should -BeExactly $manifestV2Path322        $modules[1].Version | Should -Be $v2323    }324 325    It "Gets high version by FullyQualifiedName with path with high version" {326        $modules = Get-Module -ListAvailable -FullyQualifiedName @{ ModuleName = $modulePath; ModuleVersion = '2.0' } | Sort-Object -Property Version327 328        $modules | Should -HaveCount 1329        $modules[0].Name | Should -BeExactly $moduleName330        $modules[0].Path | Should -BeExactly $manifestV2Path331        $modules[0].Version | Should -Be $v2332    }333 334    It "Gets low version by FullyQualifiedName with path with low maximum version" {335        $modules = Get-Module -ListAvailable -FullyQualifiedName @{ ModuleName = $modulePath; MaximumVersion = '2.0' } | Sort-Object -Property Version336 337        $modules | Should -HaveCount 1338        $modules[0].Name | Should -BeExactly $moduleName339        $modules[0].Path | Should -BeExactly $manifestV1Path340        $modules[0].Version | Should -Be $v1341    }342 343    It "Gets low version by FullyQualifiedName with path with low maximum version and version" {344        $modules = Get-Module -ListAvailable -FullyQualifiedName @{ ModuleName = $modulePath; MaximumVersion = '2.0'; ModuleVersion = '1.0' } | Sort-Object -Property Version345 346        $modules | Should -HaveCount 1347        $modules[0].Name | Should -BeExactly $moduleName348        $modules[0].Path | Should -BeExactly $manifestV1Path349        $modules[0].Version | Should -Be $v1350    }351 352    It "Gets correct version by FullyQualifiedName with path with required version" -TestCases @(353        @{ Version = $v1 }354        @{ Version = $v2 }355    ) {356        param([version]$Version)357 358        switch ($Version)359        {360            $v1361            {362                $expectedPath = $manifestV1Path363                break364            }365 366            $v2367            {368                $expectedPath = $manifestV2Path369            }370        }371 372        $modules = Get-Module -ListAvailable -FullyQualifiedName @{ ModuleName = $modulePath; RequiredVersion = $Version }373 374        $modules | Should -HaveCount 1375        $modules[0].Name | Should -BeExactly $moduleName376        $modules[0].Path | Should -BeExactly $expectedPath377        $modules[0].Version | Should -Be $Version378    }379}380