Tests/MSFT_xRegistryResource.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
$ErrorActionPreference = 'Stop'

Get-Module MSFT_xRegistryResource | Remove-Module -Force
Import-Module $PSScriptRoot\..\DSCResources\MSFT_xRegistryResource\MSFT_xRegistryResource.psm1 -Prefix UnitTest

Describe 'MSFT_xRegistryResource' {
    BeforeAll {
        $rootPath = 'Software\__MSFT_xRegistryResource__'
        $rootPathWithDrive = "HKCU:\$rootPath"
        if (Test-Path -LiteralPath $rootPathWithDrive)
        {
            Remove-Item -LiteralPath $rootPathWithDrive -Recurse -Force
        }

        New-Item -Path $rootPathWithDrive
    }

    AfterAll {
        if (Test-Path -LiteralPath $rootPathWithDrive)
        {
            Remove-Item -LiteralPath $rootPathWithDrive -Recurse -Force
        }
    }

    It 'Supports keys containing forward slashes' {
        $keyName = 'Test/Key'
        $valueName = 'Testing'
        $valueData = 'TestValue'

        $scriptBlock = {
            Set-UnitTestTargetResource -Key $rootPathWithDrive\$keyName `
                                       -ValueName $valueName `
                                       -ValueData $valueData `
                                       -ValueType String `
                                       -Force $true `
                                       -ErrorAction Stop
        }

        $scriptBlock | Should Not Throw

        $regKey = (Get-Item HKCU:\).OpenSubKey("$rootPath\$keyName")
        
        $regKey | Should Not Be Null
        $regKey.GetValue($valueName) | Should Be $valueData
    }
}