Skip to content

Commit 68ca2b3

Browse files
authored
Merge pull request #1089 from SteveL-MSFT/test-cleanup
Fix tests to not emit unused stderr output
2 parents 5b9d420 + e12e181 commit 68ca2b3

14 files changed

+65
-73
lines changed

dsc/tests/dsc_discovery.tests.ps1

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ Describe 'tests for resource discovery' {
9595
$env:DSC_RESOURCE_PATH = $testdrive
9696
Set-Content -Path "$testdrive/test.dsc.resource.json" -Value $manifest
9797
$out = dsc resource list 2>&1
98-
write-verbose -verbose ($out | Out-String)
9998
$out | Should -Match 'WARN.*?Validation.*?invalid version' -Because ($out | Out-String)
10099
}
101100
finally {
@@ -166,21 +165,18 @@ Describe 'tests for resource discovery' {
166165
$env:PSModulePath = $oldPSModulePath
167166
}
168167

169-
It 'Verify non-zero exit code when resource not found' {
168+
It 'Verify non-zero exit code when resource not found: <cmdline>' -TestCases @(
169+
@{ cmdline = "dsc resource get -r abc/def" }
170+
@{ cmdline = "dsc resource get --all -r abc/def" }
171+
@{ cmdline = "dsc resource set -r abc/def -i 'abc'" }
172+
@{ cmdline = "dsc resource test -r abc/def -i 'abc'" }
173+
@{ cmdline = "dsc resource delete -r abc/def -i 'abc'" }
174+
@{ cmdline = "dsc resource export -r abc/def" }
175+
@{ cmdline = "dsc resource schema -r abc/def" }
176+
) {
177+
param($cmdline)
170178

171-
$out = dsc resource get -r abc/def
172-
$LASTEXITCODE | Should -Be 7
173-
$out = dsc resource get --all -r abc/def
174-
$LASTEXITCODE | Should -Be 7
175-
$out = 'abc' | dsc resource set -r abc/def -f -
176-
$LASTEXITCODE | Should -Be 7
177-
$out = 'abc' | dsc resource test -r abc/def -f -
178-
$LASTEXITCODE | Should -Be 7
179-
$out = 'abc' | dsc resource delete -r abc/def -f -
180-
$LASTEXITCODE | Should -Be 7
181-
$out = dsc resource export -r abc/def
182-
$LASTEXITCODE | Should -Be 7
183-
$out = dsc resource schema -r abc/def
179+
Invoke-Expression $cmdline 2>$null
184180
$LASTEXITCODE | Should -Be 7
185181
}
186182

dsc/tests/dsc_extension_discover.tests.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ Describe 'Discover extension tests' {
108108
$out.Count | Should -Be 1 -Because ($out | Out-String)
109109
$out.type | Should -Be 'Test/DiscoverRelative'
110110
$out = dsc resource list 2> $TestDrive/error.log
111-
write-verbose -verbose (Get-Content -Path "$TestDrive/error.log" -Raw)
112111
$LASTEXITCODE | Should -Be 0
113112
$out | Should -BeNullOrEmpty
114113
$errorMessage = Get-Content -Path "$TestDrive/error.log" -Raw

dsc/tests/dsc_include.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Describe 'Include tests' {
3535
"@
3636
$configPath = Join-Path $TestDrive 'config.dsc.yaml'
3737
$config | Set-Content -Path $configPath
38-
dsc config get -f $configPath
38+
dsc config get -f $configPath 2>$null
3939
$LASTEXITCODE | Should -Be 2
4040
}
4141

dsc/tests/dsc_securitycontext.tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Describe 'Tests for configuration security context metadata' {
1313
}
1414

1515
It 'Require admin' {
16-
$out = dsc config get -f $PSScriptRoot/../examples/require_admin.yaml
16+
$out = dsc config get -f $PSScriptRoot/../examples/require_admin.yaml 2>$null
1717
if ($isAdmin) {
1818
$LASTEXITCODE | Should -Be 0
1919
$out | Should -Not -BeNullOrEmpty
@@ -24,7 +24,7 @@ Describe 'Tests for configuration security context metadata' {
2424
}
2525

2626
It 'Require non-admin' {
27-
$out = dsc config get -f $PSScriptRoot/../examples/require_nonadmin.yaml
27+
$out = dsc config get -f $PSScriptRoot/../examples/require_nonadmin.yaml 2>$null
2828
if ($isAdmin) {
2929
$LASTEXITCODE | Should -Be 2
3030
}

dsc/tests/dsc_set.tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ Describe 'resource set tests' {
6161
"_exist": false
6262
}
6363
'@
64-
$null = registry config set --input $json
64+
$null = registry config set --input $json 2>$null
6565
}
6666
}
6767

@@ -73,7 +73,7 @@ Describe 'resource set tests' {
7373
"_exist": false
7474
}
7575
'@
76-
$null = registry config set --input $json
76+
$null = registry config set --input $json 2>$null
7777
}
7878
}
7979

dsc/tests/dsc_test.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Describe 'resource test tests' {
99
"valueName": "ProductName"
1010
}
1111
'@
12-
$current = registry config get --input $json
12+
$current = registry config get --input $json 2>$null
1313
$out = $current | dsc resource test -r Microsoft.Windows/Registry -f -
1414
$LASTEXITCODE | Should -Be 0
1515
$out = $out | ConvertFrom-Json

dsc/tests/dsc_variables.tests.ps1

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ resources:
2222
output: "[variables('myVariable')]"
2323
'@
2424
$out = dsc config get -i $configYaml | ConvertFrom-Json
25-
Write-Verbose -Verbose $out
2625
$LASTEXITCODE | Should -Be 0
2726
$out.results[0].result.actualState.output | Should -Be 'bar'
2827
}
@@ -39,7 +38,6 @@ resources:
3938
output: "[variables('myVariable')]"
4039
'@
4140
$out = dsc config get -i $configYaml 2>&1 | Out-String
42-
Write-Verbose -Verbose $out
4341
$LASTEXITCODE | Should -Be 2
4442
$out | Should -BeLike "*Variable 'myVariable' does not exist or has not been initialized yet*"
4543
}

powershell-adapter/psDscAdapter/powershell.resource.ps1

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ trap {
2626
Write-DscTrace -Operation Debug -Message ($_ | Format-List -Force | Out-String)
2727
}
2828

29-
# Adding some debug info to STDERR
30-
'PSVersion=' + $PSVersionTable.PSVersion.ToString() | Write-DscTrace
31-
'PSPath=' + $PSHome | Write-DscTrace
32-
'PSModulePath=' + $env:PSModulePath | Write-DscTrace
33-
3429
if ($Operation -eq 'ClearCache') {
3530
$cacheFilePath = if ($IsWindows) {
3631
# PS 6+ on Windows
@@ -44,11 +39,15 @@ if ($Operation -eq 'ClearCache') {
4439
}
4540
}
4641

47-
'Deleting cache file ' + $cacheFilePath | Write-DscTrace
4842
Remove-Item -Force -ea SilentlyContinue -Path $cacheFilePath
4943
exit 0
5044
}
5145

46+
# Adding some debug info to STDERR
47+
'PSVersion=' + $PSVersionTable.PSVersion.ToString() | Write-DscTrace
48+
'PSPath=' + $PSHome | Write-DscTrace
49+
'PSModulePath=' + $env:PSModulePath | Write-DscTrace
50+
5251
if ($PSVersionTable.PSVersion.Major -le 5) {
5352
# For Windows PowerShell, we want to remove any PowerShell 7 paths from PSModulePath
5453
$env:PSModulePath = ($env:PSModulePath -split ';' | Where-Object { $_ -notlike '*\powershell\*' }) -join ';'
@@ -87,7 +86,7 @@ switch ($Operation) {
8786

8887
# cache was refreshed on script load
8988
foreach ($dscResource in $dscResourceCache) {
90-
89+
9190
# https://learn.microsoft.com/dotnet/api/system.management.automation.dscresourceinfo
9291
$DscResourceInfo = $dscResource.DscResourceInfo
9392

@@ -99,7 +98,7 @@ switch ($Operation) {
9998
if ($DscResourceInfo.Capabilities) {
10099
$capabilities = $DscResourceInfo.Capabilities
101100
} elseif ($module.PrivateData.PSData.DscCapabilities) {
102-
101+
103102
$capabilities = $module.PrivateData.PSData.DscCapabilities
104103
} else {
105104
$capabilities = @('get', 'set', 'test')
@@ -183,7 +182,7 @@ switch ($Operation) {
183182
}
184183
$result += $actualState
185184
}
186-
185+
187186
# OUTPUT json to stderr for debug, and to stdout
188187
if ($Operation -eq 'Test') {
189188
$result = @{ result = $result; _inDesiredState = $inDesiredState } | ConvertTo-Json -Depth 10 -Compress
@@ -196,7 +195,7 @@ switch ($Operation) {
196195
}
197196
'Validate' {
198197
# VALIDATE not implemented
199-
198+
200199
# OUTPUT
201200
@{ valid = $true } | ConvertTo-Json
202201
}

registry/tests/registry.config.get.tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Describe 'Registry config get tests' {
88
"keyPath": "HKLM\\Software\\Microsoft\\Windows\\CurrentVersion"
99
}
1010
'@
11-
$out = registry config get --input $json
11+
$out = registry config get --input $json 2>$null
1212
$LASTEXITCODE | Should -Be 0
1313
$result = $out | ConvertFrom-Json
1414
$result.keyPath | Should -Be 'HKLM\Software\Microsoft\Windows\CurrentVersion'
@@ -22,7 +22,7 @@ Describe 'Registry config get tests' {
2222
"valueName": "ProgramFilesPath"
2323
}
2424
'@
25-
$out = registry config get --input $json
25+
$out = registry config get --input $json 2>$null
2626
$LASTEXITCODE | Should -Be 0
2727
$result = $out | ConvertFrom-Json
2828
$result.keyPath | Should -Be 'HKLM\Software\Microsoft\Windows\CurrentVersion'

registry/tests/registry.config.set.tests.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ Describe 'registry config set tests' {
1818
}
1919
}
2020
'@
21-
$out = registry config set --input $json
21+
$out = registry config set --input $json 2>$null
2222
$LASTEXITCODE | Should -Be 0
2323
$out | Should -BeNullOrEmpty
24-
$result = registry config get --input $json | ConvertFrom-Json
24+
$result = registry config get --input $json 2>$null | ConvertFrom-Json
2525
$result.keyPath | Should -Be 'HKCU\1\2\3'
2626
$result.valueName | Should -Be 'Hello'
2727
$result.valueData.String | Should -Be 'World'
2828
($result.psobject.properties | Measure-Object).Count | Should -Be 3
2929

30-
$out = registry config get --input $json
30+
$out = registry config get --input $json 2>$null
3131
$LASTEXITCODE | Should -Be 0
3232
$result = $out | ConvertFrom-Json
3333
$result.keyPath | Should -Be 'HKCU\1\2\3'
@@ -82,7 +82,7 @@ Describe 'registry config set tests' {
8282
properties:
8383
keyPath: 'HKCU\1'
8484
valueName: Test
85-
_exist: true
85+
_exist: true
8686
'@
8787

8888
$out = dsc config set -i $configYaml | ConvertFrom-Json

0 commit comments

Comments
 (0)