From 3ceca01b648ffc61192fabe9f6164cf4e246dba1 Mon Sep 17 00:00:00 2001 From: James Truher Date: Mon, 29 Aug 2016 15:00:26 -0700 Subject: [PATCH] address some review feedback --- test/powershell/Common/Test.Helpers.psm1 | 26 ++++++++++++++++++- .../Scripting/Debugging/Debugging.Tests.ps1 | 5 ++-- .../Set-Date.Tests.ps1 | 15 ++--------- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/test/powershell/Common/Test.Helpers.psm1 b/test/powershell/Common/Test.Helpers.psm1 index b168c9791..a83bae4ef 100644 --- a/test/powershell/Common/Test.Helpers.psm1 +++ b/test/powershell/Common/Test.Helpers.psm1 @@ -22,4 +22,28 @@ function Wait-CompleteExecution } return $true } -export-modulemember -function Wait-CompleteExecution + +function Test-IsElevated +{ + $IsElevated = $False + if ( $IsWindows ) { + # on Windows we can determine whether we're executing in an + # elevated context + $identity = [System.Security.Principal.WindowsIdentity]::GetCurrent() + $windowsPrincipal = new-object 'Security.Principal.WindowsPrincipal' $identity + if ($windowsPrincipal.IsInRole("Administrators") -eq 1) + { + $IsElevated = $true + } + } + else { + # on Linux, tests run via sudo will generally report "root" for whoami + if ( (whoami) -match "root" ) { + $IsElevated = $true + } + } + return $IsElevated +} + +export-modulemember -function Wait-CompleteExecution,Test-IsElevated + diff --git a/test/powershell/Language/Scripting/Debugging/Debugging.Tests.ps1 b/test/powershell/Language/Scripting/Debugging/Debugging.Tests.ps1 index 88c55cee3..90ed52764 100644 --- a/test/powershell/Language/Scripting/Debugging/Debugging.Tests.ps1 +++ b/test/powershell/Language/Scripting/Debugging/Debugging.Tests.ps1 @@ -12,9 +12,8 @@ $script2 = @' "line 3" '@ -$root = git rev-parse --show-toplevel -$pestertestroot = join-path $root test/powershell -$common = join-path $pestertestroot Common +$testroot = resolve-path (join-path $psscriptroot ../../..) +$common = join-path $testroot Common $helperModule = join-path $common Test.Helpers.psm1 Describe "Breakpoints when set should be hit" -tag "CI" { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Set-Date.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Set-Date.Tests.ps1 index 47eccb785..d523146dd 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Set-Date.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Set-Date.Tests.ps1 @@ -3,19 +3,8 @@ # determine whether we're elevated Describe "Set-Date" -Tag "CI" { BeforeAll { - if ( $IsWindows ) { - $identity = [System.Security.Principal.WindowsIdentity]::GetCurrent() - $windowsPrincipal = new-object 'Security.Principal.WindowsPrincipal' $identity - if ($windowsPrincipal.IsInRole("Administrators") -eq 1) { $IsElevated = $true } else { $IsElevated = $false } - } - else { - if ( (whoami) -match "root" ) { - $IsElevated = $true - } - else { - $IsElevated = $false - } - } + Import-Module (join-path $psscriptroot "../../Common/Test.Helpers.psm1") + $IsElevated = Test-IsElevated } It "Set-Date should be able to set the date in an elevated context" -Skip:(! $IsElevated) { { get-date | set-date } | Should not throw