Skip to content

Commit

Permalink
Fix enum that does not work
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju committed Feb 17, 2024
1 parent 260cb53 commit 5ac2541
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 68 deletions.
27 changes: 18 additions & 9 deletions source/Classes/020.BootstrapPSResourceGet.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@
default value is 'CurrentUser'. This parameter may not be used at the same time
as the parameter Destination.
The value 'None' is not allowed to use in a configuration, it is used internally
and used in the output from method `Get()` to indicate that the module is not in
any scope.
.PARAMETER Version
Specifies the version of the Microsoft.PowerShell.PSResourceGet module to download.
If not specified, the latest version will be downloaded.
Expand Down Expand Up @@ -102,13 +98,15 @@ class BootstrapPSResourceGet : ResourceBase
$Destination

<#
The ModuleScope is evaluated if exist in AssertProperties().
The ModuleScope is evaluated in AssertProperties(). This parameter cannot
use the ValidateSet() attribute since it is not possible to set a null value,
unless it is set to [ValidateSet('CurrentUser', 'AllUsers', $null)] .
The parameter name Scope could not be used as it is a reserved keyword in
PowerShell DSC, if used it throws an error when parsing a configuration.
#>
[DscProperty()]
[Nullable[Scope]]
[System.String]
$ModuleScope

# The Version is evaluated if exist in AssertProperties().
Expand Down Expand Up @@ -182,7 +180,7 @@ class BootstrapPSResourceGet : ResourceBase
$this.localizedData.EvaluatingScope -f $assignedDscProperties.ModuleScope
)

$currentState.ModuleScope = 'None'
$currentState.ModuleScope = $null

$testModuleExistParameters.Scope = $assignedDscProperties.ModuleScope

Expand Down Expand Up @@ -270,9 +268,20 @@ class BootstrapPSResourceGet : ResourceBase

if ($property.Keys -contains 'ModuleScope')
{
if ($property.ModuleScope -eq [Scope]::None)
<#
It is not possible to set a null value to the parameter ModuleScope
when it has a [ValidateSet()] unless it would be set to
[ValidateSet('CurrentUser', 'AllUsers', $null)]. But that would
give a strange output if giving the wrong value to the parameter:
E.g.
'The argument "CurrentUser2" does not belong to the set
"CurrentUser,AllUsers," specified by the ValidateSet
attribute.'
#>
if ($property.ModuleScope -notin ('CurrentUser', 'AllUsers'))
{
$errorMessage = $this.localizedData.ScopeNoneNotAllowed
$errorMessage = $this.localizedData.ModuleScopeInvalid -f $property.ModuleScope

New-InvalidArgumentException -ArgumentName 'ModuleScope' -Message $errorMessage
}
Expand Down
6 changes: 0 additions & 6 deletions source/Enum/Scope.ps1

This file was deleted.

2 changes: 1 addition & 1 deletion source/en-US/BootstrapPSResourceGet.strings.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ ConvertFrom-StringData @'
EvaluatingDestination = Evaluating if module is present in the destination path '{0}'. (BPSRG0006)
VersionInvalid = The version '{0}' is not a valid semantic version or one of the supported NuGet version ranges. (BPSRG0007)
Bootstrapping = Bootstrapping the module Microsoft.PowerShell.PSResourceGet. (BPSRG0008)
ScopeNoneNotAllowed = The value 'None' is not allowed, it is used internally and returned in the output from Get(). (BPSRG0009)
MissingRequiredParameter = At least one of the parameters 'ModuleScope' or 'Destination' bust be specified. (BPSRG0010)
ModuleScopeInvalid = The module scope '{0}' is not a valid module scope. The value must be one of "CurrentUser" or "AllUsers". (BPSRG0011)
'@
118 changes: 66 additions & 52 deletions tests/Unit/Classes/BootstrapPSResourceGet.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Describe 'BootstrapPSResourceGet\Get()' -Tag 'Get' {
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetCurrentState' -Value {
return [System.Collections.Hashtable] @{
IsSingleInstance = 'Yes'
ModuleScope = [Scope]::CurrentUser
ModuleScope = 'CurrentUser'
}
} -PassThru |
Add-Member -Force -MemberType 'ScriptMethod' -Name 'AssertProperties' -Value {
Expand All @@ -106,7 +106,7 @@ Describe 'BootstrapPSResourceGet\Get()' -Tag 'Get' {
$currentState = $script:mockBootstrapPSResourceGetInstance.Get()

$currentState.IsSingleInstance | Should -Be 'Yes'
$currentState.ModuleScope | Should -Be ([Scope]::CurrentUser)
$currentState.ModuleScope | Should -Be 'CurrentUser'
$currentState.Destination | Should -BeNullOrEmpty
$currentState.Version | Should -BeNullOrEmpty
$currentState.Reasons | Should -BeNullOrEmpty
Expand All @@ -119,7 +119,7 @@ Describe 'BootstrapPSResourceGet\Get()' -Tag 'Get' {
InModuleScope -ScriptBlock {
$script:mockBootstrapPSResourceGetInstance = [BootstrapPSResourceGet] @{
IsSingleInstance = 'Yes'
ModuleScope = [Scope]::CurrentUser
ModuleScope = 'CurrentUser'
}

<#
Expand All @@ -133,7 +133,7 @@ Describe 'BootstrapPSResourceGet\Get()' -Tag 'Get' {
Add-Member -Force -MemberType 'ScriptMethod' -Name 'GetCurrentState' -Value {
return [System.Collections.Hashtable] @{
IsSingleInstance = 'Yes'
ModuleScope = 'None'
ModuleScope = $null
}
} -PassThru |
Add-Member -Force -MemberType 'ScriptMethod' -Name 'AssertProperties' -Value {
Expand All @@ -147,13 +147,13 @@ Describe 'BootstrapPSResourceGet\Get()' -Tag 'Get' {
$currentState = $script:mockBootstrapPSResourceGetInstance.Get()

$currentState.IsSingleInstance | Should -Be 'Yes'
$currentState.ModuleScope | Should -Be ([Scope]::None)
$currentState.ModuleScope | Should -BeNullOrEmpty
$currentState.Destination | Should -BeNullOrEmpty
$currentState.Version | Should -BeNullOrEmpty

$currentState.Reasons | Should -HaveCount 1
$currentState.Reasons.Code | Should -Be 'BootstrapPSResourceGet:BootstrapPSResourceGet:ModuleScope'
$currentState.Reasons.Phrase | Should -Be 'The property ModuleScope should be "CurrentUser", but was "None"'
$currentState.Reasons.Phrase | Should -Be 'The property ModuleScope should be "CurrentUser", but was null'
}
}
}
Expand All @@ -164,7 +164,7 @@ Describe 'BootstrapPSResourceGet\Set()' -Tag 'Set' {
InModuleScope -ScriptBlock {
$script:mockBootstrapPSResourceGetInstance = [BootstrapPSResourceGet] @{
IsSingleInstance = 'Yes'
ModuleScope = [Scope]::CurrentUser
ModuleScope = 'CurrentUser'
} |
# Mock method Modify which is called by the base method Set().
Add-Member -Force -MemberType 'ScriptMethod' -Name 'Modify' -Value {
Expand Down Expand Up @@ -213,8 +213,8 @@ Describe 'BootstrapPSResourceGet\Set()' -Tag 'Set' {
Add-Member -Force -MemberType 'ScriptMethod' -Name 'Compare' -Value {
return @{
Property = 'ModuleScope'
ExpectedValue = [Scope]::CurrentUser
ActualValue = [Scope]::None
ExpectedValue = 'CurrentUser'
ActualValue = $null
}
} -PassThru |
Add-Member -Force -MemberType 'ScriptMethod' -Name 'AssertProperties' -Value {
Expand All @@ -238,7 +238,7 @@ Describe 'BootstrapPSResourceGet\Test()' -Tag 'Test' {
InModuleScope -ScriptBlock {
$script:mockBootstrapPSResourceGetInstance = [BootstrapPSResourceGet] @{
IsSingleInstance = 'Yes'
ModuleScope = [Scope]::CurrentUser
ModuleScope = 'CurrentUser'
}
}
}
Expand Down Expand Up @@ -272,8 +272,8 @@ Describe 'BootstrapPSResourceGet\Test()' -Tag 'Test' {
Add-Member -Force -MemberType 'ScriptMethod' -Name 'Compare' -Value {
return @{
Property = 'ModuleScope'
ExpectedValue = [Scope]::CurrentUser
ActualValue = [Scope]::None
ExpectedValue = 'CurrentUser'
ActualValue = $null
}
} -PassThru |
Add-Member -Force -MemberType 'ScriptMethod' -Name 'AssertProperties' -Value {
Expand All @@ -297,7 +297,7 @@ Describe 'BootstrapPSResourceGet\GetCurrentState()' -Tag 'GetCurrentState' {
InModuleScope -ScriptBlock {
$script:mockBootstrapPSResourceGetInstance = [BootstrapPSResourceGet] @{
IsSingleInstance = 'Yes'
ModuleScope = [Scope]::CurrentUser
ModuleScope = 'CurrentUser'
}
}

Expand All @@ -315,7 +315,7 @@ Describe 'BootstrapPSResourceGet\GetCurrentState()' -Tag 'GetCurrentState' {
)

$currentState.IsSingleInstance | Should -Be 'Yes'
$currentState.ModuleScope | Should -Be ([Scope]::None)
$currentState.ModuleScope | Should -BeNullOrEmpty
}
}
}
Expand All @@ -325,7 +325,7 @@ Describe 'BootstrapPSResourceGet\GetCurrentState()' -Tag 'GetCurrentState' {
InModuleScope -ScriptBlock {
$script:mockBootstrapPSResourceGetInstance = [BootstrapPSResourceGet] @{
IsSingleInstance = 'Yes'
ModuleScope = [Scope]::CurrentUser
ModuleScope = 'CurrentUser'
}
}

Expand All @@ -343,7 +343,7 @@ Describe 'BootstrapPSResourceGet\GetCurrentState()' -Tag 'GetCurrentState' {
)

$currentState.IsSingleInstance | Should -Be 'Yes'
$currentState.ModuleScope | Should -Be ([Scope]::CurrentUser)
$currentState.ModuleScope | Should -Be 'CurrentUser'
}
}
}
Expand All @@ -355,7 +355,7 @@ Describe 'BootstrapPSResourceGet\GetCurrentState()' -Tag 'GetCurrentState' {
InModuleScope -ScriptBlock {
$script:mockBootstrapPSResourceGetInstance = [BootstrapPSResourceGet] @{
IsSingleInstance = 'Yes'
ModuleScope = [Scope]::AllUsers
ModuleScope = 'AllUsers'
}
}

Expand All @@ -373,7 +373,7 @@ Describe 'BootstrapPSResourceGet\GetCurrentState()' -Tag 'GetCurrentState' {
)

$currentState.IsSingleInstance | Should -Be 'Yes'
$currentState.ModuleScope | Should -Be ([Scope]::None)
$currentState.ModuleScope | Should -BeNullOrEmpty
}
}
}
Expand All @@ -383,7 +383,7 @@ Describe 'BootstrapPSResourceGet\GetCurrentState()' -Tag 'GetCurrentState' {
InModuleScope -ScriptBlock {
$script:mockBootstrapPSResourceGetInstance = [BootstrapPSResourceGet] @{
IsSingleInstance = 'Yes'
ModuleScope = [Scope]::AllUsers
ModuleScope = 'AllUsers'
}
}

Expand All @@ -401,7 +401,7 @@ Describe 'BootstrapPSResourceGet\GetCurrentState()' -Tag 'GetCurrentState' {
)

$currentState.IsSingleInstance | Should -Be 'Yes'
$currentState.ModuleScope | Should -Be ([Scope]::AllUsers)
$currentState.ModuleScope | Should -Be ('AllUsers')
}
}
}
Expand Down Expand Up @@ -557,7 +557,7 @@ Describe 'BootstrapPSResourceGet\GetCurrentState()' -Tag 'GetCurrentState' {

$currentState.IsSingleInstance | Should -Be 'Yes'
$currentState.Destination | Should -BeNullOrEmpty
$currentState.ModuleScope | Should -Be ([Scope]::None)
$currentState.ModuleScope | Should -BeNullOrEmpty
$currentState.Version | Should -BeNullOrEmpty
}
}
Expand Down Expand Up @@ -588,7 +588,7 @@ Describe 'BootstrapPSResourceGet\GetCurrentState()' -Tag 'GetCurrentState' {

$currentState.IsSingleInstance | Should -Be 'Yes'
$currentState.Destination | Should -BeNullOrEmpty
$currentState.ModuleScope | Should -Be ([Scope]::CurrentUser)
$currentState.ModuleScope | Should -Be 'CurrentUser'
$currentState.Version | Should -Be '1.0.0'
}
}
Expand Down Expand Up @@ -723,40 +723,13 @@ Describe 'BootstrapPSResourceGet\Modify()' -Tag 'Modify' {
}

Describe 'BootstrapPSResourceGet\AssertProperties()' -Tag 'AssertProperties' {
Context 'When passing parameter ModuleScope with the value ''None''' {
BeforeAll {
InModuleScope -ScriptBlock {
$script:mockBootstrapPSResourceGetInstance = [BootstrapPSResourceGet] @{
IsSingleInstance = 'Yes'
ModuleScope = 'None'
}
}
}

It 'Should throw the correct error' {
InModuleScope -ScriptBlock {
$mockErrorMessage = $script:mockBootstrapPSResourceGetInstance.localizedData.ScopeNoneNotAllowed

$mockErrorMessage += ' (Parameter ''ModuleScope'')'

{
$script:mockBootstrapPSResourceGetInstance.AssertProperties(
@{
ModuleScope = 'None'
}
)
} | Should -Throw -ExpectedMessage $mockErrorMessage
}
}
}

<#
These tests just check for the string localized ID. Since the error is part
of a command outside of SqlServerDsc, a small changes to the localized
string should not fail these tests.
of a command outside of PSResourceGet.Bootstrap a small change to the
localized string should not fail these tests.
#>
Context 'When passing mutually exclusive parameters' {
Context 'When passing ModuleScope and ModuleScope' {
Context 'When passing ModuleScope and Destination' {
BeforeAll {
InModuleScope -ScriptBlock {
$script:mockBootstrapPSResourceGetInstance = [BootstrapPSResourceGet] @{
Expand Down Expand Up @@ -953,4 +926,45 @@ Describe 'BootstrapPSResourceGet\AssertProperties()' -Tag 'AssertProperties' {
}
}
}

Context 'When an invalid value is passed in ModuleScope' {
BeforeAll {
InModuleScope -ScriptBlock {
$script:mockBootstrapPSResourceGetInstance = [BootstrapPSResourceGet] @{
IsSingleInstance = 'Yes'
ModuleScope = 'InvalidScope'
}
}
}

It 'Should throw the correct error for Get()' {
InModuleScope -ScriptBlock {
$mockErrorMessage = $script:mockBootstrapPSResourceGetInstance.localizedData.ModuleScopeInvalid -f 'InvalidScope'

$mockErrorMessage += ' (Parameter ''ModuleScope'')'

{ $script:mockBootstrapPSResourceGetInstance.Get() } | Should -Throw -ExpectedMessage $mockErrorMessage
}
}

It 'Should throw the correct error for Set()' {
InModuleScope -ScriptBlock {
$mockErrorMessage = $script:mockBootstrapPSResourceGetInstance.localizedData.ModuleScopeInvalid -f 'InvalidScope'

$mockErrorMessage += ' (Parameter ''ModuleScope'')'

{ $script:mockBootstrapPSResourceGetInstance.Set() } | Should -Throw -ExpectedMessage $mockErrorMessage
}
}

It 'Should throw the correct error for Test()' {
InModuleScope -ScriptBlock {
$mockErrorMessage = $script:mockBootstrapPSResourceGetInstance.localizedData.ModuleScopeInvalid -f 'InvalidScope'

$mockErrorMessage += ' (Parameter ''ModuleScope'')'

{ $script:mockBootstrapPSResourceGetInstance.Test() } | Should -Throw -ExpectedMessage $mockErrorMessage
}
}
}
}

0 comments on commit 5ac2541

Please sign in to comment.