Skip to content

Commit 71350a0

Browse files
committed
fix default for targetPath if not specified
1 parent 405794a commit 71350a0

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

dsc/tests/dsc_functions.tests.ps1

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,24 @@ Describe 'tests for function expressions' {
4242
$out = $config_yaml | dsc config --target-path $PSHOME get | ConvertFrom-Json
4343
$out.results[0].result.actualState.output | Should -BeExactly $expected
4444
}
45+
46+
It 'default targetPath() is correct for the OS' {
47+
$config_yaml = @'
48+
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
49+
resources:
50+
- name: Echo
51+
type: Microsoft.DSC.Debug/Echo
52+
properties:
53+
output: "[targetPath()]"
54+
'@
55+
56+
$expected = if ($IsWindows) {
57+
$env:SYSTEMDRIVE + '\'
58+
} else {
59+
'/'
60+
}
61+
$out = $config_yaml | dsc config get | ConvertFrom-Json
62+
$LASTEXITCODE | Should -Be 0
63+
$out.results[0].result.actualState.output | Should -BeExactly $expected
64+
}
4565
}

dsc_lib/src/configure/context.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl Context {
2525
Self {
2626
execution_type: ExecutionKind::Actual,
2727
outputs: HashMap::new(),
28-
target_path: PathBuf::new(),
28+
target_path: get_default_os_target_path(),
2929
parameters: HashMap::new(),
3030
security_context: match get_security_context() {
3131
SecurityContext::Admin => SecurityContextKind::Elevated,
@@ -42,3 +42,15 @@ impl Default for Context {
4242
Self::new()
4343
}
4444
}
45+
46+
#[cfg(target_os = "windows")]
47+
fn get_default_os_target_path() -> PathBuf {
48+
// use SYSTEMDRIVE env var to get the default target path
49+
let system_drive = std::env::var("SYSTEMDRIVE").unwrap_or_else(|_| "C:".to_string());
50+
PathBuf::from(system_drive).join("\\")
51+
}
52+
53+
#[cfg(not(target_os = "windows"))]
54+
fn get_default_os_target_path() -> PathBuf {
55+
PathBuf::from("/")
56+
}

0 commit comments

Comments
 (0)