tests/functions/configuration/Register-PSFConfigValidation.Tests.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Describe "Register-PSFConfigValidation Unit Tests" -Tag "CI", "Pipeline", "Unit" {
    BeforeAll {
        Get-PSFConfig -Module Register-PSFConfigValidation -Force | ForEach-Object {
            $null = [PSFramework.Configuration.ConfigurationHost]::Configurations.Remove($_.FullName)
        }
    }
    AfterAll {
        Get-PSFConfig -Module Register-PSFConfigValidation -Force | ForEach-Object {
            $null = [PSFramework.Configuration.ConfigurationHost]::Configurations.Remove($_.FullName)
        }
    }
    
    # Catch any signature changes to force revisiting the command
    It "Should have the designed for parameters & sets" {
        (Get-Command Register-PSFConfigValidation).ParameterSets.Name | Should -Be '__AllParameterSets'
        $properties = 'Name', 'ScriptBlock', 'Verbose', 'Debug', 'ErrorAction', 'WarningAction', 'InformationAction', 'ErrorVariable', 'WarningVariable', 'InformationVariable', 'OutVariable', 'OutBuffer', 'PipelineVariable'
        Compare-Object $properties ((Get-Command Register-PSFConfigValidation).Parameters.Keys | Remove-PSFNull -Enumerate) | Should -BeNullOrEmpty
    }
    
    Context "Ensuring validation can be created, assigned and used" {
        #region Scriptblock
        $scriptblock = {
            Param (
                $Value
            )
            
            $Result = New-Object PSObject -Property @{
                Success = $True
                Value   = $null
                Message = ""
            }
            
            if ($Value -notin 0, 1)
            {
                $Result.Message = "Not a '0' or '1': $Value"
                $Result.Success = $False
                return $Result
            }
                        
            $Result.Value = [int]$Value
            
            return $Result
        }
        #endregion Scriptblock
        # Create Validation Rule that should be valid
        It "Should create the validation rule without an issue" {
            { Register-PSFConfigValidation -Name 'integer0or1' -ScriptBlock $scriptblock } | Should -Not -Throw
            [PSFramework.Configuration.ConfigurationHost]::Validation["integer0or1"] | Should -Be $scriptblock
        }
        
        # Assign to new setting
        It "Should correctly create new configuration values with this valdiation" {
            $config = Set-PSFConfig -FullName 'Register-PSFConfigValidation.Phase1.Setting1' -Value 1 -Validation 'integer0or1' -EnableException -PassThru
            $config.Validation | Should -Be $scriptblock
        }
        
        # Change Setting that should be legal
        It "Should accept legal input" {
            { Set-PSFConfig -FullName 'Register-PSFConfigValidation.Phase1.Setting1' -Value 0 -EnableException } | Should -Not -Throw
        }
        # Try change setting that should fail
        It "Should refuse illegal input" {
            { Set-PSFConfig -FullName 'Register-PSFConfigValidation.Phase1.Setting1' -Value 2 -EnableException } | Should -Throw
        }
    }
}