Skip to content

Commit

Permalink
Assert-ElevatedUser - Add custom error message parameter (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-hughes authored Sep 30, 2024
1 parent d46fe33 commit 00506d4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Public command:
- `Get-UserName` - get current user name cross platform.

### Changed

- `Assert-ElevatedUser`
- Add new parameter `ErrorMessage` to allow custom error messages.

### Fixed

- `Get-PSModulePath`
Expand Down
17 changes: 15 additions & 2 deletions source/Public/Assert-ElevatedUser.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,19 @@
throw a statement-terminating error if the script is not run from an elevated
session.
.PARAMETER ErrorMessage
The error message to assign to the exception.
.EXAMPLE
Assert-ElevatedUser
Throws an exception if the user has not elevated the PowerShell session.
.EXAMPLE
Assert-ElevatedUser -ErrorMessage 'A custom error message to throw'
Throws an exception if the user has not elevated the PowerShell session.
.EXAMPLE
`Assert-ElevatedUser -ErrorAction 'Stop'`
Expand All @@ -24,7 +32,12 @@
function Assert-ElevatedUser
{
[CmdletBinding()]
param ()
param (
[Parameter()]
[ValidateNotNullOrEmpty()]
[System.String]
$ErrorMessage = $script:localizedData.ElevatedUser_UserNotElevated
)

$isElevated = $false

Expand All @@ -43,7 +56,7 @@ function Assert-ElevatedUser
{
$PSCmdlet.ThrowTerminatingError(
[System.Management.Automation.ErrorRecord]::new(
$script:localizedData.ElevatedUser_UserNotElevated,
$ErrorMessage,
'UserNotElevated',
[System.Management.Automation.ErrorCategory]::InvalidOperation,
'Command parameters'
Expand Down
29 changes: 23 additions & 6 deletions tests/Unit/Public/Assert-ElevatedUser.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,33 @@ Describe 'Assert-ElevatedUser' -Tag 'Public' {
}
}

It 'Should throw the correct error' -Skip:$mockIsElevated {
$mockErrorMessage = InModuleScope -ScriptBlock {
$script:localizedData.ElevatedUser_UserNotElevated
Context 'When not supplying an error message' {
It 'Should throw the correct error' -Skip:$mockIsElevated {
$mockErrorMessage = InModuleScope -ScriptBlock {
$script:localizedData.ElevatedUser_UserNotElevated
}

{ Assert-ElevatedUser } | Should -Throw -ExpectedMessage $mockErrorMessage
}

{ Assert-ElevatedUser } | Should -Throw -ExpectedMessage $mockErrorMessage
It 'Should not throw an exception' -Skip:(-not $mockIsElevated) {
{ Assert-ElevatedUser } | Should -Not -Throw
}
}

It 'Should not throw an exception' -Skip:(-not $mockIsElevated) {
{ Assert-ElevatedUser } | Should -Not -Throw
Context 'When supplying a custom error message' {
BeforeAll {
$mockCustomMessage = 'This is a custom error message'
}

It 'Should throw the correct error' -Skip:$mockIsElevated {
{ Assert-ElevatedUser -ErrorMessage $mockCustomMessage } | Should -Throw -ExpectedMessage $mockCustomMessage
}


It 'Should not throw an exception' -Skip:(-not $mockIsElevated) {
{ Assert-ElevatedUser -ErrorMessage $mockCustomMessage} | Should -Not -Throw
}
}

Context 'When on Linux or macOS' {
Expand Down

0 comments on commit 00506d4

Please sign in to comment.