MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4# NOTE: This test file tests the Test-MergeConflictMarker function which detects Git merge conflict markers.5# IMPORTANT: Do NOT use here-strings or literal conflict markers (e.g., "<<<<<<<", "=======", ">>>>>>>")6# in this file, as they will trigger conflict marker detection in CI pipelines.7# Instead, use string multiplication (e.g., '<' * 7) to dynamically generate these markers at runtime.8 9Describe "Test-MergeConflictMarker" {10 BeforeAll {11 # Import the module12 Import-Module "$PSScriptRoot/../../tools/ci.psm1" -Force13 14 # Create a temporary test workspace15 $script:testWorkspace = Join-Path $TestDrive "workspace"16 New-Item -ItemType Directory -Path $script:testWorkspace -Force | Out-Null17 18 # Create temporary output files19 $script:testOutputPath = Join-Path $TestDrive "outputs.txt"20 $script:testSummaryPath = Join-Path $TestDrive "summary.md"21 }22 23 AfterEach {24 # Clean up test files after each test25 if (Test-Path $script:testWorkspace) {26 Get-ChildItem $script:testWorkspace -File -ErrorAction SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue27 }28 Remove-Item $script:testOutputPath -Force -ErrorAction SilentlyContinue29 Remove-Item $script:testSummaryPath -Force -ErrorAction SilentlyContinue30 }31 32 Context "When no files are provided" {33 It "Should handle empty file array gracefully" {34 # The function now accepts empty arrays to handle cases like delete-only PRs35 $emptyArray = @()36 Test-MergeConflictMarker -File $emptyArray -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath37 38 $outputs = Get-Content $script:testOutputPath39 $outputs | Should -Contain "files-checked=0"40 $outputs | Should -Contain "conflicts-found=0"41 42 $summary = Get-Content $script:testSummaryPath -Raw43 $summary | Should -Match "No Files to Check"44 }45 }46 47 Context "When files have no conflicts" {48 It "Should pass for clean files" {49 $testFile = Join-Path $script:testWorkspace "clean.txt"50 "This is a clean file" | Out-File $testFile -Encoding utf851 52 Test-MergeConflictMarker -File @("clean.txt") -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath53 54 $outputs = Get-Content $script:testOutputPath55 $outputs | Should -Contain "files-checked=1"56 $outputs | Should -Contain "conflicts-found=0"57 58 $summary = Get-Content $script:testSummaryPath -Raw59 $summary | Should -Match "No Conflicts Found"60 }61 }62 63 Context "When files have conflict markers" {64 It "Should detect <<<<<<< marker" {65 $testFile = Join-Path $script:testWorkspace "conflict1.txt"66 "Some content`n" + ('<' * 7) + " HEAD`nConflicting content" | Out-File $testFile -Encoding utf867 68 { Test-MergeConflictMarker -File @("conflict1.txt") -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath } | Should -Throw69 70 $outputs = Get-Content $script:testOutputPath71 $outputs | Should -Contain "files-checked=1"72 $outputs | Should -Contain "conflicts-found=1"73 }74 75 It "Should detect ======= marker" {76 $testFile = Join-Path $script:testWorkspace "conflict2.txt"77 "Some content`n" + ('=' * 7) + "`nMore content" | Out-File $testFile -Encoding utf878 79 { Test-MergeConflictMarker -File @("conflict2.txt") -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath } | Should -Throw80 }81 82 It "Should detect >>>>>>> marker" {83 $testFile = Join-Path $script:testWorkspace "conflict3.txt"84 "Some content`n" + ('>' * 7) + " branch-name`nMore content" | Out-File $testFile -Encoding utf885 86 { Test-MergeConflictMarker -File @("conflict3.txt") -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath } | Should -Throw87 }88 89 It "Should detect multiple markers in one file" {90 $testFile = Join-Path $script:testWorkspace "conflict4.txt"91 $content = "Some content`n" + ('<' * 7) + " HEAD`nContent A`n" + ('=' * 7) + "`nContent B`n" + ('>' * 7) + " branch`nMore content"92 $content | Out-File $testFile -Encoding utf893 94 { Test-MergeConflictMarker -File @("conflict4.txt") -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath } | Should -Throw95 96 $summary = Get-Content $script:testSummaryPath -Raw97 $summary | Should -Match "Conflicts Detected"98 $summary | Should -Match "conflict4.txt"99 }100 101 It "Should detect conflicts in multiple files" {102 $testFile1 = Join-Path $script:testWorkspace "conflict5.txt"103 ('<' * 7) + " HEAD" | Out-File $testFile1 -Encoding utf8104 105 $testFile2 = Join-Path $script:testWorkspace "conflict6.txt"106 ('=' * 7) | Out-File $testFile2 -Encoding utf8107 108 { Test-MergeConflictMarker -File @("conflict5.txt", "conflict6.txt") -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath } | Should -Throw109 110 $outputs = Get-Content $script:testOutputPath111 $outputs | Should -Contain "files-checked=2"112 $outputs | Should -Contain "conflicts-found=2"113 }114 }115 116 Context "When markers are not at line start" {117 It "Should not detect markers in middle of line" {118 $testFile = Join-Path $script:testWorkspace "notconflict.txt"119 "This line has <<<<<<< in the middle" | Out-File $testFile -Encoding utf8120 121 Test-MergeConflictMarker -File @("notconflict.txt") -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath122 123 $outputs = Get-Content $script:testOutputPath124 $outputs | Should -Contain "conflicts-found=0"125 }126 127 It "Should not detect markers with wrong number of characters" {128 $testFile = Join-Path $script:testWorkspace "wrongcount.txt"129 ('<' * 6) + " Only 6`n" + ('<' * 8) + " 8 characters" | Out-File $testFile -Encoding utf8130 131 Test-MergeConflictMarker -File @("wrongcount.txt") -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath132 133 $outputs = Get-Content $script:testOutputPath134 $outputs | Should -Contain "conflicts-found=0"135 }136 }137 138 Context "When handling special file scenarios" {139 It "Should skip non-existent files" {140 Test-MergeConflictMarker -File @("nonexistent.txt") -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath141 142 $outputs = Get-Content $script:testOutputPath143 $outputs | Should -Contain "files-checked=0"144 }145 146 It "Should handle absolute paths" {147 $testFile = Join-Path $script:testWorkspace "absolute.txt"148 "Clean content" | Out-File $testFile -Encoding utf8149 150 Test-MergeConflictMarker -File @($testFile) -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath151 152 $outputs = Get-Content $script:testOutputPath153 $outputs | Should -Contain "conflicts-found=0"154 }155 156 It "Should handle mixed relative and absolute paths" {157 $testFile1 = Join-Path $script:testWorkspace "relative.txt"158 "Clean" | Out-File $testFile1 -Encoding utf8159 160 $testFile2 = Join-Path $script:testWorkspace "absolute.txt"161 "Clean" | Out-File $testFile2 -Encoding utf8162 163 Test-MergeConflictMarker -File @("relative.txt", $testFile2) -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath164 165 $outputs = Get-Content $script:testOutputPath166 $outputs | Should -Contain "files-checked=2"167 $outputs | Should -Contain "conflicts-found=0"168 }169 }170 171 Context "When summary and output generation" {172 It "Should generate proper GitHub Actions outputs format" {173 $testFile = Join-Path $script:testWorkspace "test.txt"174 "Clean file" | Out-File $testFile -Encoding utf8175 176 Test-MergeConflictMarker -File @("test.txt") -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath177 178 $outputs = Get-Content $script:testOutputPath179 $outputs | Where-Object {$_ -match "^files-checked=\d+$"} | Should -Not -BeNullOrEmpty180 $outputs | Where-Object {$_ -match "^conflicts-found=\d+$"} | Should -Not -BeNullOrEmpty181 }182 183 It "Should generate markdown summary with conflict details" {184 $testFile = Join-Path $script:testWorkspace "marked.txt"185 $content = "Line 1`n" + ('<' * 7) + " HEAD`nLine 3`n" + ('=' * 7) + "`nLine 5"186 $content | Out-File $testFile -Encoding utf8187 188 { Test-MergeConflictMarker -File @("marked.txt") -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath } | Should -Throw189 190 $summary = Get-Content $script:testSummaryPath -Raw191 $summary | Should -Match "# Merge Conflict Marker Check Results"192 $summary | Should -Match "marked.txt"193 $summary | Should -Match "\| Line \| Marker \|"194 }195 }196}197 198Describe "Install-CIPester" {199 BeforeAll {200 # Import the module201 Import-Module "$PSScriptRoot/../../tools/ci.psm1" -Force202 }203 204 Context "When checking function exists" {205 It "Should export Install-CIPester function" {206 $function = Get-Command Install-CIPester -ErrorAction SilentlyContinue207 $function | Should -Not -BeNullOrEmpty208 $function.ModuleName | Should -Be 'ci'209 }210 211 It "Should have expected parameters" {212 $function = Get-Command Install-CIPester213 $function.Parameters.Keys | Should -Contain 'MinimumVersion'214 $function.Parameters.Keys | Should -Contain 'MaximumVersion'215 $function.Parameters.Keys | Should -Contain 'Force'216 }217 218 It "Should accept version parameters" {219 $function = Get-Command Install-CIPester220 $function.Parameters['MinimumVersion'].ParameterType.Name | Should -Be 'String'221 $function.Parameters['MaximumVersion'].ParameterType.Name | Should -Be 'String'222 $function.Parameters['Force'].ParameterType.Name | Should -Be 'SwitchParameter'223 }224 }225 226 Context "When validating real execution" {227 # These tests only run in CI where we can safely install/test Pester228 229 It "Should successfully run without errors when Pester exists" {230 if (!$env:CI) {231 Set-ItResult -Skipped -Because "Test requires CI environment to safely install Pester"232 }233 234 { Install-CIPester -ErrorAction Stop } | Should -Not -Throw235 }236 237 It "Should accept custom version parameters" {238 if (!$env:CI) {239 Set-ItResult -Skipped -Because "Test requires CI environment to safely install Pester"240 }241 242 { Install-CIPester -MinimumVersion '4.0.0' -MaximumVersion '5.99.99' -ErrorAction Stop } | Should -Not -Throw243 }244 }245}246 247 