Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

Commit b48681c

Browse files
ThomasNietoalerickson
authored andcommitted
Add public variable containing module and script paths (#486)
1 parent 6f6620c commit b48681c

File tree

5 files changed

+129
-91
lines changed

5 files changed

+129
-91
lines changed

Tests/PowerShellGet.Tests.ps1

Lines changed: 64 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -19,53 +19,43 @@ function IsCoreCLR { $PSVersionTable.ContainsKey('PSEdition') -and $PSVersionTab
1919

2020
#region Install locations for modules and scripts
2121

22-
if(IsInbox)
23-
{
22+
if (IsInbox) {
2423
$script:ProgramFilesPSPath = Microsoft.PowerShell.Management\Join-Path -Path $env:ProgramFiles -ChildPath "WindowsPowerShell"
2524
}
26-
elseif(IsCoreCLR){
27-
if(IsWindows) {
25+
elseif (IsCoreCLR) {
26+
if (IsWindows) {
2827
$script:ProgramFilesPSPath = Microsoft.PowerShell.Management\Join-Path -Path $env:ProgramFiles -ChildPath 'PowerShell'
2928
}
3029
else {
3130
$script:ProgramFilesPSPath = Split-Path -Path ([System.Management.Automation.Platform]::SelectProductNameForDirectory('SHARED_MODULES')) -Parent
3231
}
3332
}
3433

35-
try
36-
{
34+
try {
3735
$script:MyDocumentsFolderPath = [Environment]::GetFolderPath("MyDocuments")
3836
}
39-
catch
40-
{
37+
catch {
4138
$script:MyDocumentsFolderPath = $null
4239
}
4340

44-
if(IsInbox)
45-
{
46-
$script:MyDocumentsPSPath = if($script:MyDocumentsFolderPath)
47-
{
48-
Microsoft.PowerShell.Management\Join-Path -Path $script:MyDocumentsFolderPath -ChildPath "WindowsPowerShell"
49-
}
50-
else
51-
{
52-
Microsoft.PowerShell.Management\Join-Path -Path $env:USERPROFILE -ChildPath "Documents\WindowsPowerShell"
53-
}
41+
if (IsInbox) {
42+
$script:MyDocumentsPSPath = if ($script:MyDocumentsFolderPath) {
43+
Microsoft.PowerShell.Management\Join-Path -Path $script:MyDocumentsFolderPath -ChildPath "WindowsPowerShell"
44+
}
45+
else {
46+
Microsoft.PowerShell.Management\Join-Path -Path $env:USERPROFILE -ChildPath "Documents\WindowsPowerShell"
47+
}
5448
}
55-
elseif(IsCoreCLR) {
56-
if(IsWindows)
57-
{
58-
$script:MyDocumentsPSPath = if($script:MyDocumentsFolderPath)
59-
{
49+
elseif (IsCoreCLR) {
50+
if (IsWindows) {
51+
$script:MyDocumentsPSPath = if ($script:MyDocumentsFolderPath) {
6052
Microsoft.PowerShell.Management\Join-Path -Path $script:MyDocumentsFolderPath -ChildPath 'PowerShell'
61-
}
62-
else
63-
{
53+
}
54+
else {
6455
Microsoft.PowerShell.Management\Join-Path -Path $HOME -ChildPath "Documents\PowerShell"
6556
}
6657
}
67-
else
68-
{
58+
else {
6959
$script:MyDocumentsPSPath = Split-Path -Path ([System.Management.Automation.Platform]::SelectProductNameForDirectory('USER_MODULES')) -Parent
7060
}
7161
}
@@ -80,32 +70,57 @@ $script:MyDocumentsScriptsPath = Microsoft.PowerShell.Management\Join-Path -Path
8070

8171
#region Register a test repository
8272

83-
function Initialize
84-
{
73+
function Initialize {
8574
# Cleaned up commands whose output to console by deleting or piping to Out-Null
8675
Import-Module PackageManagement
8776
Get-PackageProvider -ListAvailable | Out-Null
8877

8978
$repo = Get-PSRepository -ErrorAction SilentlyContinue |
90-
Where-Object {$_.SourceLocation.StartsWith($SourceLocation, [System.StringComparison]::OrdinalIgnoreCase)}
91-
if($repo)
92-
{
79+
Where-Object { $_.SourceLocation.StartsWith($SourceLocation, [System.StringComparison]::OrdinalIgnoreCase) }
80+
if ($repo) {
9381
$script:RepositoryName = $repo.Name
9482
}
95-
else
96-
{
83+
else {
9784
Register-PSRepository -Name $RepositoryName -SourceLocation $SourceLocation -InstallationPolicy Trusted
9885
$script:RegisteredINTRepo = $true
9986
}
10087
}
10188

10289
#endregion
10390

104-
function Remove-InstalledModules
105-
{
91+
function Remove-InstalledModules {
10692
Get-InstalledModule -Name $ContosoServer -AllVersions -ErrorAction SilentlyContinue | PowerShellGet\Uninstall-Module -Force
10793
}
10894

95+
Describe "PowerShellGet - Module public variable tests" {
96+
BeforeAll {
97+
if ($script:Initialized -eq $false) {
98+
Initialize
99+
$script:Initialized = $true
100+
}
101+
}
102+
103+
It "PSGetPath variable should exist" {
104+
Test-Path -Path variable:PSGetPath | Should -BeTrue
105+
}
106+
107+
It "PSGetPath - AllUsersModules should be $ProgramFilesModulesPath" {
108+
$PSGetPath.AllUsersModules | Should -Be $script:ProgramFilesModulesPath
109+
}
110+
111+
It "PSGetPath - AllUsersScripts should be $ProgramFilesScriptsPath" {
112+
$PSGetPath.AllUsersScripts | Should -Be $script:ProgramFilesScriptsPath
113+
}
114+
115+
It "PSGetPath - CurrentUserModules should be $ProgramFilesModulesPath" {
116+
$PSGetPath.CurrentUserModules | Should -Be $script:MyDocumentsModulesPath
117+
}
118+
119+
It "PSGetPath - CurrentUserScripts should be $ProgramFilesScriptsPath" {
120+
$PSGetPath.CurrentUserScripts | Should -Be $script:MyDocumentsScriptsPath
121+
}
122+
}
123+
109124
Describe "PowerShellGet - Module tests" -tags "Feature" {
110125

111126
BeforeAll {
@@ -162,8 +177,7 @@ Describe "PowerShellGet - Module tests (Admin)" -tags @('Feature', 'RequireAdmin
162177

163178
$installedModuleInfo | Should Not Be $null
164179
$installedModuleInfo.Name | Should Be $ContosoServer
165-
if ($script:IsCoreCLR)
166-
{
180+
if ($script:IsCoreCLR) {
167181
$installedModuleInfo.InstalledLocation.StartsWith($script:MyDocumentsModulesPath, [System.StringComparison]::OrdinalIgnoreCase) | Should Be $true
168182
}
169183
else {
@@ -180,8 +194,7 @@ Describe "PowerShellGet - Module tests (Admin)" -tags @('Feature', 'RequireAdmin
180194
}
181195
}
182196

183-
function Remove-InstalledScripts
184-
{
197+
function Remove-InstalledScripts {
185198
Get-InstalledScript -Name $FabrikamServerScript -ErrorAction SilentlyContinue | Uninstall-Script -Force
186199
}
187200

@@ -237,22 +250,20 @@ Describe "PowerShellGet - Script tests (Admin)" -tags @('Feature', 'RequireAdmin
237250

238251
$installedScriptInfo | Should Not Be $null
239252
$installedScriptInfo.Name | Should Be $FabrikamServerScript
240-
if ($script:IsCoreCLR)
241-
{
253+
if ($script:IsCoreCLR) {
242254
$installedScriptInfo.InstalledLocation.StartsWith($script:MyDocumentsScriptsPath, [System.StringComparison]::OrdinalIgnoreCase) | Should Be $true
243255
}
244-
else
245-
{
256+
else {
246257
$installedScriptInfo.InstalledLocation.StartsWith($script:ProgramFilesScriptsPath, [System.StringComparison]::OrdinalIgnoreCase) | Should Be $true
247258
}
248-
}
259+
}
249260

250261
AfterAll {
251262
Remove-InstalledScripts
252263
}
253264
}
254265

255-
Describe 'PowerShellGet Type tests' -tags @('BVT','CI') {
266+
Describe 'PowerShellGet Type tests' -tags @('BVT', 'CI') {
256267
BeforeAll {
257268
Import-Module PowerShellGet -Force
258269
}
@@ -263,15 +274,15 @@ Describe 'PowerShellGet Type tests' -tags @('BVT','CI') {
263274
InternalWebProxy = @('GetProxy', 'IsBypassed')
264275
}
265276

266-
if((IsWindows)) {
267-
$PowerShellGetTypeDetails['CERT_CHAIN_POLICY_PARA'] = @('cbSize','dwFlags','pvExtraPolicyPara')
268-
$PowerShellGetTypeDetails['CERT_CHAIN_POLICY_STATUS'] = @('cbSize','dwError','lChainIndex','lElementIndex','pvExtraPolicyStatus')
277+
if ((IsWindows)) {
278+
$PowerShellGetTypeDetails['CERT_CHAIN_POLICY_PARA'] = @('cbSize', 'dwFlags', 'pvExtraPolicyPara')
279+
$PowerShellGetTypeDetails['CERT_CHAIN_POLICY_STATUS'] = @('cbSize', 'dwError', 'lChainIndex', 'lElementIndex', 'pvExtraPolicyStatus')
269280
$PowerShellGetTypeDetails['InternalSafeHandleZeroOrMinusOneIsInvalid'] = @('IsInvalid')
270-
$PowerShellGetTypeDetails['InternalSafeX509ChainHandle'] = @('CertFreeCertificateChain','ReleaseHandle','InvalidHandle')
281+
$PowerShellGetTypeDetails['InternalSafeX509ChainHandle'] = @('CertFreeCertificateChain', 'ReleaseHandle', 'InvalidHandle')
271282
$PowerShellGetTypeDetails['Win32Helpers'] = @('CertVerifyCertificateChainPolicy', 'CertDuplicateCertificateChain', 'IsMicrosoftCertificate')
272283
}
273284

274-
if('Microsoft.PowerShell.Telemetry.Internal.TelemetryAPI' -as [Type]) {
285+
if ('Microsoft.PowerShell.Telemetry.Internal.TelemetryAPI' -as [Type]) {
275286
$PowerShellGetTypeDetails['Telemetry'] = @('TraceMessageArtifactsNotFound', 'TraceMessageNonPSGalleryRegistration')
276287
}
277288

@@ -284,7 +295,6 @@ Describe 'PowerShellGet Type tests' -tags @('BVT','CI') {
284295
}
285296
}
286297

287-
if($RegisteredINTRepo)
288-
{
298+
if ($RegisteredINTRepo) {
289299
Get-PSRepository -Name $RepositoryName -ErrorAction SilentlyContinue | Unregister-PSRepository
290300
}

src/PowerShellGet/PSGet.Format.ps1xml

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<TableColumnHeader>
1818
<Width>20</Width>
1919
</TableColumnHeader>
20-
<TableColumnHeader/>
20+
<TableColumnHeader />
2121
</TableHeaders>
2222
<TableRowEntries>
2323
<TableRowEntry>
@@ -52,7 +52,7 @@
5252
<TableColumnHeader>
5353
<Width>20</Width>
5454
</TableColumnHeader>
55-
<TableColumnHeader/>
55+
<TableColumnHeader />
5656
</TableHeaders>
5757
<TableRowEntries>
5858
<TableRowEntry>
@@ -87,7 +87,7 @@
8787
<TableColumnHeader>
8888
<Width>20</Width>
8989
</TableColumnHeader>
90-
<TableColumnHeader/>
90+
<TableColumnHeader />
9191
</TableHeaders>
9292
<TableRowEntries>
9393
<TableRowEntry>
@@ -126,7 +126,7 @@
126126
<TableColumnHeader>
127127
<Width>35</Width>
128128
</TableColumnHeader>
129-
<TableColumnHeader/>
129+
<TableColumnHeader />
130130
</TableHeaders>
131131
<TableRowEntries>
132132
<TableRowEntry>
@@ -164,7 +164,7 @@
164164
<TableColumnHeader>
165165
<Width>35</Width>
166166
</TableColumnHeader>
167-
<TableColumnHeader/>
167+
<TableColumnHeader />
168168
</TableHeaders>
169169
<TableRowEntries>
170170
<TableRowEntry>
@@ -186,5 +186,31 @@
186186
</TableRowEntries>
187187
</TableControl>
188188
</View>
189+
<View>
190+
<Name>PSGetPath</Name>
191+
<ViewSelectedBy>
192+
<TypeName>Microsoft.PowerShell.Commands.PSGetPath</TypeName>
193+
</ViewSelectedBy>
194+
<ListControl>
195+
<ListEntries>
196+
<ListEntry>
197+
<ListItems>
198+
<ListItem>
199+
<PropertyName>AllUsersModules</PropertyName>
200+
</ListItem>
201+
<ListItem>
202+
<PropertyName>AllUsersScripts</PropertyName>
203+
</ListItem>
204+
<ListItem>
205+
<PropertyName>CurrentUserModules</PropertyName>
206+
</ListItem>
207+
<ListItem>
208+
<PropertyName>CurrentUserScripts</PropertyName>
209+
</ListItem>
210+
</ListItems>
211+
</ListEntry>
212+
</ListEntries>
213+
</ListControl>
214+
</View>
189215
</ViewDefinitions>
190216
</Configuration>

src/PowerShellGet/PowerShellGet.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
'Update-Script',
3636
'Update-ScriptFileInfo')
3737

38-
VariablesToExport = "*"
38+
VariablesToExport = 'PSGetPath'
3939
AliasesToExport = @('inmo', 'fimo', 'upmo', 'pumo')
4040
FileList = @('PSModule.psm1',
4141
'PSGet.Format.ps1xml',

src/PowerShellGet/private/modulefile/PartOne.ps1

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ $script:MyDocumentsModulesPath = Microsoft.PowerShell.Management\Join-Path -Path
7878
$script:ProgramFilesScriptsPath = Microsoft.PowerShell.Management\Join-Path -Path $script:ProgramFilesPSPath -ChildPath 'Scripts'
7979
$script:MyDocumentsScriptsPath = Microsoft.PowerShell.Management\Join-Path -Path $script:MyDocumentsPSPath -ChildPath 'Scripts'
8080

81+
$script:PSGetPath = [pscustomobject]@{
82+
AllUsersModules = $script:ProgramFilesModulesPath
83+
AllUsersScripts = $script:ProgramFilesScriptsPath
84+
CurrentUserModules = $script:MyDocumentsModulesPath
85+
CurrentUserScripts = $script:MyDocumentsScriptsPath
86+
PSTypeName = 'Microsoft.PowerShell.Commands.PSGetPath'
87+
}
88+
8189
$script:TempPath = [System.IO.Path]::GetTempPath()
8290
$script:PSGetItemInfoFileName = "PSGetModuleInfo.xml"
8391

@@ -133,9 +141,9 @@ $script:NuGetProviderName = "NuGet"
133141
$script:NuGetProviderVersion = [Version]'2.8.5.201'
134142

135143
$script:SupportsPSModulesFeatureName = "supports-powershell-modules"
136-
$script:FastPackRefHashtable = @{}
137-
$script:NuGetBinaryProgramDataPath = if ($script:IsWindows) {"$env:ProgramFiles\PackageManagement\ProviderAssemblies"}
138-
$script:NuGetBinaryLocalAppDataPath = if ($script:IsWindows) {"$env:LOCALAPPDATA\PackageManagement\ProviderAssemblies"}
144+
$script:FastPackRefHashtable = @{ }
145+
$script:NuGetBinaryProgramDataPath = if ($script:IsWindows) { "$env:ProgramFiles\PackageManagement\ProviderAssemblies" }
146+
$script:NuGetBinaryLocalAppDataPath = if ($script:IsWindows) { "$env:LOCALAPPDATA\PackageManagement\ProviderAssemblies" }
139147
# go fwlink for 'https://dist.nuget.org/win-x86-commandline/latest/nuget.exe'
140148
$script:NuGetClientSourceURL = 'https://aka.ms/psget-nugetexe'
141149
$script:NuGetExeMinRequiredVersion = [Version]'4.1.0'
@@ -261,7 +269,7 @@ $script:PackageManagementSaveModuleMessageResolverScriptBlock = {
261269

262270
switch ($i) {
263271
'ActionInstallPackage' { return "Save-Module" }
264-
'QueryInstallUntrustedPackage' {return $QuerySaveUntrustedPackage}
272+
'QueryInstallUntrustedPackage' { return $QuerySaveUntrustedPackage }
265273
'TargetPackage' { return $PackageTarget }
266274
Default {
267275
$Message = $Message -creplace "Install", "Download"
@@ -326,7 +334,7 @@ function PackageManagementMessageResolver($MsgID, $Message) {
326334
'SourceNotFound' { return $SourceNotFound }
327335
'CaptionPackageNotTrusted' { return $ModuleIsNotTrusted }
328336
'CaptionSourceNotTrusted' { return $RepositoryIsNotTrusted }
329-
'QueryInstallUntrustedPackage' {return $QueryInstallUntrustedPackage}
337+
'QueryInstallUntrustedPackage' { return $QueryInstallUntrustedPackage }
330338
Default {
331339
if ($Message) {
332340
$tempMessage = $Message -creplace "PackageSource", "PSRepository"
@@ -359,7 +367,7 @@ $script:PackageManagementSaveScriptMessageResolverScriptBlock = {
359367

360368
switch ($i) {
361369
'ActionInstallPackage' { return "Save-Script" }
362-
'QueryInstallUntrustedPackage' {return $QuerySaveUntrustedPackage}
370+
'QueryInstallUntrustedPackage' { return $QuerySaveUntrustedPackage }
363371
'TargetPackage' { return $PackageTarget }
364372
Default {
365373
$Message = $Message -creplace "Install", "Download"
@@ -418,7 +426,7 @@ function PackageManagementMessageResolverForScripts($MsgID, $Message) {
418426
'SourceNotFound' { return $SourceNotFound }
419427
'CaptionPackageNotTrusted' { return $ScriptIsNotTrusted }
420428
'CaptionSourceNotTrusted' { return $RepositoryIsNotTrusted }
421-
'QueryInstallUntrustedPackage' {return $QueryInstallUntrustedPackage}
429+
'QueryInstallUntrustedPackage' { return $QueryInstallUntrustedPackage }
422430
Default {
423431
if ($Message) {
424432
$tempMessage = $Message -creplace "PackageSource", "PSRepository"

0 commit comments

Comments
 (0)