Skip to content

Commit

Permalink
Get-ClassResourceProperty: No longer throws exception for missing s…
Browse files Browse the repository at this point in the history
…ource file (#128)

- `Get-ClassResourceProperty`
  - Now longer throws an exception if a parent class that isn't part of the
    module is being used. If a class's source file is not found the class
    is skipped (issue #127).
  • Loading branch information
johlju authored Jan 3, 2023
1 parent 3ab3806 commit 74bc240
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- `Get-ClassResourceProperty`
- Regression tests for PR #123.
- Now longer throws an exception if a parent class that isn't part of the
module is being used. If a class's source file is not found the class
is skipped (fixes [issue #127](https://github.com/dsccommunity/DscResource.DocGenerator/issues/127)).

## [0.11.1] - 2022-08-09

Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ stages:
- job: test_linux
displayName: 'Unit Linux'
pool:
vmImage: 'ubuntu-latest'
vmImage: 'ubuntu-20.04'
timeoutInMinutes: 0
steps:
- task: DownloadPipelineArtifact@2
Expand Down
9 changes: 9 additions & 0 deletions source/Private/Get-ClassResourceProperty.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ function Get-ClassResourceProperty

$sourceFilePath = Join-Path -Path $SourcePath -ChildPath ('Classes/???.{0}.ps1' -f $currentClassName)

<#
Skip if the class's source file does not exist. Thi can happen if the
class uses a parent class from a different module.
#>
if (-not (Test-Path -Path $sourceFilePath))
{
continue
}

$dscResourceCommentBasedHelp = Get-CommentBasedHelp -Path $sourceFilePath

$astFilter = {
Expand Down
121 changes: 121 additions & 0 deletions tests/unit/private/Get-ClassResourceProperty.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,127 @@ class ResourceBase
}
}

Context 'When the resource has a parent class that does not have a source file (part of another module)' {
BeforeAll {
$mockBuiltModulePath = Join-Path -Path $TestDrive -ChildPath 'output\MyClassModule\1.0.0'
$mockSourcePath = Join-Path -Path $TestDrive -ChildPath 'source'

New-Item -Path $mockBuiltModulePath -ItemType 'Directory' -Force
New-Item -Path "$mockSourcePath\Classes" -ItemType 'Directory' -Force

# The class DSC resource in the built module.
$mockBuiltModuleScript = @'
class ResourceBase
{
hidden [System.String] $NotADscProperty
[DscProperty()]
[System.String]
$Ensure
}
[DscResource()]
class MyDscResource : ResourceBase
{
[MyDscResource] Get()
{
return [MyDscResource] $this
}
[System.Boolean] Test()
{
return $true
}
[void] Set() {}
[DscProperty(Key)]
[System.String] $ProjectName
[DscProperty()]
[ValidateSet('Up', 'Down')]
[System.String[]] $ValidateSetProperty
}
'@
# Uses Microsoft.PowerShell.Utility\Out-File to override the stub that is needed for the mocks.
$mockBuiltModuleScript | Microsoft.PowerShell.Utility\Out-File -FilePath "$mockBuiltModulePath\MyClassModule.psm1" -Encoding ascii -Force

<#
The source file of class DSC resource. This file is not actually
referencing the base class to simplify the tests.
The property ValidateSetProperty does not have a parameter description
to be able to test missing description.
#>
$mockResourceSourceScript = @'
<#
.SYNOPSIS
Resource synopsis.
.DESCRIPTION
Resource description.
.PARAMETER ProjectName
ProjectName description.
#>
[DscResource()]
class MyDscResource
{
[MyDscResource] Get()
{
return [MyDscResource] $this
}
[System.Boolean] Test()
{
return $true
}
[void] Set() {}
[DscProperty(Key)]
[System.String] $ProjectName
[DscProperty()]
[ValidateSet('Up', 'Down')]
[System.String[]] $ValidateSetProperty
}
'@
# Uses Microsoft.PowerShell.Utility\Out-File to override the stub that is needed for the mocks.
$mockResourceSourceScript | Microsoft.PowerShell.Utility\Out-File -FilePath "$mockSourcePath\Classes\010.MyDscResource.ps1" -Encoding ascii -Force

It 'Should return the expected DSC class resource properties' {
$mockGetClassResourcePropertyParameters = @{
SourcePath = $mockSourcePath
BuiltModuleScriptFilePath = Join-Path -Path $mockBuiltModulePath -ChildPath 'MyClassModule.psm1'
ClassName = @(
'ResourceBase'
'MyDscResource'
)
}

$getClassResourcePropertyResult = Get-ClassResourceProperty @mockGetClassResourcePropertyParameters
$getClassResourcePropertyResult | Should -HaveCount 2
$getClassResourcePropertyResult.Name | Should -Contain 'ProjectName'
$getClassResourcePropertyResult.Name | Should -Contain 'ValidateSetProperty'

$ensurePropertyResult = $getClassResourcePropertyResult.Where({$_.Name -eq 'ProjectName'})
$ensurePropertyResult.State | Should -Be 'Key'
$ensurePropertyResult.Description | Should -Be 'ProjectName description.'
$ensurePropertyResult.DataType | Should -Be 'System.String'
$ensurePropertyResult.IsArray | Should -BeFalse
$ensurePropertyResult.ValueMap | Should -BeNullOrEmpty

$ensurePropertyResult = $getClassResourcePropertyResult.Where({$_.Name -eq 'ValidateSetProperty'})
$ensurePropertyResult.State | Should -Be 'Write'
$ensurePropertyResult.Description | Should -BeNullOrEmpty
$ensurePropertyResult.DataType | Should -Be 'System.String[]'
$ensurePropertyResult.IsArray | Should -BeFalse
$ensurePropertyResult.ValueMap | Should -Contain 'Up'
$ensurePropertyResult.ValueMap | Should -Contain 'Down'
}
}
}

Context 'When a base class is missing comment-based help' {
BeforeAll {
$mockBuiltModulePath = Join-Path -Path $TestDrive -ChildPath 'output\MyClassModule\1.0.0'
Expand Down

0 comments on commit 74bc240

Please sign in to comment.