|
| 1 | +# .build scripts |
| 2 | + |
| 3 | +Documentation for the SqlServerDsc module build and pipeline scripts. |
| 4 | + |
| 5 | +## `Test-ShouldRunDscResourceIntegrationTests.ps1` |
| 6 | + |
| 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 |
| 13 | +changes between two references and determines if DSC resource integration tests |
| 14 | +need to run. It automatically discovers which public commands are used by DSC |
| 15 | +resources and classes, then checks if any relevant files have been modified. |
| 16 | + |
| 17 | +### How It Works |
| 18 | + |
| 19 | +The script performs an optimized analysis by checking for changes in this order: |
| 20 | + |
| 21 | +1. **Early Source Check**: First checks if any files under the configured |
| 22 | + source path (`-SourcePath`, default `source/`) have changed |
| 23 | + - If no source changes, skips integration tests immediately |
| 24 | +1. **DSC Resources**: Files under `source/DSCResources/` |
| 25 | +1. **Classes**: Files under `source/Classes/` |
| 26 | +1. **Public Commands**: Commands that are actually used by DSC resources or |
| 27 | + classes (dynamically discovered) |
| 28 | +1. **Private Functions**: Functions used by the monitored public commands or |
| 29 | + class-based DSC resources |
| 30 | +1. **Integration Tests**: DSC resource integration test files under |
| 31 | + `tests/Integration/Resources/` |
| 32 | + |
| 33 | +### Flow Diagram |
| 34 | + |
| 35 | +The following diagram illustrates the decision flow of the script: |
| 36 | + |
| 37 | +<!-- markdownlint-disable MD013 - Mermaid diagram has long lines --> |
| 38 | +```mermaid |
| 39 | +flowchart TD |
| 40 | + Start([Start Script]) --> Init[Initialize Parameters<br/>BaseBranch, CurrentBranch, UseMergeBase] |
| 41 | + Init --> GetChanges[Get Changed Files<br/>git diff between branches] |
| 42 | +
|
| 43 | + GetChanges --> HasChanges{Any Changed<br/>Files?} |
| 44 | + HasChanges -->|No| RunTrue[Return TRUE<br/>Run integration tests] |
| 45 | +
|
| 46 | + HasChanges -->|Yes| CheckSource{Any Changes<br/>Under SourcePath?} |
| 47 | + CheckSource -->|No| Skip[Return FALSE<br/>Skip integration tests] |
| 48 | +
|
| 49 | + CheckSource -->|Yes| Discover[Discover Public Commands<br/>Used by DSC Resources] |
| 50 | + Discover --> CheckDscRes{DSC Resources or<br/>Classes Changed?<br/>source/DSCResources/<br/>source/Classes/} |
| 51 | + CheckDscRes -->|Yes| RunTrue |
| 52 | +
|
| 53 | + CheckDscRes -->|No| CheckPublic{Public Commands<br/>Used by DSC<br/>Resources Changed?<br/>source/Public/} |
| 54 | + CheckPublic -->|Yes| RunTrue |
| 55 | +
|
| 56 | + CheckPublic -->|No| CheckPrivate{Private Functions<br/>Used by DSC-related<br/>Commands Changed?<br/>source/Private/} |
| 57 | + CheckPrivate -->|Yes| RunTrue |
| 58 | +
|
| 59 | + CheckPrivate -->|No| CheckTests{Integration Test<br/>Files Changed?<br/>tests/Integration/Resources/} |
| 60 | + CheckTests -->|Yes| RunTrue |
| 61 | +
|
| 62 | + CheckTests -->|No| Skip |
| 63 | +
|
| 64 | + RunTrue --> End([End]) |
| 65 | + Skip --> End |
| 66 | +
|
| 67 | + style Start fill:#e1f5fe |
| 68 | + style End fill:#e8f5e8 |
| 69 | + style RunTrue fill:#fff3e0 |
| 70 | + style Skip fill:#f3e5f5 |
| 71 | +``` |
| 72 | +<!-- markdownlint-enable MD013 --> |
| 73 | + |
| 74 | +### Parameters |
| 75 | + |
| 76 | +| Parameter | Type | Default | Purpose | |
| 77 | +|-----------|------|---------|---------| |
| 78 | +| `BaseBranch` | String | `'origin/main'` | Base branch to compare against | |
| 79 | +| `CurrentBranch` | String | `'HEAD'` | Current branch or commit to compare | |
| 80 | +| `UseMergeBase` | Switch | `$false` | Use merge-base to compute diff base | |
| 81 | + |
| 82 | +### Outputs |
| 83 | + |
| 84 | +<!-- markdownlint-disable MD013 - Table with long descriptions --> |
| 85 | +| Output | Type | Description | |
| 86 | +|--------|------|-------------| |
| 87 | +| Return value | Boolean | `$true` when the monitored categories have relevant changes between the specified refs, `$false` when no such changes are detected | |
| 88 | +<!-- markdownlint-enable MD013 --> |
| 89 | + |
| 90 | +### Usage |
| 91 | + |
| 92 | +#### Azure Pipelines |
| 93 | + |
| 94 | +The Azure Pipelines task sets an output variable that downstream stages can |
| 95 | +use to conditionally run DSC resource integration tests. The script returns |
| 96 | +a boolean value that the pipeline captures, e.g.: |
| 97 | + |
| 98 | +<!-- markdownlint-disable MD013 --> |
| 99 | +```yaml |
| 100 | +- powershell: | |
| 101 | + $shouldRun = & ./.build/Test-ShouldRunDscResourceIntegrationTests.ps1 -BaseBranch $targetBranch -CurrentBranch HEAD -UseMergeBase |
| 102 | + Write-Host "##vso[task.setvariable variable=ShouldRunDscResourceIntegrationTests;isOutput=true]$shouldRun" |
| 103 | + displayName: 'Determine if DSC resource tests should run' |
| 104 | + name: determineShouldRun |
| 105 | +``` |
| 106 | +<!-- markdownlint-enable MD013 --> |
| 107 | +
|
| 108 | +Downstream stages reference this output variable in a stage `condition:` |
| 109 | +using the pattern: |
| 110 | +<!-- markdownlint-disable MD013 --> |
| 111 | +```yaml |
| 112 | +condition: | |
| 113 | +and( |
| 114 | + succeeded(), |
| 115 | + eq(lower(dependencies.stageName.outputs['jobName.taskName.ShouldRunDscResourceIntegrationTests']), 'true') |
| 116 | +) |
| 117 | +``` |
| 118 | +<!-- markdownlint-enable MD013 --> |
| 119 | + |
| 120 | +#### Command Line |
| 121 | + |
| 122 | +```powershell |
| 123 | +# Basic usage (compares current HEAD with origin/main) |
| 124 | +.build/Test-ShouldRunDscResourceIntegrationTests.ps1 |
| 125 | +
|
| 126 | +# Custom branches |
| 127 | +.build/Test-ShouldRunDscResourceIntegrationTests.ps1 -BaseBranch 'origin/dev' \ |
| 128 | + -CurrentBranch 'feature-branch' |
| 129 | +``` |
0 commit comments