Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions registry/src/registry_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,12 @@ impl RegistryHelper {
Err(e) => return Err(e),
};
if let Some(value_name) = &self.config.value_name {
reg_key.delete_value(value_name)?;
match reg_key.delete_value(value_name) {
Ok(()) | Err(value::Error::NotFound(_, _)) => {
// if the value doesn't exist, we don't need to do anything
},
Err(e) => return Err(RegistryError::RegistryValue(e)),
}
} else {
// to delete the key, we need to open the parent key first
let parent_path = get_parent_key_path(&self.config.key_path);
Expand All @@ -215,7 +220,12 @@ impl RegistryHelper {
return Err(RegistryError::Utf16Conversion("subkey_name".to_string()));
};

parent_reg_key.delete(subkey_name, true)?;
match parent_reg_key.delete(subkey_name, true) {
Ok(()) | Err(key::Error::NotFound(_, _)) => {
// if the subkey doesn't exist, we don't need to do anything
},
Err(e) => return Err(RegistryError::RegistryKey(e)),
}
}
Ok(())
}
Expand Down
46 changes: 46 additions & 0 deletions registry/tests/registry.config.set.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,50 @@ Describe 'registry config set tests' {

Remove-Item -Path 'HKCU:\1' -Recurse -ErrorAction Ignore
}

It 'Should succeed when _exist is false and value does not exist' -Skip:(!$IsWindows) {
$config = @{
'$schema' = 'https://aka.ms/dsc/schemas/v3/bundled/config/document.json'
resources = @(
@{
name = 'reg'
type = 'Microsoft.Windows/Registry'
properties = @{
keyPath = 'HKCU'
valueName = 'Test'
valueData = @{
String = 'Test'
}
_exist = $false
}
}
)
}

$out = dsc config set -i ($config | ConvertTo-Json -Depth 10) | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.results[0].result.afterState._exist | Should -Be $false

Get-ItemProperty -Path 'HKCU:\1\2' -Name 'Test' -ErrorAction Ignore | Should -BeNullOrEmpty
}

It 'Should succeed when _exist is false and key does not exist' -Skip:(!$IsWindows) {
$config = @{
'$schema' = 'https://aka.ms/dsc/schemas/v3/bundled/config/document.json'
resources = @(
@{
name = 'reg'
type = 'Microsoft.Windows/Registry'
properties = @{
keyPath = 'HKCU\1'
_exist = $false
}
}
)
}

$out = dsc config set -i ($config | ConvertTo-Json -Depth 10) | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.results[0].result.afterState._exist | Should -Be $false
}
}
Loading