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

Commit 6926ad4

Browse files
edyoungalerickson
authored andcommitted
Fix #78 (#421)
1 parent 87783e9 commit 6926ad4

File tree

8 files changed

+558
-530
lines changed

8 files changed

+558
-530
lines changed

Tests/PSGetTestUtils.psm1

Lines changed: 140 additions & 117 deletions
Large diffs are not rendered by default.

Tests/PSGetUpdateModule.Tests.ps1

Lines changed: 78 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,6 @@ function SuiteSetup {
4040
PSGetTestUtils\Uninstall-Module ContosoServer
4141
PSGetTestUtils\Uninstall-Module ContosoClient
4242

43-
if($PSEdition -ne 'Core')
44-
{
45-
$script:userName = "PSGetUser"
46-
$password = "Password1"
47-
$null = net user $script:userName $password /add
48-
$secstr = ConvertTo-SecureString $password -AsPlainText -Force
49-
$script:credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $script:userName, $secstr
50-
}
51-
$script:assertTimeOutms = 20000
5243
}
5344

5445
function SuiteCleanup {
@@ -63,17 +54,73 @@ function SuiteCleanup {
6354

6455
# Import the PowerShellGet provider to reload the repositories.
6556
$null = Import-PackageProvider -Name PowerShellGet -Force
57+
}
6658

67-
if($PSEdition -ne 'Core')
68-
{
69-
# Delete the user
70-
net user $script:UserName /delete | Out-Null
71-
# Delete the user profile
72-
$userProfile = (Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.LocalPath -match $script:UserName})
73-
if($userProfile)
74-
{
75-
RemoveItem $userProfile.LocalPath
76-
}
59+
Describe UpdateModuleFromAlternateRepo -Tags 'BVT' {
60+
BeforeAll {
61+
SuiteSetup
62+
}
63+
64+
AfterAll {
65+
SuiteCleanup
66+
}
67+
68+
AfterEach {
69+
PSGetTestUtils\Uninstall-Module ContosoServer
70+
PSGetTestUtils\Uninstall-Module ContosoClient
71+
}
72+
73+
It "Check that removing a slash from a repo doesn't break update" {
74+
$withSlash = "https://www.poshtestgallery.com/api/v2/"
75+
$noSlash = "https://www.poshtestgallery.com/api/v2"
76+
#Write-Host (Get-PSRepository | Out-String)
77+
(Get-PSRepository PSGallery).SourceLocation | Should Be $withSlash
78+
79+
Install-Module ContosoServer -RequiredVersion 1.0
80+
(Get-InstalledModule ContosoServer).RepositorySourceLocation | Should Be $withSlash
81+
#Write-Host (Get-InstalledModule ContosoServer -AllVersions | Format-List | Out-String)
82+
83+
# now update where PSGallery Source Location is
84+
Set-PSGallerySourceLocation -Location $noSlash
85+
#Write-Host (Get-PSRepository | Out-String)
86+
(Get-PSRepository PSGallery).SourceLocation | Should Be $noSlash
87+
88+
# reload powershellget to force-update cached repository info
89+
Import-Module PowerShellGet -Force
90+
91+
# now try and update module isntalled using other SourceLocation
92+
Update-Module ContosoServer -RequiredVersion 2.0 -ErrorAction Stop
93+
#Write-Host (Get-InstalledModule ContosoServer -AllVersions | Format-List | Out-String)
94+
(Get-InstalledModule ContosoServer).RepositorySourceLocation | Should Be $noSlash
95+
(Get-InstalledModule ContosoServer).Version | Should Be 2.0
96+
}
97+
98+
It "Check that adding a slash to a repo doesn't break update" {
99+
$withSlash = "https://www.poshtestgallery.com/api/v2/"
100+
$noSlash = "https://www.poshtestgallery.com/api/v2"
101+
#Write-Host (Get-PSRepository | Out-String)
102+
103+
Set-PSGallerySourceLocation -Location $noSlash
104+
105+
(Get-PSRepository PSGallery).SourceLocation | Should Be $noSlash
106+
107+
Install-Module ContosoServer -RequiredVersion 1.0
108+
(Get-InstalledModule ContosoServer).RepositorySourceLocation | Should Be $noSlash
109+
#Write-Host (Get-InstalledModule ContosoServer -AllVersions | Format-List | Out-String)
110+
111+
# now update where PSGallery Source Location is
112+
Set-PSGallerySourceLocation -Location $withSlash
113+
#Write-Host (Get-PSRepository | Out-String)
114+
(Get-PSRepository PSGallery).SourceLocation | Should Be $withSlash
115+
116+
# reload powershellget to force-update cached repository info
117+
Import-Module PowerShellGet -Force
118+
119+
# now try and update module isntalled using other SourceLocation
120+
Update-Module ContosoServer -RequiredVersion 2.0 -ErrorAction Stop
121+
#Write-Host (Get-InstalledModule ContosoServer -AllVersions | Format-List | Out-String)
122+
(Get-InstalledModule ContosoServer).RepositorySourceLocation | Should Be $withSlash
123+
(Get-InstalledModule ContosoServer).Version | Should Be 2.0
77124
}
78125
}
79126

@@ -402,7 +449,9 @@ Describe PowerShell.PSGet.UpdateModuleTests -Tags 'BVT','InnerLoop' {
402449
It "UpdateAllModules" {
403450
Install-Module ContosoClient -RequiredVersion 1.0
404451
Install-Module ContosoServer -RequiredVersion 1.0
405-
Update-Module
452+
Update-Module -ErrorVariable err -ErrorAction SilentlyContinue
453+
#if we have other modules not from test repo they will error, keep the noise down but complain about real problems
454+
$err | ? { $_.FullyQualifiedErrorId -notmatch "SourceNotFound" } | % { Write-Error $_ }
406455

407456
if(Test-ModuleSxSVersionSupport)
408457
{
@@ -648,22 +697,17 @@ Describe PowerShell.PSGet.UpdateModuleTests.P2 -Tags 'P2','OuterLoop' {
648697
#
649698
# Action: Install a module as admin and try to update it as non-admin user
650699
#
651-
# Expected Result: should fail with an error
700+
# Expected Result: should successfully save module to currentuser scope
652701
#
653-
It "AdminPrivilegesAreRequiredForUpdatingAllUsersModule" {
702+
It "AdminPrivilegesAreNotRequiredForUpdatingAllUsersModule" {
654703
Install-Module -Name ContosoServer -RequiredVersion 1.0
655-
$NonAdminConsoleOutput = Join-Path ([System.IO.Path]::GetTempPath()) 'nonadminconsole-out.txt'
656-
Start-Process "$PSHOME\PowerShell.exe" -ArgumentList '$null = Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Scope CurrentUser;
657-
$null = Import-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force;
658-
Update-Module -Name ContosoServer' `
659-
-Credential $script:credential `
660-
-Wait `
661-
-WorkingDirectory $PSHOME `
662-
-RedirectStandardOutput $NonAdminConsoleOutput
663-
waitFor {Test-Path $NonAdminConsoleOutput} -timeoutInMilliseconds $script:assertTimeOutms -exceptionMessage "Install-Module on non-admin console failed to complete"
664-
$content = Get-Content $NonAdminConsoleOutput
665-
Assert ($content -match "AdminPrivilegesAreRequiredForUpdate") "update-module should fail when non-admin user is trying to update a module installed to alluser scope, $content"
666-
RemoveItem $NonAdminConsoleOutput
704+
$content = Invoke-WithoutAdminPrivileges (@'
705+
Import-Module "{0}\PowerShellGet.psd1" -Force
706+
Update-Module -Name ContosoServer
707+
'@ -f (Get-Module PowerShellGet).ModuleBase)
708+
709+
$updatedModule = Get-InstalledModule ContosoServer
710+
Assert ($updatedModule.Version -gt 1.0) "Module wasn't updated"
667711
} `
668712
-Skip:$(
669713
$whoamiValue = (whoami)
@@ -672,7 +716,6 @@ Describe PowerShell.PSGet.UpdateModuleTests.P2 -Tags 'P2','OuterLoop' {
672716
($whoamiValue -eq "NT AUTHORITY\LOCAL SERVICE") -or
673717
($whoamiValue -eq "NT AUTHORITY\NETWORK SERVICE") -or
674718
($env:APPVEYOR_TEST_PASS -eq 'True') -or
675-
($PSEdition -eq 'Core') -or
676719
($PSVersionTable.PSVersion -lt '4.0.0')
677720
)
678721

Tests/PSGetUpdateScript.Tests.ps1

Lines changed: 33 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ function SuiteSetup {
1818
Import-Module "$PSScriptRoot\PSGetTestUtils.psm1" -WarningAction SilentlyContinue
1919
Import-Module "$PSScriptRoot\Asserts.psm1" -WarningAction SilentlyContinue
2020

21-
$script:ProgramFilesScriptsPath = Get-AllUsersScriptsPath
22-
$script:MyDocumentsScriptsPath = Get-CurrentUserScriptsPath
21+
$script:ProgramFilesScriptsPath = Get-AllUsersScriptsPath
22+
$script:MyDocumentsScriptsPath = Get-CurrentUserScriptsPath
2323
$script:PSGetLocalAppDataPath = Get-PSGetLocalAppDataPath
2424
$script:TempPath = Get-TempPath
2525

@@ -41,17 +41,6 @@ function SuiteSetup {
4141
Get-InstalledScript -Name Fabrikam-ServerScript -ErrorAction SilentlyContinue | Uninstall-Script -Force
4242
Get-InstalledScript -Name Fabrikam-ClientScript -ErrorAction SilentlyContinue | Uninstall-Script -Force
4343

44-
if($PSEdition -ne 'Core')
45-
{
46-
$script:userName = "PSGetUser"
47-
$password = "Password1"
48-
$null = net user $script:userName $password /add
49-
$secstr = ConvertTo-SecureString $password -AsPlainText -Force
50-
$script:credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $script:userName, $secstr
51-
}
52-
53-
$script:assertTimeOutms = 20000
54-
5544
# Create temp folder for saving the scripts
5645
$script:TempSavePath = Join-Path -Path $script:TempPath -ChildPath "PSGet_$(Get-Random)"
5746
$null = New-Item -Path $script:TempSavePath -ItemType Directory -Force
@@ -73,17 +62,6 @@ function SuiteCleanup {
7362
# Import the PowerShellGet provider to reload the repositories.
7463
$null = Import-PackageProvider -Name PowerShellGet -Force
7564

76-
if($PSEdition -ne 'Core')
77-
{
78-
# Delete the user
79-
net user $script:UserName /delete | Out-Null
80-
# Delete the user profile
81-
$userProfile = (Get-WmiObject -Class Win32_UserProfile | Where-Object {$_.LocalPath -match $script:UserName})
82-
if($userProfile)
83-
{
84-
RemoveItem $userProfile.LocalPath
85-
}
86-
}
8765
RemoveItem $script:TempSavePath
8866

8967

@@ -343,7 +321,7 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' {
343321
$DateTimeBeforeUpdate = Get-Date
344322
Update-Script Fabrikam-* -Force -ErrorVariable MyError
345323
Assert ($MyError.Count -eq 0) "There should not be any error when updating multiple scripts with wildcard in name, $MyError"
346-
324+
347325
$res = Get-InstalledScript -Name Fabrikam-ServerScript -MinimumVersion '1.1'
348326
Assert ($res -and ($res.Name -eq "Fabrikam-ServerScript") -and ($res.Version -gt [Version]"1.0")) "Update-Script should update when wildcard specified in name"
349327

@@ -415,33 +393,25 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' {
415393
#
416394
# Action: Install a script as admin and try to update it as non-admin user
417395
#
418-
# Expected Result: should fail with an error
396+
# Expected Result: should pass, installing the update in currentuser scope
419397
#
420-
It "AdminPrivilegesAreRequiredForUpdatingAllUsersScript" {
398+
It "AdminPrivilegesAreNotRequiredForUpdatingAllUsersScript" {
421399
Install-Script -Name Fabrikam-ServerScript -RequiredVersion 1.0 -Scope AllUsers
422-
$NonAdminConsoleOutput = Join-Path ([System.IO.Path]::GetTempPath()) 'nonadminconsole-out.txt'
423-
Start-Process "$PSHOME\PowerShell.exe" -ArgumentList '$null = Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force -Scope CurrentUser;
424-
$null = Import-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force;
425-
Update-Script -Name Fabrikam-ServerScript' `
426-
-Credential $script:credential `
427-
-Wait `
428-
-WorkingDirectory $PSHOME `
429-
-RedirectStandardOutput $NonAdminConsoleOutput
430-
431-
waitFor {Test-Path $NonAdminConsoleOutput} -timeoutInMilliseconds $script:assertTimeOutms -exceptionMessage "Install-Script on non-admin console failed to complete"
432-
$content = Get-Content $NonAdminConsoleOutput
433-
Assert ($content -match 'AdminPrivilegesAreRequiredForUpdate') "Update-Script should fail when non-admin user is trying to update a script installed to allusers scope, $content"
434-
RemoveItem $NonAdminConsoleOutput
435-
} `
400+
$content = Invoke-WithoutAdminPrivileges (@'
401+
Import-Module "{0}\PowerShellGet.psd1" -Force -Passthru | select ModuleBase
402+
Update-Script -Name Fabrikam-ServerScript
403+
'@ -f (Get-Module PowerShellGet).ModuleBase)
404+
405+
$updatedScript = Get-InstalledScript Fabrikam-ServerScript
406+
Assert ($updatedScript.Version -gt 1.0) "Update-Script failed to updated script running as non-admin: $content"
407+
} `
436408
-Skip:$(
437409
$whoamiValue = (whoami)
438-
439410
($whoamiValue -eq "NT AUTHORITY\SYSTEM") -or
440411
($whoamiValue -eq "NT AUTHORITY\LOCAL SERVICE") -or
441412
($whoamiValue -eq "NT AUTHORITY\NETWORK SERVICE") -or
442413
($env:APPVEYOR_TEST_PASS -eq 'True') -or
443-
($PSEdition -eq 'Core') -or
444-
($PSVersionTable.PSVersion -lt '4.0.0')
414+
($PSVersionTable.PSVersion -lt '4.0.0')
445415
)
446416

447417
# Purpose: UpdateScriptWithLowerReqVersionShouldNotUpdate
@@ -451,7 +421,7 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' {
451421
# Expected Result: Script should not be downgraded to 1.0
452422
#
453423
It "UpdateScriptWithLowerReqVersionShouldNotUpdate" {
454-
Install-Script Fabrikam-ServerScript
424+
Install-Script Fabrikam-ServerScript -Force
455425
Update-Script Fabrikam-ServerScript -RequiredVersion 1.0
456426
$res = Get-InstalledScript Fabrikam-ServerScript
457427
Assert ($res.Name -eq "Fabrikam-ServerScript" -and $res.Version -gt [Version]"1.0") "Update-Script should not downgrade the script version with -RequiredVersion, Name: $($res.Name), Version: $($res.Version)"
@@ -483,7 +453,8 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' {
483453
It "UpdateAllScripts" {
484454
Install-Script Fabrikam-ClientScript -RequiredVersion 1.0
485455
Install-Script Fabrikam-ServerScript -RequiredVersion 1.0
486-
Update-Script
456+
Update-Script -ErrorAction SilentlyContinue -ErrorVariable err
457+
$err | ? { $_.FullyQualifiedErrorId -notmatch "NoMatchFoundForCriteria"} | % { Write-Error $_ }
487458

488459
$res = Get-InstalledScript -Name Fabrikam-ServerScript,Fabrikam-ClientScript
489460
Assert (($res.Count -eq 2) -and ($res[0].Version -gt [Version]"1.0") -and ($res[1].Version -gt [Version]"1.0")) "Multiple script should be updated"
@@ -498,11 +469,11 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' {
498469
#
499470
It "UpdateMultipleScriptsWithForce" {
500471
Install-Script Fabrikam-ClientScript,Fabrikam-ServerScript
501-
502-
$MyError = $null
472+
473+
$MyError = $null
503474
Update-Script Fabrikam-ClientScript,Fabrikam-ServerScript -Force -ErrorVariable MyError
504475
Assert ($MyError.Count -eq 0) "There should not be any error from force update for multiple scripts, $MyError"
505-
476+
506477
$res = Get-InstalledScript Fabrikam-ServerScript
507478
Assert (($res.Name -eq 'Fabrikam-ServerScript') -and ($res.Version -gt [Version]"1.0")) "Update-Script should update when multiple scripts are specified"
508479

@@ -518,6 +489,7 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' {
518489
#
519490
It "UpdateScriptUnderCurrentUserScope" {
520491
$scriptName = 'Fabrikam-ServerScript'
492+
521493
Install-Script $scriptName -Scope CurrentUser -RequiredVersion 1.0
522494
Update-Script $scriptName
523495

@@ -531,17 +503,22 @@ Describe PowerShell.PSGet.UpdateScriptTests -Tags 'BVT','InnerLoop' {
531503
#
532504
# Action: Install a script with AllUsers scope then update it
533505
#
534-
# Expected Result: updated script should be under AllUsers windows powershell scripts folder
506+
# Expected Result: updated script should be under AllUsers windows powershell scripts folder for an admin on Windows Powershell, currentuser otherwise
535507
#
536508
It "UpdateScriptUnderAllUsersScope" {
537509
$scriptName = 'Fabrikam-ServerScript'
510+
$shouldBeInAllUsers = ($PSVersionTable.PSVersion -lt "5.0" -or $PSEdition -eq 'Desktop') # when running these tests we always need to be an admin
538511
Install-Script $scriptName -Scope AllUsers -RequiredVersion 1.0
539512
Update-Script $scriptName
540513

541514
$res = Get-InstalledScript $scriptName
542515

543-
Assert (($res.Name -eq $scriptName) -and ($res.Version -gt [Version]"1.0")) "Update-Script should update the script installed to current user scope, $res"
544-
AssertEquals $res.InstalledLocation $script:ProgramFilesScriptsPath "Update-Script should update the script installed to current user scope, updated script base: $($res.InstalledLocation)"
516+
Assert (($res.Name -eq $scriptName) -and ($res.Version -gt [Version]"1.0")) "Update-Script should update the script, $res"
517+
if ($shouldBeInAllUsers) {
518+
AssertEquals $res.InstalledLocation $script:ProgramFilesScriptsPath "Update-Script should put update in all users scope, but updated script base: $($res.InstalledLocation)"
519+
} else {
520+
AssertEquals $res.InstalledLocation $script:MyDocumentsScriptsPath "Update-Script should put update in current user scope, updated script base: $($res.InstalledLocation)"
521+
}
545522
}
546523
}
547524

@@ -578,11 +555,11 @@ Describe PowerShell.PSGet.UpdateScriptTests.P1 -Tags 'P1','OuterLoop' {
578555
$DepencyNames = $res1.Dependencies.Name
579556
$res2 = Find-Script -Name $ScriptName -IncludeDependencies -MaximumVersion "1.0" -MinimumVersion "0.1"
580557
Assert ($res2.Count -ge ($DepencyNames.Count+1)) "Find-Script with -IncludeDependencies returned wrong results, $res2"
581-
558+
582559
Install-Script -Name $ScriptName -MaximumVersion "1.0" -MinimumVersion "0.1"
583560
$ActualScriptDetails = Get-InstalledScript -Name $ScriptName -RequiredVersion $res1.Version
584561
AssertNotNull $ActualScriptDetails "$ScriptName script with dependencies is not installed properly"
585-
562+
586563
$NamesToUninstall += $res2.Name
587564

588565
$res2 | ForEach-Object {
@@ -595,7 +572,7 @@ Describe PowerShell.PSGet.UpdateScriptTests.P1 -Tags 'P1','OuterLoop' {
595572

596573
# Find the latest available version
597574
$res3 = Find-Script -Name $ScriptName -IncludeDependencies
598-
575+
599576
Update-Script -Name $ScriptName
600577

601578
$NamesToUninstall += $res3.Name
@@ -616,5 +593,5 @@ Describe PowerShell.PSGet.UpdateScriptTests.P1 -Tags 'P1','OuterLoop' {
616593
PowerShellGet\Uninstall-Module $_ -Force -ErrorAction SilentlyContinue
617594
}
618595
}
619-
} -Skip:$($PSVersionTable.PSVersion -lt '5.0.0')
596+
} -Skip:$($PSVersionTable.PSVersion -lt '5.0.0')
620597
}

0 commit comments

Comments
 (0)