File size: 11,841 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

# NOTE: This test file tests the Test-MergeConflictMarker function which detects Git merge conflict markers.
# IMPORTANT: Do NOT use here-strings or literal conflict markers (e.g., "<<<<<<<", "=======", ">>>>>>>")
# in this file, as they will trigger conflict marker detection in CI pipelines.
# Instead, use string multiplication (e.g., '<' * 7) to dynamically generate these markers at runtime.

Describe "Test-MergeConflictMarker" {
    BeforeAll {
        # Import the module
        Import-Module "$PSScriptRoot/../../tools/ci.psm1" -Force

        # Create a temporary test workspace
        $script:testWorkspace = Join-Path $TestDrive "workspace"
        New-Item -ItemType Directory -Path $script:testWorkspace -Force | Out-Null

        # Create temporary output files
        $script:testOutputPath = Join-Path $TestDrive "outputs.txt"
        $script:testSummaryPath = Join-Path $TestDrive "summary.md"
    }

    AfterEach {
        # Clean up test files after each test
        if (Test-Path $script:testWorkspace) {
            Get-ChildItem $script:testWorkspace -File -ErrorAction SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue
        }
        Remove-Item $script:testOutputPath -Force -ErrorAction SilentlyContinue
        Remove-Item $script:testSummaryPath -Force -ErrorAction SilentlyContinue
    }

    Context "When no files are provided" {
        It "Should handle empty file array gracefully" {
            # The function now accepts empty arrays to handle cases like delete-only PRs
            $emptyArray = @()
            Test-MergeConflictMarker -File $emptyArray -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath
            
            $outputs = Get-Content $script:testOutputPath
            $outputs | Should -Contain "files-checked=0"
            $outputs | Should -Contain "conflicts-found=0"
            
            $summary = Get-Content $script:testSummaryPath -Raw
            $summary | Should -Match "No Files to Check"
        }
    }

    Context "When files have no conflicts" {
        It "Should pass for clean files" {
            $testFile = Join-Path $script:testWorkspace "clean.txt"
            "This is a clean file" | Out-File $testFile -Encoding utf8

            Test-MergeConflictMarker -File @("clean.txt") -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath

            $outputs = Get-Content $script:testOutputPath
            $outputs | Should -Contain "files-checked=1"
            $outputs | Should -Contain "conflicts-found=0"

            $summary = Get-Content $script:testSummaryPath -Raw
            $summary | Should -Match "No Conflicts Found"
        }
    }

    Context "When files have conflict markers" {
        It "Should detect <<<<<<< marker" {
            $testFile = Join-Path $script:testWorkspace "conflict1.txt"
            "Some content`n" + ('<' * 7) + " HEAD`nConflicting content" | Out-File $testFile -Encoding utf8

            { Test-MergeConflictMarker -File @("conflict1.txt") -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath } | Should -Throw

            $outputs = Get-Content $script:testOutputPath
            $outputs | Should -Contain "files-checked=1"
            $outputs | Should -Contain "conflicts-found=1"
        }

        It "Should detect ======= marker" {
            $testFile = Join-Path $script:testWorkspace "conflict2.txt"
            "Some content`n" + ('=' * 7) + "`nMore content" | Out-File $testFile -Encoding utf8

            { Test-MergeConflictMarker -File @("conflict2.txt") -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath } | Should -Throw
        }

        It "Should detect >>>>>>> marker" {
            $testFile = Join-Path $script:testWorkspace "conflict3.txt"
            "Some content`n" + ('>' * 7) + " branch-name`nMore content" | Out-File $testFile -Encoding utf8

            { Test-MergeConflictMarker -File @("conflict3.txt") -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath } | Should -Throw
        }

        It "Should detect multiple markers in one file" {
            $testFile = Join-Path $script:testWorkspace "conflict4.txt"
            $content = "Some content`n" + ('<' * 7) + " HEAD`nContent A`n" + ('=' * 7) + "`nContent B`n" + ('>' * 7) + " branch`nMore content"
            $content | Out-File $testFile -Encoding utf8

            { Test-MergeConflictMarker -File @("conflict4.txt") -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath } | Should -Throw

            $summary = Get-Content $script:testSummaryPath -Raw
            $summary | Should -Match "Conflicts Detected"
            $summary | Should -Match "conflict4.txt"
        }

        It "Should detect conflicts in multiple files" {
            $testFile1 = Join-Path $script:testWorkspace "conflict5.txt"
            ('<' * 7) + " HEAD" | Out-File $testFile1 -Encoding utf8

            $testFile2 = Join-Path $script:testWorkspace "conflict6.txt"
            ('=' * 7) | Out-File $testFile2 -Encoding utf8

            { Test-MergeConflictMarker -File @("conflict5.txt", "conflict6.txt") -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath } | Should -Throw

            $outputs = Get-Content $script:testOutputPath
            $outputs | Should -Contain "files-checked=2"
            $outputs | Should -Contain "conflicts-found=2"
        }
    }

    Context "When markers are not at line start" {
        It "Should not detect markers in middle of line" {
            $testFile = Join-Path $script:testWorkspace "notconflict.txt"
            "This line has <<<<<<< in the middle" | Out-File $testFile -Encoding utf8

            Test-MergeConflictMarker -File @("notconflict.txt") -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath

            $outputs = Get-Content $script:testOutputPath
            $outputs | Should -Contain "conflicts-found=0"
        }

        It "Should not detect markers with wrong number of characters" {
            $testFile = Join-Path $script:testWorkspace "wrongcount.txt"
            ('<' * 6) + " Only 6`n" + ('<' * 8) + " 8 characters" | Out-File $testFile -Encoding utf8

            Test-MergeConflictMarker -File @("wrongcount.txt") -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath

            $outputs = Get-Content $script:testOutputPath
            $outputs | Should -Contain "conflicts-found=0"
        }
    }

    Context "When handling special file scenarios" {
        It "Should skip non-existent files" {
            Test-MergeConflictMarker -File @("nonexistent.txt") -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath

            $outputs = Get-Content $script:testOutputPath
            $outputs | Should -Contain "files-checked=0"
        }

        It "Should handle absolute paths" {
            $testFile = Join-Path $script:testWorkspace "absolute.txt"
            "Clean content" | Out-File $testFile -Encoding utf8

            Test-MergeConflictMarker -File @($testFile) -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath

            $outputs = Get-Content $script:testOutputPath
            $outputs | Should -Contain "conflicts-found=0"
        }

        It "Should handle mixed relative and absolute paths" {
            $testFile1 = Join-Path $script:testWorkspace "relative.txt"
            "Clean" | Out-File $testFile1 -Encoding utf8

            $testFile2 = Join-Path $script:testWorkspace "absolute.txt"
            "Clean" | Out-File $testFile2 -Encoding utf8

            Test-MergeConflictMarker -File @("relative.txt", $testFile2) -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath

            $outputs = Get-Content $script:testOutputPath
            $outputs | Should -Contain "files-checked=2"
            $outputs | Should -Contain "conflicts-found=0"
        }
    }

    Context "When summary and output generation" {
        It "Should generate proper GitHub Actions outputs format" {
            $testFile = Join-Path $script:testWorkspace "test.txt"
            "Clean file" | Out-File $testFile -Encoding utf8

            Test-MergeConflictMarker -File @("test.txt") -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath

            $outputs = Get-Content $script:testOutputPath
            $outputs | Where-Object {$_ -match "^files-checked=\d+$"} | Should -Not -BeNullOrEmpty
            $outputs | Where-Object {$_ -match "^conflicts-found=\d+$"} | Should -Not -BeNullOrEmpty
        }

        It "Should generate markdown summary with conflict details" {
            $testFile = Join-Path $script:testWorkspace "marked.txt"
            $content = "Line 1`n" + ('<' * 7) + " HEAD`nLine 3`n" + ('=' * 7) + "`nLine 5"
            $content | Out-File $testFile -Encoding utf8

            { Test-MergeConflictMarker -File @("marked.txt") -WorkspacePath $script:testWorkspace -OutputPath $script:testOutputPath -SummaryPath $script:testSummaryPath } | Should -Throw

            $summary = Get-Content $script:testSummaryPath -Raw
            $summary | Should -Match "# Merge Conflict Marker Check Results"
            $summary | Should -Match "marked.txt"
            $summary | Should -Match "\| Line \| Marker \|"
        }
    }
}

Describe "Install-CIPester" {
    BeforeAll {
        # Import the module
        Import-Module "$PSScriptRoot/../../tools/ci.psm1" -Force
    }

    Context "When checking function exists" {
        It "Should export Install-CIPester function" {
            $function = Get-Command Install-CIPester -ErrorAction SilentlyContinue
            $function | Should -Not -BeNullOrEmpty
            $function.ModuleName | Should -Be 'ci'
        }

        It "Should have expected parameters" {
            $function = Get-Command Install-CIPester
            $function.Parameters.Keys | Should -Contain 'MinimumVersion'
            $function.Parameters.Keys | Should -Contain 'MaximumVersion'
            $function.Parameters.Keys | Should -Contain 'Force'
        }

        It "Should accept version parameters" {
            $function = Get-Command Install-CIPester
            $function.Parameters['MinimumVersion'].ParameterType.Name | Should -Be 'String'
            $function.Parameters['MaximumVersion'].ParameterType.Name | Should -Be 'String'
            $function.Parameters['Force'].ParameterType.Name | Should -Be 'SwitchParameter'
        }
    }

    Context "When validating real execution" {
        # These tests only run in CI where we can safely install/test Pester

        It "Should successfully run without errors when Pester exists" {
            if (!$env:CI) {
                Set-ItResult -Skipped -Because "Test requires CI environment to safely install Pester"
            }

            { Install-CIPester -ErrorAction Stop } | Should -Not -Throw
        }

        It "Should accept custom version parameters" {
            if (!$env:CI) {
                Set-ItResult -Skipped -Because "Test requires CI environment to safely install Pester"
            }

            { Install-CIPester -MinimumVersion '4.0.0' -MaximumVersion '5.99.99' -ErrorAction Stop } | Should -Not -Throw
        }
    }
}