|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +#Requires -Version 7.0 |
| 5 | +#Requires -Modules @{ ModuleName = 'Pester'; ModuleVersion = '5.0' } |
| 6 | + |
| 7 | +BeforeAll { |
| 8 | + . $PSScriptRoot/../../linting/Invoke-TFLint.ps1 |
| 9 | + $ErrorActionPreference = 'Continue' |
| 10 | + Import-Module (Join-Path $PSScriptRoot '../Mocks/GitMocks.psm1') -Force |
| 11 | + function tflint { } |
| 12 | +} |
| 13 | + |
| 14 | +Describe 'Invoke-TFLintCore' -Tag 'Unit' { |
| 15 | + BeforeAll { Save-CIEnvironment } |
| 16 | + AfterAll { Restore-CIEnvironment } |
| 17 | + |
| 18 | + BeforeEach { |
| 19 | + $script:MockFiles = Initialize-MockCIEnvironment -Workspace $TestDrive |
| 20 | + $script:TestOutputPath = Join-Path $TestDrive 'logs/tflint-results.json' |
| 21 | + $script:TestTerraformDir = Join-Path $TestDrive 'infrastructure/terraform' |
| 22 | + $script:TestConfigPath = Join-Path $TestDrive '.tflint.hcl' |
| 23 | + |
| 24 | + New-Item -ItemType Directory -Force -Path $script:TestTerraformDir | Out-Null |
| 25 | + '{}' | Out-File $script:TestConfigPath |
| 26 | + |
| 27 | + Mock git { return $TestDrive } -ParameterFilter { $args[0] -eq 'rev-parse' } |
| 28 | + Mock tflint { return '{"issues":[],"errors":[]}' } |
| 29 | + } |
| 30 | + |
| 31 | + AfterEach { |
| 32 | + Restore-CIEnvironment |
| 33 | + Remove-MockCIFiles -MockFiles $script:MockFiles |
| 34 | + } |
| 35 | + |
| 36 | + Context 'tool availability' { |
| 37 | + It 'Returns 1 when tflint is not installed' { |
| 38 | + Mock Get-Command { return $null } -ParameterFilter { $Name -eq 'tflint' } |
| 39 | + $result = Invoke-TFLintCore -ConfigPath $script:TestConfigPath ` |
| 40 | + -OutputPath $script:TestOutputPath -TerraformDir $script:TestTerraformDir |
| 41 | + $result | Should -Be 1 |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + Context 'clean run' { |
| 46 | + It 'Returns 0 when no issues found' { |
| 47 | + Mock tflint { $global:LASTEXITCODE = 0; return '{"issues":[],"errors":[]}' } |
| 48 | + $result = Invoke-TFLintCore -ConfigPath $script:TestConfigPath ` |
| 49 | + -OutputPath $script:TestOutputPath -TerraformDir $script:TestTerraformDir |
| 50 | + $result | Should -Be 0 |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + Context 'violations found' { |
| 55 | + It 'Returns non-zero when violations found' { |
| 56 | + Mock tflint { $global:LASTEXITCODE = 2; return '{"issues":[{"rule":{"name":"terraform_naming_convention","severity":"warning","link":""},"message":"test violation","range":{"filename":"main.tf","start":{"line":1,"column":1},"end":{"line":1,"column":10}},"callers":[]}],"errors":[]}' } |
| 57 | + $result = Invoke-TFLintCore -ConfigPath $script:TestConfigPath ` |
| 58 | + -OutputPath $script:TestOutputPath -TerraformDir $script:TestTerraformDir |
| 59 | + $result | Should -Not -Be 0 |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + Context 'output file creation' { |
| 64 | + It 'Creates output file' { |
| 65 | + Mock tflint { $global:LASTEXITCODE = 0; return '{"issues":[],"errors":[]}' } |
| 66 | + Invoke-TFLintCore -ConfigPath $script:TestConfigPath ` |
| 67 | + -OutputPath $script:TestOutputPath -TerraformDir $script:TestTerraformDir |
| 68 | + $script:TestOutputPath | Should -Exist |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments