From 0dbf83192e8283f13733926436fc18a031bfa339 Mon Sep 17 00:00:00 2001 From: Keith Jackson Date: Mon, 4 Oct 2021 22:34:06 -0500 Subject: [PATCH] Fixed failing tests on Windows when not an admin --- test/InstallPSResource.Tests.ps1 | 2 +- test/PSGetTestUtils.psm1 | 9 +++++++++ test/UpdatePSResource.Tests.ps1 | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/test/InstallPSResource.Tests.ps1 b/test/InstallPSResource.Tests.ps1 index ef206fece..4a61f1549 100644 --- a/test/InstallPSResource.Tests.ps1 +++ b/test/InstallPSResource.Tests.ps1 @@ -143,7 +143,7 @@ Describe 'Test Install-PSResource for Module' { } # Windows only - It "Install resource under AllUsers scope - Windows only" -Skip:(!(Get-IsWindows)) { + It "Install resource under AllUsers scope - Windows only" -Skip:(!((Get-IsWindows) -and (Test-IsAdmin))) { Install-PSResource -Name "TestModule" -Repository $TestGalleryName -Scope AllUsers $pkg = Get-Module "TestModule" -ListAvailable $pkg.Name | Should -Be "TestModule" diff --git a/test/PSGetTestUtils.psm1 b/test/PSGetTestUtils.psm1 index 4cae5c5ec..6b83bd5d4 100644 --- a/test/PSGetTestUtils.psm1 +++ b/test/PSGetTestUtils.psm1 @@ -100,6 +100,15 @@ $script:moduleSourcesFilePath = Microsoft.PowerShell.Management\Join-Path -Path $script:CurrentPSGetFormatVersion = "1.0" $script:PSGetFormatVersionPrefix = "PowerShellGetFormatVersion_" +function Test-IsAdmin { + [OutputType([bool])] + param() + + [System.Security.Principal.WindowsPrincipal]::new( + [Security.Principal.WindowsIdentity]::GetCurrent() + ).IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator) +} + function Get-IsWindows { return $script:IsWindows } diff --git a/test/UpdatePSResource.Tests.ps1 b/test/UpdatePSResource.Tests.ps1 index 12c84574c..834823bbe 100644 --- a/test/UpdatePSResource.Tests.ps1 +++ b/test/UpdatePSResource.Tests.ps1 @@ -177,7 +177,7 @@ Describe 'Test Update-PSResource' { } # Windows only - It "update resource under AllUsers scope" -skip:(!$IsWindows) { + It "update resource under AllUsers scope" -skip:(!($IsWindows -and (Test-IsAdmin))) { Install-PSResource -Name "TestModule" -Version "1.1.0.0" -Repository $TestGalleryName -Scope AllUsers Install-PSResource -Name "TestModule" -Version "1.1.0.0" -Repository $TestGalleryName -Scope CurrentUser