Skip to content

Commit c40236a

Browse files
authored
Update .build README (#2139)
1 parent 4881d27 commit c40236a

File tree

6 files changed

+146
-54
lines changed

6 files changed

+146
-54
lines changed

.build/README.md

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
# DSC Resource Integration Test Optimization
1+
# .build scripts
22

3-
This document describes the script used to dynamically determine whether DSC
4-
resource integration tests should run in Azure Pipelines.
3+
Documentation for the SqlServerDsc module build and pipeline scripts.
54

6-
## What the Script Does
5+
## `Test-ShouldRunDscResourceIntegrationTests.ps1`
76

8-
The `Test-ShouldRunDscResourceIntegrationTests.ps1` script analyzes git
7+
This script dynamically determines whether DSC resource integration tests
8+
should run in Azure Pipelines.
9+
10+
### What the Script Does
11+
<!-- markdownlint-disable-next-line MD013 -->
12+
The [`Test-ShouldRunDscResourceIntegrationTests.ps1`](./Test-ShouldRunDscResourceIntegrationTests.ps1) script analyzes git
913
changes between two references and determines if DSC resource integration tests
1014
need to run. It automatically discovers which public commands are used by DSC
1115
resources and classes, then checks if any relevant files have been modified.
1216

13-
## How It Works
17+
### How It Works
1418

1519
The script checks for changes to:
1620

@@ -23,26 +27,53 @@ The script checks for changes to:
2327
1. **Integration Tests**: DSC resource integration test files under
2428
`tests/Integration/Resources/`
2529

26-
## Usage
30+
### Parameters
31+
32+
| Parameter | Type | Default | Purpose |
33+
|-----------|------|---------|---------|
34+
| `BaseBranch` | String | `'origin/main'` | Base branch to compare against |
35+
| `CurrentBranch` | String | `'HEAD'` | Current branch or commit to compare |
36+
| `UseMergeBase` | Switch | `$false` | Use merge-base to compute diff base |
37+
38+
### Outputs
39+
40+
<!-- markdownlint-disable MD013 - Table with long descriptions -->
41+
| Output | Type | Description |
42+
|--------|------|-------------|
43+
| Return value | Boolean | `$true` when the monitored categories have relevant changes between the specified refs, `$false` when no such changes are detected |
44+
<!-- markdownlint-enable MD013 -->
45+
46+
### Usage
2747

28-
### Azure Pipelines
48+
#### Azure Pipelines
2949

3050
The Azure Pipelines task sets an output variable that downstream stages can
3151
use to conditionally run DSC resource integration tests. The script returns
3252
a boolean value that the pipeline captures, e.g.:
3353

54+
<!-- markdownlint-disable MD013 -->
3455
```yaml
3556
- powershell: |
36-
$shouldRun = ./.build/Test-ShouldRunDscResourceIntegrationTests.ps1 -BaseBranch $targetBranch -CurrentBranch HEAD
57+
$shouldRun = & ./.build/Test-ShouldRunDscResourceIntegrationTests.ps1 -BaseBranch $targetBranch -CurrentBranch HEAD -UseMergeBase
3758
Write-Host "##vso[task.setvariable variable=ShouldRunDscResourceIntegrationTests;isOutput=true]$shouldRun"
3859
displayName: 'Determine if DSC resource tests should run'
60+
name: determineShouldRun
3961
```
62+
<!-- markdownlint-enable MD013 -->
4063
41-
Downstream stages reference this output variable using the pattern:
42-
`dependencies.JobName.outputs['StepName.VariableName']` to gate their
43-
execution based on whether DSC resource tests should run.
64+
Downstream stages reference this output variable in a stage `condition:`
65+
using the pattern:
66+
<!-- markdownlint-disable MD013 -->
67+
```yaml
68+
condition: |
69+
and(
70+
succeeded(),
71+
eq(lower(dependencies.stageName.outputs['jobName.taskName.ShouldRunDscResourceIntegrationTests']), 'true')
72+
)
73+
```
74+
<!-- markdownlint-enable MD013 -->
4475

45-
### Command Line
76+
#### Command Line
4677

4778
```powershell
4879
# Basic usage (compares current HEAD with origin/main)
@@ -52,9 +83,3 @@ execution based on whether DSC resource tests should run.
5283
.build/Test-ShouldRunDscResourceIntegrationTests.ps1 -BaseBranch 'origin/dev' \
5384
-CurrentBranch 'feature-branch'
5485
```
55-
56-
## Dynamic Discovery
57-
58-
The script automatically discovers public commands used by DSC resources by
59-
scanning source files, eliminating the need to maintain hardcoded lists.
60-
This ensures accuracy and reduces maintenance overhead.

.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 introduced by $CurrentBranch since merge-base with $BaseBranch..."
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"

.github/instructions/dsc-community-style-guidelines-markdown.instructions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ applyTo: "**/*.md"
55

66
# Markdown Style Guidelines
77

8-
- Wrap lines at word boundaries when over 80 characters
8+
- Wrap lines at word boundaries when over 80 characters (except tables/code blocks)
99
- Use 2 spaces for indentation
1010
- Use '1.' for all items in ordered lists (1/1/1 numbering style)
1111
- Surround fenced code blocks with blank lines
12+
- Disable `MD013` rule by adding a comment for tables/code blocks exceeding 80 characters

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@
8686
"sqlps",
8787
"securestring",
8888
"encryptorname",
89-
"wsfc"
89+
"wsfc",
90+
"SOURCEBRANCH",
91+
"SOURCEBRANCHNAME",
92+
"setvariable"
9093
],
9194
"cSpell.ignorePaths": [
9295
".git"

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7575
resources, public commands used by resources, or related components.
7676
- Unit tests, QA tests, and command integration tests continue to run for
7777
all changes.
78-
- Provides time savings for non-DSC changes while maintaining coverage.
7978

8079
## [17.1.0] - 2025-05-22
8180

0 commit comments

Comments
 (0)