Skip to content

Commit db17be0

Browse files
authored
Merge pull request #825 from SteveL-MSFT/dscresourcepath-path
Fix `DSC_RESOURCE_PATH` env var to also limit exe search
2 parents 04dc007 + 80b4ad5 commit db17be0

File tree

8 files changed

+49
-27
lines changed

8 files changed

+49
-27
lines changed

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_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

dsc_lib/locales/en-us.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ couldNotReadSetting = "Could not read 'resourcePath' setting"
7272
appendingEnvPath = "Appending PATH to resourcePath"
7373
originalPath = "Original PATH: %{path}"
7474
failedGetEnvPath = "Failed to get PATH environment variable"
75+
failedJoinEnvPath = "Failed to join PATH environment variable"
7576
exeHomeAlreadyInPath = "Exe home is already in path: %{path}"
7677
addExeHomeToPath = "Adding exe home to path: %{path}"
7778
usingResourcePath = "Using Resource Path: %{path}"

dsc_lib/src/discovery/command_discovery.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,16 @@ impl CommandDiscovery {
149149
let mut uniques: HashSet<PathBuf> = HashSet::new();
150150
paths.retain(|e|uniques.insert((*e).clone()));
151151

152-
// if exe home is not already in PATH env var then add it to env var and list of searched paths
153-
if !using_custom_path {
152+
if using_custom_path {
153+
// when using custom path, intent is to isolate the search of manifests and executables to the custom path
154+
// so we replace the PATH with the custom path
155+
if let Ok(new_path) = env::join_paths(paths.clone()) {
156+
env::set_var("PATH", new_path);
157+
} else {
158+
return Err(DscError::Operation(t!("discovery.commandDiscovery.failedJoinEnvPath").to_string()));
159+
}
160+
} else {
161+
// if exe home is not already in PATH env var then add it to env var and list of searched paths
154162
if let Some(exe_home) = get_exe_path()?.parent() {
155163
let exe_home_pb = exe_home.to_path_buf();
156164
if paths.contains(&exe_home_pb) {

runcommandonset/tests/runcommandonset.get.tests.ps1

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@
22
# Licensed under the MIT License.
33

44
Describe 'tests for runcommandonset get' {
5-
BeforeAll {
6-
$oldPath = $env:DSC_RESOURCE_PATH
7-
$env:DSC_RESOURCE_PATH = Join-Path $PSScriptRoot ".."
8-
}
9-
10-
AfterAll {
11-
$env:DSC_RESOURCE_PATH = $oldPath
12-
}
13-
145
It 'Input passed for executable, arguments, and exit code' {
156
$json = @"
167
{
@@ -21,6 +12,7 @@ Describe 'tests for runcommandonset get' {
2112
"@
2213

2314
$result = $json | dsc resource get -r Microsoft.DSC.Transitional/RunCommandOnSet -f - | ConvertFrom-Json
15+
$LASTEXITCODE | Should -Be 0
2416
$result.actualState.arguments | Should -BeExactly @('bar', 'baz')
2517
$result.actualState.executable | Should -BeExactly 'foo'
2618
$result.actualState.exitCode | Should -BeExactly 5

runcommandonset/tests/runcommandonset.set.tests.ps1

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,12 @@
22
# Licensed under the MIT License.
33

44
Describe 'tests for runcommandonset set' {
5-
BeforeAll {
6-
$oldPath = $env:DSC_RESOURCE_PATH
7-
$env:DSC_RESOURCE_PATH = Join-Path $PSScriptRoot ".."
8-
}
9-
105
AfterEach {
116
if (Test-Path $TestDrive/output.txt) {
127
Remove-Item -Path $TestDrive/output.txt
138
}
149
}
1510

16-
AfterAll {
17-
$env:DSC_RESOURCE_PATH = $oldPath
18-
}
19-
2011
It 'Input for executable and arguments can be sent to the resource' {
2112
$input_json = @"
2213
{

0 commit comments

Comments
 (0)