Skip to content

Commit 3ea98ff

Browse files
authored
Merge pull request #505 from SteveL-MSFT/psadapter-export-error
Add specific error if PS class resource doesn't implement export
2 parents 3049f4a + 309cfbb commit 3ea98ff

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ VariablesToExport = @()
3434
AliasesToExport = @()
3535

3636
# DSC resources to export from this module
37-
DscResourcesToExport = 'TestClassResource'
37+
DscResourcesToExport = @('TestClassResource', 'NoExport')
3838

3939
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
4040
PrivateData = @{

powershell-adapter/Tests/TestClassResource/0.0.1/TestClassResource.psm1

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,33 @@ class TestClassResource : BaseTestClass
7474
}
7575
}
7676

77+
[DscResource()]
78+
class NoExport: BaseTestClass
79+
{
80+
[DscProperty(Key)]
81+
[string] $Name
82+
83+
[DscProperty()]
84+
[string] $Prop1
85+
86+
[DscProperty()]
87+
[string] $EnumProp
88+
89+
[void] Set()
90+
{
91+
}
92+
93+
[bool] Test()
94+
{
95+
return $true
96+
}
97+
98+
[NoExport] Get()
99+
{
100+
return $this
101+
}
102+
}
103+
77104
function Test-World()
78105
{
79106
"Hello world from PSTestModule!"

powershell-adapter/Tests/powershellgroup.config.tests.ps1

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,23 @@ Describe 'PowerShell adapter resource tests' {
7171
$res.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1"
7272
}
7373

74+
It 'Export fails when class-based resource does not implement' {
75+
$yaml = @'
76+
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
77+
resources:
78+
- name: Working with class-based resources
79+
type: Microsoft.DSC/PowerShell
80+
properties:
81+
resources:
82+
- name: Class-resource Info
83+
type: TestClassResource/NoExport
84+
'@
85+
$out = $yaml | dsc config export 2>&1 | Out-String
86+
$LASTEXITCODE | Should -Be 2
87+
$out | Should -Not -BeNullOrEmpty
88+
$out | Should -BeLike "*ERROR*Export method not implemented by resource 'TestClassResource/NoExport'*"
89+
}
90+
7491
It 'Custom psmodulepath in config works' {
7592

7693
$OldPSModulePath = $env:PSModulePath

powershell-adapter/psDscAdapter/psDscAdapter.psm1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,10 @@ function Invoke-DscOperation {
475475
'Export' {
476476
$t = $dscResourceInstance.GetType()
477477
$method = $t.GetMethod('Export')
478+
if ($null -eq $method) {
479+
"Export method not implemented by resource '$($DesiredState.Type)'" | Write-DscTrace -Operation Error
480+
exit 1
481+
}
478482
$resultArray = $method.Invoke($null,$null)
479483
$addToActualState = $resultArray
480484
}

0 commit comments

Comments
 (0)