MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4# no progress output during these tests5$ProgressPreference = "SilentlyContinue"6 7$RepositoryName = 'PSGallery'8$SourceLocation = 'https://www.powershellgallery.com'9$TestModule = 'newTestModule'10$TestScript = 'TestTestScript'11$Initialized = $false12 13#region Utility functions14 15function IsInbox { $PSHOME.EndsWith('\WindowsPowerShell\v1.0', [System.StringComparison]::OrdinalIgnoreCase) }16function IsWindows { $PSVariable = Get-Variable -Name IsWindows -ErrorAction Ignore; return (-not $PSVariable -or $PSVariable.Value) }17function IsCoreCLR { $PSVersionTable.ContainsKey('PSEdition') -and $PSVersionTable.PSEdition -eq 'Core' }18 19#endregion20 21#region Install locations for modules and scripts22 23if(IsInbox)24{25 $script:ProgramFilesPSPath = Microsoft.PowerShell.Management\Join-Path -Path $env:ProgramFiles -ChildPath "WindowsPowerShell"26}27elseif(IsCoreCLR) {28 if(IsWindows) {29 $script:ProgramFilesPSPath = Microsoft.PowerShell.Management\Join-Path -Path $env:ProgramFiles -ChildPath 'PowerShell'30 }31 else {32 $script:ProgramFilesPSPath = Split-Path -Path ([System.Management.Automation.Platform]::SelectProductNameForDirectory('SHARED_MODULES')) -Parent33 }34}35 36try37{38 $script:MyDocumentsFolderPath = [Environment]::GetFolderPath("MyDocuments")39}40catch41{42 $script:MyDocumentsFolderPath = $null43}44 45if(IsInbox)46{47 $script:MyDocumentsPSPath = if($script:MyDocumentsFolderPath)48 {49 Microsoft.PowerShell.Management\Join-Path -Path $script:MyDocumentsFolderPath -ChildPath "WindowsPowerShell"50 }51 else52 {53 Microsoft.PowerShell.Management\Join-Path -Path $env:USERPROFILE -ChildPath "Documents\WindowsPowerShell"54 }55}56elseif(IsCoreCLR) {57 if(IsWindows)58 {59 $script:MyDocumentsPSPath = if($script:MyDocumentsFolderPath)60 {61 Microsoft.PowerShell.Management\Join-Path -Path $script:MyDocumentsFolderPath -ChildPath 'PowerShell'62 }63 else64 {65 Microsoft.PowerShell.Management\Join-Path -Path $HOME -ChildPath "Documents\PowerShell"66 }67 }68 else69 {70 $script:MyDocumentsPSPath = Split-Path -Path ([System.Management.Automation.Platform]::SelectProductNameForDirectory('USER_MODULES')) -Parent71 }72}73 74$script:ProgramFilesModulesPath = Microsoft.PowerShell.Management\Join-Path -Path $script:ProgramFilesPSPath -ChildPath 'Modules'75$script:MyDocumentsModulesPath = Microsoft.PowerShell.Management\Join-Path -Path $script:MyDocumentsPSPath -ChildPath 'Modules'76 77$script:ProgramFilesScriptsPath = Microsoft.PowerShell.Management\Join-Path -Path $script:ProgramFilesPSPath -ChildPath 'Scripts'78$script:MyDocumentsScriptsPath = Microsoft.PowerShell.Management\Join-Path -Path $script:MyDocumentsPSPath -ChildPath 'Scripts'79 80#endregion81 82#region Register a test repository83 84function Initialize85{86 # Cleaned up commands whose output to console by deleting or piping to Out-Null87 Import-Module PackageManagement88 Get-PackageProvider -ListAvailable | Out-Null89 90 $repo = Get-PSRepository -ErrorAction SilentlyContinue |91 Where-Object {$_.SourceLocation.StartsWith($SourceLocation, [System.StringComparison]::OrdinalIgnoreCase)}92 if($repo)93 {94 $script:RepositoryName = $repo.Name95 Set-PackageSource -Name $repo.Name -Trusted96 }97 else98 {99 Register-PSRepository -Name $RepositoryName -SourceLocation $SourceLocation -InstallationPolicy Trusted100 }101}102 103#endregion104 105function Remove-InstalledModules106{107 try {108 $mod = Get-InstalledModule -Name $TestModule -AllVersions -ErrorAction SilentlyContinue109 if ($null -eq $mod) {110 return111 }112 113 if (Get-Module -Name $TestModule -ErrorAction Ignore) {114 Remove-Module -Force -Name $TestModule115 }116 117 $installedPath = $mod.InstalledLocation118 if (Test-Path $installedPath) {119 Remove-Item -Force -Recurse $installedPath -ErrorAction Ignore120 }121 }122 catch {123 Write-Warning "Remove-InstalledModules: $_"124 }125}126 127Describe "PowerShellGet - Module tests" -tags "Feature" {128 129 BeforeAll {130 if ($script:Initialized -eq $false) {131 Initialize132 $script:Initialized = $true133 }134 }135 136 BeforeEach {137 Remove-InstalledModules138 }139 140 It "Should find a module correctly" {141 $psgetModuleInfo = Find-Module -Name $TestModule -Repository $RepositoryName142 $psgetModuleInfo.Name | Should -Be $TestModule143 $psgetModuleInfo.Repository | Should -Be $RepositoryName144 }145 146 It "Should install a module correctly to the required location with default CurrentUser scope" {147 Install-Module -Name $TestModule -Repository $RepositoryName148 $module = Get-Module -Name $TestModule -ListAvailable149 $module | Should -Not -BeNullOrEmpty150 $module.Name | Should -Be $TestModule151 $module.ModuleBase.StartsWith($script:MyDocumentsModulesPath, [System.StringComparison]::OrdinalIgnoreCase) | Should -BeTrue152 }153 154 AfterAll {155 Remove-InstalledModules156 }157}158 159Describe "PowerShellGet - Module tests (Admin)" -Tags @('Feature', 'RequireAdminOnWindows', 'RequireSudoOnUnix') {160 161 BeforeAll {162 if ($script:Initialized -eq $false) {163 Initialize164 $script:Initialized = $true165 }166 }167 168 BeforeEach {169 Remove-InstalledModules170 }171 172 It "Should install a module correctly to the required location with AllUsers scope" {173 Install-Module -Name $TestModule -Repository $RepositoryName -Scope AllUsers -ErrorAction Stop174 175 $module = Get-Module $TestModule -ListAvailable176 $module.Name | Should -Be $TestModule177 $module.ModuleBase.StartsWith($script:programFilesModulesPath, [System.StringComparison]::OrdinalIgnoreCase) | Should -BeTrue178 }179 180 AfterAll {181 Remove-InstalledModules182 }183}184 185function Remove-InstalledScripts186{187 $installedScript = Get-InstalledScript -Name $TestScript -ErrorAction SilentlyContinue188 if ($null -eq $installedScript) {189 return190 }191 192 $scriptPath = Join-Path ${installedScript}.InstalledLocation "${TestScript}.ps1"193 if (test-Path -Type Leaf -Path $scriptPath) {194 Remove-Item -Force -Path $scriptPath -ErrorAction Ignore195 }196 197 $xmlPath = Join-Path ${installedScript}.InstalledLocation InstalledScriptInfos "${TestScript}_InstalledScriptInfo.xml"198 if (test-Path -Type Leaf -Path $xmlPath) {199 Remove-Item -Force -Path $xmlPath -ErrorAction Ignore200 }201}202 203Describe "PowerShellGet - Script tests" -tags "Feature" {204 205 BeforeAll {206 if ($script:Initialized -eq $false) {207 Initialize208 $script:Initialized = $true209 }210 }211 212 BeforeEach {213 Remove-InstalledScripts214 }215 216 It "Should find a script correctly" {217 $psgetScriptInfo = Find-Script -Name $TestScript -Repository $RepositoryName218 $psgetScriptInfo.Name | Should -Be $TestScript219 $psgetScriptInfo.Repository | Should -Be $RepositoryName220 }221 222 It "Should install a script correctly to the required location with default CurrentUser scope" {223 Install-Script -Name $TestScript -Repository $RepositoryName -NoPathUpdate224 $installedScriptInfo = Get-InstalledScript -Name $TestScript225 226 $installedScriptInfo | Should -Not -BeNullOrEmpty227 $installedScriptInfo.Name | Should -Be $TestScript228 $installedScriptInfo.InstalledLocation.StartsWith($script:MyDocumentsScriptsPath, [System.StringComparison]::OrdinalIgnoreCase) | Should -BeTrue229 }230 231 AfterAll {232 Remove-InstalledScripts233 }234}235 236Describe "PowerShellGet - Script tests (Admin)" -Tags @('Feature', 'RequireAdminOnWindows', 'RequireSudoOnUnix') {237 238 BeforeAll {239 if ($script:Initialized -eq $false) {240 Initialize241 $script:Initialized = $true242 }243 }244 245 BeforeEach {246 Remove-InstalledScripts247 }248 249 It "Should install a script correctly to the required location with AllUsers scope" {250 Install-Script -Name $TestScript -Repository $RepositoryName -NoPathUpdate -Scope AllUsers -ErrorAction Stop251 $installedScriptInfo = Get-InstalledScript -Name $TestScript252 253 $installedScriptInfo | Should -Not -BeNullOrEmpty254 $installedScriptInfo.Name | Should -Be $TestScript255 $installedScriptInfo.InstalledLocation.StartsWith($script:ProgramFilesScriptsPath, [System.StringComparison]::OrdinalIgnoreCase) | Should -BeTrue256 }257 258 AfterAll {259 Remove-InstalledScripts260 }261}262 263Describe 'PowerShellGet Type tests' -tags @('CI') {264 BeforeAll {265 Import-Module PowerShellGet -Force266 }267 268 It 'Ensure PowerShellGet Types are available' {269 $PowerShellGetNamespace = 'Microsoft.PowerShell.Commands.PowerShellGet'270 $PowerShellGetTypeDetails = @{271 InternalWebProxy = @('GetProxy', 'IsBypassed')272 }273 274 if((IsWindows)) {275 $PowerShellGetTypeDetails['CERT_CHAIN_POLICY_PARA'] = @('cbSize','dwFlags','pvExtraPolicyPara')276 $PowerShellGetTypeDetails['CERT_CHAIN_POLICY_STATUS'] = @('cbSize','dwError','lChainIndex','lElementIndex','pvExtraPolicyStatus')277 $PowerShellGetTypeDetails['InternalSafeHandleZeroOrMinusOneIsInvalid'] = @('IsInvalid')278 $PowerShellGetTypeDetails['InternalSafeX509ChainHandle'] = @('CertFreeCertificateChain','ReleaseHandle','InvalidHandle')279 $PowerShellGetTypeDetails['Win32Helpers'] = @('CertVerifyCertificateChainPolicy', 'CertDuplicateCertificateChain', 'IsMicrosoftCertificate')280 }281 282 if('Microsoft.PowerShell.Telemetry.Internal.TelemetryAPI' -as [Type]) {283 $PowerShellGetTypeDetails['Telemetry'] = @('TraceMessageArtifactsNotFound', 'TraceMessageNonPSGalleryRegistration')284 }285 286 $PowerShellGetTypeDetails.GetEnumerator() | ForEach-Object {287 $ClassName = $_.Name288 $Type = "$PowerShellGetNamespace.$ClassName" -as [Type]289 $Type | Select-Object -ExpandProperty Name | Should -Be $ClassName290 $_.Value | ForEach-Object { $Type.DeclaredMembers.Name -contains $_ | Should -BeTrue }291 }292 }293}294 