diff --git a/dsc/tests/dsc_discovery.tests.ps1 b/dsc/tests/dsc_discovery.tests.ps1 index d68024204..6d377a3bc 100644 --- a/dsc/tests/dsc_discovery.tests.ps1 +++ b/dsc/tests/dsc_discovery.tests.ps1 @@ -220,4 +220,35 @@ Describe 'tests for resource discovery' { $env:DSC_RESOURCE_PATH = $oldPath } } + + It 'DSC_RESOURCE_PATH should be used for executable lookup' { + $dscTest = Get-Command dscecho -ErrorAction Stop + $target = if ($IsWindows) { + 'echoIt.exe' + } else { + 'echoIt' + } + Copy-Item -Path "$($dscTest.Source)" -Destination "$testdrive\$target" + $manifest = Get-Content -Raw -Path "$(Split-Path -Path $dscTest.Source -Parent)\echo.dsc.resource.json" | ConvertFrom-Json + $manifest.type = 'Test/MyEcho' + $manifest.get.executable = $target + $manifest.set = $null + $manifest.test = $null + $manifest.schema.command.executable = $target + Set-Content -Path "$testdrive/test.dsc.resource.json" -Value ($manifest | ConvertTo-Json -Depth 10) + + $oldPath = $env:DSC_RESOURCE_PATH + try { + $env:DSC_RESOURCE_PATH = $testdrive + $out = dsc resource get -r 'Test/MyEcho' -i '{"output":"Custom"}' 2> "$testdrive/error.txt" | ConvertFrom-Json + $LASTEXITCODE | Should -Be 0 + $out.actualState.output | Should -BeExactly 'Custom' + dsc resource get -r 'Microsoft.DSC.Debug/Echo' -i '{"output":"Custom"}' 2> "$testdrive/error.txt" | ConvertFrom-Json + $LASTEXITCODE | Should -Be 7 + Get-Content -Raw -Path "$testdrive/error.txt" | Should -Match "ERROR.*?Resource not found" + } + finally { + $env:DSC_RESOURCE_PATH = $oldPath + } + } } diff --git a/dsc/tests/dsc_extension_discover.tests.ps1 b/dsc/tests/dsc_extension_discover.tests.ps1 index ff5f92044..d865c9599 100644 --- a/dsc/tests/dsc_extension_discover.tests.ps1 +++ b/dsc/tests/dsc_extension_discover.tests.ps1 @@ -4,9 +4,8 @@ Describe 'Discover extension tests' { BeforeAll { $oldPath = $env:PATH - $separator = [System.IO.Path]::PathSeparator $toolPath = Resolve-Path -Path "$PSScriptRoot/../../extensions/test/discover" - $env:PATH = "$toolPath$separator$oldPath" + $env:PATH = "$toolPath" + [System.IO.Path]::PathSeparator + $oldPath } AfterAll { @@ -75,10 +74,10 @@ Describe 'Discover extension tests' { Set-Content -Path "$TestDrive/test.dsc.extension.json" -Value $extension_json Copy-Item -Path "$toolPath/discover.ps1" -Destination $TestDrive | Out-Null Copy-Item -Path "$toolPath/resources" -Destination $TestDrive -Recurse | Out-Null - $env:DSC_RESOURCE_PATH = $TestDrive + $env:DSC_RESOURCE_PATH = "$TestDrive" + [System.IO.Path]::PathSeparator + (Split-Path (Get-Command pwsh).Source -Parent) try { $out = dsc extension list | ConvertFrom-Json - $out.Count | Should -Be 1 + $out.Count | Should -Be 1 -Because ($out | Out-String) $out.type | Should -Be 'Test/DiscoverRelative' $out = dsc resource list 2> $TestDrive/error.log write-verbose -verbose (Get-Content -Path "$TestDrive/error.log" -Raw) diff --git a/dsc/tests/dsc_resource_input.tests.ps1 b/dsc/tests/dsc_resource_input.tests.ps1 index 75da70949..74f33bf79 100644 --- a/dsc/tests/dsc_resource_input.tests.ps1 +++ b/dsc/tests/dsc_resource_input.tests.ps1 @@ -85,7 +85,7 @@ Describe 'tests for resource input' { } '@ $oldPath = $env:DSC_RESOURCE_PATH - $env:DSC_RESOURCE_PATH = $TestDrive + $env:DSC_RESOURCE_PATH = $TestDrive + [System.IO.Path]::PathSeparator + $env:PATH Set-Content $TestDrive/EnvVarInput.dsc.resource.json -Value $manifest } diff --git a/dsc/tests/dsc_set.tests.ps1 b/dsc/tests/dsc_set.tests.ps1 index 8a58cbbb8..d6fa25a77 100644 --- a/dsc/tests/dsc_set.tests.ps1 +++ b/dsc/tests/dsc_set.tests.ps1 @@ -147,7 +147,7 @@ changedProperties: $oldPath = $env:DSC_RESOURCE_PATH try { - $env:DSC_RESOURCE_PATH = $TestDrive + $env:DSC_RESOURCE_PATH = $TestDrive + [System.IO.Path]::PathSeparator + $env:PATH $out = '{ "test": true }' | dsc resource set -r Test/SetNoTest -f - --output-format $format | Out-String $LASTEXITCODE | Should -Be 0 $out.Trim() | Should -BeExactly $expected @@ -160,7 +160,7 @@ changedProperties: It 'set can be used on a resource that does not implement test' { $oldPath = $env:DSC_RESOURCE_PATH try { - $env:DSC_RESOURCE_PATH = $TestDrive + $env:DSC_RESOURCE_PATH = $TestDrive + [System.IO.Path]::PathSeparator + $env:PATH $out = '{ "test": true }' | dsc resource set -r Test/SetNoTest -f - | ConvertFrom-Json $LASTEXITCODE | Should -Be 0 $out.BeforeState.test | Should -Be $true diff --git a/dsc_lib/locales/en-us.toml b/dsc_lib/locales/en-us.toml index 528faf557..2b89e7b89 100644 --- a/dsc_lib/locales/en-us.toml +++ b/dsc_lib/locales/en-us.toml @@ -72,6 +72,7 @@ couldNotReadSetting = "Could not read 'resourcePath' setting" appendingEnvPath = "Appending PATH to resourcePath" originalPath = "Original PATH: %{path}" failedGetEnvPath = "Failed to get PATH environment variable" +failedJoinEnvPath = "Failed to join PATH environment variable" exeHomeAlreadyInPath = "Exe home is already in path: %{path}" addExeHomeToPath = "Adding exe home to path: %{path}" usingResourcePath = "Using Resource Path: %{path}" diff --git a/dsc_lib/src/discovery/command_discovery.rs b/dsc_lib/src/discovery/command_discovery.rs index ec604ee30..61bacc2b6 100644 --- a/dsc_lib/src/discovery/command_discovery.rs +++ b/dsc_lib/src/discovery/command_discovery.rs @@ -149,8 +149,16 @@ impl CommandDiscovery { let mut uniques: HashSet = HashSet::new(); paths.retain(|e|uniques.insert((*e).clone())); - // if exe home is not already in PATH env var then add it to env var and list of searched paths - if !using_custom_path { + if using_custom_path { + // when using custom path, intent is to isolate the search of manifests and executables to the custom path + // so we replace the PATH with the custom path + if let Ok(new_path) = env::join_paths(paths.clone()) { + env::set_var("PATH", new_path); + } else { + return Err(DscError::Operation(t!("discovery.commandDiscovery.failedJoinEnvPath").to_string())); + } + } else { + // if exe home is not already in PATH env var then add it to env var and list of searched paths if let Some(exe_home) = get_exe_path()?.parent() { let exe_home_pb = exe_home.to_path_buf(); if paths.contains(&exe_home_pb) { diff --git a/runcommandonset/tests/runcommandonset.get.tests.ps1 b/runcommandonset/tests/runcommandonset.get.tests.ps1 index 18b50ac45..aec3ddab8 100644 --- a/runcommandonset/tests/runcommandonset.get.tests.ps1 +++ b/runcommandonset/tests/runcommandonset.get.tests.ps1 @@ -2,15 +2,6 @@ # Licensed under the MIT License. Describe 'tests for runcommandonset get' { - BeforeAll { - $oldPath = $env:DSC_RESOURCE_PATH - $env:DSC_RESOURCE_PATH = Join-Path $PSScriptRoot ".." - } - - AfterAll { - $env:DSC_RESOURCE_PATH = $oldPath - } - It 'Input passed for executable, arguments, and exit code' { $json = @" { @@ -21,6 +12,7 @@ Describe 'tests for runcommandonset get' { "@ $result = $json | dsc resource get -r Microsoft.DSC.Transitional/RunCommandOnSet -f - | ConvertFrom-Json + $LASTEXITCODE | Should -Be 0 $result.actualState.arguments | Should -BeExactly @('bar', 'baz') $result.actualState.executable | Should -BeExactly 'foo' $result.actualState.exitCode | Should -BeExactly 5 diff --git a/runcommandonset/tests/runcommandonset.set.tests.ps1 b/runcommandonset/tests/runcommandonset.set.tests.ps1 index 6f38ea0ed..3ca57e9f9 100644 --- a/runcommandonset/tests/runcommandonset.set.tests.ps1 +++ b/runcommandonset/tests/runcommandonset.set.tests.ps1 @@ -2,21 +2,12 @@ # Licensed under the MIT License. Describe 'tests for runcommandonset set' { - BeforeAll { - $oldPath = $env:DSC_RESOURCE_PATH - $env:DSC_RESOURCE_PATH = Join-Path $PSScriptRoot ".." - } - AfterEach { if (Test-Path $TestDrive/output.txt) { Remove-Item -Path $TestDrive/output.txt } } - AfterAll { - $env:DSC_RESOURCE_PATH = $oldPath - } - It 'Input for executable and arguments can be sent to the resource' { $input_json = @" {