Skip to content

Commit c15e72a

Browse files
Copilotjohlju
andcommitted
Add detection of private functions used by class-based DSC resources
Added logic to ensure private functions used by class-based resources (those with [DscResource(...)] decoration) are also monitored for changes that should trigger DSC resource integration tests. Co-authored-by: johlju <[email protected]>
1 parent 3e8f0b1 commit c15e72a

File tree

2 files changed

+86
-4
lines changed

2 files changed

+86
-4
lines changed

.build/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The script checks for changes to:
1313
1. **DSC Resources**: Files under `source/DSCResources/`
1414
2. **Classes**: Files under `source/Classes/`
1515
3. **Public Commands**: Commands that are actually used by DSC resources or classes (dynamically discovered)
16-
4. **Private Functions**: Functions used by the monitored public commands
16+
4. **Private Functions**: Functions used by the monitored public commands or class-based DSC resources
1717
5. **Integration Tests**: DSC resource integration test files under `tests/Integration/Resources/`
1818
6. **Pipeline Configuration**: Azure Pipelines configuration and build scripts
1919

.build/Test-ShouldRunDscResourceIntegrationTests.ps1

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- DSC resources themselves
99
- Public commands used by DSC resources or classes
1010
- Private functions used by those public commands
11+
- Private functions used by class-based DSC resources
1112
- Classes used by DSC resources
1213
1314
.PARAMETER BaseBranch
@@ -259,6 +260,77 @@ function Get-PrivateFunctionsUsedByCommand
259260
return $privateFunctions
260261
}
261262

263+
<#
264+
.SYNOPSIS
265+
Gets private functions that class-based DSC resources depend on.
266+
267+
.DESCRIPTION
268+
This function analyzes class-based DSC resource files (those with [DscResource(...)]
269+
decoration) to identify which private functions they depend on by searching for
270+
function calls in the source code.
271+
272+
.PARAMETER SourcePath
273+
The source path containing Classes and Private directories.
274+
275+
.EXAMPLE
276+
Get-PrivateFunctionsUsedByClassResources -SourcePath 'source'
277+
278+
.OUTPUTS
279+
System.String[]. Array of private function names that are used by class-based DSC resources.
280+
#>
281+
function Get-PrivateFunctionsUsedByClassResources
282+
{
283+
[CmdletBinding()]
284+
param
285+
(
286+
[Parameter(Mandatory = $true)]
287+
[System.String]
288+
$SourcePath
289+
)
290+
291+
$privateFunctions = @()
292+
$classesPath = Join-Path -Path $SourcePath -ChildPath 'Classes'
293+
294+
if (-not (Test-Path -Path $classesPath))
295+
{
296+
return @()
297+
}
298+
299+
# Get all private function names
300+
$privateFunctionFiles = Get-ChildItem -Path (Join-Path -Path $SourcePath -ChildPath "Private") -Filter "*.ps1" -ErrorAction SilentlyContinue
301+
if (-not $privateFunctionFiles)
302+
{
303+
return @()
304+
}
305+
306+
$privateFunctionNames = $privateFunctionFiles | Select-Object -ExpandProperty BaseName
307+
308+
# Get all class files and check if they have [DscResource(...)] decoration
309+
$classFiles = Get-ChildItem -Path $classesPath -Filter '*.ps1' -ErrorAction SilentlyContinue
310+
foreach ($file in $classFiles)
311+
{
312+
$content = Get-Content -Path $file.FullName -Raw -ErrorAction SilentlyContinue
313+
if ($content)
314+
{
315+
# Check if this is a class-based DSC resource (has [DscResource(...)] decoration)
316+
if ($content -match '\[DscResource\([^\]]*\)\]')
317+
{
318+
# Look for private function usage in this class-based DSC resource
319+
foreach ($privateFunction in $privateFunctionNames)
320+
{
321+
if ($content -match "\b$privateFunction\b")
322+
{
323+
$privateFunctions += $privateFunction
324+
}
325+
}
326+
}
327+
}
328+
}
329+
330+
# Return unique private functions
331+
return $privateFunctions | Sort-Object -Unique
332+
}
333+
262334
<#
263335
.SYNOPSIS
264336
Main function that determines if DSC resource integration tests should run.
@@ -267,7 +339,8 @@ function Get-PrivateFunctionsUsedByCommand
267339
This function analyzes the changes between two git references and determines
268340
if DSC resource integration tests should run based on the files that have
269341
been modified. It checks for changes to DSC resources, classes, public
270-
commands used by DSC resources, private functions, integration tests, and
342+
commands used by DSC resources, private functions used by those public commands,
343+
private functions used by class-based DSC resources, integration tests, and
271344
pipeline configuration.
272345
273346
.PARAMETER BaseBranch
@@ -352,20 +425,29 @@ function Test-ShouldRunDscResourceIntegrationTests
352425
return $true
353426
}
354427

355-
# Check if any private functions used by the affected public commands are changed
428+
# Check if any private functions used by the affected public commands or class-based DSC resources are changed
356429
$changedPrivateFunctions = $changedFiles | Where-Object -FilterScript { $_ -match '^source/Private/(.+)\.ps1$' } |
357430
ForEach-Object -Process { [System.IO.Path]::GetFileNameWithoutExtension((Split-Path -Path $_ -Leaf)) }
358431

359432
$affectedPrivateFunctions = @()
433+
434+
# Check private functions used by public commands
360435
foreach ($command in $PublicCommandsUsedByDscResources)
361436
{
362437
$privateFunctionsUsed = Get-PrivateFunctionsUsedByCommand -CommandName $command -SourcePath $SourcePath
363438
$affectedPrivateFunctions += $privateFunctionsUsed | Where-Object -FilterScript { $_ -in $changedPrivateFunctions }
364439
}
365440

441+
# Check private functions used by class-based DSC resources
442+
$privateFunctionsUsedByClassResources = Get-PrivateFunctionsUsedByClassResources -SourcePath $SourcePath
443+
$affectedPrivateFunctions += $privateFunctionsUsedByClassResources | Where-Object -FilterScript { $_ -in $changedPrivateFunctions }
444+
445+
# Remove duplicates
446+
$affectedPrivateFunctions = $affectedPrivateFunctions | Sort-Object -Unique
447+
366448
if ($affectedPrivateFunctions)
367449
{
368-
Write-Host "Private functions used by DSC resource-related public commands have been modified. DSC resource integration tests will run."
450+
Write-Host "Private functions used by DSC resource-related public commands or class-based DSC resources have been modified. DSC resource integration tests will run."
369451
Write-Host "Affected private functions:"
370452
$affectedPrivateFunctions | ForEach-Object -Process { Write-Host " $_" }
371453
Write-Host ""

0 commit comments

Comments
 (0)