Skip to content

Commit 28b8083

Browse files
author
Steve Lee (POWERSHELL HE/HIM) (from Dev Box)
committed
Add specific error if PS class resource doesn't implement export
1 parent ce554dd commit 28b8083

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,49 @@ 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+
if (($this.Name -eq "TestClassResource1") -and ($this.Prop1 -eq "ValueForProp1"))
96+
{
97+
return $true
98+
}
99+
else
100+
{
101+
return $false
102+
}
103+
}
104+
105+
[NoExport] Get()
106+
{
107+
if ($this.Name -eq "TestClassResource1")
108+
{
109+
$this.Prop1 = "ValueForProp1"
110+
}
111+
else
112+
{
113+
$this.Prop1 = $env:DSC_CONFIG_ROOT
114+
}
115+
$this.EnumProp = ([EnumPropEnumeration]::Expected).ToString()
116+
return $this
117+
}
118+
}
119+
77120
function Test-World()
78121
{
79122
"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)