Skip to content

Commit 2c87561

Browse files
authored
Merge pull request #1031 from SteveL-MSFT/not-parse-again
Fix no parsing expressions again when passed to an adapted resource
2 parents ac77397 + a91df62 commit 2c87561

File tree

10 files changed

+45
-13
lines changed

10 files changed

+45
-13
lines changed

dsc_lib/locales/en-us.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ failedToParseEscapedStringLiteral = "Unable to parse escaped string literal"
442442
parsingEscapedStringLiteral = "Parsing escaped string literal: %{value}"
443443
parsingExpression = "Parsing expression"
444444
unknownExpressionType = "Unknown expression type: %{kind}"
445+
skippingExpressionProcessing = "Skipping expression processing"
445446

446447
[dscerror]
447448
adapterNotFound = "Adapter not found"

dsc_lib/src/configure/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub struct Context {
1919
pub variables: Map<String, Value>,
2020
pub start_datetime: DateTime<Local>,
2121
pub restart_required: Option<Vec<RestartRequired>>,
22+
pub process_expressions: bool,
2223
}
2324

2425
impl Context {
@@ -37,6 +38,7 @@ impl Context {
3738
variables: Map::new(),
3839
start_datetime: chrono::Local::now(),
3940
restart_required: None,
41+
process_expressions: true,
4042
}
4143
}
4244
}

dsc_lib/src/dscresources/dscresource.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,9 @@ impl DscResource {
114114
};
115115
configuration.resources.push(adapter_resource);
116116
let config_json = serde_json::to_string(&configuration)?;
117-
let configurator = Configurator::new(&config_json, crate::progress::ProgressFormat::None)?;
117+
let mut configurator = Configurator::new(&config_json, crate::progress::ProgressFormat::None)?;
118+
// don't process expressions again as they would have already been processed before being passed to the adapter
119+
configurator.context.process_expressions = false;
118120
Ok(configurator)
119121
}
120122
}

dsc_lib/src/functions/equals.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ mod tests {
4949
#[test]
5050
fn int_notequal() {
5151
let mut parser = Statement::new().unwrap();
52-
let result = parser.parse_and_execute("[equals(1,2]", &Context::new()).unwrap();
52+
let result = parser.parse_and_execute("[equals(1,2)]", &Context::new()).unwrap();
5353
assert_eq!(result, Value::Bool(false));
5454
}
5555

dsc_lib/src/functions/less.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ mod tests {
8282
#[test]
8383
fn type_mismatch_string_number() {
8484
let mut parser = Statement::new().unwrap();
85-
let result = parser.parse_and_execute("[lessOrEquals('5', 3)]", &Context::new());
85+
let result = parser.parse_and_execute("[less('5', 3)]", &Context::new());
8686
assert!(result.is_err());
8787
assert!(result.unwrap_err().to_string().contains("Arguments must be of the same type"));
8888
}

dsc_lib/src/functions/max.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ mod tests {
6969
#[test]
7070
fn array() {
7171
let mut parser = Statement::new().unwrap();
72-
let result = parser.parse_and_execute("[max(createArray(0, 3, 2, 7, 4)]", &Context::new()).unwrap();
72+
let result = parser.parse_and_execute("[max(createArray(0, 3, 2, 7, 4))]", &Context::new()).unwrap();
7373
assert_eq!(result, 7);
7474
}
7575

7676
#[test]
7777
fn array_single_value() {
7878
let mut parser = Statement::new().unwrap();
79-
let result = parser.parse_and_execute("[max(createArray(0)]", &Context::new()).unwrap();
79+
let result = parser.parse_and_execute("[max(createArray(0))]", &Context::new()).unwrap();
8080
assert_eq!(result, 0);
8181
}
8282

dsc_lib/src/functions/min.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ mod tests {
6969
#[test]
7070
fn array() {
7171
let mut parser = Statement::new().unwrap();
72-
let result = parser.parse_and_execute("[min(createArray(0, 3, 2, 5, 4)]", &Context::new()).unwrap();
72+
let result = parser.parse_and_execute("[min(createArray(0, 3, 2, 5, 4))]", &Context::new()).unwrap();
7373
assert_eq!(result, 0);
7474
}
7575

7676
#[test]
7777
fn array_single_value() {
7878
let mut parser = Statement::new().unwrap();
79-
let result = parser.parse_and_execute("[min(createArray(0)]", &Context::new()).unwrap();
79+
let result = parser.parse_and_execute("[min(createArray(0))]", &Context::new()).unwrap();
8080
assert_eq!(result, 0);
8181
}
8282

dsc_lib/src/parser/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ impl Statement {
4646
/// This function will return an error if the statement fails to parse or execute.
4747
pub fn parse_and_execute(&mut self, statement: &str, context: &Context) -> Result<Value, DscError> {
4848
debug!("{}", t!("parser.parsingStatement", statement = statement));
49+
if !context.process_expressions {
50+
debug!("{}", t!("parser.skippingExpressionProcessing"));
51+
return Ok(Value::String(statement.to_string()));
52+
}
53+
4954
let Some(tree) = &mut self.parser.parse(statement, None) else {
5055
return Err(DscError::Parser(t!("parser.failedToParse", statement = statement).to_string()));
5156
};

powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ class TestClassResource : BaseTestClass
7171
{
7272
$this.Prop1 = "ValueForProp1"
7373
}
74+
elseif ($this.Name -eq 'EchoBack')
75+
{
76+
# don't change the property, just echo it back
77+
}
7478
else
7579
{
7680
$this.Prop1 = $env:DSC_CONFIG_ROOT
@@ -98,11 +102,11 @@ class TestClassResource : BaseTestClass
98102

99103
static [TestClassResource[]] Export([bool]$UseExport)
100104
{
101-
if ($UseExport)
105+
if ($UseExport)
102106
{
103107
return [TestClassResource]::Export()
104108
}
105-
else
109+
else
106110
{
107111
$resultList = [List[TestClassResource]]::new()
108112
$resultCount = 5

powershell-adapter/Tests/powershellgroup.config.tests.ps1

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Describe 'PowerShell adapter resource tests' {
3434
}
3535

3636
It 'Get does not work on config when module does not exist' {
37-
37+
3838
$yaml = @'
3939
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
4040
resources:
@@ -195,7 +195,7 @@ Describe 'PowerShell adapter resource tests' {
195195
type: TestClassResource/TestClassResource
196196
properties:
197197
Name: 'TestClassResource1'
198-
HashTableProp:
198+
HashTableProp:
199199
Name: 'DSCv3'
200200
"@
201201

@@ -218,7 +218,7 @@ Describe 'PowerShell adapter resource tests' {
218218
type: TestClassResource/TestClassResource
219219
properties:
220220
Name: 'TestClassResource1'
221-
HashTableProp:
221+
HashTableProp:
222222
Name: 'DSCv3'
223223
"@
224224

@@ -269,7 +269,7 @@ Describe 'PowerShell adapter resource tests' {
269269
Credential:
270270
UserName: 'User'
271271
OtherProperty: 'Password'
272-
"@
272+
"@
273273
$out = dsc config get -i $yaml 2>&1 | Out-String
274274
$LASTEXITCODE | Should -Be 2
275275
$out | Should -Not -BeNullOrEmpty
@@ -310,5 +310,23 @@ Describe 'PowerShell adapter resource tests' {
310310
$out.resources[0].properties.result[0].Name | Should -Be "Object1"
311311
$out.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1"
312312
}
313+
314+
It 'Expressions get passed correctly to adapted resource' {
315+
$yaml = @"
316+
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
317+
resources:
318+
- name: Class-resource Info
319+
type: TestClassResource/TestClassResource
320+
properties:
321+
Name: EchoBack
322+
Prop1: "[[this is a string literal]"
323+
EnumProp: 'Expected'
324+
"@
325+
$out = dsc config get -i $yaml | ConvertFrom-Json
326+
$LASTEXITCODE | Should -Be 0
327+
$out.results.result.actualState.Name | Should -BeExactly 'EchoBack'
328+
$out.results.result.actualState.Prop1 | Should -BeExactly '[this is a string literal]'
329+
$out.results.result.actualState.EnumProp | Should -BeExactly 'Expected'
330+
}
313331
}
314332

0 commit comments

Comments
 (0)