1717 . PARAMETER CurrentBranch
1818 The current branch or commit to compare. Default is 'HEAD'.
1919
20+ . PARAMETER UseMergeBase
21+ When specified, compares the current branch against the merge-base with the base branch
22+ instead of directly comparing against the base branch. This is useful for comparing
23+ only the changes introduced by the current branch.
24+
2025 . EXAMPLE
2126 Test-ShouldRunDscResourceIntegrationTests
2227
2328 . EXAMPLE
2429 Test-ShouldRunDscResourceIntegrationTests -BaseBranch 'origin/main' -CurrentBranch 'HEAD'
2530
31+ . EXAMPLE
32+ Test-ShouldRunDscResourceIntegrationTests -BaseBranch 'origin/main' -CurrentBranch 'HEAD' -UseMergeBase
33+
2634 . OUTPUTS
2735 System.Boolean. Returns $true if DSC resource integration tests should run, $false otherwise.
2836#>
3543
3644 [Parameter ()]
3745 [System.String ]
38- $CurrentBranch = ' HEAD'
46+ $CurrentBranch = ' HEAD' ,
47+
48+ [Parameter ()]
49+ [System.Management.Automation.SwitchParameter ]
50+ $UseMergeBase
3951)
4052
4153<#
@@ -137,17 +149,25 @@ function Get-PublicCommandsUsedByDscResources
137149 . DESCRIPTION
138150 This function retrieves the list of files that have been modified between
139151 two git references using git diff. It handles various scenarios including
140- different diff syntax and untracked files.
152+ different diff syntax and untracked files. Optionally can compare against
153+ the merge-base of the two references.
141154
142155 . PARAMETER From
143156 The source git reference (branch, commit, tag).
144157
145158 . PARAMETER To
146159 The target git reference (branch, commit, tag).
147160
161+ . PARAMETER UseMergeBase
162+ When specified, finds the merge-base between From and To references and
163+ compares To against that merge-base instead of directly against From.
164+
148165 . EXAMPLE
149166 Get-ChangedFiles -From 'origin/main' -To 'HEAD'
150167
168+ . EXAMPLE
169+ Get-ChangedFiles -From 'origin/main' -To 'HEAD' -UseMergeBase
170+
151171 . OUTPUTS
152172 System.String[]. Array of file paths that have been changed.
153173#>
@@ -162,23 +182,46 @@ function Get-ChangedFiles
162182
163183 [Parameter (Mandatory = $true )]
164184 [System.String ]
165- $To
185+ $To ,
186+
187+ [Parameter ()]
188+ [System.Management.Automation.SwitchParameter ]
189+ $UseMergeBase
166190 )
167191
168192 try
169193 {
194+ $compareFrom = $From
195+
196+ # If UseMergeBase is specified, find the merge-base between From and To
197+ if ($UseMergeBase )
198+ {
199+ Write-Verbose " Finding merge-base between $From and $To "
200+ $mergeBase = & git merge-base $From $To 2>&1
201+ if ($LASTEXITCODE -eq 0 -and $mergeBase )
202+ {
203+ $compareFrom = $mergeBase.Trim ()
204+ Write-Verbose " Using merge-base: $compareFrom "
205+ }
206+ else
207+ {
208+ Write-Warning " Failed to find merge-base between $From and $To . Falling back to direct comparison. Exit code: $LASTEXITCODE . Output: $mergeBase "
209+ $compareFrom = $From
210+ }
211+ }
212+
170213 # Try different git diff approaches
171214 $gitDiffOutput = $null
172215
173216 # First, try the standard diff
174- $gitDiffOutput = & git diff -- name- only " $From ..$To " 2>&1
217+ $gitDiffOutput = & git diff -- name- only " $compareFrom ..$To " 2>&1
175218 if ($LASTEXITCODE -eq 0 -and $gitDiffOutput )
176219 {
177220 return $gitDiffOutput | Where-Object - FilterScript { $_ -and $_.Trim () }
178221 }
179222
180223 # If that fails, try without the range syntax
181- $gitDiffOutput = & git diff -- name- only $From $To 2>&1
224+ $gitDiffOutput = & git diff -- name- only $compareFrom $To 2>&1
182225 if ($LASTEXITCODE -eq 0 -and $gitDiffOutput )
183226 {
184227 return $gitDiffOutput | Where-Object - FilterScript { $_ -and $_.Trim () }
@@ -194,7 +237,7 @@ function Get-ChangedFiles
194237 }
195238 }
196239
197- Write-Warning " Failed to get git diff between $From and $To . Exit code: $LASTEXITCODE . Output: $gitDiffOutput "
240+ Write-Warning " Failed to get git diff between $compareFrom and $To . Exit code: $LASTEXITCODE . Output: $gitDiffOutput "
198241 return @ ()
199242 }
200243 catch
@@ -337,16 +380,23 @@ function Get-PrivateFunctionsUsedByClassResources
337380 . DESCRIPTION
338381 This function analyzes the changes between two git references and determines
339382 if DSC resource integration tests should run based on the files that have
340- been modified. It checks for changes to DSC resources, classes, public
341- commands used by DSC resources, private functions used by those public commands,
342- private functions used by class-based DSC resources, and integration
343- tests.
383+ been modified. It performs an optimized analysis by first checking if any
384+ changes exist under the source/ folder. If no source changes are detected,
385+ it skips the expensive analysis and returns false. Otherwise, it checks for
386+ changes to DSC resources, classes, public commands used by DSC resources,
387+ private functions used by those public commands, private functions used by
388+ class-based DSC resources, and integration tests.
344389 . PARAMETER BaseBranch
345390 The base branch to compare against. Default is 'origin/main'.
346391
347392 . PARAMETER CurrentBranch
348393 The current branch or commit to compare. Default is 'HEAD'.
349394
395+ . PARAMETER UseMergeBase
396+ When specified, compares the current branch against the merge-base with the base branch
397+ instead of directly comparing against the base branch. This is useful for comparing
398+ only the changes introduced by the current branch.
399+
350400 . PARAMETER SourcePath
351401 The source path containing the source code directories. Default is 'source'.
352402
@@ -356,6 +406,9 @@ function Get-PrivateFunctionsUsedByClassResources
356406 . EXAMPLE
357407 Test-ShouldRunDscResourceIntegrationTests -BaseBranch 'origin/main' -CurrentBranch 'feature-branch'
358408
409+ . EXAMPLE
410+ Test-ShouldRunDscResourceIntegrationTests -BaseBranch 'origin/main' -CurrentBranch 'feature-branch' -UseMergeBase
411+
359412 . OUTPUTS
360413 System.Boolean. Returns $true if DSC resource integration tests should run, $false otherwise.
361414#>
@@ -372,21 +425,27 @@ function Test-ShouldRunDscResourceIntegrationTests
372425 [System.String ]
373426 $CurrentBranch = ' HEAD' ,
374427
428+ [Parameter ()]
429+ [System.Management.Automation.SwitchParameter ]
430+ $UseMergeBase ,
431+
375432 [Parameter ()]
376433 [System.String ]
377434 $SourcePath = ' source'
378435 )
379436
380437 Write-Host " ##[section]Analyzing DSC Resource Integration Test Requirements"
381- Write-Host " Analyzing changes between $BaseBranch and $CurrentBranch ..."
382- Write-Host " "
383-
384- # Get list of public commands used by DSC resources dynamically
385- $PublicCommandsUsedByDscResources = Get-PublicCommandsUsedByDscResources - SourcePath $SourcePath
386- Write-Host " Discovered $ ( $PublicCommandsUsedByDscResources.Count ) public commands used by DSC resources and classes."
438+ if ($UseMergeBase )
439+ {
440+ Write-Host " Analyzing changes introduced by $CurrentBranch since merge-base with $BaseBranch ..."
441+ }
442+ else
443+ {
444+ Write-Host " Analyzing changes between $BaseBranch and $CurrentBranch ..."
445+ }
387446 Write-Host " "
388447
389- $changedFiles = Get-ChangedFiles - From $BaseBranch - To $CurrentBranch
448+ $changedFiles = Get-ChangedFiles - From $BaseBranch - To $CurrentBranch - UseMergeBase: $UseMergeBase
390449
391450 if (-not $changedFiles )
392451 {
@@ -400,6 +459,21 @@ function Test-ShouldRunDscResourceIntegrationTests
400459 Write-Host " ##[endgroup]"
401460 Write-Host " "
402461
462+ # Early optimization: Check if any changes are under the source folder
463+ $sourcePrefix = ' ^' + [regex ]::Escape((($SourcePath -replace ' \\' , ' /' ) -replace ' /+$' , ' ' )) + ' /'
464+ $changedSourceFiles = $changedFiles | Where-Object - FilterScript { $_ -match $sourcePrefix }
465+ if (-not $changedSourceFiles )
466+ {
467+ Write-Host " No changes detected under the source folder. DSC resource integration tests can be skipped."
468+ Write-Host " "
469+ return $false
470+ }
471+
472+ # Get list of public commands used by DSC resources dynamically (only when needed)
473+ $PublicCommandsUsedByDscResources = Get-PublicCommandsUsedByDscResources - SourcePath $SourcePath
474+ Write-Host " Discovered $ ( $PublicCommandsUsedByDscResources.Count ) public commands used by DSC resources and classes."
475+ Write-Host " "
476+
403477 # Check if any DSC resources are directly changed
404478 $changedDscResources = $changedFiles | Where-Object - FilterScript { $_ -match ' ^source/DSCResources/' -or $_ -match ' ^source/Classes/' }
405479 if ($changedDscResources )
@@ -413,7 +487,7 @@ function Test-ShouldRunDscResourceIntegrationTests
413487 }
414488
415489 # Check if any public commands used by DSC resources are changed
416- $changedPublicCommands = $changedFiles | Where-Object - FilterScript { $_ -match ' ^source/Public/(.+)\.ps1$' } |
490+ $changedPublicCommands = $changedFiles | Where-Object - FilterScript { $_ -match ' ^source/Public/(.+)\.ps1$' } |
417491 ForEach-Object - Process { [System.IO.Path ]::GetFileNameWithoutExtension((Split-Path - Path $_ - Leaf)) }
418492
419493 $affectedCommands = $changedPublicCommands | Where-Object - FilterScript { $_ -in $PublicCommandsUsedByDscResources }
@@ -428,7 +502,7 @@ function Test-ShouldRunDscResourceIntegrationTests
428502 }
429503
430504 # Check if any private functions used by the affected public commands or class-based DSC resources are changed
431- $changedPrivateFunctions = $changedFiles | Where-Object - FilterScript { $_ -match ' ^source/Private/(.+)\.ps1$' } |
505+ $changedPrivateFunctions = $changedFiles | Where-Object - FilterScript { $_ -match ' ^source/Private/(.+)\.ps1$' } |
432506 ForEach-Object - Process { [System.IO.Path ]::GetFileNameWithoutExtension((Split-Path - Path $_ - Leaf)) }
433507
434508 $affectedPrivateFunctions = @ ()
@@ -477,7 +551,7 @@ function Test-ShouldRunDscResourceIntegrationTests
477551# If script is run directly (not imported), execute the main function
478552if ($MyInvocation.InvocationName -ne ' .' )
479553{
480- $shouldRun = Test-ShouldRunDscResourceIntegrationTests - BaseBranch $BaseBranch - CurrentBranch $CurrentBranch
554+ $shouldRun = Test-ShouldRunDscResourceIntegrationTests - BaseBranch $BaseBranch - CurrentBranch $CurrentBranch - UseMergeBase: $UseMergeBase
481555
482556 # Provide clear final result with appropriate color coding
483557 Write-Host " ##[section]Test Requirements Decision"
@@ -490,10 +564,6 @@ if ($MyInvocation.InvocationName -ne '.')
490564 Write-Host " RESULT: DSC resource integration tests will be SKIPPED"
491565 }
492566
493- # Output the result for the calling script to capture
494- Write-Output - InputObject " "
495- Write-Output - InputObject " ShouldRunDscResourceIntegrationTests: $shouldRun "
496-
497567 # Return the boolean value for pipeline script to use
498568 return $shouldRun
499569}
0 commit comments