From 37a9325245d5c155ac828b521c1035a69f403ef6 Mon Sep 17 00:00:00 2001 From: "tgauth@bu.edu" Date: Wed, 31 Jan 2024 11:59:10 -0500 Subject: [PATCH 1/9] add --format to resource subcommands --- dsc/src/args.rs | 12 ++++++++++++ dsc/src/main.rs | 2 +- dsc/src/subcommand.rs | 14 +++++++------- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/dsc/src/args.rs b/dsc/src/args.rs index 7172d565f..5e9cbd2e2 100644 --- a/dsc/src/args.rs +++ b/dsc/src/args.rs @@ -81,6 +81,8 @@ pub enum ResourceSubCommand { description: Option, #[clap(short, long, help = "Tag to search for in the resource tags")] tags: Option>, + #[clap(short = 'f', long)] + format: Option, }, #[clap(name = "get", about = "Invoke the get operation to a resource", arg_required_else_help = true)] Get { @@ -90,6 +92,8 @@ pub enum ResourceSubCommand { resource: String, #[clap(short, long, help = "The input to pass to the resource as JSON")] input: Option, + #[clap(short = 'f', long)] + format: Option, }, #[clap(name = "set", about = "Invoke the set operation to a resource", arg_required_else_help = true)] Set { @@ -97,6 +101,8 @@ pub enum ResourceSubCommand { resource: String, #[clap(short, long, help = "The input to pass to the resource as JSON")] input: Option, + #[clap(short = 'f', long)] + format: Option, }, #[clap(name = "test", about = "Invoke the test operation to a resource", arg_required_else_help = true)] Test { @@ -104,16 +110,22 @@ pub enum ResourceSubCommand { resource: String, #[clap(short, long, help = "The input to pass to the resource as JSON")] input: Option, + #[clap(short = 'f', long)] + format: Option, }, #[clap(name = "schema", about = "Get the JSON schema for a resource", arg_required_else_help = true)] Schema { #[clap(short, long, help = "The name of the resource to get the JSON schema")] resource: String, + #[clap(short = 'f', long)] + format: Option, }, #[clap(name = "export", about = "Retrieve all resource instances", arg_required_else_help = true)] Export { #[clap(short, long, help = "The name or DscResource JSON of the resource to invoke `export` on")] resource: String, + #[clap(short = 'f', long)] + format: Option, }, } diff --git a/dsc/src/main.rs b/dsc/src/main.rs index 92535f3bc..3088b43a3 100644 --- a/dsc/src/main.rs +++ b/dsc/src/main.rs @@ -97,7 +97,7 @@ fn main() { } }, SubCommand::Resource { subcommand } => { - subcommand::resource(&subcommand, &args.format, &input); + subcommand::resource(&subcommand, &input); }, SubCommand::Schema { dsc_type } => { let schema = util::get_schema(dsc_type); diff --git a/dsc/src/subcommand.rs b/dsc/src/subcommand.rs index a269ebca5..315028f88 100644 --- a/dsc/src/subcommand.rs +++ b/dsc/src/subcommand.rs @@ -324,7 +324,7 @@ pub fn validate_config(config: &str) { exit(EXIT_SUCCESS); } -pub fn resource(subcommand: &ResourceSubCommand, format: &Option, stdin: &Option) { +pub fn resource(subcommand: &ResourceSubCommand, stdin: &Option) { let mut dsc = match DscManager::new() { Ok(dsc) => dsc, Err(err) => { @@ -334,7 +334,7 @@ pub fn resource(subcommand: &ResourceSubCommand, format: &Option, }; match subcommand { - ResourceSubCommand::List { resource_name, description, tags } => { + ResourceSubCommand::List { resource_name, description, tags, format } => { let mut write_table = false; let mut table = Table::new(&["Type", "Version", "Requires", "Description"]); @@ -411,26 +411,26 @@ pub fn resource(subcommand: &ResourceSubCommand, format: &Option, table.print(); } }, - ResourceSubCommand::Get { resource, input, all } => { + ResourceSubCommand::Get { resource, input, all, format } => { dsc.discover_resources(&[resource.to_lowercase().to_string()]); if *all { resource_command::get_all(&dsc, resource, input, stdin, format); } else { resource_command::get(&dsc, resource, input, stdin, format); }; }, - ResourceSubCommand::Set { resource, input } => { + ResourceSubCommand::Set { resource, input, format } => { dsc.discover_resources(&[resource.to_lowercase().to_string()]); resource_command::set(&dsc, resource, input, stdin, format); }, - ResourceSubCommand::Test { resource, input } => { + ResourceSubCommand::Test { resource, input, format } => { dsc.discover_resources(&[resource.to_lowercase().to_string()]); resource_command::test(&dsc, resource, input, stdin, format); }, - ResourceSubCommand::Schema { resource } => { + ResourceSubCommand::Schema { resource , format } => { dsc.discover_resources(&[resource.to_lowercase().to_string()]); resource_command::schema(&dsc, resource, format); }, - ResourceSubCommand::Export { resource} => { + ResourceSubCommand::Export { resource, format } => { dsc.discover_resources(&[resource.to_lowercase().to_string()]); resource_command::export(&mut dsc, resource, format); }, From 3cfd1963972f7afac5595560ba5d781a78f7ae46 Mon Sep 17 00:00:00 2001 From: "tgauth@bu.edu" Date: Wed, 31 Jan 2024 14:21:07 -0500 Subject: [PATCH 2/9] add --format to config subcommands --- dsc/src/args.rs | 25 ++++++++++++++++++------- dsc/src/main.rs | 8 ++++---- dsc/src/subcommand.rs | 10 +++++----- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/dsc/src/args.rs b/dsc/src/args.rs index 5e9cbd2e2..a94ffeaa3 100644 --- a/dsc/src/args.rs +++ b/dsc/src/args.rs @@ -18,9 +18,6 @@ pub struct Args { /// The subcommand to run #[clap(subcommand)] pub subcommand: SubCommand, - /// The output format to use - #[clap(short = 'f', long)] - pub format: Option, #[clap(short = 'i', long, help = "The input to pass to the configuration or resource", conflicts_with = "input_file")] pub input: Option, #[clap(short = 'p', long, help = "The path to a file used as input to the configuration or resource")] @@ -54,21 +51,35 @@ pub enum SubCommand { Schema { #[clap(name = "type", short, long, help = "The type of DSC schema to get")] dsc_type: DscType, + #[clap(short = 'f', long)] + format: Option, }, } #[derive(Debug, PartialEq, Eq, Subcommand)] pub enum ConfigSubCommand { #[clap(name = "get", about = "Retrieve the current configuration")] - Get, + Get { + #[clap(short = 'f', long)] + format: Option, + }, #[clap(name = "set", about = "Set the current configuration")] - Set, + Set { + #[clap(short = 'f', long)] + format: Option, + }, #[clap(name = "test", about = "Test the current configuration")] - Test, + Test { + #[clap(short = 'f', long)] + format: Option, + }, #[clap(name = "validate", about = "Validate the current configuration", hide = true)] Validate, #[clap(name = "export", about = "Export the current configuration")] - Export + Export { + #[clap(short = 'f', long)] + format: Option, + } } #[derive(Debug, PartialEq, Eq, Subcommand)] diff --git a/dsc/src/main.rs b/dsc/src/main.rs index 3088b43a3..56509e565 100644 --- a/dsc/src/main.rs +++ b/dsc/src/main.rs @@ -85,7 +85,7 @@ fn main() { if let Some(file_name) = parameters_file { info!("Reading parameters from file {}", file_name); match std::fs::read_to_string(file_name) { - Ok(parameters) => subcommand::config(&subcommand, &Some(parameters), &args.format, &input), + Ok(parameters) => subcommand::config(&subcommand, &Some(parameters), &input), Err(err) => { error!("Error: Failed to read parameters file: {err}"); exit(util::EXIT_INVALID_INPUT); @@ -93,13 +93,13 @@ fn main() { } } else { - subcommand::config(&subcommand, ¶meters, &args.format, &input); + subcommand::config(&subcommand, ¶meters, &input); } }, SubCommand::Resource { subcommand } => { subcommand::resource(&subcommand, &input); }, - SubCommand::Schema { dsc_type } => { + SubCommand::Schema { dsc_type , format } => { let schema = util::get_schema(dsc_type); let json = match serde_json::to_string(&schema) { Ok(json) => json, @@ -108,7 +108,7 @@ fn main() { exit(util::EXIT_JSON_ERROR); } }; - util::write_output(&json, &args.format); + util::write_output(&json, &format); }, } diff --git a/dsc/src/subcommand.rs b/dsc/src/subcommand.rs index 315028f88..ce4225051 100644 --- a/dsc/src/subcommand.rs +++ b/dsc/src/subcommand.rs @@ -116,7 +116,7 @@ pub fn config_export(configurator: &mut Configurator, format: &Option, format: &Option, stdin: &Option) { +pub fn config(subcommand: &ConfigSubCommand, parameters: &Option, stdin: &Option) { let Some(stdin) = stdin else { error!("Configuration must be piped to STDIN"); exit(EXIT_INVALID_ARGS); @@ -184,19 +184,19 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option, format } match subcommand { - ConfigSubCommand::Get => { + ConfigSubCommand::Get { format } => { config_get(&mut configurator, format); }, - ConfigSubCommand::Set => { + ConfigSubCommand::Set { format } => { config_set(&mut configurator, format); }, - ConfigSubCommand::Test => { + ConfigSubCommand::Test { format } => { config_test(&mut configurator, format); }, ConfigSubCommand::Validate => { validate_config(&json_string); }, - ConfigSubCommand::Export => { + ConfigSubCommand::Export { format } => { config_export(&mut configurator, format); } } From 9e766f19b9898273a39a2097bed7f3256fd1cafb Mon Sep 17 00:00:00 2001 From: "tgauth@bu.edu" Date: Wed, 31 Jan 2024 14:42:29 -0500 Subject: [PATCH 3/9] update tests with format --- dsc/tests/dsc_args.tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsc/tests/dsc_args.tests.ps1 b/dsc/tests/dsc_args.tests.ps1 index 339191d42..94db1a92a 100644 --- a/dsc/tests/dsc_args.tests.ps1 +++ b/dsc/tests/dsc_args.tests.ps1 @@ -85,7 +85,7 @@ actualState: ) { param($format, $expected) - $out = dsc --format $format resource get -r Test/Hello | Out-String + $out = dsc resource get -r Test/Hello --format $format | Out-String $LASTEXITCODE | Should -Be 0 $out.Trim() | Should -BeExactly $expected } From 9d5aedab55a6e7fb96d0f009a69bfea1697862ab Mon Sep 17 00:00:00 2001 From: "tgauth@bu.edu" Date: Wed, 31 Jan 2024 14:46:59 -0500 Subject: [PATCH 4/9] add comment back --- dsc/src/args.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/dsc/src/args.rs b/dsc/src/args.rs index a94ffeaa3..44de24dcb 100644 --- a/dsc/src/args.rs +++ b/dsc/src/args.rs @@ -5,6 +5,7 @@ use clap::{Parser, Subcommand, ValueEnum}; use clap_complete::Shell; use crate::util::LogLevel; +/// The output format to use #[derive(Debug, Clone, PartialEq, Eq, ValueEnum)] pub enum OutputFormat { Json, From 6c99ae031215c6f40f827f4193d0ec93f5d89907 Mon Sep 17 00:00:00 2001 From: "tgauth@bu.edu" Date: Wed, 31 Jan 2024 15:10:12 -0500 Subject: [PATCH 5/9] add help to all format descriptions --- dsc/src/args.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/dsc/src/args.rs b/dsc/src/args.rs index 44de24dcb..13ffebe3d 100644 --- a/dsc/src/args.rs +++ b/dsc/src/args.rs @@ -5,7 +5,6 @@ use clap::{Parser, Subcommand, ValueEnum}; use clap_complete::Shell; use crate::util::LogLevel; -/// The output format to use #[derive(Debug, Clone, PartialEq, Eq, ValueEnum)] pub enum OutputFormat { Json, @@ -52,7 +51,7 @@ pub enum SubCommand { Schema { #[clap(name = "type", short, long, help = "The type of DSC schema to get")] dsc_type: DscType, - #[clap(short = 'f', long)] + #[clap(short = 'f', long, help = "The output format to use")] format: Option, }, } @@ -61,24 +60,24 @@ pub enum SubCommand { pub enum ConfigSubCommand { #[clap(name = "get", about = "Retrieve the current configuration")] Get { - #[clap(short = 'f', long)] + #[clap(short = 'f', long, help = "The output format to use")] format: Option, }, #[clap(name = "set", about = "Set the current configuration")] Set { - #[clap(short = 'f', long)] + #[clap(short = 'f', long, help = "The output format to use")] format: Option, }, #[clap(name = "test", about = "Test the current configuration")] Test { - #[clap(short = 'f', long)] + #[clap(short = 'f', long, help = "The output format to use")] format: Option, }, #[clap(name = "validate", about = "Validate the current configuration", hide = true)] Validate, #[clap(name = "export", about = "Export the current configuration")] Export { - #[clap(short = 'f', long)] + #[clap(short = 'f', long, help = "The output format to use")] format: Option, } } @@ -93,7 +92,7 @@ pub enum ResourceSubCommand { description: Option, #[clap(short, long, help = "Tag to search for in the resource tags")] tags: Option>, - #[clap(short = 'f', long)] + #[clap(short = 'f', long, help = "The output format to use")] format: Option, }, #[clap(name = "get", about = "Invoke the get operation to a resource", arg_required_else_help = true)] @@ -104,7 +103,7 @@ pub enum ResourceSubCommand { resource: String, #[clap(short, long, help = "The input to pass to the resource as JSON")] input: Option, - #[clap(short = 'f', long)] + #[clap(short = 'f', long, help = "The output format to use")] format: Option, }, #[clap(name = "set", about = "Invoke the set operation to a resource", arg_required_else_help = true)] @@ -113,7 +112,7 @@ pub enum ResourceSubCommand { resource: String, #[clap(short, long, help = "The input to pass to the resource as JSON")] input: Option, - #[clap(short = 'f', long)] + #[clap(short = 'f', long, help = "The output format to use")] format: Option, }, #[clap(name = "test", about = "Invoke the test operation to a resource", arg_required_else_help = true)] @@ -122,21 +121,21 @@ pub enum ResourceSubCommand { resource: String, #[clap(short, long, help = "The input to pass to the resource as JSON")] input: Option, - #[clap(short = 'f', long)] + #[clap(short = 'f', long, help = "The output format to use")] format: Option, }, #[clap(name = "schema", about = "Get the JSON schema for a resource", arg_required_else_help = true)] Schema { #[clap(short, long, help = "The name of the resource to get the JSON schema")] resource: String, - #[clap(short = 'f', long)] + #[clap(short = 'f', long, help = "The output format to use")] format: Option, }, #[clap(name = "export", about = "Retrieve all resource instances", arg_required_else_help = true)] Export { #[clap(short, long, help = "The name or DscResource JSON of the resource to invoke `export` on")] resource: String, - #[clap(short = 'f', long)] + #[clap(short = 'f', long, help = "The output format to use")] format: Option, }, } From bf6cd5b44718b80ef05b962d25c84a6f1a3cbe64 Mon Sep 17 00:00:00 2001 From: "tgauth@bu.edu" Date: Wed, 31 Jan 2024 16:21:24 -0500 Subject: [PATCH 6/9] add tests for --format as subcommand --- dsc/tests/dsc_config_get.tests.ps1 | 7 +++++ dsc/tests/dsc_export.tests.ps1 | 30 ++++++++++++++++++- dsc/tests/dsc_resource_list.tests.ps1 | 11 +++++++ dsc/tests/dsc_schema.tests.ps1 | 9 ++++++ dsc/tests/dsc_set.tests.ps1 | 43 +++++++++++++++++++++++++++ dsc/tests/dsc_test.tests.ps1 | 12 ++++++++ 6 files changed, 111 insertions(+), 1 deletion(-) diff --git a/dsc/tests/dsc_config_get.tests.ps1 b/dsc/tests/dsc_config_get.tests.ps1 index 61308b78d..6a8275b57 100644 --- a/dsc/tests/dsc_config_get.tests.ps1 +++ b/dsc/tests/dsc_config_get.tests.ps1 @@ -30,4 +30,11 @@ Describe 'dsc config get tests' { $null = $config | dsc config get | ConvertFrom-Json $LASTEXITCODE | Should -Be 2 } + + It 'can accept the use of --format as a subcommand' -Skip:(!$IsWindows) { + $jsonPath = Join-Path $PSScriptRoot '../examples/osinfo_registry.dsc.json' + $config = Get-Content $jsonPath -Raw + $config | dsc config get --format pretty-json | Out-String + $LASTEXITCODE | Should -Be 0 + } } diff --git a/dsc/tests/dsc_export.tests.ps1 b/dsc/tests/dsc_export.tests.ps1 index 20f834cdb..a930cec4b 100644 --- a/dsc/tests/dsc_export.tests.ps1 +++ b/dsc/tests/dsc_export.tests.ps1 @@ -2,7 +2,7 @@ # Licensed under the MIT License. Describe 'resource export tests' { - + It 'Export can be called on individual resource' { $out = dsc resource export -r Microsoft/Process @@ -74,4 +74,32 @@ Describe 'resource export tests' { $LASTEXITCODE | Should -Be 2 $out | out-string | Should -BeLike '*specified multiple times*' } + + It 'Export can be called on individual resource with the use of --format as a subcommand' { + + $out = dsc resource export -r Microsoft/Process -f pretty-json + $LASTEXITCODE | Should -Be 0 + $config_with_process_list = $out | ConvertFrom-Json + $config_with_process_list.'$schema' | Should -BeExactly 'https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/config/document.json' + $config_with_process_list.'resources' | Should -Not -BeNullOrEmpty + $config_with_process_list.resources.count | Should -BeGreaterThan 1 + } + + It 'Export can be called on a configuration with the use of --format as a subcommand' { + + $yaml = @' + $schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json + resources: + - name: Processes + type: Microsoft/Process + properties: + pid: 0 +'@ + $out = $yaml | dsc config export -f pretty-json + $LASTEXITCODE | Should -Be 0 + $config_with_process_list = $out | ConvertFrom-Json + $config_with_process_list.'$schema' | Should -BeExactly 'https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/config/document.json' + $config_with_process_list.'resources' | Should -Not -BeNullOrEmpty + $config_with_process_list.resources.count | Should -BeGreaterThan 1 + } } diff --git a/dsc/tests/dsc_resource_list.tests.ps1 b/dsc/tests/dsc_resource_list.tests.ps1 index b3b4a5937..3d3d9cc96 100644 --- a/dsc/tests/dsc_resource_list.tests.ps1 +++ b/dsc/tests/dsc_resource_list.tests.ps1 @@ -39,4 +39,15 @@ Describe 'Tests for listing resources' { $resources.type | Should -BeExactly $expectedType } } + + It 'can accept the use of --format as a subcommand' { + $expectedCount = 1 + $expectedType = 'Microsoft/OSInfo' + $resources = dsc resource list --description "operating system" --format pretty-json | ConvertFrom-Json + $LASTEXITCODE | Should -Be 0 + $resources.Count | Should -Be $expectedCount + if ($expectedCount -gt 0) { + $resources.type | Should -BeExactly $expectedType + } + } } diff --git a/dsc/tests/dsc_schema.tests.ps1 b/dsc/tests/dsc_schema.tests.ps1 index 7c07771d4..4afb2444a 100644 --- a/dsc/tests/dsc_schema.tests.ps1 +++ b/dsc/tests/dsc_schema.tests.ps1 @@ -28,4 +28,13 @@ Describe 'config schema tests' { $schema = $schema | ConvertFrom-Json $schema.'$schema' | Should -BeExactly 'http://json-schema.org/draft-07/schema#' } + + It 'can accept the use of --format as a subcommand' -Skip:(!$IsWindows) { + + $schema = dsc schema -t get-result -f pretty-json + $LASTEXITCODE | Should -Be 0 + $schema | Should -Not -BeNullOrEmpty + $schema = $schema | ConvertFrom-Json + $schema.'$schema' | Should -BeExactly 'http://json-schema.org/draft-07/schema#' + } } \ No newline at end of file diff --git a/dsc/tests/dsc_set.tests.ps1 b/dsc/tests/dsc_set.tests.ps1 index 8be4d4d33..fc30f4885 100644 --- a/dsc/tests/dsc_set.tests.ps1 +++ b/dsc/tests/dsc_set.tests.ps1 @@ -67,6 +67,49 @@ Describe 'config set tests' { ($result.psobject.properties | Measure-Object).Count | Should -Be 3 } + It 'can accept the use of --format as a subcommand' -Skip:(!$IsWindows) -TestCases @( + @{ format = 'yaml'; expected = @' +beforeState: + $id: https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json + keyPath: HKCU\1 + _exist: false +afterState: + $id: https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json + keyPath: HKCU\1 + _exist: false +changedProperties: [] +'@ } + @{ format = 'json'; expected = '{"beforeState":{"$id":"https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json","keyPath":"HKCU\\1","_exist":false},"afterState":{"$id":"https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json","keyPath":"HKCU\\1","_exist":false},"changedProperties":[]}' } + @{ format = 'pretty-json'; expected = @' +{ + "beforeState": { + "$id": "https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json", + "keyPath": "HKCU\\1", + "_exist": false + }, + "afterState": { + "$id": "https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json", + "keyPath": "HKCU\\1", + "_exist": false + }, + "changedProperties": [] +} +'@ } + ) { + param($format, $expected) + + $json = @' + { + "keyPath": "HKCU\\1", + "_exist": false + } +'@ + + $out = $json | dsc resource set -r Microsoft.Windows/registry --format $format | Out-String + $LASTEXITCODE | Should -Be 0 + $out.Trim() | Should -BeExactly $expected + } + It 'set can be used on a resource that does not implement test' { $manifest = @' { diff --git a/dsc/tests/dsc_test.tests.ps1 b/dsc/tests/dsc_test.tests.ps1 index 8e602b921..e1c0cd3d2 100644 --- a/dsc/tests/dsc_test.tests.ps1 +++ b/dsc/tests/dsc_test.tests.ps1 @@ -54,4 +54,16 @@ Describe 'config test tests' { $out.differingProperties[1] | Should -BeExactly 'valueData' $out.differingProperties[2] | Should -BeExactly '_exist' } + + It 'can accept the use of --format as a subcommand' -Skip:(!$IsWindows) { + $json = @' + { + "keyPath": "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion", + "valueName": "ProductName" + } +'@ + $current = $json | registry config get + $current | dsc resource test -r Microsoft.Windows/registry --format pretty-json + $LASTEXITCODE | Should -Be 0 + } } \ No newline at end of file From e74a9e8e2828382d73d3d814b17ed91b7fdff350 Mon Sep 17 00:00:00 2001 From: "tgauth@bu.edu" Date: Tue, 6 Feb 2024 14:22:01 -0500 Subject: [PATCH 7/9] fix typo --- dsc/src/subcommand.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsc/src/subcommand.rs b/dsc/src/subcommand.rs index ce4225051..376a50ad0 100644 --- a/dsc/src/subcommand.rs +++ b/dsc/src/subcommand.rs @@ -179,7 +179,7 @@ pub fn config(subcommand: &ConfigSubCommand, parameters: &Option, stdin: }; if let Err(err) = configurator.set_parameters(¶meters) { - error!("Error: Paramter input failure: {err}"); + error!("Error: Parameter input failure: {err}"); exit(EXIT_INVALID_INPUT); } From eef52c62c39e9cbc15538be8a5c076290cd20e51 Mon Sep 17 00:00:00 2001 From: "tgauth@bu.edu" Date: Tue, 6 Feb 2024 14:25:13 -0500 Subject: [PATCH 8/9] refactor tests to run on all distros --- dsc/tests/dsc_config_get.tests.ps1 | 14 ++- dsc/tests/dsc_schema.tests.ps1 | 7 +- dsc/tests/dsc_set.tests.ps1 | 142 ++++++++++++++--------------- dsc/tests/dsc_test.tests.ps1 | 11 +-- 4 files changed, 85 insertions(+), 89 deletions(-) diff --git a/dsc/tests/dsc_config_get.tests.ps1 b/dsc/tests/dsc_config_get.tests.ps1 index 6a8275b57..2bf907c99 100644 --- a/dsc/tests/dsc_config_get.tests.ps1 +++ b/dsc/tests/dsc_config_get.tests.ps1 @@ -31,10 +31,16 @@ Describe 'dsc config get tests' { $LASTEXITCODE | Should -Be 2 } - It 'can accept the use of --format as a subcommand' -Skip:(!$IsWindows) { - $jsonPath = Join-Path $PSScriptRoot '../examples/osinfo_registry.dsc.json' - $config = Get-Content $jsonPath -Raw - $config | dsc config get --format pretty-json | Out-String + It 'can accept the use of --format as a subcommand' { + $config_yaml = @" + `$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/10/config/document.json + resources: + - name: Echo + type: Test/Echo + properties: + text: hello +"@ + $null = $config_yaml | dsc config get --format pretty-json | Out-String $LASTEXITCODE | Should -Be 0 } } diff --git a/dsc/tests/dsc_schema.tests.ps1 b/dsc/tests/dsc_schema.tests.ps1 index 4afb2444a..48c308bc4 100644 --- a/dsc/tests/dsc_schema.tests.ps1 +++ b/dsc/tests/dsc_schema.tests.ps1 @@ -29,12 +29,11 @@ Describe 'config schema tests' { $schema.'$schema' | Should -BeExactly 'http://json-schema.org/draft-07/schema#' } - It 'can accept the use of --format as a subcommand' -Skip:(!$IsWindows) { - - $schema = dsc schema -t get-result -f pretty-json + It 'can accept the use of --format as a subcommand' { + $schema = dsc resource schema -r Test/Echo -f pretty-json $LASTEXITCODE | Should -Be 0 $schema | Should -Not -BeNullOrEmpty $schema = $schema | ConvertFrom-Json $schema.'$schema' | Should -BeExactly 'http://json-schema.org/draft-07/schema#' } -} \ No newline at end of file +} diff --git a/dsc/tests/dsc_set.tests.ps1 b/dsc/tests/dsc_set.tests.ps1 index fc30f4885..0bda542bb 100644 --- a/dsc/tests/dsc_set.tests.ps1 +++ b/dsc/tests/dsc_set.tests.ps1 @@ -2,6 +2,57 @@ # Licensed under the MIT License. Describe 'config set tests' { + BeforeAll { + $manifest = @' + { + "$schema": "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/bundled/resource/manifest.json", + "type": "Test/SetNoTest", + "version": "0.1.0", + "get": { + "executable": "pwsh", + "args": [ + "-NoLogo", + "-NonInteractive", + "-NoProfile", + "-Command", + "'{ \"test\": true }'" + ] + }, + "set": { + "executable": "pwsh", + "input": "stdin", + "args": [ + "-NoLogo", + "-NonInteractive", + "-NoProfile", + "-Command", + "'{ \"test\": false }'" + ], + "return": "state" + }, + "schema": { + "embedded": { + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "https://test", + "title": "test", + "description": "test", + "type": "object", + "required": [], + "additionalProperties": false, + "properties": { + "test": { + "type": "boolean", + "description": "test" + } + } + } + } + } +'@ + + Set-Content -Path "$TestDrive/SetNoTest.dsc.resource.json" -Value $manifest + } + BeforeEach { if ($IsWindows) { $json = @' @@ -70,95 +121,42 @@ Describe 'config set tests' { It 'can accept the use of --format as a subcommand' -Skip:(!$IsWindows) -TestCases @( @{ format = 'yaml'; expected = @' beforeState: - $id: https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json - keyPath: HKCU\1 - _exist: false + test: true afterState: - $id: https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json - keyPath: HKCU\1 - _exist: false -changedProperties: [] + test: false +changedProperties: +- test '@ } - @{ format = 'json'; expected = '{"beforeState":{"$id":"https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json","keyPath":"HKCU\\1","_exist":false},"afterState":{"$id":"https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json","keyPath":"HKCU\\1","_exist":false},"changedProperties":[]}' } + @{ format = 'json'; expected = '{"beforeState":{"test":true},"afterState":{"test":false},"changedProperties":["test"]}' } @{ format = 'pretty-json'; expected = @' { "beforeState": { - "$id": "https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json", - "keyPath": "HKCU\\1", - "_exist": false + "test": true }, "afterState": { - "$id": "https://developer.microsoft.com/json-schemas/windows/registry/20230303/Microsoft.Windows.Registry.schema.json", - "keyPath": "HKCU\\1", - "_exist": false + "test": false }, - "changedProperties": [] + "changedProperties": [ + "test" + ] } '@ } ) { param($format, $expected) - $json = @' - { - "keyPath": "HKCU\\1", - "_exist": false + $oldPath = $env:DSC_RESOURCE_PATH + try { + $env:DSC_RESOURCE_PATH = $TestDrive + $out = '{ "test": true }' | dsc resource set -r Test/SetNoTest --format $format | Out-String + $LASTEXITCODE | Should -Be 0 + $out.Trim() | Should -BeExactly $expected + } + finally { + $env:DSC_RESOURCE_PATH = $oldPath } -'@ - - $out = $json | dsc resource set -r Microsoft.Windows/registry --format $format | Out-String - $LASTEXITCODE | Should -Be 0 - $out.Trim() | Should -BeExactly $expected } It 'set can be used on a resource that does not implement test' { - $manifest = @' - { - "$schema": "https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2023/08/bundled/resource/manifest.json", - "type": "Test/SetNoTest", - "version": "0.1.0", - "get": { - "executable": "pwsh", - "args": [ - "-NoLogo", - "-NonInteractive", - "-NoProfile", - "-Command", - "'{ \"test\": true }'" - ] - }, - "set": { - "executable": "pwsh", - "input": "stdin", - "args": [ - "-NoLogo", - "-NonInteractive", - "-NoProfile", - "-Command", - "'{ \"test\": false }'" - ], - "return": "state" - }, - "schema": { - "embedded": { - "$schema": "http://json-schema.org/draft-07/schema#", - "$id": "https://test", - "title": "test", - "description": "test", - "type": "object", - "required": [], - "additionalProperties": false, - "properties": { - "test": { - "type": "boolean", - "description": "test" - } - } - } - } - } -'@ - - Set-Content -Path "$TestDrive/SetNoTest.dsc.resource.json" -Value $manifest $oldPath = $env:DSC_RESOURCE_PATH try { $env:DSC_RESOURCE_PATH = $TestDrive diff --git a/dsc/tests/dsc_test.tests.ps1 b/dsc/tests/dsc_test.tests.ps1 index e1c0cd3d2..1a3fc6f0c 100644 --- a/dsc/tests/dsc_test.tests.ps1 +++ b/dsc/tests/dsc_test.tests.ps1 @@ -55,15 +55,8 @@ Describe 'config test tests' { $out.differingProperties[2] | Should -BeExactly '_exist' } - It 'can accept the use of --format as a subcommand' -Skip:(!$IsWindows) { - $json = @' - { - "keyPath": "HKLM\\Software\\Microsoft\\Windows NT\\CurrentVersion", - "valueName": "ProductName" - } -'@ - $current = $json | registry config get - $current | dsc resource test -r Microsoft.Windows/registry --format pretty-json + It 'can accept the use of --format as a subcommand' { + $null = "text: hello" | dsc resource test -r Test/Echo --format pretty-json $LASTEXITCODE | Should -Be 0 } } \ No newline at end of file From a9d350f639dbcbe8dbffc27e6faa5f6bb7f1d3ad Mon Sep 17 00:00:00 2001 From: "tgauth@bu.edu" Date: Tue, 6 Feb 2024 14:27:02 -0500 Subject: [PATCH 9/9] add newline --- dsc/tests/dsc_test.tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsc/tests/dsc_test.tests.ps1 b/dsc/tests/dsc_test.tests.ps1 index 1a3fc6f0c..da3e51494 100644 --- a/dsc/tests/dsc_test.tests.ps1 +++ b/dsc/tests/dsc_test.tests.ps1 @@ -59,4 +59,4 @@ Describe 'config test tests' { $null = "text: hello" | dsc resource test -r Test/Echo --format pretty-json $LASTEXITCODE | Should -Be 0 } -} \ No newline at end of file +}