microsoft/hve-core
Publicmirrored from https://github.com/microsoft/hve-coreAvailable
scripts/tests/Mocks/AnalyzerFixtures.Tests.ps1
103lines · modecode
| 1 | #Requires -Modules Pester |
| 2 | # Copyright (c) Microsoft Corporation. |
| 3 | # SPDX-License-Identifier: MIT |
| 4 | |
| 5 | BeforeAll { |
| 6 | $script:ModulePath = Join-Path $PSScriptRoot 'AnalyzerFixtures.psm1' |
| 7 | Import-Module $script:ModulePath -Force |
| 8 | } |
| 9 | |
| 10 | AfterAll { |
| 11 | Remove-Module AnalyzerFixtures -Force -ErrorAction SilentlyContinue |
| 12 | } |
| 13 | |
| 14 | Describe 'New-MockAnalyzerIssue' -Tag 'Unit' { |
| 15 | Context 'Default invocation' { |
| 16 | BeforeAll { |
| 17 | $script:Issue = New-MockAnalyzerIssue |
| 18 | } |
| 19 | |
| 20 | It 'Returns a PSCustomObject' { |
| 21 | $script:Issue | Should -BeOfType ([pscustomobject]) |
| 22 | } |
| 23 | |
| 24 | It 'Exposes the documented default <Property> value' -TestCases @( |
| 25 | @{ Property = 'ScriptPath'; Expected = 'test.ps1' } |
| 26 | @{ Property = 'Line'; Expected = 1 } |
| 27 | @{ Property = 'Column'; Expected = 1 } |
| 28 | @{ Property = 'RuleName'; Expected = 'TestRule' } |
| 29 | @{ Property = 'Severity'; Expected = 'Warning' } |
| 30 | @{ Property = 'Message'; Expected = 'Test message' } |
| 31 | ) { |
| 32 | param($Property, $Expected) |
| 33 | $script:Issue.$Property | Should -Be $Expected |
| 34 | } |
| 35 | |
| 36 | It 'Exposes only the documented default property set' { |
| 37 | ($script:Issue.PSObject.Properties.Name | Sort-Object) | |
| 38 | Should -Be (@('Column', 'Line', 'Message', 'RuleName', 'ScriptPath', 'Severity') | Sort-Object) |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | Context 'Parameter overrides' { |
| 43 | It 'Honors the <Parameter> override' -TestCases @( |
| 44 | @{ Parameter = 'ScriptPath'; Value = 'src/foo.ps1' } |
| 45 | @{ Parameter = 'Line'; Value = 42 } |
| 46 | @{ Parameter = 'Column'; Value = 7 } |
| 47 | @{ Parameter = 'RuleName'; Value = 'PSAvoidUsingCmdletAliases' } |
| 48 | @{ Parameter = 'Severity'; Value = 'Error' } |
| 49 | @{ Parameter = 'Message'; Value = 'avoid alias' } |
| 50 | ) { |
| 51 | param($Parameter, $Value) |
| 52 | $splat = @{ $Parameter = $Value } |
| 53 | $issue = New-MockAnalyzerIssue @splat |
| 54 | $issue.$Parameter | Should -Be $Value |
| 55 | } |
| 56 | |
| 57 | It 'Honors all overrides together' { |
| 58 | $issue = New-MockAnalyzerIssue ` |
| 59 | -ScriptPath 'src/bar.ps1' ` |
| 60 | -Line 10 ` |
| 61 | -Column 5 ` |
| 62 | -RuleName 'PSUseDeclaredVarsMoreThanAssignments' ` |
| 63 | -Severity 'Information' ` |
| 64 | -Message 'declared but unused' |
| 65 | |
| 66 | $issue.ScriptPath | Should -Be 'src/bar.ps1' |
| 67 | $issue.Line | Should -Be 10 |
| 68 | $issue.Column | Should -Be 5 |
| 69 | $issue.RuleName | Should -Be 'PSUseDeclaredVarsMoreThanAssignments' |
| 70 | $issue.Severity | Should -Be 'Information' |
| 71 | $issue.Message | Should -Be 'declared but unused' |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | Context 'Parameter validation' { |
| 76 | It 'Rejects Severity values outside the PSScriptAnalyzer enum' { |
| 77 | { New-MockAnalyzerIssue -Severity 'Critical' } | |
| 78 | Should -Throw -ErrorId 'ParameterArgumentValidationError,New-MockAnalyzerIssue' |
| 79 | } |
| 80 | |
| 81 | It 'Accepts the documented Severity value <Value>' -TestCases @( |
| 82 | @{ Value = 'ParseError' } |
| 83 | @{ Value = 'Error' } |
| 84 | @{ Value = 'Warning' } |
| 85 | @{ Value = 'Information' } |
| 86 | ) { |
| 87 | param($Value) |
| 88 | (New-MockAnalyzerIssue -Severity $Value).Severity | Should -Be $Value |
| 89 | } |
| 90 | |
| 91 | It 'Rejects a non-positive <Parameter> value of <Value>' -TestCases @( |
| 92 | @{ Parameter = 'Line'; Value = 0 } |
| 93 | @{ Parameter = 'Line'; Value = -1 } |
| 94 | @{ Parameter = 'Column'; Value = 0 } |
| 95 | @{ Parameter = 'Column'; Value = -1 } |
| 96 | ) { |
| 97 | param($Parameter, $Value) |
| 98 | $splat = @{ $Parameter = $Value } |
| 99 | { New-MockAnalyzerIssue @splat } | |
| 100 | Should -Throw -ErrorId 'ParameterArgumentValidationError,New-MockAnalyzerIssue' |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | |