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

Commit c3a1f0c

Browse files
committed
Fix for AppVeyor Test run issue on PSCore.
With the recent refactoring in build.psm1, latest version of PowerShellGet gets copied to a wrong path for the PSCore test run. In AppVeyor test runs, OneGet and PSGet modules are installed from Windows PowerShell process. Ensured that PowerShellGet module gets installed to the proper location for PSCore test run. Couple of minor changes: - Removed some unused script varaibles - Formatted this file using VSCode formatting option
1 parent 0921a0e commit c3a1f0c

1 file changed

Lines changed: 62 additions & 85 deletions

File tree

tools/build.psm1

Lines changed: 62 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
$script:PowerShellGet = 'PowerShellGet'
33
$script:IsInbox = $PSHOME.EndsWith('\WindowsPowerShell\v1.0', [System.StringComparison]::OrdinalIgnoreCase)
44
$script:IsWindows = (-not (Get-Variable -Name IsWindows -ErrorAction Ignore)) -or $IsWindows
5-
$script:IsLinux = (Get-Variable -Name IsLinux -ErrorAction Ignore) -and $IsLinux
6-
$script:IsMacOS = (Get-Variable -Name IsMacOS -ErrorAction Ignore) -and $IsMacOS
75
$script:IsCoreCLR = $PSVersionTable.ContainsKey('PSEdition') -and $PSVersionTable.PSEdition -eq 'Core'
86

97
$script:ProjectRoot = Split-Path -Path $PSScriptRoot -Parent
@@ -16,71 +14,61 @@ $script:PublicPSGetFunctions = @( Get-ChildItem -Path $ModuleRoot\public\psgetfu
1614
$script:PublicProviderFunctions = @( Get-ChildItem -Path $ModuleRoot\public\providerfunctions\*.ps1 -ErrorAction SilentlyContinue )
1715
$script:PrivateFunctions = @( Get-ChildItem -Path $ModuleRoot\private\functions\*.ps1 -ErrorAction SilentlyContinue )
1816

19-
if($script:IsInbox) {
17+
if ($script:IsInbox) {
2018
$script:ProgramFilesPSPath = Microsoft.PowerShell.Management\Join-Path -Path $env:ProgramFiles -ChildPath "WindowsPowerShell"
21-
} else {
22-
$script:ProgramFilesPSPath = $PSHome
2319
}
24-
25-
if($script:IsInbox) {
26-
try {
27-
$script:MyDocumentsFolderPath = [Environment]::GetFolderPath("MyDocuments")
28-
} catch {
29-
$script:MyDocumentsFolderPath = $null
20+
elseif ($script:IsCoreCLR) {
21+
if ($script:IsWindows) {
22+
$script:ProgramFilesPSPath = Microsoft.PowerShell.Management\Join-Path -Path $env:ProgramFiles -ChildPath 'PowerShell'
23+
}
24+
else {
25+
$script:ProgramFilesPSPath = Split-Path -Path ([System.Management.Automation.Platform]::SelectProductNameForDirectory('SHARED_MODULES')) -Parent
3026
}
31-
32-
$script:MyDocumentsPSPath = if($script:MyDocumentsFolderPath) {
33-
Microsoft.PowerShell.Management\Join-Path -Path $script:MyDocumentsFolderPath -ChildPath "WindowsPowerShell"
34-
} else {
35-
Microsoft.PowerShell.Management\Join-Path -Path $env:USERPROFILE -ChildPath "Documents\WindowsPowerShell"
36-
}
37-
} elseif($script:IsWindows) {
38-
$script:MyDocumentsPSPath = Microsoft.PowerShell.Management\Join-Path -Path $HOME -ChildPath 'Documents\PowerShell'
39-
} else {
40-
$script:MyDocumentsPSPath = Microsoft.PowerShell.Management\Join-Path -Path $HOME -ChildPath ".local/share/powershell"
4127
}
4228

4329
$script:ProgramFilesModulesPath = Microsoft.PowerShell.Management\Join-Path -Path $script:ProgramFilesPSPath -ChildPath "Modules"
44-
$script:MyDocumentsModulesPath = Microsoft.PowerShell.Management\Join-Path -Path $script:MyDocumentsPSPath -ChildPath "Modules"
45-
$script:ProgramFilesScriptsPath = Microsoft.PowerShell.Management\Join-Path -Path $script:ProgramFilesPSPath -ChildPath "Scripts"
46-
$script:MyDocumentsScriptsPath = Microsoft.PowerShell.Management\Join-Path -Path $script:MyDocumentsPSPath -ChildPath "Scripts"
47-
$script:TempPath = if($script:IsWindows) { ([System.IO.DirectoryInfo]$env:TEMP).FullName } else { '/tmp' }
30+
$script:TempPath = [System.IO.Path]::GetTempPath()
4831

49-
if($script:IsWindows) {
32+
if ($script:IsWindows) {
5033
$script:PSGetProgramDataPath = Microsoft.PowerShell.Management\Join-Path -Path $env:ProgramData -ChildPath 'Microsoft\Windows\PowerShell\PowerShellGet\'
51-
$script:PSGetAppLocalPath = Microsoft.PowerShell.Management\Join-Path -Path $env:LOCALAPPDATA -ChildPath 'Microsoft\Windows\PowerShell\PowerShellGet\'
52-
} else {
53-
$script:PSGetProgramDataPath = "$HOME/.config/powershell/powershellget"
54-
$script:PSGetAppLocalPath = "$HOME/.config/powershell/powershellget"
34+
}
35+
else {
36+
$script:PSGetProgramDataPath = Join-Path -Path ([System.Management.Automation.Platform]::SelectProductNameForDirectory('CONFIG')) -ChildPath 'PowerShellGet'
5537
}
5638

39+
$AllUsersModulesPath = $script:ProgramFilesModulesPath
40+
5741
# AppVeyor.yml sets a value to $env:PowerShellEdition variable,
5842
# otherwise set $script:PowerShellEdition value based on the current PowerShell Edition.
5943
$script:PowerShellEdition = [System.Environment]::GetEnvironmentVariable("PowerShellEdition")
60-
if(-not $script:PowerShellEdition) {
61-
if($script:IsCoreCLR) {
44+
if (-not $script:PowerShellEdition) {
45+
if ($script:IsCoreCLR) {
6246
$script:PowerShellEdition = 'Core'
63-
} else {
47+
}
48+
else {
6449
$script:PowerShellEdition = 'Desktop'
6550
}
6651
}
52+
elseif (($script:PowerShellEdition -eq 'Core') -and ($script:IsWindows)) {
53+
# In AppVeyor test runs, OneGet and PSGet modules are installed from Windows PowerShell process
54+
# Set AllUsersModulesPath to $env:ProgramFiles\PowerShell\Modules
55+
$AllUsersModulesPath = Join-Path -Path $env:ProgramFiles -ChildPath 'PowerShell' | Join-Path -ChildPath 'Modules'
56+
}
6757
Write-Host "PowerShellEdition value: $script:PowerShellEdition"
6858

69-
$AllUsersModulesPath = $script:ProgramFilesModulesPath
70-
7159
#endregion script variables
7260

7361
function Install-Dependencies {
7462
# Update build title for daily builds
75-
if($script:IsWindows -and (Test-DailyBuild)) {
76-
if($env:APPVEYOR_PULL_REQUEST_TITLE)
77-
{
63+
if ($script:IsWindows -and (Test-DailyBuild)) {
64+
if ($env:APPVEYOR_PULL_REQUEST_TITLE) {
7865
$buildName += $env:APPVEYOR_PULL_REQUEST_TITLE
79-
} else {
66+
}
67+
else {
8068
$buildName += $env:APPVEYOR_REPO_COMMIT_MESSAGE
8169
}
8270

83-
if(-not ($buildName.StartsWith("[Daily]", [System.StringComparison]::OrdinalIgnoreCase))) {
71+
if (-not ($buildName.StartsWith("[Daily]", [System.StringComparison]::OrdinalIgnoreCase))) {
8472
Update-AppveyorBuild -message "[Daily] $buildName"
8573
}
8674
}
@@ -93,9 +81,8 @@ function Install-PackageManagement {
9381
$NuGetExeName = 'NuGet.exe'
9482
$NugetExeFilePath = Microsoft.PowerShell.Management\Join-Path -Path $script:PSGetProgramDataPath -ChildPath $NuGetExeName
9583

96-
if(-not (Test-Path -Path $NugetExeFilePath -PathType Leaf)) {
97-
if(-not (Microsoft.PowerShell.Management\Test-Path -Path $script:PSGetProgramDataPath))
98-
{
84+
if (-not (Test-Path -Path $NugetExeFilePath -PathType Leaf)) {
85+
if (-not (Microsoft.PowerShell.Management\Test-Path -Path $script:PSGetProgramDataPath)) {
9986
$null = Microsoft.PowerShell.Management\New-Item -Path $script:PSGetProgramDataPath -ItemType Directory -Force
10087
}
10188

@@ -108,42 +95,37 @@ function Install-PackageManagement {
10895
# Install latest PackageManagement from Gallery
10996
$OneGetModuleName = 'PackageManagement'
11097
$OneGetModuleInfo = Get-Module -ListAvailable -Name $OneGetModuleName | Select-Object -First 1
111-
if ($OneGetModuleInfo)
112-
{
98+
if ($OneGetModuleInfo) {
11399
$NuGetProvider = Get-PackageProvider | Where-Object { $_.Name -eq 'NuGet' }
114-
if(-not $NuGetProvider) {
100+
if (-not $NuGetProvider) {
115101
Install-PackageProvider -Name NuGet -Force
116102
}
117103

118104
$LatestOneGetInPSGallery = Find-Module -Name $OneGetModuleName
119-
if($LatestOneGetInPSGallery.Version -gt $OneGetModuleInfo.Version) {
105+
if ($LatestOneGetInPSGallery.Version -gt $OneGetModuleInfo.Version) {
120106
Install-Module -InputObject $LatestOneGetInPSGallery -Force
121107
}
122108
}
123-
else
124-
{
109+
else {
125110
# Install latest PackageManagement module from PSGallery
126111
$TempModulePath = Microsoft.PowerShell.Management\Join-Path -Path $script:TempPath -ChildPath "$(Get-Random)"
127112
$null = Microsoft.PowerShell.Management\New-Item -Path $TempModulePath -Force -ItemType Directory
128113
$OneGetModuleName = 'PackageManagement'
129-
try
130-
{
114+
try {
131115
& $NugetExeFilePath install $OneGetModuleName -source https://www.powershellgallery.com/api/v2 -outputDirectory $TempModulePath -verbosity detailed
132116
$OneGetWithVersion = Microsoft.PowerShell.Management\Get-ChildItem -Path $TempModulePath -Directory
133-
$OneGetVersion = ($OneGetWithVersion.Name.Split('.',2))[1]
117+
$OneGetVersion = ($OneGetWithVersion.Name.Split('.', 2))[1]
134118

135119
$OneGetModulePath = Microsoft.PowerShell.Management\Join-Path -Path $AllUsersModulesPath -ChildPath $OneGetModuleName
136-
if($PSVersionTable.PSVersion -ge '5.0.0')
137-
{
120+
if ($PSVersionTable.PSVersion -ge '5.0.0') {
138121
$OneGetModulePath = Microsoft.PowerShell.Management\Join-Path -Path $OneGetModulePath -ChildPath $OneGetVersion
139122
}
140123

141124
$null = Microsoft.PowerShell.Management\New-Item -Path $OneGetModulePath -Force -ItemType Directory
142125
Microsoft.PowerShell.Management\Copy-Item -Path "$($OneGetWithVersion.FullName)\*" -Destination "$OneGetModulePath\" -Recurse -Force
143126
Get-Module -ListAvailable -Name $OneGetModuleName | Microsoft.PowerShell.Core\Where-Object {$_.Version -eq $OneGetVersion}
144127
}
145-
finally
146-
{
128+
finally {
147129
Remove-Item -Path $TempModulePath -Recurse -Force
148130
}
149131
}
@@ -152,17 +134,15 @@ function Get-PSHome {
152134
$PowerShellHome = $PSHOME
153135

154136
# Install PowerShell Core on Windows.
155-
if(($script:PowerShellEdition -eq 'Core') -and $script:IsWindows)
156-
{
137+
if (($script:PowerShellEdition -eq 'Core') -and $script:IsWindows) {
157138
$InstallPSCoreUrl = 'https://aka.ms/install-pscore'
158139
$InstallPSCorePath = Microsoft.PowerShell.Management\Join-Path -Path $PSScriptRoot -ChildPath 'install-powershell.ps1'
159140
Microsoft.PowerShell.Utility\Invoke-RestMethod -Uri $InstallPSCoreUrl -OutFile $InstallPSCorePath
160141

161142
$PowerShellHome = "$env:SystemDrive\PowerShellCore"
162143
& $InstallPSCorePath -Destination $PowerShellHome -Daily
163144

164-
if(-not $PowerShellHome -or -not (Microsoft.PowerShell.Management\Test-Path -Path $PowerShellHome -PathType Container))
165-
{
145+
if (-not $PowerShellHome -or -not (Microsoft.PowerShell.Management\Test-Path -Path $PowerShellHome -PathType Container)) {
166146
Throw "$PowerShellHome path is not available."
167147
}
168148

@@ -184,7 +164,7 @@ function Invoke-PowerShellGetTest {
184164
Write-Host -ForegroundColor Green "`$env:APPVEYOR_REPO_TAG_NAME value $env:APPVEYOR_REPO_TAG_NAME"
185165
Write-Host -ForegroundColor Green "TRAVIS_EVENT_TYPE environment variable value $([System.Environment]::GetEnvironmentVariable('TRAVIS_EVENT_TYPE'))"
186166

187-
if(-not $IsFullTestPass){
167+
if (-not $IsFullTestPass) {
188168
$IsFullTestPass = Test-DailyBuild
189169
}
190170
Write-Host -ForegroundColor Green "`$IsFullTestPass value $IsFullTestPass"
@@ -194,14 +174,15 @@ function Invoke-PowerShellGetTest {
194174
$ClonedProjectPath = Resolve-Path "$PSScriptRoot\.."
195175
$PowerShellGetTestsPath = "$ClonedProjectPath\Tests\"
196176
$PowerShellHome = Get-PSHome
197-
if($script:IsWindows){
177+
if ($script:IsWindows) {
198178
if ($script:PowerShellEdition -eq 'Core') {
199179
$PowerShellExePath = Join-Path -Path $PowerShellHome -ChildPath 'pwsh.exe'
200180
}
201181
else {
202182
$PowerShellExePath = Join-Path -Path $PowerShellHome -ChildPath 'PowerShell.exe'
203183
}
204-
} else {
184+
}
185+
else {
205186
$PowerShellExePath = 'pwsh'
206187
}
207188

@@ -211,21 +192,21 @@ function Invoke-PowerShellGetTest {
211192
# -- Where PowerShellGet module was installed from MyGet feed https://powershell.myget.org/F/powershellmodule/api/v2/
212193
# -- This option is used only for Daily builds
213194
$TestScenarios = @()
214-
if(($script:PowerShellEdition -eq 'Core') -and $IsFullTestPass -and $script:IsWindows){
195+
if (($script:PowerShellEdition -eq 'Core') -and $IsFullTestPass -and $script:IsWindows) {
215196
# Disabled NoUpdate test scenario on PWSH
216197
#$TestScenarios += 'NoUpdate'
217198
}
218199
# We should run PSCore_PSGet_TestRun first before updating the PowerShellGet module from current branch.
219200
$TestScenarios += 'Current'
220201

221202
$PesterTag = '' # Conveys all test priorities
222-
if(-not $IsFullTestPass){
203+
if (-not $IsFullTestPass) {
223204
$PesterTag = 'BVT' # Only BVTs
224205
}
225206

226207
$TestResults = @()
227208

228-
foreach ($TestScenario in $TestScenarios){
209+
foreach ($TestScenario in $TestScenarios) {
229210

230211
Write-Host "TestScenario: $TestScenario"
231212

@@ -292,26 +273,24 @@ function Invoke-PowerShellGetTest {
292273

293274
$FailedTestCount = 0
294275
$TestResults | ForEach-Object { $FailedTestCount += ([int]$_.'test-results'.failures) }
295-
if ($FailedTestCount)
296-
{
276+
if ($FailedTestCount) {
297277
throw "$FailedTestCount tests failed"
298278
}
299279
}
300280
# tests if we should run a daily build
301281
# returns true if the build is scheduled
302282
# or is a pushed tag
303-
function Test-DailyBuild{
283+
function Test-DailyBuild {
304284

305285
# https://docs.travis-ci.com/user/environment-variables/
306286
# TRAVIS_EVENT_TYPE: Indicates how the build was triggered.
307287
# One of push, pull_request, api, cron.
308288
$TRAVIS_EVENT_TYPE = [System.Environment]::GetEnvironmentVariable('TRAVIS_EVENT_TYPE')
309-
if(($env:PS_DAILY_BUILD -eq 'True') -or
310-
($env:APPVEYOR_SCHEDULED_BUILD -eq 'True') -or
311-
($env:APPVEYOR_REPO_TAG_NAME) -or
312-
($TRAVIS_EVENT_TYPE -eq 'cron') -or
313-
($TRAVIS_EVENT_TYPE -eq 'api'))
314-
{
289+
if (($env:PS_DAILY_BUILD -eq 'True') -or
290+
($env:APPVEYOR_SCHEDULED_BUILD -eq 'True') -or
291+
($env:APPVEYOR_REPO_TAG_NAME) -or
292+
($TRAVIS_EVENT_TYPE -eq 'cron') -or
293+
($TRAVIS_EVENT_TYPE -eq 'api')) {
315294
return $true
316295
}
317296

@@ -367,7 +346,7 @@ function Update-ModuleManifestFunctions {
367346

368347
# FunctionsToExport string needs to be array definition with function names surrounded by quotes.
369348
$formatedFunctionNames = @()
370-
foreach($function in $PublicPSGetFunctions.basename) {
349+
foreach ($function in $PublicPSGetFunctions.basename) {
371350
$function = "`'$function`'"
372351
$formatedFunctionNames += $function
373352
}
@@ -387,8 +366,8 @@ function Remove-ModuleManifestFunctions ($Path) {
387366
$rawFile = Get-Content -Path $Path -Raw
388367
$arrFile = Get-Content -Path $Path
389368

390-
$functionsStartPos = ($arrFile | Select-String -Pattern 'FunctionsToExport').LineNumber -1
391-
$functionsEndPos = ($arrFile | Select-String -Pattern 'VariablesToExport').LineNumber -2
369+
$functionsStartPos = ($arrFile | Select-String -Pattern 'FunctionsToExport').LineNumber - 1
370+
$functionsEndPos = ($arrFile | Select-String -Pattern 'VariablesToExport').LineNumber - 2
392371

393372
$functionsExportString = $arrFile[$functionsStartPos..$functionsEndPos] | Out-String
394373

@@ -398,7 +377,7 @@ function Remove-ModuleManifestFunctions ($Path) {
398377
}
399378
function Publish-ModuleArtifacts {
400379

401-
if(Test-Path -Path $ArtifactRoot) {
380+
if (Test-Path -Path $ArtifactRoot) {
402381
Remove-Item -Path $ArtifactRoot -Recurse -Force
403382
}
404383

@@ -420,17 +399,16 @@ function Publish-ModuleArtifacts {
420399
$artifactZipFile = Join-Path -Path $ArtifactRoot -ChildPath $zipFileName
421400
$tempZipfile = Join-Path -Path $TempPath -ChildPath $zipFileName
422401

423-
if($PSEdition -ne 'Core')
424-
{
402+
if ($PSEdition -ne 'Core') {
425403
Add-Type -assemblyname System.IO.Compression.FileSystem
426404
}
427405

428-
if(Test-Path -Path $tempZipfile) {
406+
if (Test-Path -Path $tempZipfile) {
429407
Remove-Item -Path $tempZipfile -Force
430408
}
431409

432410
Write-Verbose "Zipping module artifacts in $ArtifactRoot"
433-
[System.IO.Compression.ZipFile]::CreateFromDirectory($ArtifactRoot,$tempZipfile)
411+
[System.IO.Compression.ZipFile]::CreateFromDirectory($ArtifactRoot, $tempZipfile)
434412

435413
Move-Item -Path $tempZipfile -Destination $artifactZipFile -Force
436414
}
@@ -441,8 +419,7 @@ function Install-PublishedModule {
441419
$ModuleVersion = "$($PowerShellGetModuleInfo.Version)"
442420
$InstallLocation = Join-Path -Path $AllUsersModulesPath -ChildPath 'PowerShellGet'
443421

444-
if(($script:PowerShellEdition -eq 'Core') -or ($PSVersionTable.PSVersion -ge '5.0.0'))
445-
{
422+
if (($script:PowerShellEdition -eq 'Core') -or ($PSVersionTable.PSVersion -ge '5.0.0')) {
446423
$InstallLocation = Join-Path -Path $InstallLocation -ChildPath $ModuleVersion
447424
}
448425
New-Item -Path $InstallLocation -ItemType Directory -Force | Out-Null

0 commit comments

Comments
 (0)