Skip to content

Commit 7ed105e

Browse files
authored
Merge pull request #847 from SteveL-MSFT/reg-delete-nonexist
Fix registry resource to handle deleting non-existing reg value/key
2 parents b7a8702 + 2e55eb8 commit 7ed105e

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

registry/src/registry_helper.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,12 @@ impl RegistryHelper {
200200
Err(e) => return Err(e),
201201
};
202202
if let Some(value_name) = &self.config.value_name {
203-
reg_key.delete_value(value_name)?;
203+
match reg_key.delete_value(value_name) {
204+
Ok(()) | Err(value::Error::NotFound(_, _)) => {
205+
// if the value doesn't exist, we don't need to do anything
206+
},
207+
Err(e) => return Err(RegistryError::RegistryValue(e)),
208+
}
204209
} else {
205210
// to delete the key, we need to open the parent key first
206211
let parent_path = get_parent_key_path(&self.config.key_path);
@@ -215,7 +220,12 @@ impl RegistryHelper {
215220
return Err(RegistryError::Utf16Conversion("subkey_name".to_string()));
216221
};
217222

218-
parent_reg_key.delete(subkey_name, true)?;
223+
match parent_reg_key.delete(subkey_name, true) {
224+
Ok(()) | Err(key::Error::NotFound(_, _)) => {
225+
// if the subkey doesn't exist, we don't need to do anything
226+
},
227+
Err(e) => return Err(RegistryError::RegistryKey(e)),
228+
}
219229
}
220230
Ok(())
221231
}

registry/tests/registry.config.set.tests.ps1

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,50 @@ Describe 'registry config set tests' {
9999

100100
Remove-Item -Path 'HKCU:\1' -Recurse -ErrorAction Ignore
101101
}
102+
103+
It 'Should succeed when _exist is false and value does not exist' -Skip:(!$IsWindows) {
104+
$config = @{
105+
'$schema' = 'https://aka.ms/dsc/schemas/v3/bundled/config/document.json'
106+
resources = @(
107+
@{
108+
name = 'reg'
109+
type = 'Microsoft.Windows/Registry'
110+
properties = @{
111+
keyPath = 'HKCU'
112+
valueName = 'Test'
113+
valueData = @{
114+
String = 'Test'
115+
}
116+
_exist = $false
117+
}
118+
}
119+
)
120+
}
121+
122+
$out = dsc config set -i ($config | ConvertTo-Json -Depth 10) | ConvertFrom-Json
123+
$LASTEXITCODE | Should -Be 0
124+
$out.results[0].result.afterState._exist | Should -Be $false
125+
126+
Get-ItemProperty -Path 'HKCU:\1\2' -Name 'Test' -ErrorAction Ignore | Should -BeNullOrEmpty
127+
}
128+
129+
It 'Should succeed when _exist is false and key does not exist' -Skip:(!$IsWindows) {
130+
$config = @{
131+
'$schema' = 'https://aka.ms/dsc/schemas/v3/bundled/config/document.json'
132+
resources = @(
133+
@{
134+
name = 'reg'
135+
type = 'Microsoft.Windows/Registry'
136+
properties = @{
137+
keyPath = 'HKCU\1'
138+
_exist = $false
139+
}
140+
}
141+
)
142+
}
143+
144+
$out = dsc config set -i ($config | ConvertTo-Json -Depth 10) | ConvertFrom-Json
145+
$LASTEXITCODE | Should -Be 0
146+
$out.results[0].result.afterState._exist | Should -Be $false
147+
}
102148
}

0 commit comments

Comments
 (0)