microsoft/hve-core
Publicmirrored from https://github.com/microsoft/hve-coreAvailable
scripts/tests/linting/AdrConsistency.Tests.ps1
165lines · modecode
| 1 | #Requires -Modules Pester |
| 2 | # Copyright (c) Microsoft Corporation. |
| 3 | # SPDX-License-Identifier: MIT |
| 4 | |
| 5 | [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'RuleCases', |
| 6 | Justification = 'Consumed by Pester -ForEach on It blocks at discovery time')] |
| 7 | param() |
| 8 | |
| 9 | BeforeDiscovery { |
| 10 | $RuleCases = @( |
| 11 | @{ RuleId = 'ADR-CONSISTENCY-001'; Dir = 'affected-components-mirror' } |
| 12 | @{ RuleId = 'ADR-CONSISTENCY-002'; Dir = 'success-criteria-source-resolves' } |
| 13 | @{ RuleId = 'ADR-CONSISTENCY-003'; Dir = 'state-placeholder-resolved' } |
| 14 | @{ RuleId = 'ADR-CONSISTENCY-004'; Dir = 'peer-planner-names' } |
| 15 | @{ RuleId = 'ADR-CONSISTENCY-005'; Dir = 'drivers-matrix-cardinality' } |
| 16 | @{ RuleId = 'ADR-CONSISTENCY-006'; Dir = 'risks-consequences-pairing' } |
| 17 | @{ RuleId = 'ADR-CONSISTENCY-007'; Dir = 'numeric-claim-generalized' } |
| 18 | @{ RuleId = 'ADR-CONSISTENCY-008'; Dir = 'driver-trigger-map-complete' } |
| 19 | @{ RuleId = 'ADR-CONSISTENCY-009'; Dir = 'affected-components-cited' } |
| 20 | ) |
| 21 | } |
| 22 | |
| 23 | BeforeAll { |
| 24 | Import-Module (Join-Path $PSScriptRoot '../../linting/Modules/AdrConsistency.psm1') -Force |
| 25 | Import-Module (Join-Path $PSScriptRoot '../../linting/Modules/AdrBodyParser.psm1') -Force |
| 26 | Mock Write-Host {} |
| 27 | $script:RepoRoot = (Resolve-Path -LiteralPath (Join-Path $PSScriptRoot '../../..')).Path |
| 28 | $script:FixtureRoot = Join-Path $PSScriptRoot 'fixtures/adr-consistency' |
| 29 | } |
| 30 | |
| 31 | AfterAll { |
| 32 | Remove-Module AdrConsistency -Force -ErrorAction SilentlyContinue |
| 33 | Remove-Module AdrBodyParser -Force -ErrorAction SilentlyContinue |
| 34 | } |
| 35 | |
| 36 | Describe 'Invoke-AdrConsistencyValidation pass fixtures' -Tag 'Unit' { |
| 37 | It 'rule <RuleId> (<Dir>) pass fixture produces zero violations' -ForEach $RuleCases { |
| 38 | $path = Join-Path $script:FixtureRoot $Dir 'pass.md' |
| 39 | $result = Invoke-AdrConsistencyValidation -Path $path -RepoRoot $script:RepoRoot |
| 40 | $result.Violations.Count | Should -Be 0 |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | Describe 'Invoke-AdrConsistencyValidation affected component citations' -Tag 'Unit' { |
| 45 | It 'accepts directory affected components cited in context' { |
| 46 | $path = Join-Path $TestDrive 'directory-affected-component.md' |
| 47 | @' |
| 48 | --- |
| 49 | status: accepted |
| 50 | date: 2025-01-01 |
| 51 | deciders: |
| 52 | - Engineering System |
| 53 | consulted: |
| 54 | - Documentation Maintainers |
| 55 | informed: |
| 56 | - Contributors |
| 57 | related_requirements: |
| 58 | - REQ-directory-citations |
| 59 | supersedes: [] |
| 60 | superseded_by: [] |
| 61 | affected_components: |
| 62 | - evals/ |
| 63 | success_criteria: |
| 64 | - metric: directory-citation-validation |
| 65 | target: zero false positive violations |
| 66 | measurement_window: per-PR |
| 67 | source: README.md |
| 68 | decisionMetadata: |
| 69 | driverToTriggerMap: |
| 70 | Directory affected component citations: ASR-directory-citation |
| 71 | --- |
| 72 | |
| 73 | # ADR 9999: Accept directory affected component citations |
| 74 | |
| 75 | ## Status |
| 76 | |
| 77 | Accepted |
| 78 | |
| 79 | ## Context |
| 80 | |
| 81 | The `evals/` directory is cited directly because it is the affected component. |
| 82 | |
| 83 | ## Decision |
| 84 | |
| 85 | Directory affected components may be cited as directory paths. |
| 86 | |
| 87 | ## Consequences |
| 88 | |
| 89 | The ADR consistency validator accepts directory citations. |
| 90 | |
| 91 | ## Affected Components |
| 92 | |
| 93 | * evals/ |
| 94 | |
| 95 | ## More Information |
| 96 | |
| 97 | See README.md for repository validation context. |
| 98 | '@ | Set-Content -LiteralPath $path -Encoding UTF8 |
| 99 | |
| 100 | $result = Invoke-AdrConsistencyValidation -Path $path -RepoRoot $script:RepoRoot |
| 101 | |
| 102 | $result.Violations.Count | Should -Be 0 |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | Describe 'Invoke-AdrConsistencyValidation fail fixtures' -Tag 'Unit' { |
| 107 | It 'rule <RuleId> (<Dir>) fail fixture fires the target rule' -ForEach $RuleCases { |
| 108 | $path = Join-Path $script:FixtureRoot $Dir 'fail.md' |
| 109 | $result = Invoke-AdrConsistencyValidation -Path $path -RepoRoot $script:RepoRoot |
| 110 | |
| 111 | $hits = @($result.Violations | Where-Object { $_.ruleId -eq $RuleId }) |
| 112 | $hits.Count | Should -BeGreaterThan 0 |
| 113 | } |
| 114 | |
| 115 | It 'rule <RuleId> (<Dir>) fail violation message has all template tokens substituted' -ForEach $RuleCases { |
| 116 | $path = Join-Path $script:FixtureRoot $Dir 'fail.md' |
| 117 | $result = Invoke-AdrConsistencyValidation -Path $path -RepoRoot $script:RepoRoot |
| 118 | |
| 119 | $hits = @($result.Violations | Where-Object { $_.ruleId -eq $RuleId }) |
| 120 | foreach ($hit in $hits) { |
| 121 | $hit.message | Should -Not -Match '\{[A-Za-z_]+\}' |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | Describe 'Invoke-AdrConsistencyValidation return contract' -Tag 'Unit' { |
| 127 | BeforeAll { |
| 128 | $passPath = Join-Path $script:FixtureRoot 'affected-components-mirror' 'pass.md' |
| 129 | $failPath = Join-Path $script:FixtureRoot 'affected-components-mirror' 'fail.md' |
| 130 | $script:PassResult = Invoke-AdrConsistencyValidation -Path $passPath -RepoRoot $script:RepoRoot |
| 131 | $script:FailResult = Invoke-AdrConsistencyValidation -Path $failPath -RepoRoot $script:RepoRoot |
| 132 | } |
| 133 | |
| 134 | It 'returns an object exposing File and Violations properties' { |
| 135 | $script:PassResult.PSObject.Properties.Name | Should -Contain 'File' |
| 136 | $script:PassResult.PSObject.Properties.Name | Should -Contain 'Violations' |
| 137 | } |
| 138 | |
| 139 | It 'File property echoes the input path' { |
| 140 | $expected = Join-Path $script:FixtureRoot 'affected-components-mirror' 'pass.md' |
| 141 | $script:PassResult.File | Should -Be $expected |
| 142 | } |
| 143 | |
| 144 | It 'Violations is an array' { |
| 145 | ($script:PassResult.Violations -is [System.Array]) | Should -BeTrue |
| 146 | } |
| 147 | |
| 148 | It 'each violation exposes file, ruleId, severity, message, line' { |
| 149 | $v = $script:FailResult.Violations[0] |
| 150 | $v.PSObject.Properties.Name | Should -Contain 'file' |
| 151 | $v.PSObject.Properties.Name | Should -Contain 'ruleId' |
| 152 | $v.PSObject.Properties.Name | Should -Contain 'severity' |
| 153 | $v.PSObject.Properties.Name | Should -Contain 'message' |
| 154 | $v.PSObject.Properties.Name | Should -Contain 'line' |
| 155 | } |
| 156 | |
| 157 | It 'violation severity is a registry-defined label' { |
| 158 | $script:FailResult.Violations[0].severity | Should -BeIn @('error', 'warn') |
| 159 | } |
| 160 | |
| 161 | It 'throws when the ADR file does not exist' { |
| 162 | $missing = Join-Path $TestDrive 'does-not-exist.md' |
| 163 | { Invoke-AdrConsistencyValidation -Path $missing -RepoRoot $script:RepoRoot } | Should -Throw |
| 164 | } |
| 165 | } |
| 166 | |