Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions dsc/src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::args::{ConfigSubCommand, SchemaType, ExtensionSubCommand, FunctionSub
use crate::resolve::{get_contents, Include};
use crate::resource_command::{get_resource, self};
use crate::tablewriter::Table;
use crate::util::{current_dsc_version, get_input, get_schema, in_desired_state, set_dscconfigroot, write_object, DSC_CONFIG_ROOT, EXIT_DSC_ASSERTION_FAILED, EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_INVALID_INPUT, EXIT_JSON_ERROR};
use crate::util::{current_dsc_version, get_input, get_schema, in_desired_state, set_dscconfigroot, write_object, DSC_CONFIG_ROOT, EXIT_DSC_ASSERTION_FAILED, EXIT_DSC_ERROR, EXIT_INVALID_ARGS, EXIT_INVALID_INPUT, EXIT_JSON_ERROR, EXIT_VALIDATION_FAILED};
use dsc_lib::types::{FullyQualifiedTypeName, ResourceVersionReq, TypeNameFilter};
use dsc_lib::{
configure::{
Expand Down Expand Up @@ -207,12 +207,35 @@ pub fn config_test(configurator: &mut Configurator, format: Option<&OutputFormat
},
Err(err) => {
error!("{err}");
return Err(ExitCode::from(EXIT_DSC_ERROR));
let exit_code = config_test_exit_code(&err);
return Err(ExitCode::from(exit_code));
}
}
Ok(())
}

fn config_test_exit_code(error: &DscError) -> u8 {
match error {
DscError::Schema(_) => EXIT_VALIDATION_FAILED,
_ => EXIT_DSC_ERROR,
}
}

#[cfg(test)]
mod tests {
use super::{config_test_exit_code, DscError, EXIT_DSC_ERROR, EXIT_VALIDATION_FAILED};

#[test]
fn schema_errors_use_validation_failed_exit_code() {
assert_eq!(config_test_exit_code(&DscError::Schema("invalid value".to_string())), EXIT_VALIDATION_FAILED);
}

#[test]
fn other_errors_keep_dsc_error_exit_code() {
assert_eq!(config_test_exit_code(&DscError::Operation("failed".to_string())), EXIT_DSC_ERROR);
}
}

pub fn config_export(configurator: &mut Configurator, format: Option<&OutputFormat>) -> Result<(), ExitCode> {
match configurator.invoke_export() {
Ok(result) => {
Expand Down
18 changes: 18 additions & 0 deletions dsc/tests/dsc_config_test.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,22 @@ Describe 'dsc config test tests' {
$log = Get-Content "$TestDrive/trace.log" -Raw
$log | Should -Match ".*Resource named 'MyTest' for type 'Microsoft.DSC.Debug/Echo' is specified more than once.*" -Because ($log | Out-String)
}

It 'Schema validation failures return exit code 5' {
$configYaml = @'
$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json
resources:
- name: invalid Echo property
type: Microsoft.DSC.Debug/Echo
properties:
output: 1
outpt: 2
'@
$tracePath = Join-Path $TestDrive 'trace.log'

$null = dsc config test --input $configYaml 2> $tracePath
$LASTEXITCODE | Should -Be 5
$log = Get-Content -Path $tracePath -Raw
$log | Should -Match "'outpt' was unexpected"
}
}
Loading