Skip to content

Commit 383658a

Browse files
committed
add test, fix clippy
1 parent 4410b23 commit 383658a

File tree

3 files changed

+67
-21
lines changed

3 files changed

+67
-21
lines changed

dsc/tests/dsc_variables.tests.ps1

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# Licensed under the MIT License.
3+
4+
Describe 'Configruation variables tests' {
5+
It 'Variables example config works' {
6+
$configFile = "$PSSCriptRoot/../examples/variables.dsc.yaml"
7+
$out = dsc config get -p $configFile | ConvertFrom-Json
8+
$LASTEXITCODE | Should -Be 0
9+
$out.results[0].result.actualState.output | Should -BeExactly 'myOutput is: Hello world!, myObject is: baz'
10+
}
11+
12+
It 'Duplicated variable takes last value' {
13+
$configYaml = @'
14+
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
15+
variables:
16+
myVariable: foo
17+
myVariable: bar
18+
resources:
19+
- name: test
20+
type: Test/Echo
21+
properties:
22+
output: "[variables('myVariable')]"
23+
'@
24+
$out = dsc config get -d $configYaml | ConvertFrom-Json
25+
Write-Verbose -Verbose $out
26+
$LASTEXITCODE | Should -Be 0
27+
$out.results[0].result.actualState.output | Should -Be 'bar'
28+
}
29+
30+
It 'Missing variable returns error' {
31+
$configYaml = @'
32+
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
33+
variables:
34+
hello: world
35+
resources:
36+
- name: test
37+
type: Test/Echo
38+
properties:
39+
output: "[variables('myVariable')]"
40+
'@
41+
$out = dsc config get -d $configYaml 2>&1 | Out-String
42+
Write-Verbose -Verbose $out
43+
$LASTEXITCODE | Should -Be 2
44+
$out | Should -BeLike "*Variable 'myVariable' does not exist or has not been initialized yet*"
45+
}
46+
}

dsc_lib/src/configure/config_doc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use schemars::JsonSchema;
55
use serde::{Deserialize, Serialize};
66
use serde_json::{Map, Value};
7-
use std::collections::HashMap;
7+
use std::{collections::HashMap, hash::Hash};
88

99
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
1010
pub enum ContextKind {

dsc_lib/src/configure/mod.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ impl Configurator {
519519
} else {
520520
default_value.clone()
521521
};
522-
Configurator::validate_parameter_type(&name, &value, &parameter.parameter_type)?;
522+
Configurator::validate_parameter_type(name, &value, &parameter.parameter_type)?;
523523
self.context.parameters.insert(name.clone(), (value, parameter.parameter_type.clone()));
524524
}
525525
}
@@ -569,28 +569,28 @@ impl Configurator {
569569
Ok(())
570570
}
571571

572-
pub fn set_variables(&mut self, config: &Configuration) -> Result<(), DscError> {
573-
let Some(variables) = &config.variables else {
574-
debug!("No variables defined in configuration");
575-
return Ok(());
576-
};
577-
578-
for (name, value) in variables {
579-
let new_value = if let Some(string) = value.as_str() {
580-
self.statement_parser.parse_and_execute(string, &self.context)?
581-
}
582-
else {
583-
value.clone()
572+
fn set_variables(&mut self, config: &Configuration) -> Result<(), DscError> {
573+
let Some(variables) = &config.variables else {
574+
debug!("No variables defined in configuration");
575+
return Ok(());
584576
};
585-
info!("Set variable '{name}' to '{new_value}'");
586-
if self.context.variables.contains_key(name) {
587-
return Err(DscError::Validation(format!("Variable '{name}' defined mnore than once")));
588-
}
589577

590-
self.context.variables.insert(name.to_string(), new_value);
578+
for (name, value) in variables {
579+
let new_value = if let Some(string) = value.as_str() {
580+
self.statement_parser.parse_and_execute(string, &self.context)?
581+
}
582+
else {
583+
value.clone()
584+
};
585+
info!("Set variable '{name}' to '{new_value}'");
586+
if self.context.variables.contains_key(name) {
587+
return Err(DscError::Validation(format!("Variable '{name}' defined mnore than once")));
588+
}
589+
590+
self.context.variables.insert(name.to_string(), new_value);
591+
}
592+
Ok(())
591593
}
592-
Ok(())
593-
}
594594

595595
fn get_result_metadata(&self, operation: Operation) -> Metadata {
596596
let end_datetime = chrono::Local::now();

0 commit comments

Comments
 (0)