Skip to content

Commit 5726e95

Browse files
committed
fix tests, add runcommandonset to vscode project
1 parent 5e06410 commit 5726e95

File tree

9 files changed

+58
-37
lines changed

9 files changed

+58
-37
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"./dsc_lib/Cargo.toml",
55
"./osinfo/Cargo.toml",
66
"./registry/Cargo.toml",
7+
"./runcommandonset/Cargo.toml",
78
"./tools/test_group_resource/Cargo.toml",
89
"./tools/dsctest/Cargo.toml",
910
"./tree-sitter-dscexpression/Cargo.toml",

dsc/tests/dsc_include.tests.ps1

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,19 @@ Describe 'Include tests' {
9494
$log | Should -BeLike "*ERROR*"
9595
}
9696

97-
It 'Valid file path: <test>' -TestCases @(
98-
@{ test = 'absolute configuration'; config = (Join-Path $TestDrive 'include/osinfo_parameters.dsc.yaml'); parameters = $null }
99-
@{ test = 'absolute parameters'; config = 'include/osinfo_parameters.dsc.yaml'; parameters = (Join-Path $TestDrive 'include/osinfo.parameters.yaml') }
97+
It 'Valid absolute file path: <test>' -TestCases @(
98+
@{ test = 'configuration'; config = 'include/osinfo_parameters.dsc.yaml'; parameters = $null }
99+
@{ test = 'parameters'; config = 'include/osinfo_parameters.dsc.yaml'; parameters = 'include/osinfo.parameters.yaml' }
100100
) {
101-
param($config, $parameters)
101+
param($test, $config, $parameters)
102+
103+
if ($test -eq 'configuration') {
104+
$config = Join-Path $TestDrive $config
105+
} elseif ($test -eq 'parameters') {
106+
$parameters = Join-Path $TestDrive $parameters
107+
} else {
108+
throw "Invalid test case: $test"
109+
}
102110

103111
$configYaml = @"
104112
`$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json

dsc/tests/dsc_tracing.tests.ps1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ Describe 'tracing tests' {
6565
}
6666

6767
It 'trace level <level> is passed to resource' -TestCases @(
68-
@{ level = 'error'; expectedLevel = 'ERROR' }
69-
@{ level = 'warn'; expectedLevel = 'WARN' }
70-
@{ level = 'info'; expectedLevel = 'INFO' }
71-
@{ level = 'debug'; expectedLevel = 'DEBUG'}
72-
@{ level = 'trace'; expectedLevel = 'TRACE'}
68+
@{ level = 'error' }
69+
@{ level = 'warn' }
70+
@{ level = 'info' }
71+
@{ level = 'debug' }
72+
@{ level = 'trace' }
7373
) {
74-
param($level, $expectedLevel)
74+
param($level)
7575

7676
$configYaml = @"
7777
`$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
@@ -83,6 +83,6 @@ Describe 'tracing tests' {
8383
"@
8484

8585
$out = (dsc -l $level config get -d $configYaml 2> $null) | ConvertFrom-Json
86-
$out.results[0].result.actualState.level | Should -BeExactly $expectedLevel
86+
$out.results[0].result.actualState.level | Should -BeExactly $level
8787
}
8888
}

dsc_lib/src/dscresources/command_resource.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,9 +771,11 @@ pub fn log_resource_traces(process_name: &str, process_id: &u32, stderr: &str)
771771
debug!("Process '{process_name}' id {process_id} : {}", msg.as_str().unwrap_or_default());
772772
} else if let Some(msg) = json_obj.get("Trace") {
773773
trace!("Process '{process_name}' id {process_id} : {}", msg.as_str().unwrap_or_default());
774+
} else {
775+
// TODO: deserialize tracing JSON to have better presentation
776+
trace!("Process '{process_name}' id {process_id} : {trace_line}");
774777
};
775778
} else {
776-
// TODO: deserialize tracing JSON to have better presentation
777779
trace!("Process '{process_name}' id {process_id} : {trace_line}");
778780
}
779781
}

osinfo/tests/osinfo.tests.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
Describe 'osinfo resource tests' {
55
It 'should get osinfo' {
6-
$out = dsc resource get -r Microsoft/osinfo | ConvertFrom-Json
6+
$out = dsc resource get -r Microsoft/OSInfo | ConvertFrom-Json
77
$LASTEXITCODE | Should -Be 0
88
if ($IsWindows) {
99
$out.actualState.family | Should -BeExactly 'Windows'
@@ -31,7 +31,7 @@ Describe 'osinfo resource tests' {
3131
else {
3232
$invalid = 'Windows'
3333
}
34-
$out = "{`"family`": `"$invalid`"}" | dsc resource test -r 'Microsoft/osinfo' | ConvertFrom-Json
34+
$out = "{`"family`": `"$invalid`"}" | dsc resource test -r 'Microsoft/OSInfo' | ConvertFrom-Json
3535
$actual = dsc resource get -r Microsoft/OSInfo | ConvertFrom-Json
3636
$out.actualState.family | Should -BeExactly $actual.actualState.family
3737
$out.actualState.version | Should -BeExactly $actual.actualState.version
@@ -41,7 +41,7 @@ Describe 'osinfo resource tests' {
4141
}
4242

4343
It 'should support export' {
44-
$out = dsc resource export -r Microsoft/osinfo | ConvertFrom-Json
44+
$out = dsc resource export -r Microsoft/OSInfo | ConvertFrom-Json
4545
$out.'$schema' | Should -BeExactly 'https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json'
4646
if ($IsWindows) {
4747
$out.resources[0].properties.family | Should -BeExactly 'Windows'

runcommandonset/src/args.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub enum TraceFormat {
1313
#[derive(Debug, Clone, PartialEq, Eq, ValueEnum)]
1414
pub enum TraceLevel {
1515
Error,
16-
Warning,
16+
Warn,
1717
Info,
1818
Debug,
1919
Trace
@@ -26,7 +26,7 @@ pub struct Arguments {
2626
#[clap(subcommand)]
2727
pub subcommand: SubCommand,
2828
#[clap(short = 'l', long, help = "Trace level to use", value_enum, default_value = "info")]
29-
pub trace_level: TraceLevel,
29+
pub trace_level: Option<TraceLevel>,
3030
#[clap(short = 'f', long, help = "Trace format to use", value_enum, default_value = "json")]
3131
pub trace_format: TraceFormat,
3232
}

runcommandonset/src/main.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
// Licensed under the MIT License.
33

44
use atty::Stream;
5-
use clap::{Parser};
5+
use clap::Parser;
66
use std::{io::{self, Read}, process::exit};
77
use tracing::{error, warn, debug};
88

9-
use args::{Arguments, SubCommand};
10-
use runcommand::{RunCommand};
9+
use args::{Arguments, SubCommand, TraceLevel};
10+
use runcommand::RunCommand;
1111
use utils::{enable_tracing, invoke_command, parse_input, EXIT_INVALID_ARGS};
1212

1313
pub mod args;
@@ -16,7 +16,29 @@ pub mod utils;
1616

1717
fn main() {
1818
let args = Arguments::parse();
19-
enable_tracing(&args.trace_level, &args.trace_format);
19+
let trace_level = match args.trace_level {
20+
Some(trace_level) => trace_level,
21+
None => {
22+
// get from DSC_TRACE_LEVEL env var
23+
if let Ok(trace_level) = std::env::var("DSC_TRACE_LEVEL") {
24+
match trace_level.to_lowercase().as_str() {
25+
"error" => TraceLevel::Error,
26+
"warn" => TraceLevel::Warn,
27+
"info" => TraceLevel::Info,
28+
"debug" => TraceLevel::Debug,
29+
"trace" => TraceLevel::Trace,
30+
_ => {
31+
warn!("Invalid trace level: {trace_level}");
32+
TraceLevel::Info
33+
}
34+
}
35+
} else {
36+
// default to info
37+
TraceLevel::Info
38+
}
39+
}
40+
};
41+
enable_tracing(&trace_level, &args.trace_format);
2042
warn!("This resource is not idempotent");
2143

2244
let stdin = if atty::is(Stream::Stdin) {

runcommandonset/src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub fn enable_tracing(trace_level: &TraceLevel, trace_format: &TraceFormat) {
6565
// originally implemented in dsc/src/util.rs
6666
let tracing_level = match trace_level {
6767
TraceLevel::Error => Level::ERROR,
68-
TraceLevel::Warning => Level::WARN,
68+
TraceLevel::Warn => Level::WARN,
6969
TraceLevel::Info => Level::INFO,
7070
TraceLevel::Debug => Level::DEBUG,
7171
TraceLevel::Trace => Level::TRACE,

runcommandonset/tests/runcommandonset.set.tests.ps1

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,25 +76,13 @@ Describe 'tests for runcommandonset set' {
7676
}
7777

7878
It 'Executable does not exist' {
79-
'{ "executable": "foo" }' | dsc resource set -r Microsoft.DSC.Transitional/RunCommandOnSet 2> $TestDrive/output.txt
80-
$actual = Get-Content -Path $TestDrive/output.txt
79+
'{ "executable": "foo" }' | dsc -l trace resource set -r Microsoft.DSC.Transitional/RunCommandOnSet 2> $TestDrive/output.txt
80+
$actual = Get-Content -Path $TestDrive/output.txt -Raw
8181
$expected_logging = 'Failed to execute foo: No such file or directory (os error 2)'
8282
if ($IsWindows) {
8383
$expected_logging = 'Failed to execute foo: program not found'
8484
}
85-
$found_logging = $false
86-
ForEach ($line in $actual) {
87-
try {
88-
$log = $line | ConvertFrom-Json
89-
if ($log.fields.message -eq $expected_logging) {
90-
$found_logging = $true
91-
break
92-
}
93-
} catch {
94-
# skip lines that aren't JSON
95-
}
96-
}
97-
$found_logging | Should -Be $true
85+
$actual | Should -BeLike "*$expected_logging*"
9886
$LASTEXITCODE | Should -Be 2
9987
}
10088
}

0 commit comments

Comments
 (0)