Skip to content

Commit c346935

Browse files
authored
Merge branch 'main' into fix-pending-reboot
2 parents 0e1ddd2 + 07e26f9 commit c346935

16 files changed

+141
-35
lines changed

build.ps1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ if ($null -ne $packageType) {
158158
Remove-Item temp:/rustup-init.exe -ErrorAction Ignore
159159
}
160160
}
161+
else {
162+
Write-Verbose -Verbose "Rust found, updating..."
163+
& $rustup update
164+
}
161165

162166
$BuildToolsPath = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC"
163167

dsc/locales/en-us.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ implementedAs = "implemented as"
7373
invalidOperationOnAdapter = "Can not perform this operation on the adapter itself"
7474
setInputEmpty = "Desired input is empty"
7575
testInputEmpty = "Expected input is required"
76+
jsonError = "JSON: %{err}"
7677

7778
[subcommand]
7879
actualStateNotObject = "actual_state is not an object"
@@ -108,6 +109,7 @@ tableHeader_capabilities = "Capabilities"
108109
tableHeader_adapter = "RequireAdapter"
109110
tableHeader_description = "Description"
110111
invalidManifest = "Error in manifest for"
112+
jsonArrayNotSupported = "JSON array output format is only supported for `--all'"
111113

112114
[util]
113115
failedToConvertJsonToString = "Failed to convert JSON to string"

dsc/src/args.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ pub enum OutputFormat {
1515
Yaml,
1616
}
1717

18+
#[derive(Debug, Clone, PartialEq, Eq, ValueEnum)]
19+
pub enum GetOutputFormat {
20+
Json,
21+
JsonArray,
22+
PrettyJson,
23+
Yaml,
24+
}
25+
1826
#[derive(Debug, Clone, PartialEq, Eq, ValueEnum)]
1927
pub enum ListOutputFormat {
2028
Json,
@@ -195,7 +203,7 @@ pub enum ResourceSubCommand {
195203
#[clap(short = 'f', long, help = t!("args.file").to_string(), conflicts_with = "input")]
196204
file: Option<String>,
197205
#[clap(short = 'o', long, help = t!("args.outputFormat").to_string())]
198-
output_format: Option<OutputFormat>,
206+
output_format: Option<GetOutputFormat>,
199207
},
200208
#[clap(name = "set", about = "Invoke the set operation to a resource", arg_required_else_help = true)]
201209
Set {

dsc/src/resource_command.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
use crate::args::OutputFormat;
4+
use crate::args::{GetOutputFormat, OutputFormat};
55
use crate::util::{EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_JSON_ERROR, EXIT_DSC_RESOURCE_NOT_FOUND, write_object};
66
use dsc_lib::configure::config_doc::{Configuration, ExecutionKind};
77
use dsc_lib::configure::add_resource_export_results_to_configuration;
@@ -47,7 +47,7 @@ pub fn get(dsc: &DscManager, resource_type: &str, input: &str, format: Option<&O
4747
}
4848
}
4949

50-
pub fn get_all(dsc: &DscManager, resource_type: &str, format: Option<&OutputFormat>) {
50+
pub fn get_all(dsc: &DscManager, resource_type: &str, format: Option<&GetOutputFormat>) {
5151
let input = String::new();
5252
let Some(resource) = get_resource(dsc, resource_type) else {
5353
error!("{}", DscError::ResourceNotFound(resource_type.to_string()).to_string());
@@ -68,6 +68,18 @@ pub fn get_all(dsc: &DscManager, resource_type: &str, format: Option<&OutputForm
6868
}
6969
};
7070

71+
if format == Some(&GetOutputFormat::JsonArray) {
72+
let json = match serde_json::to_string(&export_result.actual_state) {
73+
Ok(json) => json,
74+
Err(err) => {
75+
error!("{}", t!("resource_command.jsonError", err = err));
76+
exit(EXIT_JSON_ERROR);
77+
}
78+
};
79+
write_object(&json, Some(&OutputFormat::Json), false);
80+
return;
81+
}
82+
7183
let mut include_separator = false;
7284
for instance in export_result.actual_state
7385
{
@@ -78,10 +90,15 @@ pub fn get_all(dsc: &DscManager, resource_type: &str, format: Option<&OutputForm
7890
let json = match serde_json::to_string(&get_result) {
7991
Ok(json) => json,
8092
Err(err) => {
81-
error!("JSON Error: {err}");
93+
error!("{}", t!("resource_command.jsonError", err = err));
8294
exit(EXIT_JSON_ERROR);
8395
}
8496
};
97+
let format = match format {
98+
Some(&GetOutputFormat::PrettyJson) => Some(&OutputFormat::PrettyJson),
99+
Some(&GetOutputFormat::Yaml) => Some(&OutputFormat::Yaml),
100+
_ => Some(&OutputFormat::Json),
101+
};
85102
write_object(&json, format, include_separator);
86103
include_separator = true;
87104
}

dsc/src/subcommand.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
use crate::args::{ConfigSubCommand, DscType, ExtensionSubCommand, ListOutputFormat, OutputFormat, ResourceSubCommand};
4+
use crate::args::{ConfigSubCommand, DscType, ExtensionSubCommand, GetOutputFormat, ListOutputFormat, OutputFormat, ResourceSubCommand};
55
use crate::resolve::{get_contents, Include};
66
use crate::resource_command::{get_resource, self};
77
use crate::tablewriter::Table;
@@ -590,7 +590,17 @@ pub fn resource(subcommand: &ResourceSubCommand, progress_format: ProgressFormat
590590
if *all { resource_command::get_all(&dsc, resource, output_format.as_ref()); }
591591
else {
592592
let parsed_input = get_input(input.as_ref(), path.as_ref());
593-
resource_command::get(&dsc, resource, &parsed_input, output_format.as_ref());
593+
let format = match output_format {
594+
Some(GetOutputFormat::Json) => Some(OutputFormat::Json),
595+
Some(GetOutputFormat::JsonArray) => {
596+
error!("{}", t!("subcommand.jsonArrayNotSupported"));
597+
exit(EXIT_INVALID_ARGS);
598+
},
599+
Some(GetOutputFormat::PrettyJson) => Some(OutputFormat::PrettyJson),
600+
Some(GetOutputFormat::Yaml) => Some(OutputFormat::Yaml),
601+
None => None,
602+
};
603+
resource_command::get(&dsc, resource, &parsed_input, format.as_ref());
594604
}
595605
},
596606
ResourceSubCommand::Set { resource, input, file: path, output_format } => {

dsc/tests/dsc_discovery.tests.ps1

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,35 @@ Describe 'tests for resource discovery' {
220220
$env:DSC_RESOURCE_PATH = $oldPath
221221
}
222222
}
223+
224+
It 'DSC_RESOURCE_PATH should be used for executable lookup' {
225+
$dscTest = Get-Command dscecho -ErrorAction Stop
226+
$target = if ($IsWindows) {
227+
'echoIt.exe'
228+
} else {
229+
'echoIt'
230+
}
231+
Copy-Item -Path "$($dscTest.Source)" -Destination "$testdrive\$target"
232+
$manifest = Get-Content -Raw -Path "$(Split-Path -Path $dscTest.Source -Parent)\echo.dsc.resource.json" | ConvertFrom-Json
233+
$manifest.type = 'Test/MyEcho'
234+
$manifest.get.executable = $target
235+
$manifest.set = $null
236+
$manifest.test = $null
237+
$manifest.schema.command.executable = $target
238+
Set-Content -Path "$testdrive/test.dsc.resource.json" -Value ($manifest | ConvertTo-Json -Depth 10)
239+
240+
$oldPath = $env:DSC_RESOURCE_PATH
241+
try {
242+
$env:DSC_RESOURCE_PATH = $testdrive
243+
$out = dsc resource get -r 'Test/MyEcho' -i '{"output":"Custom"}' 2> "$testdrive/error.txt" | ConvertFrom-Json
244+
$LASTEXITCODE | Should -Be 0
245+
$out.actualState.output | Should -BeExactly 'Custom'
246+
dsc resource get -r 'Microsoft.DSC.Debug/Echo' -i '{"output":"Custom"}' 2> "$testdrive/error.txt" | ConvertFrom-Json
247+
$LASTEXITCODE | Should -Be 7
248+
Get-Content -Raw -Path "$testdrive/error.txt" | Should -Match "ERROR.*?Resource not found"
249+
}
250+
finally {
251+
$env:DSC_RESOURCE_PATH = $oldPath
252+
}
253+
}
223254
}

dsc/tests/dsc_extension_discover.tests.ps1

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
Describe 'Discover extension tests' {
55
BeforeAll {
66
$oldPath = $env:PATH
7-
$separator = [System.IO.Path]::PathSeparator
87
$toolPath = Resolve-Path -Path "$PSScriptRoot/../../extensions/test/discover"
9-
$env:PATH = "$toolPath$separator$oldPath"
8+
$env:PATH = "$toolPath" + [System.IO.Path]::PathSeparator + $oldPath
109
}
1110

1211
AfterAll {
@@ -75,10 +74,10 @@ Describe 'Discover extension tests' {
7574
Set-Content -Path "$TestDrive/test.dsc.extension.json" -Value $extension_json
7675
Copy-Item -Path "$toolPath/discover.ps1" -Destination $TestDrive | Out-Null
7776
Copy-Item -Path "$toolPath/resources" -Destination $TestDrive -Recurse | Out-Null
78-
$env:DSC_RESOURCE_PATH = $TestDrive
77+
$env:DSC_RESOURCE_PATH = "$TestDrive" + [System.IO.Path]::PathSeparator + (Split-Path (Get-Command pwsh).Source -Parent)
7978
try {
8079
$out = dsc extension list | ConvertFrom-Json
81-
$out.Count | Should -Be 1
80+
$out.Count | Should -Be 1 -Because ($out | Out-String)
8281
$out.type | Should -Be 'Test/DiscoverRelative'
8382
$out = dsc resource list 2> $TestDrive/error.log
8483
write-verbose -verbose (Get-Content -Path "$TestDrive/error.log" -Raw)

dsc/tests/dsc_get.tests.ps1 renamed to dsc/tests/dsc_resource_get.tests.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,10 @@ Describe 'resource get tests' {
4848
$testError[0] | SHould -match 'error:'
4949
$LASTEXITCODE | Should -Be 2
5050
}
51+
52+
It '--output-format json-array returns single object' {
53+
$out = dsc resource get -r Microsoft/Process --all --output-format json-array
54+
$LASTEXITCODE | Should -Be 0
55+
($out | Measure-Object).Count | Should -Be 1
56+
}
5157
}

dsc/tests/dsc_resource_input.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ Describe 'tests for resource input' {
8585
}
8686
'@
8787
$oldPath = $env:DSC_RESOURCE_PATH
88-
$env:DSC_RESOURCE_PATH = $TestDrive
88+
$env:DSC_RESOURCE_PATH = $TestDrive + [System.IO.Path]::PathSeparator + $env:PATH
8989
Set-Content $TestDrive/EnvVarInput.dsc.resource.json -Value $manifest
9090
}
9191

dsc/tests/dsc_set.tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ changedProperties:
147147

148148
$oldPath = $env:DSC_RESOURCE_PATH
149149
try {
150-
$env:DSC_RESOURCE_PATH = $TestDrive
150+
$env:DSC_RESOURCE_PATH = $TestDrive + [System.IO.Path]::PathSeparator + $env:PATH
151151
$out = '{ "test": true }' | dsc resource set -r Test/SetNoTest -f - --output-format $format | Out-String
152152
$LASTEXITCODE | Should -Be 0
153153
$out.Trim() | Should -BeExactly $expected
@@ -160,7 +160,7 @@ changedProperties:
160160
It 'set can be used on a resource that does not implement test' {
161161
$oldPath = $env:DSC_RESOURCE_PATH
162162
try {
163-
$env:DSC_RESOURCE_PATH = $TestDrive
163+
$env:DSC_RESOURCE_PATH = $TestDrive + [System.IO.Path]::PathSeparator + $env:PATH
164164
$out = '{ "test": true }' | dsc resource set -r Test/SetNoTest -f - | ConvertFrom-Json
165165
$LASTEXITCODE | Should -Be 0
166166
$out.BeforeState.test | Should -Be $true

0 commit comments

Comments
 (0)