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
1 change: 1 addition & 0 deletions dsc_lib/locales/en-us.toml
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ failedToParseEscapedStringLiteral = "Unable to parse escaped string literal"
parsingEscapedStringLiteral = "Parsing escaped string literal: %{value}"
parsingExpression = "Parsing expression"
unknownExpressionType = "Unknown expression type: %{kind}"
skippingExpressionProcessing = "Skipping expression processing"

[dscerror]
adapterNotFound = "Adapter not found"
Expand Down
2 changes: 2 additions & 0 deletions dsc_lib/src/configure/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct Context {
pub variables: Map<String, Value>,
pub start_datetime: DateTime<Local>,
pub restart_required: Option<Vec<RestartRequired>>,
pub process_expressions: bool,
}

impl Context {
Expand All @@ -37,6 +38,7 @@ impl Context {
variables: Map::new(),
start_datetime: chrono::Local::now(),
restart_required: None,
process_expressions: true,
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion dsc_lib/src/dscresources/dscresource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ impl DscResource {
};
configuration.resources.push(adapter_resource);
let config_json = serde_json::to_string(&configuration)?;
let configurator = Configurator::new(&config_json, crate::progress::ProgressFormat::None)?;
let mut configurator = Configurator::new(&config_json, crate::progress::ProgressFormat::None)?;
// don't process expressions again as they would have already been processed before being passed to the adapter
configurator.context.process_expressions = false;
Ok(configurator)
}
}
Expand Down
2 changes: 1 addition & 1 deletion dsc_lib/src/functions/equals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mod tests {
#[test]
fn int_notequal() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[equals(1,2]", &Context::new()).unwrap();
let result = parser.parse_and_execute("[equals(1,2)]", &Context::new()).unwrap();
assert_eq!(result, Value::Bool(false));
}

Expand Down
2 changes: 1 addition & 1 deletion dsc_lib/src/functions/less.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ mod tests {
#[test]
fn type_mismatch_string_number() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[lessOrEquals('5', 3)]", &Context::new());
let result = parser.parse_and_execute("[less('5', 3)]", &Context::new());
assert!(result.is_err());
assert!(result.unwrap_err().to_string().contains("Arguments must be of the same type"));
}
Expand Down
4 changes: 2 additions & 2 deletions dsc_lib/src/functions/max.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ mod tests {
#[test]
fn array() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[max(createArray(0, 3, 2, 7, 4)]", &Context::new()).unwrap();
let result = parser.parse_and_execute("[max(createArray(0, 3, 2, 7, 4))]", &Context::new()).unwrap();
assert_eq!(result, 7);
}

#[test]
fn array_single_value() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[max(createArray(0)]", &Context::new()).unwrap();
let result = parser.parse_and_execute("[max(createArray(0))]", &Context::new()).unwrap();
assert_eq!(result, 0);
}

Expand Down
4 changes: 2 additions & 2 deletions dsc_lib/src/functions/min.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,14 @@ mod tests {
#[test]
fn array() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[min(createArray(0, 3, 2, 5, 4)]", &Context::new()).unwrap();
let result = parser.parse_and_execute("[min(createArray(0, 3, 2, 5, 4))]", &Context::new()).unwrap();
assert_eq!(result, 0);
}

#[test]
fn array_single_value() {
let mut parser = Statement::new().unwrap();
let result = parser.parse_and_execute("[min(createArray(0)]", &Context::new()).unwrap();
let result = parser.parse_and_execute("[min(createArray(0))]", &Context::new()).unwrap();
assert_eq!(result, 0);
}

Expand Down
5 changes: 5 additions & 0 deletions dsc_lib/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ impl Statement {
/// This function will return an error if the statement fails to parse or execute.
pub fn parse_and_execute(&mut self, statement: &str, context: &Context) -> Result<Value, DscError> {
debug!("{}", t!("parser.parsingStatement", statement = statement));
if !context.process_expressions {
debug!("{}", t!("parser.skippingExpressionProcessing"));
return Ok(Value::String(statement.to_string()));
}

let Some(tree) = &mut self.parser.parse(statement, None) else {
return Err(DscError::Parser(t!("parser.failedToParse", statement = statement).to_string()));
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ class TestClassResource : BaseTestClass
{
$this.Prop1 = "ValueForProp1"
}
elseif ($this.Name -eq 'EchoBack')
{
# don't change the property, just echo it back
}
else
{
$this.Prop1 = $env:DSC_CONFIG_ROOT
Expand Down Expand Up @@ -98,11 +102,11 @@ class TestClassResource : BaseTestClass

static [TestClassResource[]] Export([bool]$UseExport)
{
if ($UseExport)
if ($UseExport)
{
return [TestClassResource]::Export()
}
else
else
{
$resultList = [List[TestClassResource]]::new()
$resultCount = 5
Expand Down
26 changes: 22 additions & 4 deletions powershell-adapter/Tests/powershellgroup.config.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Describe 'PowerShell adapter resource tests' {
}

It 'Get does not work on config when module does not exist' {

$yaml = @'
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
resources:
Expand Down Expand Up @@ -195,7 +195,7 @@ Describe 'PowerShell adapter resource tests' {
type: TestClassResource/TestClassResource
properties:
Name: 'TestClassResource1'
HashTableProp:
HashTableProp:
Name: 'DSCv3'
"@

Expand All @@ -218,7 +218,7 @@ Describe 'PowerShell adapter resource tests' {
type: TestClassResource/TestClassResource
properties:
Name: 'TestClassResource1'
HashTableProp:
HashTableProp:
Name: 'DSCv3'
"@

Expand Down Expand Up @@ -269,7 +269,7 @@ Describe 'PowerShell adapter resource tests' {
Credential:
UserName: 'User'
OtherProperty: 'Password'
"@
"@
$out = dsc config get -i $yaml 2>&1 | Out-String
$LASTEXITCODE | Should -Be 2
$out | Should -Not -BeNullOrEmpty
Expand Down Expand Up @@ -310,5 +310,23 @@ Describe 'PowerShell adapter resource tests' {
$out.resources[0].properties.result[0].Name | Should -Be "Object1"
$out.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1"
}

It 'Expressions get passed correctly to adapted resource' {
$yaml = @"
`$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: Class-resource Info
type: TestClassResource/TestClassResource
properties:
Name: EchoBack
Prop1: "[[this is a string literal]"
EnumProp: 'Expected'
"@
$out = dsc config get -i $yaml | ConvertFrom-Json
$LASTEXITCODE | Should -Be 0
$out.results.result.actualState.Name | Should -BeExactly 'EchoBack'
$out.results.result.actualState.Prop1 | Should -BeExactly '[this is a string literal]'
$out.results.result.actualState.EnumProp | Should -BeExactly 'Expected'
}
}

Loading