Skip to content

Commit 9db4985

Browse files
committed
Add UseMergeBase parameter to integration test functions for improved comparison
1 parent f1a1201 commit 9db4985

File tree

1 file changed

+71
-9
lines changed

1 file changed

+71
-9
lines changed

.build/Test-ShouldRunDscResourceIntegrationTests.ps1

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,20 @@
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
#>
@@ -35,7 +43,11 @@ param
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
@@ -347,6 +390,11 @@ function Get-PrivateFunctionsUsedByClassResources
347390
.PARAMETER CurrentBranch
348391
The current branch or commit to compare. Default is 'HEAD'.
349392
393+
.PARAMETER UseMergeBase
394+
When specified, compares the current branch against the merge-base with the base branch
395+
instead of directly comparing against the base branch. This is useful for comparing
396+
only the changes introduced by the current branch.
397+
350398
.PARAMETER SourcePath
351399
The source path containing the source code directories. Default is 'source'.
352400
@@ -356,6 +404,9 @@ function Get-PrivateFunctionsUsedByClassResources
356404
.EXAMPLE
357405
Test-ShouldRunDscResourceIntegrationTests -BaseBranch 'origin/main' -CurrentBranch 'feature-branch'
358406
407+
.EXAMPLE
408+
Test-ShouldRunDscResourceIntegrationTests -BaseBranch 'origin/main' -CurrentBranch 'feature-branch' -UseMergeBase
409+
359410
.OUTPUTS
360411
System.Boolean. Returns $true if DSC resource integration tests should run, $false otherwise.
361412
#>
@@ -372,21 +423,32 @@ function Test-ShouldRunDscResourceIntegrationTests
372423
[System.String]
373424
$CurrentBranch = 'HEAD',
374425

426+
[Parameter()]
427+
[System.Management.Automation.SwitchParameter]
428+
$UseMergeBase,
429+
375430
[Parameter()]
376431
[System.String]
377432
$SourcePath = 'source'
378433
)
379434

380435
Write-Host "##[section]Analyzing DSC Resource Integration Test Requirements"
381-
Write-Host "Analyzing changes between $BaseBranch and $CurrentBranch..."
436+
if ($UseMergeBase)
437+
{
438+
Write-Host "Analyzing changes between $BaseBranch (merge-base) and $CurrentBranch..."
439+
}
440+
else
441+
{
442+
Write-Host "Analyzing changes between $BaseBranch and $CurrentBranch..."
443+
}
382444
Write-Host ""
383445

384446
# Get list of public commands used by DSC resources dynamically
385447
$PublicCommandsUsedByDscResources = Get-PublicCommandsUsedByDscResources -SourcePath $SourcePath
386448
Write-Host "Discovered $($PublicCommandsUsedByDscResources.Count) public commands used by DSC resources and classes."
387449
Write-Host ""
388450

389-
$changedFiles = Get-ChangedFiles -From $BaseBranch -To $CurrentBranch
451+
$changedFiles = Get-ChangedFiles -From $BaseBranch -To $CurrentBranch -UseMergeBase:$UseMergeBase
390452

391453
if (-not $changedFiles)
392454
{
@@ -477,7 +539,7 @@ function Test-ShouldRunDscResourceIntegrationTests
477539
# If script is run directly (not imported), execute the main function
478540
if ($MyInvocation.InvocationName -ne '.')
479541
{
480-
$shouldRun = Test-ShouldRunDscResourceIntegrationTests -BaseBranch $BaseBranch -CurrentBranch $CurrentBranch
542+
$shouldRun = Test-ShouldRunDscResourceIntegrationTests -BaseBranch $BaseBranch -CurrentBranch $CurrentBranch -UseMergeBase:$UseMergeBase
481543

482544
# Provide clear final result with appropriate color coding
483545
Write-Host "##[section]Test Requirements Decision"

0 commit comments

Comments
 (0)