Skip to content

Commit 60e97bc

Browse files
committed
update output and add tests
1 parent bcc1545 commit 60e97bc

File tree

4 files changed

+155
-50
lines changed

4 files changed

+155
-50
lines changed

dsc/tests/dsc_whatif.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Describe 'whatif tests' {
4141
$what_if_result.results.result.beforeState._exist | Should -Be $false
4242
$what_if_result.results.result.beforeState.keyPath | Should -Be $set_result.results.result.beforeState.keyPath
4343
$what_if_result.results.result.afterState.KeyPath | Should -Be $set_result.results.result.afterState.keyPath
44-
$what_if_result.results.result.changedProperties | Should -Be @('changeType', 'depth', '_exist')
44+
$what_if_result.results.result.changedProperties | Should -Be @('_exist')
4545
$what_if_result.hadErrors | Should -BeFalse
4646
$what_if_result.results.Count | Should -Be 1
4747
$LASTEXITCODE | Should -Be 0

registry/src/config.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,6 @@ pub struct Registry {
3636
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
3737
#[serde(deny_unknown_fields)]
3838
pub struct WhatIf {
39-
pub category: Action,
40-
#[serde(skip_serializing_if = "Option::is_none")]
41-
pub depth: Option<usize>,
42-
#[serde(rename = "proposedValueData", skip_serializing_if = "Option::is_none")]
43-
pub proposed_data: Option<RegistryValueData>,
4439
#[serde(skip_serializing_if = "Option::is_none")]
4540
pub message: Option<String>
4641
}
47-
48-
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
49-
#[serde(deny_unknown_fields)]
50-
pub enum Action {
51-
#[serde(rename = "clobber")]
52-
Clobber,
53-
#[serde(rename = "error")]
54-
Error,
55-
#[serde(rename = "new")]
56-
New
57-
}

registry/src/registry_helper.rs

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use registry::{Data, Hive, RegKey, Security, key, value};
55
use utfx::{U16CString, UCString};
6-
use crate::config::{Action, Registry, RegistryValueData, WhatIf};
6+
use crate::config::{Registry, RegistryValueData, WhatIf};
77
use crate::error::RegistryError;
88

99
pub struct RegistryHelper {
@@ -77,27 +77,29 @@ impl RegistryHelper {
7777
}
7878

7979
pub fn set(&self, is_what_if: bool) -> Result<Option<Registry>, RegistryError> {
80-
let mut depth = None;
81-
let mut proposed_data: Option<RegistryValueData> = None;
8280
let reg_key = match self.open(Security::Write) {
83-
Ok((reg_key, _subkey)) => reg_key,
81+
Ok((reg_key, _subkey)) => Some(reg_key),
8482
// handle NotFound error
8583
Err(RegistryError::RegistryKeyNotFound(_)) => {
8684
// if the key doesn't exist, some of the parent keys may
8785
// not exist either, so we need to find the valid parent key
8886
// and then create the subkeys that don't exist
89-
let (parent_key, subkeys) = self.get_valid_parent_key_and_subkeys()?;
90-
depth = Some(subkeys.len());
91-
let mut reg_key = parent_key;
92-
for subkey in subkeys {
93-
let Ok(path) = UCString::<u16>::from_str(subkey) else {
94-
return Err(RegistryError::Utf16Conversion("subkey".to_string()));
95-
};
96-
97-
reg_key = reg_key.create(path, Security::CreateSubKey)?;
87+
if is_what_if {
88+
None
9889
}
90+
else {
91+
let (parent_key, subkeys) = self.get_valid_parent_key_and_subkeys()?;
92+
let mut reg_key = parent_key;
93+
for subkey in subkeys {
94+
let Ok(path) = UCString::<u16>::from_str(subkey) else {
95+
return Err(RegistryError::Utf16Conversion("subkey".to_string()));
96+
};
9997

100-
self.open(Security::Write)?.0
98+
reg_key = reg_key.create(path, Security::CreateSubKey)?;
99+
}
100+
101+
Some(self.open(Security::Write)?.0)
102+
}
101103
},
102104
Err(e) => return self.handle_error_or_what_if(e, is_what_if)
103105
};
@@ -142,26 +144,24 @@ impl RegistryHelper {
142144
};
143145

144146
if is_what_if {
145-
proposed_data = Some(convert_reg_value(&data)?);
147+
return Ok(Some(Registry {
148+
key_path: self.config.key_path.clone(),
149+
value_data: Some(convert_reg_value(&data)?),
150+
value_name: self.config.value_name.clone(),
151+
what_if: Some(WhatIf { message: None }),
152+
..Default::default()
153+
}));
146154
}
147-
else {
155+
156+
if let Some(reg_key) = reg_key {
148157
reg_key.set_value(&value_name, &data)?;
149-
}
158+
};
150159
}
151160

152161
if is_what_if {
153-
let category = if let Some(name_exists) = self.get()?.exist {
154-
if name_exists { Action::Clobber }
155-
else { Action::New }
156-
} else { Action::Clobber };
157162
return Ok(Some(Registry {
158163
key_path: self.config.key_path.clone(),
159-
what_if: Some(WhatIf {
160-
category,
161-
proposed_data,
162-
depth,
163-
message: None
164-
}),
164+
what_if: Some(WhatIf { message: None }),
165165
..Default::default()
166166
}));
167167
}
@@ -241,15 +241,11 @@ impl RegistryHelper {
241241
}
242242

243243
fn handle_error_or_what_if(&self, error: RegistryError, is_what_if: bool) -> Result<Option<Registry>, RegistryError> {
244+
// TODO: return error message via metadata instead of as property within set state
244245
if is_what_if {
245246
return Ok(Some(Registry {
246247
key_path: self.config.key_path.clone(),
247-
what_if: Some(WhatIf {
248-
category: Action::Error,
249-
depth: None,
250-
proposed_data: None,
251-
message: Some(error.to_string())
252-
}),
248+
what_if: Some(WhatIf { message: Some(error.to_string()) }),
253249
..Default::default()
254250
}));
255251
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
Describe 'registry config whatif tests' {
5+
BeforeAll {
6+
Remove-Item -Path 'HKCU:\1' -Recurse -ErrorAction Ignore
7+
}
8+
9+
AfterEach {
10+
Remove-Item -Path 'HKCU:\1' -Recurse -ErrorAction Ignore
11+
}
12+
13+
It 'Can whatif a new deeply nested key' -Skip:(!$IsWindows) {
14+
$json = @'
15+
{
16+
"keyPath": "HKCU\\1\\2\\3"
17+
}
18+
'@
19+
$get_before = registry config get --input $json
20+
$result = registry config set -w --input $json | ConvertFrom-Json
21+
$LASTEXITCODE | Should -Be 0
22+
$result.keyPath | Should -Be 'HKCU\1\2\3'
23+
$get_after = registry config get --input $json
24+
$get_before | Should -EQ $get_after
25+
}
26+
27+
It 'Can whatif a new deeply nested key and value' -Skip:(!$IsWindows) {
28+
$json = @'
29+
{
30+
"keyPath": "HKCU\\1\\2\\3",
31+
"valueName": "Hello",
32+
"valueData": {
33+
"String": "World"
34+
}
35+
}
36+
'@
37+
$result = registry config set -w --input $json | ConvertFrom-Json
38+
$LASTEXITCODE | Should -Be 0
39+
$result.keyPath | Should -Be 'HKCU\1\2\3'
40+
$result.valueName | Should -Be 'Hello'
41+
$result.valueData.String | Should -Be 'World'
42+
}
43+
44+
It 'Can whatif an existing key with new value' -Skip:(!$IsWindows) {
45+
$set_json = @'
46+
{
47+
"keyPath": "HKCU\\1\\2"
48+
}
49+
'@
50+
registry config set --input $set_json
51+
$whatif_json = @'
52+
{
53+
"keyPath": "HKCU\\1\\2",
54+
"valueName": "Hello",
55+
"valueData": {
56+
"String": "World"
57+
}
58+
}
59+
'@
60+
$result = registry config set -w --input $whatif_json | ConvertFrom-Json
61+
$LASTEXITCODE | Should -Be 0
62+
$result.keyPath | Should -Be 'HKCU\1\2'
63+
$result.valueName | Should -Be 'Hello'
64+
$result.valueData.String | Should -Be 'World'
65+
}
66+
67+
It 'Can whatif an existing deeply nested key and value' -Skip:(!$IsWindows) {
68+
$set_json = @'
69+
{
70+
"keyPath": "HKCU\\1\\2\\3",
71+
"valueName": "Hello",
72+
"valueData": {
73+
"String": "World"
74+
}
75+
}
76+
'@
77+
registry config set --input $set_json
78+
$whatif_json = @'
79+
{
80+
"keyPath": "HKCU\\1\\2\\3",
81+
"valueName": "Hello",
82+
"valueData": {
83+
"String": "World-WhatIf"
84+
}
85+
}
86+
'@
87+
$result = registry config set -w --input $whatif_json | ConvertFrom-Json
88+
$LASTEXITCODE | Should -Be 0
89+
$result.keyPath | Should -Be 'HKCU\1\2\3'
90+
$result.valueName | Should -Be 'Hello'
91+
$result.valueData.String | Should -Be 'World-WhatIf'
92+
}
93+
94+
It 'Can whatif an existing key with nested values' -Skip:(!$IsWindows) {
95+
$set_json = @'
96+
{
97+
"keyPath": "HKCU\\1\\2\\3",
98+
"valueName": "Hello",
99+
"valueData": {
100+
"String": "World"
101+
}
102+
}
103+
'@
104+
registry config set --input $set_json
105+
$set_json = @'
106+
{
107+
"keyPath": "HKCU\\1\\2\\3",
108+
"valueName": "Foo",
109+
"valueData": {
110+
"String": "Bar"
111+
}
112+
}
113+
'@
114+
registry config set --input $set_json
115+
$whatif_json = @'
116+
{
117+
"keyPath": "HKCU\\1\\2"
118+
}
119+
'@
120+
$result = registry config set -w --input $whatif_json | ConvertFrom-Json
121+
$LASTEXITCODE | Should -Be 0
122+
$result.keyPath | Should -Be 'HKCU\1\2'
123+
($result.psobject.properties | Measure-Object).Count | Should -Be 1
124+
}
125+
}

0 commit comments

Comments
 (0)