diff --git a/Tests/PowerShellGet.Tests.ps1 b/Tests/PowerShellGet.Tests.ps1 index 6a922a82..8d59c761 100644 --- a/Tests/PowerShellGet.Tests.ps1 +++ b/Tests/PowerShellGet.Tests.ps1 @@ -19,12 +19,11 @@ function IsCoreCLR { $PSVersionTable.ContainsKey('PSEdition') -and $PSVersionTab #region Install locations for modules and scripts -if(IsInbox) -{ +if (IsInbox) { $script:ProgramFilesPSPath = Microsoft.PowerShell.Management\Join-Path -Path $env:ProgramFiles -ChildPath "WindowsPowerShell" } -elseif(IsCoreCLR){ - if(IsWindows) { +elseif (IsCoreCLR) { + if (IsWindows) { $script:ProgramFilesPSPath = Microsoft.PowerShell.Management\Join-Path -Path $env:ProgramFiles -ChildPath 'PowerShell' } else { @@ -32,40 +31,31 @@ elseif(IsCoreCLR){ } } -try -{ +try { $script:MyDocumentsFolderPath = [Environment]::GetFolderPath("MyDocuments") } -catch -{ +catch { $script:MyDocumentsFolderPath = $null } -if(IsInbox) -{ - $script:MyDocumentsPSPath = if($script:MyDocumentsFolderPath) - { - Microsoft.PowerShell.Management\Join-Path -Path $script:MyDocumentsFolderPath -ChildPath "WindowsPowerShell" - } - else - { - Microsoft.PowerShell.Management\Join-Path -Path $env:USERPROFILE -ChildPath "Documents\WindowsPowerShell" - } +if (IsInbox) { + $script:MyDocumentsPSPath = if ($script:MyDocumentsFolderPath) { + Microsoft.PowerShell.Management\Join-Path -Path $script:MyDocumentsFolderPath -ChildPath "WindowsPowerShell" + } + else { + Microsoft.PowerShell.Management\Join-Path -Path $env:USERPROFILE -ChildPath "Documents\WindowsPowerShell" + } } -elseif(IsCoreCLR) { - if(IsWindows) - { - $script:MyDocumentsPSPath = if($script:MyDocumentsFolderPath) - { +elseif (IsCoreCLR) { + if (IsWindows) { + $script:MyDocumentsPSPath = if ($script:MyDocumentsFolderPath) { Microsoft.PowerShell.Management\Join-Path -Path $script:MyDocumentsFolderPath -ChildPath 'PowerShell' - } - else - { + } + else { Microsoft.PowerShell.Management\Join-Path -Path $HOME -ChildPath "Documents\PowerShell" } } - else - { + else { $script:MyDocumentsPSPath = Split-Path -Path ([System.Management.Automation.Platform]::SelectProductNameForDirectory('USER_MODULES')) -Parent } } @@ -80,20 +70,17 @@ $script:MyDocumentsScriptsPath = Microsoft.PowerShell.Management\Join-Path -Path #region Register a test repository -function Initialize -{ +function Initialize { # Cleaned up commands whose output to console by deleting or piping to Out-Null Import-Module PackageManagement Get-PackageProvider -ListAvailable | Out-Null $repo = Get-PSRepository -ErrorAction SilentlyContinue | - Where-Object {$_.SourceLocation.StartsWith($SourceLocation, [System.StringComparison]::OrdinalIgnoreCase)} - if($repo) - { + Where-Object { $_.SourceLocation.StartsWith($SourceLocation, [System.StringComparison]::OrdinalIgnoreCase) } + if ($repo) { $script:RepositoryName = $repo.Name } - else - { + else { Register-PSRepository -Name $RepositoryName -SourceLocation $SourceLocation -InstallationPolicy Trusted $script:RegisteredINTRepo = $true } @@ -101,11 +88,39 @@ function Initialize #endregion -function Remove-InstalledModules -{ +function Remove-InstalledModules { Get-InstalledModule -Name $ContosoServer -AllVersions -ErrorAction SilentlyContinue | PowerShellGet\Uninstall-Module -Force } +Describe "PowerShellGet - Module public variable tests" { + BeforeAll { + if ($script:Initialized -eq $false) { + Initialize + $script:Initialized = $true + } + } + + It "PSGetPath variable should exist" { + Test-Path -Path variable:PSGetPath | Should -BeTrue + } + + It "PSGetPath - AllUsersModules should be $ProgramFilesModulesPath" { + $PSGetPath.AllUsersModules | Should -Be $script:ProgramFilesModulesPath + } + + It "PSGetPath - AllUsersScripts should be $ProgramFilesScriptsPath" { + $PSGetPath.AllUsersScripts | Should -Be $script:ProgramFilesScriptsPath + } + + It "PSGetPath - CurrentUserModules should be $ProgramFilesModulesPath" { + $PSGetPath.CurrentUserModules | Should -Be $script:MyDocumentsModulesPath + } + + It "PSGetPath - CurrentUserScripts should be $ProgramFilesScriptsPath" { + $PSGetPath.CurrentUserScripts | Should -Be $script:MyDocumentsScriptsPath + } +} + Describe "PowerShellGet - Module tests" -tags "Feature" { BeforeAll { @@ -162,8 +177,7 @@ Describe "PowerShellGet - Module tests (Admin)" -tags @('Feature', 'RequireAdmin $installedModuleInfo | Should Not Be $null $installedModuleInfo.Name | Should Be $ContosoServer - if ($script:IsCoreCLR) - { + if ($script:IsCoreCLR) { $installedModuleInfo.InstalledLocation.StartsWith($script:MyDocumentsModulesPath, [System.StringComparison]::OrdinalIgnoreCase) | Should Be $true } else { @@ -180,8 +194,7 @@ Describe "PowerShellGet - Module tests (Admin)" -tags @('Feature', 'RequireAdmin } } -function Remove-InstalledScripts -{ +function Remove-InstalledScripts { Get-InstalledScript -Name $FabrikamServerScript -ErrorAction SilentlyContinue | Uninstall-Script -Force } @@ -237,22 +250,20 @@ Describe "PowerShellGet - Script tests (Admin)" -tags @('Feature', 'RequireAdmin $installedScriptInfo | Should Not Be $null $installedScriptInfo.Name | Should Be $FabrikamServerScript - if ($script:IsCoreCLR) - { + if ($script:IsCoreCLR) { $installedScriptInfo.InstalledLocation.StartsWith($script:MyDocumentsScriptsPath, [System.StringComparison]::OrdinalIgnoreCase) | Should Be $true } - else - { + else { $installedScriptInfo.InstalledLocation.StartsWith($script:ProgramFilesScriptsPath, [System.StringComparison]::OrdinalIgnoreCase) | Should Be $true } - } + } AfterAll { Remove-InstalledScripts } } -Describe 'PowerShellGet Type tests' -tags @('BVT','CI') { +Describe 'PowerShellGet Type tests' -tags @('BVT', 'CI') { BeforeAll { Import-Module PowerShellGet -Force } @@ -263,15 +274,15 @@ Describe 'PowerShellGet Type tests' -tags @('BVT','CI') { InternalWebProxy = @('GetProxy', 'IsBypassed') } - if((IsWindows)) { - $PowerShellGetTypeDetails['CERT_CHAIN_POLICY_PARA'] = @('cbSize','dwFlags','pvExtraPolicyPara') - $PowerShellGetTypeDetails['CERT_CHAIN_POLICY_STATUS'] = @('cbSize','dwError','lChainIndex','lElementIndex','pvExtraPolicyStatus') + if ((IsWindows)) { + $PowerShellGetTypeDetails['CERT_CHAIN_POLICY_PARA'] = @('cbSize', 'dwFlags', 'pvExtraPolicyPara') + $PowerShellGetTypeDetails['CERT_CHAIN_POLICY_STATUS'] = @('cbSize', 'dwError', 'lChainIndex', 'lElementIndex', 'pvExtraPolicyStatus') $PowerShellGetTypeDetails['InternalSafeHandleZeroOrMinusOneIsInvalid'] = @('IsInvalid') - $PowerShellGetTypeDetails['InternalSafeX509ChainHandle'] = @('CertFreeCertificateChain','ReleaseHandle','InvalidHandle') + $PowerShellGetTypeDetails['InternalSafeX509ChainHandle'] = @('CertFreeCertificateChain', 'ReleaseHandle', 'InvalidHandle') $PowerShellGetTypeDetails['Win32Helpers'] = @('CertVerifyCertificateChainPolicy', 'CertDuplicateCertificateChain', 'IsMicrosoftCertificate') } - if('Microsoft.PowerShell.Telemetry.Internal.TelemetryAPI' -as [Type]) { + if ('Microsoft.PowerShell.Telemetry.Internal.TelemetryAPI' -as [Type]) { $PowerShellGetTypeDetails['Telemetry'] = @('TraceMessageArtifactsNotFound', 'TraceMessageNonPSGalleryRegistration') } @@ -284,7 +295,6 @@ Describe 'PowerShellGet Type tests' -tags @('BVT','CI') { } } -if($RegisteredINTRepo) -{ +if ($RegisteredINTRepo) { Get-PSRepository -Name $RepositoryName -ErrorAction SilentlyContinue | Unregister-PSRepository } diff --git a/src/PowerShellGet/PSGet.Format.ps1xml b/src/PowerShellGet/PSGet.Format.ps1xml index 370d1ca1..b6ea816b 100644 --- a/src/PowerShellGet/PSGet.Format.ps1xml +++ b/src/PowerShellGet/PSGet.Format.ps1xml @@ -17,7 +17,7 @@ 20 - + @@ -52,7 +52,7 @@ 20 - + @@ -87,7 +87,7 @@ 20 - + @@ -126,7 +126,7 @@ 35 - + @@ -164,7 +164,7 @@ 35 - + @@ -186,5 +186,31 @@ + + PSGetPath + + Microsoft.PowerShell.Commands.PSGetPath + + + + + + + AllUsersModules + + + AllUsersScripts + + + CurrentUserModules + + + CurrentUserScripts + + + + + + diff --git a/src/PowerShellGet/PowerShellGet.psd1 b/src/PowerShellGet/PowerShellGet.psd1 index f2babb35..7e3fc989 100644 --- a/src/PowerShellGet/PowerShellGet.psd1 +++ b/src/PowerShellGet/PowerShellGet.psd1 @@ -35,7 +35,7 @@ 'Update-Script', 'Update-ScriptFileInfo') - VariablesToExport = "*" + VariablesToExport = 'PSGetPath' AliasesToExport = @('inmo', 'fimo', 'upmo', 'pumo') FileList = @('PSModule.psm1', 'PSGet.Format.ps1xml', diff --git a/src/PowerShellGet/private/modulefile/PartOne.ps1 b/src/PowerShellGet/private/modulefile/PartOne.ps1 index 8f4ac695..1f287a3e 100644 --- a/src/PowerShellGet/private/modulefile/PartOne.ps1 +++ b/src/PowerShellGet/private/modulefile/PartOne.ps1 @@ -78,6 +78,14 @@ $script:MyDocumentsModulesPath = Microsoft.PowerShell.Management\Join-Path -Path $script:ProgramFilesScriptsPath = Microsoft.PowerShell.Management\Join-Path -Path $script:ProgramFilesPSPath -ChildPath 'Scripts' $script:MyDocumentsScriptsPath = Microsoft.PowerShell.Management\Join-Path -Path $script:MyDocumentsPSPath -ChildPath 'Scripts' +$script:PSGetPath = [pscustomobject]@{ + AllUsersModules = $script:ProgramFilesModulesPath + AllUsersScripts = $script:ProgramFilesScriptsPath + CurrentUserModules = $script:MyDocumentsModulesPath + CurrentUserScripts = $script:MyDocumentsScriptsPath + PSTypeName = 'Microsoft.PowerShell.Commands.PSGetPath' +} + $script:TempPath = [System.IO.Path]::GetTempPath() $script:PSGetItemInfoFileName = "PSGetModuleInfo.xml" @@ -133,9 +141,9 @@ $script:NuGetProviderName = "NuGet" $script:NuGetProviderVersion = [Version]'2.8.5.201' $script:SupportsPSModulesFeatureName = "supports-powershell-modules" -$script:FastPackRefHashtable = @{} -$script:NuGetBinaryProgramDataPath = if ($script:IsWindows) {"$env:ProgramFiles\PackageManagement\ProviderAssemblies"} -$script:NuGetBinaryLocalAppDataPath = if ($script:IsWindows) {"$env:LOCALAPPDATA\PackageManagement\ProviderAssemblies"} +$script:FastPackRefHashtable = @{ } +$script:NuGetBinaryProgramDataPath = if ($script:IsWindows) { "$env:ProgramFiles\PackageManagement\ProviderAssemblies" } +$script:NuGetBinaryLocalAppDataPath = if ($script:IsWindows) { "$env:LOCALAPPDATA\PackageManagement\ProviderAssemblies" } # go fwlink for 'https://dist.nuget.org/win-x86-commandline/latest/nuget.exe' $script:NuGetClientSourceURL = 'https://aka.ms/psget-nugetexe' $script:NuGetExeMinRequiredVersion = [Version]'4.1.0' @@ -261,7 +269,7 @@ $script:PackageManagementSaveModuleMessageResolverScriptBlock = { switch ($i) { 'ActionInstallPackage' { return "Save-Module" } - 'QueryInstallUntrustedPackage' {return $QuerySaveUntrustedPackage} + 'QueryInstallUntrustedPackage' { return $QuerySaveUntrustedPackage } 'TargetPackage' { return $PackageTarget } Default { $Message = $Message -creplace "Install", "Download" @@ -326,7 +334,7 @@ function PackageManagementMessageResolver($MsgID, $Message) { 'SourceNotFound' { return $SourceNotFound } 'CaptionPackageNotTrusted' { return $ModuleIsNotTrusted } 'CaptionSourceNotTrusted' { return $RepositoryIsNotTrusted } - 'QueryInstallUntrustedPackage' {return $QueryInstallUntrustedPackage} + 'QueryInstallUntrustedPackage' { return $QueryInstallUntrustedPackage } Default { if ($Message) { $tempMessage = $Message -creplace "PackageSource", "PSRepository" @@ -359,7 +367,7 @@ $script:PackageManagementSaveScriptMessageResolverScriptBlock = { switch ($i) { 'ActionInstallPackage' { return "Save-Script" } - 'QueryInstallUntrustedPackage' {return $QuerySaveUntrustedPackage} + 'QueryInstallUntrustedPackage' { return $QuerySaveUntrustedPackage } 'TargetPackage' { return $PackageTarget } Default { $Message = $Message -creplace "Install", "Download" @@ -418,7 +426,7 @@ function PackageManagementMessageResolverForScripts($MsgID, $Message) { 'SourceNotFound' { return $SourceNotFound } 'CaptionPackageNotTrusted' { return $ScriptIsNotTrusted } 'CaptionSourceNotTrusted' { return $RepositoryIsNotTrusted } - 'QueryInstallUntrustedPackage' {return $QueryInstallUntrustedPackage} + 'QueryInstallUntrustedPackage' { return $QueryInstallUntrustedPackage } Default { if ($Message) { $tempMessage = $Message -creplace "PackageSource", "PSRepository" diff --git a/src/PowerShellGet/private/modulefile/PartTwo.ps1 b/src/PowerShellGet/private/modulefile/PartTwo.ps1 index c30e82ca..3d9bec59 100644 --- a/src/PowerShellGet/private/modulefile/PartTwo.ps1 +++ b/src/PowerShellGet/private/modulefile/PartTwo.ps1 @@ -1,29 +1,26 @@ # Create install locations for scripts if they are not already created -if(-not (Microsoft.PowerShell.Management\Test-Path -Path $script:ProgramFilesInstalledScriptInfosPath) -and (Test-RunningAsElevated)) -{ +if (-not (Microsoft.PowerShell.Management\Test-Path -Path $script:ProgramFilesInstalledScriptInfosPath) -and (Test-RunningAsElevated)) { $ev = $null $null = Microsoft.PowerShell.Management\New-Item -Path $script:ProgramFilesInstalledScriptInfosPath ` - -ItemType Directory ` - -Force ` - -ErrorVariable ev ` - -ErrorAction SilentlyContinue ` - -WarningAction SilentlyContinue ` - -Confirm:$false ` - -WhatIf:$false + -ItemType Directory ` + -Force ` + -ErrorVariable ev ` + -ErrorAction SilentlyContinue ` + -WarningAction SilentlyContinue ` + -Confirm:$false ` + -WhatIf:$false - if($ev) - { + if ($ev) { $script:IsRunningAsElevated = $false } } -if(-not (Microsoft.PowerShell.Management\Test-Path -Path $script:MyDocumentsInstalledScriptInfosPath)) -{ +if (-not (Microsoft.PowerShell.Management\Test-Path -Path $script:MyDocumentsInstalledScriptInfosPath)) { $null = Microsoft.PowerShell.Management\New-Item -Path $script:MyDocumentsInstalledScriptInfosPath ` - -ItemType Directory ` - -Force ` - -Confirm:$false ` - -WhatIf:$false + -ItemType Directory ` + -Force ` + -Confirm:$false ` + -WhatIf:$false } # allow -repository params to be tab-completed @@ -49,10 +46,8 @@ $commandsWithRepositoryAsName = @( Add-ArgumentCompleter -Cmdlets $commandsWithRepositoryParameter -ParameterName "Repository" Add-ArgumentCompleter -Cmdlets $commandsWithRepositoryAsName -ParameterName "Name" -try -{ - if (Get-Command -Name Register-ArgumentCompleter -ErrorAction SilentlyContinue) - { +try { + if (Get-Command -Name Register-ArgumentCompleter -ErrorAction SilentlyContinue) { Register-ArgumentCompleter -CommandName Publish-Module -ParameterName Name -ScriptBlock { param ($commandName, $parameterName, $wordToComplete) @@ -62,8 +57,7 @@ try } } } -catch -{ +catch { # All this functionality is optional, so suppress errors Write-Debug -Message "Error registering argument completer: $_" } @@ -74,4 +68,4 @@ Set-Alias -Name upmo -Value Update-Module Set-Alias -Name pumo -Value Publish-Module Set-Alias -Name uimo -Value Uninstall-Module -Export-ModuleMember -Alias fimo, inmo, upmo, pumo, uimo +Export-ModuleMember -Alias fimo, inmo, upmo, pumo, uimo -Variable PSGetPath