Skip to content

Commit 6db080b

Browse files
author
Steve Lee (POWERSHELL HE/HIM) (from Dev Box)
committed
address Andrew and Tess' feedback
1 parent 6218dcf commit 6db080b

File tree

2 files changed

+34
-80
lines changed

2 files changed

+34
-80
lines changed

dsc/tests/dsc_discovery.tests.ps1

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,14 @@ Describe 'tests for resource discovery' {
7272
$manifest = @'
7373
{
7474
"$schema": "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/bundled/resource/manifest.json",
75-
"type": "Test/Echo",
75+
"type": "Test/InvalidSemver",
7676
"version": "1.1.0..1",
7777
"get": {
78-
"executable": "dsctest",
79-
"args": [
80-
"echo"
81-
]
78+
"executable": "dsctest"
8279
},
8380
"schema": {
8481
"command": {
85-
"executable": "dsctest",
86-
"args": [
87-
"schema",
88-
"-s",
89-
"echo"
90-
]
82+
"executable": "dsctest"
9183
}
9284
}
9385
}
@@ -97,6 +89,7 @@ Describe 'tests for resource discovery' {
9789
$env:DSC_RESOURCE_PATH = $testdrive
9890
Set-Content -Path "$testdrive/test.dsc.resource.json" -Value $manifest
9991
$out = dsc resource list 2>&1
92+
write-verbose -verbose ($out | Out-String)
10093
$out | Should -Match 'WARN.*?Validation.*?Invalid manifest.*?version'
10194
}
10295
finally {

dsc_lib/src/dscresources/command_resource.rs

Lines changed: 30 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -141,23 +141,11 @@ pub fn invoke_set(resource: &ResourceManifest, cwd: &str, desired: &str, skip_te
141141
}
142142
}
143143

144-
let mut get_env: Option<HashMap<String, String>> = None;
145-
let mut get_input: Option<&str> = None;
146144
let args = process_args(&resource.get.args, desired);
147-
match &resource.get.input {
148-
Some(InputKind::Env) => {
149-
get_env = Some(json_to_hashmap(desired)?);
150-
},
151-
Some(InputKind::Stdin) => {
152-
get_input = Some(desired);
153-
},
154-
None => {
155-
// leave input as none
156-
},
157-
}
145+
let (get_env, get_input) = get_env_stdin(&resource.get.input, desired)?;
158146

159147
info!("Getting current state for set by invoking get {} using {}", &resource.resource_type, &resource.get.executable);
160-
let (exit_code, stdout, stderr) = invoke_command(&resource.get.executable, args, get_input, Some(cwd), get_env)?;
148+
let (exit_code, stdout, stderr) = invoke_command(&resource.get.executable, args, get_input.as_deref(), Some(cwd), get_env)?;
161149
log_resource_traces(&stderr);
162150
if exit_code != 0 {
163151
return Err(DscError::Command(resource.resource_type.clone(), exit_code, stderr));
@@ -282,23 +270,11 @@ pub fn invoke_test(resource: &ResourceManifest, cwd: &str, expected: &str) -> Re
282270

283271
verify_json(resource, cwd, expected)?;
284272

285-
let mut env: Option<HashMap<String, String>> = None;
286-
let mut input_expected: Option<&str> = None;
287273
let args = process_args(&test.args, expected);
288-
match &test.input {
289-
Some(InputKind::Env) => {
290-
env = Some(json_to_hashmap(expected)?);
291-
},
292-
Some(InputKind::Stdin) => {
293-
input_expected = Some(expected);
294-
},
295-
None => {
296-
// leave input as none
297-
},
298-
}
274+
let (env, input_expected) = get_env_stdin(&test.input, expected)?;
299275

300276
info!("Invoking test '{}' using '{}'", &resource.resource_type, &test.executable);
301-
let (exit_code, stdout, stderr) = invoke_command(&test.executable, args, input_expected, Some(cwd), env)?;
277+
let (exit_code, stdout, stderr) = invoke_command(&test.executable, args, input_expected.as_deref(), Some(cwd), env)?;
302278
log_resource_traces(&stderr);
303279
if exit_code != 0 {
304280
return Err(DscError::Command(resource.resource_type.clone(), exit_code, stderr));
@@ -412,23 +388,11 @@ pub fn invoke_delete(resource: &ResourceManifest, cwd: &str, filter: &str) -> Re
412388

413389
verify_json(resource, cwd, filter)?;
414390

415-
let mut env: Option<HashMap<String, String>> = None;
416-
let mut input_filter: Option<&str> = None;
417391
let args = process_args(&delete.args, filter);
418-
match &delete.input {
419-
Some(InputKind::Env) => {
420-
env = Some(json_to_hashmap(filter)?);
421-
},
422-
Some(InputKind::Stdin) => {
423-
input_filter = Some(filter);
424-
},
425-
None => {
426-
// leave input as none
427-
},
428-
}
392+
let (env, input_filter) = get_env_stdin(&delete.input, filter)?;
429393

430394
info!("Invoking delete '{}' using '{}'", &resource.resource_type, &delete.executable);
431-
let (exit_code, _stdout, stderr) = invoke_command(&delete.executable, args, input_filter, Some(cwd), env)?;
395+
let (exit_code, _stdout, stderr) = invoke_command(&delete.executable, args, input_filter.as_deref(), Some(cwd), env)?;
432396
log_resource_traces(&stderr);
433397
if exit_code != 0 {
434398
return Err(DscError::Command(resource.resource_type.clone(), exit_code, stderr));
@@ -459,22 +423,11 @@ pub fn invoke_validate(resource: &ResourceManifest, cwd: &str, config: &str) ->
459423
return Err(DscError::NotImplemented("validate".to_string()));
460424
};
461425

462-
let mut env: Option<HashMap<String, String>> = None;
463-
let mut input_config: Option<&str> = None;
464426
let args = process_args(&validate.args, config);
465-
match &validate.input {
466-
Some(InputKind::Env) => {
467-
env = Some(json_to_hashmap(config)?);
468-
},
469-
Some(InputKind::Stdin) => {
470-
input_config = Some(config);
471-
},
472-
None => {
473-
// leave input as none
474-
},
475-
}
427+
let (env, input_config) = get_env_stdin(&validate.input, config)?;
476428

477-
let (exit_code, stdout, stderr) = invoke_command(&validate.executable, args, input_config, Some(cwd), env)?;
429+
info!("Invoking validate '{}' using '{}'", &resource.resource_type, &validate.executable);
430+
let (exit_code, stdout, stderr) = invoke_command(&validate.executable, args, input_config.as_deref(), Some(cwd), env)?;
478431
log_resource_traces(&stderr);
479432
if exit_code != 0 {
480433
return Err(DscError::Command(resource.resource_type.clone(), exit_code, stderr));
@@ -548,30 +501,20 @@ pub fn invoke_export(resource: &ResourceManifest, cwd: &str, input: Option<&str>
548501

549502

550503
let mut env: Option<HashMap<String, String>> = None;
551-
let mut export_input: Option<&str> = None;
504+
let mut export_input: Option<String> = None;
552505
let args: Option<Vec<String>>;
553506
if let Some(input) = input {
554507
if !input.is_empty() {
555508
verify_json(resource, cwd, input)?;
556-
match &export.input {
557-
Some(InputKind::Env) => {
558-
env = Some(json_to_hashmap(input)?);
559-
},
560-
Some(InputKind::Stdin) => {
561-
export_input = Some(input);
562-
},
563-
None => {
564-
// leave input as none
565-
},
566-
}
509+
(env, export_input) = get_env_stdin(&export.input, input)?;
567510
}
568511

569512
args = process_args(&export.args, input);
570513
} else {
571514
args = process_args(&export.args, "");
572515
}
573516

574-
let (exit_code, stdout, stderr) = invoke_command(&export.executable, args, export_input, Some(cwd), env)?;
517+
let (exit_code, stdout, stderr) = invoke_command(&export.executable, args, export_input.as_deref(), Some(cwd), env)?;
575518
log_resource_traces(&stderr);
576519
if exit_code != 0 {
577520
return Err(DscError::Command(resource.resource_type.clone(), exit_code, stderr));
@@ -695,6 +638,24 @@ fn process_args(args: &Option<Vec<ArgKind>>, value: &str) -> Option<Vec<String>>
695638
Some(processed_args)
696639
}
697640

641+
fn get_env_stdin(input_kind: &Option<InputKind>, input: &str) -> Result<(Option<HashMap<String, String>>, Option<String>), DscError> {
642+
let mut env: Option<HashMap<String, String>> = None;
643+
let mut input_value: Option<String> = None;
644+
match input_kind {
645+
Some(InputKind::Env) => {
646+
env = Some(json_to_hashmap(input)?);
647+
},
648+
Some(InputKind::Stdin) => {
649+
input_value = Some(input.to_string());
650+
},
651+
None => {
652+
// leave input as none
653+
},
654+
}
655+
656+
Ok((env, input_value))
657+
}
658+
698659
fn verify_json(resource: &ResourceManifest, cwd: &str, json: &str) -> Result<(), DscError> {
699660

700661
debug!("Verify JSON for '{}'", resource.resource_type);

0 commit comments

Comments
 (0)