Skip to content

Commit

Permalink
Refactoring and improved test coverage
Browse files Browse the repository at this point in the history
Forgot to finish de-duping the DSC resource code earlier, that's done now.

Ran some coverage analysis and improved coverage a bit with some small tweaks.  Up to around 75% now.
  • Loading branch information
dlwyatt committed Apr 1, 2015
1 parent 2d0bbd6 commit 3068669
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,53 +74,16 @@ function Test-TargetResource
[Microsoft.Win32.RegistryValueKind] $Type = [Microsoft.Win32.RegistryValueKind]::String
)

if ($null -eq $Data) { $Data = @() }

try
{
Assert-ValidDataAndType -Data $Data -Type $Type
$path = GetPolFilePath -PolicyType $PolicyType
return TestTargetResourceCommon -Path $path -KeyValueName $KeyValueName -Ensure $Ensure -Data $Data -Type $Type
}
catch
{
Write-Error -ErrorRecord $_
return
}

$path = GetPolFilePath -PolicyType $PolicyType
$key, $valueName = ParseKeyValueName $KeyValueName

$fileExists = Test-Path -LiteralPath $path -PathType Leaf

if ($Ensure -eq 'Present')
{
if (-not $fileExists) { return $false }
$entry = Get-PolicyFileEntry -Path $path -Key $key -ValueName $valueName

return $null -ne $entry -and $Type -eq $entry.Type -and (DataIsEqual $entry.Data $Data -Type $Type)
}
else # Ensure is 'Absent'
{
if (-not $fileExists) { return $true }
$entry = Get-PolicyFileEntry -Path $path -Key $key -ValueName $valueName

return $null -eq $entry
}
}

function Assert-ValidDataAndType
{
param (
[string[]] $Data,
[Microsoft.Win32.RegistryValueKind] $Type
)

if ($Type -ne [Microsoft.Win32.RegistryValueKind]::MultiString -and
$Type -ne [Microsoft.Win32.RegistryValueKind]::Binary -and
$Data.Count -gt 1)
{
throw 'Do not pass arrays with multiple values to the -Data parameter when -Type is not set to either Binary or MultiString.'
}

}

Export-ModuleMember Get-TargetResource, Test-TargetResource, Set-TargetResource
43 changes: 41 additions & 2 deletions PolicyFileEditor.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ try
New-Object psobject -Property @{
Key = 'Software\Testing'
ValueName = 'Value2'
Type = 'String'
Data = 'Data'
Type = 'MultiString'
Data = 'Multi', 'String', 'Data'
}
)
}
Expand Down Expand Up @@ -389,6 +389,45 @@ try
}

}

It 'Gets values by Key and PropertyName successfully' {
$polPath = Join-Path $gpoPath Machine\registry.pol
$key = 'Software\Testing'
$valueName = 'TestValue'
$data = 'I am a string'
$type = ([Microsoft.Win32.RegistryValueKind]::String)

$scriptBlock = {
Set-PolicyFileEntry -Path $polPath `
-Key $key `
-ValueName $valueName `
-Data $data `
-Type $type
}

$scriptBlock | Should Not Throw

$entry = Get-PolicyFileEntry -Path $polPath -Key $key -ValueName $valueName

$entry | Should Not Be $null
$entry.ValueName | Should Be $valueName
$entry.Key | Should Be $key
$entry.Type | Should Be $type
$entry.Data | Should Be $data
}
}

Context 'Automatic creation of gpt.ini' {
It 'Creates a gpt.ini file if one is not found' {
Remove-Item $gptIniPath

$path = Join-Path $gpoPath Machine\registry.pol

Set-PolicyFileEntry -Path $path -Key 'Whatever' -ValueName 'Whatever' -Data 'Whatever' -Type String

$gptIniPath | Should Exist
GetGptIniVersion -Path $gptIniPath | Should Be 1
}
}
}

Expand Down

0 comments on commit 3068669

Please sign in to comment.