microsoft/hve-core

Public

mirrored fromhttps://github.com/microsoft/hve-coreAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
hve-core-v2.3.9

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

scripts/tests/linting/Invoke-LinkLanguageCheck.Tests.ps1

383lines · modecode

1#Requires -Modules Pester
2# Copyright (c) Microsoft Corporation.
3# SPDX-License-Identifier: MIT
4<#
5.SYNOPSIS
6 Pester tests for Invoke-LinkLanguageCheck.ps1 script
7.DESCRIPTION
8 Tests for Link Language Check wrapper script:
9 - Link-Lang-Check.ps1 invocation
10 - JSON parsing
11 - GitHub Actions integration
12 - Exit code handling
13#>
14
15BeforeAll {
16 $script:ScriptPath = Join-Path $PSScriptRoot '../../linting/Invoke-LinkLanguageCheck.ps1'
17 $script:ModulePath = Join-Path $PSScriptRoot '../../linting/Modules/LintingHelpers.psm1'
18 $script:CIHelpersPath = Join-Path $PSScriptRoot '../../lib/Modules/CIHelpers.psm1'
19
20 # Import modules for mocking
21 Import-Module $script:ModulePath -Force
22 Import-Module $script:CIHelpersPath -Force
23
24 . $script:ScriptPath
25}
26
27AfterAll {
28 Remove-Module LintingHelpers -Force -ErrorAction SilentlyContinue
29 Remove-Module CIHelpers -Force -ErrorAction SilentlyContinue
30}
31
32#region Link-Lang-Check Invocation Tests
33
34Describe 'Link-Lang-Check.ps1 Invocation' -Tag 'Unit' {
35 Context 'Script discovery' {
36 It 'Link-Lang-Check.ps1 exists' {
37 $linkLangCheckPath = Join-Path $PSScriptRoot '../../linting/Link-Lang-Check.ps1'
38 Test-Path $linkLangCheckPath | Should -BeTrue
39 }
40 }
41
42 Context 'Normal execution' {
43 It 'Invoke-LinkLanguageCheck.ps1 exists' {
44 $scriptExists = Test-Path $script:ScriptPath
45 $scriptExists | Should -BeTrue
46 }
47 }
48}
49
50#endregion
51
52#region Invoke-LinkLanguageCheckCore Tests
53
54Describe 'Invoke-LinkLanguageCheckCore' -Tag 'Unit' {
55 Context 'Not in git repository' {
56 BeforeEach {
57 Mock git {
58 $global:LASTEXITCODE = 128
59 return 'fatal: not a git repository'
60 } -ParameterFilter { $args -contains 'rev-parse' }
61
62 Mock Write-Error { }
63 }
64
65 It 'Returns failure exit code' {
66 Invoke-LinkLanguageCheckCore -ExcludePaths @() | Should -Be 1
67 }
68 }
69
70 Context 'Issues found in link scan' {
71 BeforeEach {
72 $script:RepoRoot = $TestDrive
73 $script:MockLinkLang = Join-Path $TestDrive 'mock-link-lang.ps1'
74
75 @'
76param([string[]]$ExcludePaths = @())
77$json = @"
78[
79 {"file":"docs/a.md","line_number":1,"original_url":"https://docs.microsoft.com/en-us/a"},
80 {"file":"docs/b.md","line_number":2,"original_url":"https://docs.microsoft.com/en-us/b"}
81]
82"@
83
84Write-Output $json
85'@ | Set-Content -Path $script:MockLinkLang -Encoding utf8
86
87 Mock git {
88 $global:LASTEXITCODE = 0
89 return $script:RepoRoot
90 } -ParameterFilter { $args -contains 'rev-parse' }
91
92 Mock Join-Path {
93 return $script:MockLinkLang
94 } -ParameterFilter { $ChildPath -eq 'Link-Lang-Check.ps1' }
95
96 Mock Write-CIAnnotation { }
97 Mock Set-CIOutput { }
98 Mock Set-CIEnv { }
99 Mock Write-CIStepSummary { }
100 Mock Write-Host { }
101 }
102
103 It 'Returns failure exit code and records outputs' {
104 Invoke-LinkLanguageCheckCore -ExcludePaths @('scripts/tests/**') | Should -Be 1
105 Should -Invoke Set-CIOutput -Times 1
106 Should -Invoke Set-CIEnv -Times 1
107 Should -Invoke Write-CIAnnotation -Times 2
108 }
109 }
110
111 Context 'No issues found' {
112 BeforeEach {
113 $script:RepoRoot = $TestDrive
114 $script:MockLinkLang = Join-Path $TestDrive 'mock-link-lang-empty.ps1'
115
116 @'
117param([string[]]$ExcludePaths = @())
118$json = @"
119[]
120"@
121
122Write-Output $json
123'@ | Set-Content -Path $script:MockLinkLang -Encoding utf8
124
125 Mock git {
126 $global:LASTEXITCODE = 0
127 return $script:RepoRoot
128 } -ParameterFilter { $args -contains 'rev-parse' }
129
130 Mock Join-Path {
131 return $script:MockLinkLang
132 } -ParameterFilter { $ChildPath -eq 'Link-Lang-Check.ps1' }
133
134 Mock Set-CIOutput { }
135 Mock Write-CIStepSummary { }
136 Mock Write-Host { }
137 }
138
139 It 'Returns success exit code and records outputs' {
140 Invoke-LinkLanguageCheckCore -ExcludePaths @() | Should -Be 0
141 Should -Invoke Set-CIOutput -Times 1
142 Should -Invoke Write-CIStepSummary -Times 1
143 }
144 }
145}
146
147#endregion
148
149#region JSON Parsing Tests
150
151Describe 'JSON Output Parsing' -Tag 'Unit' {
152 Context 'Valid JSON with issues' {
153 BeforeEach {
154 $script:JsonWithIssues = @'
155[
156 {
157 "file": "docs/guide.md",
158 "line_number": 15,
159 "original_url": "https://docs.microsoft.com/en-us/azure"
160 },
161 {
162 "file": "README.md",
163 "line_number": 42,
164 "original_url": "https://learn.microsoft.com/en-us/dotnet"
165 }
166]
167'@
168 }
169
170 It 'Parses JSON array correctly' {
171 $result = $script:JsonWithIssues | ConvertFrom-Json
172 $result | Should -HaveCount 2
173 }
174
175 It 'Extracts file property' {
176 $result = $script:JsonWithIssues | ConvertFrom-Json
177 $result[0].file | Should -Be 'docs/guide.md'
178 }
179
180 It 'Extracts line_number property' {
181 $result = $script:JsonWithIssues | ConvertFrom-Json
182 $result[0].line_number | Should -Be 15
183 }
184
185 It 'Extracts original_url property' {
186 $result = $script:JsonWithIssues | ConvertFrom-Json
187 $result[0].original_url | Should -Be 'https://docs.microsoft.com/en-us/azure'
188 }
189 }
190
191 Context 'Empty JSON array' {
192 It 'Handles empty array' {
193 $result = '[]' | ConvertFrom-Json
194 $result | Should -BeNullOrEmpty
195 }
196 }
197
198 Context 'Invalid JSON' {
199 It 'Throws on malformed JSON' {
200 { 'not valid json' | ConvertFrom-Json } | Should -Throw
201 }
202 }
203}
204
205#endregion
206
207#region GitHub Actions Integration Tests
208
209Describe 'GitHub Actions Integration' -Tag 'Unit' {
210 Context 'Module exports verification' {
211 It 'Write-CIAnnotation is available in module' {
212 $module = Get-Module CIHelpers
213 $module.ExportedFunctions.Keys | Should -Contain 'Write-CIAnnotation'
214 }
215
216 It 'Set-CIOutput is available in module' {
217 $module = Get-Module CIHelpers
218 $module.ExportedFunctions.Keys | Should -Contain 'Set-CIOutput'
219 }
220
221 It 'Write-CIStepSummary is available in module' {
222 $module = Get-Module CIHelpers
223 $module.ExportedFunctions.Keys | Should -Contain 'Write-CIStepSummary'
224 }
225 }
226
227 Context 'GitHub Actions detection' {
228 It 'Detects GitHub Actions via GITHUB_ACTIONS env var' {
229 $originalValue = $env:GITHUB_ACTIONS
230 try {
231 $env:GITHUB_ACTIONS = 'true'
232 $env:GITHUB_ACTIONS | Should -Be 'true'
233
234 $env:GITHUB_ACTIONS = $null
235 $env:GITHUB_ACTIONS | Should -BeNullOrEmpty
236 }
237 finally {
238 $env:GITHUB_ACTIONS = $originalValue
239 }
240 }
241 }
242}
243
244#endregion
245
246#region Annotation Generation Tests
247
248Describe 'Annotation Generation' -Tag 'Unit' {
249 Context 'Annotation content' {
250 BeforeEach {
251 $script:Issue = [PSCustomObject]@{
252 file = 'docs/test.md'
253 line_number = 25
254 original_url = 'https://docs.microsoft.com/en-us/azure/overview'
255 }
256 }
257
258 It 'Issue object has required properties' {
259 $script:Issue.file | Should -Not -BeNullOrEmpty
260 $script:Issue.line_number | Should -BeGreaterThan 0
261 $script:Issue.original_url | Should -Match 'en-us'
262 }
263
264 It 'File path is workspace-relative' {
265 $script:Issue.file | Should -Not -Match '^[A-Z]:\\'
266 $script:Issue.file | Should -Not -Match '^/'
267 }
268 }
269
270 Context 'Annotation severity mapping' {
271 It 'Language path issues are warnings' {
272 # Link language issues are warnings, not errors
273 $severity = 'warning'
274 $severity | Should -Be 'warning'
275 }
276 }
277}
278
279#endregion
280
281#region Exit Code Tests
282
283Describe 'Exit Code Handling' -Tag 'Unit' {
284 Context 'No issues found' {
285 It 'Empty result indicates success' {
286 $issues = @()
287 $issues.Count | Should -Be 0
288 }
289 }
290
291 Context 'Issues found' {
292 BeforeEach {
293 $script:Issues = @(
294 [PSCustomObject]@{ file = 'test.md'; line_number = 1; original_url = 'https://example.com/en-us/page' }
295 )
296 }
297
298 It 'Non-empty result indicates issues present' {
299 $script:Issues.Count | Should -BeGreaterThan 0
300 }
301
302 It 'Script should warn but not fail on issues' {
303 # Link language issues are warnings, script continues
304 $warningExpected = $true
305 $warningExpected | Should -BeTrue
306 }
307 }
308}
309
310#endregion
311
312#region Output Format Tests
313
314Describe 'Output Format' -Tag 'Unit' {
315 Context 'Console output' {
316 BeforeEach {
317 $script:SampleIssue = [PSCustomObject]@{
318 file = 'README.md'
319 line_number = 10
320 original_url = 'https://docs.microsoft.com/en-us/azure'
321 }
322 }
323
324 It 'Issue can be formatted as string' {
325 $formatted = "[$($script:SampleIssue.file):$($script:SampleIssue.line_number)] $($script:SampleIssue.original_url)"
326 $formatted | Should -Be '[README.md:10] https://docs.microsoft.com/en-us/azure'
327 }
328 }
329
330 Context 'Summary statistics' {
331 BeforeEach {
332 $script:Issues = @(
333 [PSCustomObject]@{ file = 'a.md'; line_number = 1; original_url = 'url1' },
334 [PSCustomObject]@{ file = 'a.md'; line_number = 2; original_url = 'url2' },
335 [PSCustomObject]@{ file = 'b.md'; line_number = 1; original_url = 'url3' }
336 )
337 }
338
339 It 'Can count total issues' {
340 $script:Issues.Count | Should -Be 3
341 }
342
343 It 'Can count affected files' {
344 $fileCount = ($script:Issues | Select-Object -ExpandProperty file -Unique).Count
345 $fileCount | Should -Be 2
346 }
347 }
348}
349
350#endregion
351
352#region Integration with Link-Lang-Check Tests
353
354Describe 'Link-Lang-Check Integration' -Tag 'Integration' {
355 Context 'Script dependencies' {
356 It 'LintingHelpers module can be imported' {
357 { Import-Module $script:ModulePath -Force } | Should -Not -Throw
358 }
359
360 It 'Link-Lang-Check.ps1 exists at expected path' {
361 $linkLangCheckPath = Join-Path $PSScriptRoot '../../linting/Link-Lang-Check.ps1'
362 Test-Path $linkLangCheckPath | Should -BeTrue
363 }
364 }
365
366 Context 'Output compatibility' {
367 It 'Link-Lang-Check output can be parsed as JSON' {
368 # Sample output format from Link-Lang-Check.ps1
369 $sampleOutput = '[{"file":"test.md","line_number":1,"original_url":"https://example.com/en-us/page"}]'
370 { $sampleOutput | ConvertFrom-Json } | Should -Not -Throw
371 }
372
373 It 'Parsed output has expected structure' {
374 $sampleOutput = '[{"file":"test.md","line_number":1,"original_url":"https://example.com/en-us/page"}]'
375 $parsed = $sampleOutput | ConvertFrom-Json
376 $parsed[0].PSObject.Properties.Name | Should -Contain 'file'
377 $parsed[0].PSObject.Properties.Name | Should -Contain 'line_number'
378 $parsed[0].PSObject.Properties.Name | Should -Contain 'original_url'
379 }
380 }
381}
382
383#endregion
384