microsoft/hve-core
Publicmirrored from https://github.com/microsoft/hve-coreAvailable
scripts/tests/linting/AdrConsistency.Tests.ps1
103lines · 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 fail fixtures' -Tag 'Unit' { |
| 45 | It 'rule <RuleId> (<Dir>) fail fixture fires the target rule' -ForEach $RuleCases { |
| 46 | $path = Join-Path $script:FixtureRoot $Dir 'fail.md' |
| 47 | $result = Invoke-AdrConsistencyValidation -Path $path -RepoRoot $script:RepoRoot |
| 48 | |
| 49 | $hits = @($result.Violations | Where-Object { $_.ruleId -eq $RuleId }) |
| 50 | $hits.Count | Should -BeGreaterThan 0 |
| 51 | } |
| 52 | |
| 53 | It 'rule <RuleId> (<Dir>) fail violation message has all template tokens substituted' -ForEach $RuleCases { |
| 54 | $path = Join-Path $script:FixtureRoot $Dir 'fail.md' |
| 55 | $result = Invoke-AdrConsistencyValidation -Path $path -RepoRoot $script:RepoRoot |
| 56 | |
| 57 | $hits = @($result.Violations | Where-Object { $_.ruleId -eq $RuleId }) |
| 58 | foreach ($hit in $hits) { |
| 59 | $hit.message | Should -Not -Match '\{[A-Za-z_]+\}' |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | Describe 'Invoke-AdrConsistencyValidation return contract' -Tag 'Unit' { |
| 65 | BeforeAll { |
| 66 | $passPath = Join-Path $script:FixtureRoot 'affected-components-mirror' 'pass.md' |
| 67 | $failPath = Join-Path $script:FixtureRoot 'affected-components-mirror' 'fail.md' |
| 68 | $script:PassResult = Invoke-AdrConsistencyValidation -Path $passPath -RepoRoot $script:RepoRoot |
| 69 | $script:FailResult = Invoke-AdrConsistencyValidation -Path $failPath -RepoRoot $script:RepoRoot |
| 70 | } |
| 71 | |
| 72 | It 'returns an object exposing File and Violations properties' { |
| 73 | $script:PassResult.PSObject.Properties.Name | Should -Contain 'File' |
| 74 | $script:PassResult.PSObject.Properties.Name | Should -Contain 'Violations' |
| 75 | } |
| 76 | |
| 77 | It 'File property echoes the input path' { |
| 78 | $expected = Join-Path $script:FixtureRoot 'affected-components-mirror' 'pass.md' |
| 79 | $script:PassResult.File | Should -Be $expected |
| 80 | } |
| 81 | |
| 82 | It 'Violations is an array' { |
| 83 | ($script:PassResult.Violations -is [System.Array]) | Should -BeTrue |
| 84 | } |
| 85 | |
| 86 | It 'each violation exposes file, ruleId, severity, message, line' { |
| 87 | $v = $script:FailResult.Violations[0] |
| 88 | $v.PSObject.Properties.Name | Should -Contain 'file' |
| 89 | $v.PSObject.Properties.Name | Should -Contain 'ruleId' |
| 90 | $v.PSObject.Properties.Name | Should -Contain 'severity' |
| 91 | $v.PSObject.Properties.Name | Should -Contain 'message' |
| 92 | $v.PSObject.Properties.Name | Should -Contain 'line' |
| 93 | } |
| 94 | |
| 95 | It 'violation severity is a registry-defined label' { |
| 96 | $script:FailResult.Violations[0].severity | Should -BeIn @('error', 'warn') |
| 97 | } |
| 98 | |
| 99 | It 'throws when the ADR file does not exist' { |
| 100 | $missing = Join-Path $TestDrive 'does-not-exist.md' |
| 101 | { Invoke-AdrConsistencyValidation -Path $missing -RepoRoot $script:RepoRoot } | Should -Throw |
| 102 | } |
| 103 | } |
| 104 | |