Skip to content

Commit de4cd2c

Browse files
Steve Lee (POWERSHELL HE/HIM) (from Dev Box)SteveL-MSFT
authored andcommitted
clean up code, add negative test
1 parent 8c8010e commit de4cd2c

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

dsc/tests/dsc_discovery.tests.ps1

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -246,15 +246,19 @@ Describe 'tests for resource discovery' {
246246
}
247247
}
248248

249-
It 'Resource manifest using relative path to exe works' {
250-
$manifest = @'
249+
It 'Resource manifest using relative path to exe: <path>' -TestCases @(
250+
@{ path = '../dscecho'; success = $true }
251+
@{ path = '../foo/dscecho'; success = $false }
252+
) {
253+
param($path, $success)
254+
$manifest = @"
251255
{
252-
"$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
256+
"`$schema": "https://aka.ms/dsc/schemas/v3/bundled/resource/manifest.json",
253257
"type": "Microsoft.DSC.Debug/Echo",
254258
"version": "1.0.0",
255259
"description": "Echo resource for testing and debugging purposes",
256260
"get": {
257-
"executable": "../dscecho",
261+
"executable": "$path",
258262
"args": [
259263
{
260264
"jsonInputArg": "--input",
@@ -264,24 +268,29 @@ Describe 'tests for resource discovery' {
264268
},
265269
"schema": {
266270
"command": {
267-
"executable": "../dscecho"
271+
"executable": "$path"
268272
}
269273
}
270274
}
271-
'@
275+
"@
272276
$dscEcho = Get-Command dscecho -ErrorAction Stop
273277
# copy to testdrive
274278
Copy-Item -Path "$($dscEcho.Source)" -Destination $testdrive
275279
# create manifest in subfolder
276280
$subfolder = Join-Path $testdrive 'subfolder'
277-
New-Item -Path $subfolder -ItemType Directory | Out-Null
281+
New-Item -Path $subfolder -ItemType Directory -Force | Out-Null
278282
Set-Content -Path (Join-Path $subfolder 'test.dsc.resource.json') -Value $manifest
279283

280284
try {
281285
$env:DSC_RESOURCE_PATH = $subfolder
282286
$out = dsc resource get -r 'Microsoft.DSC.Debug/Echo' -i '{"output":"RelativePathTest"}' 2> "$testdrive/error.txt" | ConvertFrom-Json
283-
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Raw -Path "$testdrive/error.txt")
284-
$out.actualState.output | Should -BeExactly 'RelativePathTest'
287+
if ($success) {
288+
$LASTEXITCODE | Should -Be 0 -Because (Get-Content -Raw -Path "$testdrive/error.txt")
289+
$out.actualState.output | Should -BeExactly 'RelativePathTest'
290+
} else {
291+
$LASTEXITCODE | Should -Be 2 -Because (Get-Content -Raw -Path "$testdrive/error.txt")
292+
(Get-Content -Raw -Path "$testdrive/error.txt") | Should -Match "ERROR.*?Executable '\.\./foo/dscecho(\.exe)?' not found"
293+
}
285294
}
286295
finally {
287296
$env:DSC_RESOURCE_PATH = $null

lib/dsc-lib/src/util.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,16 +236,16 @@ pub fn resource_id(type_name: &str, name: &str) -> String {
236236
pub fn canonicalize_which(executable: &str, cwd: Option<&str>) -> Result<String, DscError> {
237237
// Use PathBuf to handle path separators robustly
238238
let mut executable_path = PathBuf::from(executable);
239-
if cfg!(target_os = "windows") && executable_path.extension().map_or(true, |ext| ext != "exe") {
239+
if cfg!(target_os = "windows") && executable_path.extension().is_none() {
240240
executable_path.set_extension("exe");
241241
}
242242
let mut executable = executable_path.to_string_lossy().to_string();
243-
if which(&executable).is_err() && !Path::new(&executable).is_absolute() && cwd.is_some() {
243+
if which(&executable).is_err() && !Path::new(&executable).is_absolute() {
244244
if let Some(cwd) = cwd {
245245
let cwd_path = Path::new(cwd);
246246
match canonicalize(cwd_path.join(&executable)) {
247247
Err(_err) => {
248-
return Err(DscError::CommandOperation(t!("util.executableNotFound", executable = &executable, cwd = cwd).to_string(), executable.to_string()));
248+
return Err(DscError::CommandOperation(t!("util.executableNotFound", executable = &executable, cwd = cwd_path.to_string_lossy()).to_string(), executable.to_string()));
249249
},
250250
Ok(canonical_path) => {
251251
executable = canonical_path.to_string_lossy().to_string();

0 commit comments

Comments
 (0)