Skip to content

Commit

Permalink
Merge pull request #505 from SteveL-MSFT/psadapter-export-error
Browse files Browse the repository at this point in the history
Add specific error if PS class resource doesn't implement export
  • Loading branch information
SteveL-MSFT authored Aug 5, 2024
2 parents 3049f4a + 309cfbb commit 3ea98ff
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ VariablesToExport = @()
AliasesToExport = @()

# DSC resources to export from this module
DscResourcesToExport = 'TestClassResource'
DscResourcesToExport = @('TestClassResource', 'NoExport')

# 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.
PrivateData = @{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,33 @@ class TestClassResource : BaseTestClass
}
}

[DscResource()]
class NoExport: BaseTestClass
{
[DscProperty(Key)]
[string] $Name

[DscProperty()]
[string] $Prop1

[DscProperty()]
[string] $EnumProp

[void] Set()
{
}

[bool] Test()
{
return $true
}

[NoExport] Get()
{
return $this
}
}

function Test-World()
{
"Hello world from PSTestModule!"
Expand Down
17 changes: 17 additions & 0 deletions powershell-adapter/Tests/powershellgroup.config.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,23 @@ Describe 'PowerShell adapter resource tests' {
$res.resources[0].properties.result[0].Prop1 | Should -Be "Property of object1"
}

It 'Export fails when class-based resource does not implement' {
$yaml = @'
$schema: https://raw.githubusercontent.com/PowerShell/DSC/main/schemas/2024/04/config/document.json
resources:
- name: Working with class-based resources
type: Microsoft.DSC/PowerShell
properties:
resources:
- name: Class-resource Info
type: TestClassResource/NoExport
'@
$out = $yaml | dsc config export 2>&1 | Out-String
$LASTEXITCODE | Should -Be 2
$out | Should -Not -BeNullOrEmpty
$out | Should -BeLike "*ERROR*Export method not implemented by resource 'TestClassResource/NoExport'*"
}

It 'Custom psmodulepath in config works' {

$OldPSModulePath = $env:PSModulePath
Expand Down
4 changes: 4 additions & 0 deletions powershell-adapter/psDscAdapter/psDscAdapter.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,10 @@ function Invoke-DscOperation {
'Export' {
$t = $dscResourceInstance.GetType()
$method = $t.GetMethod('Export')
if ($null -eq $method) {
"Export method not implemented by resource '$($DesiredState.Type)'" | Write-DscTrace -Operation Error
exit 1
}
$resultArray = $method.Invoke($null,$null)
$addToActualState = $resultArray
}
Expand Down

0 comments on commit 3ea98ff

Please sign in to comment.