microsoft/hve-core

Public

mirrored from https://github.com/microsoft/hve-coreAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
ci/2086-enforce-powershell-coverage

Branches

Tags

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

Clone

HTTPS

Download ZIP

scripts/tests/devcontainer/Write-DevcontainerChangeLog.Tests.ps1

97lines · modecode

1#Requires -Modules Pester
2# Copyright (c) Microsoft Corporation.
3# SPDX-License-Identifier: MIT
4
5BeforeAll {
6 . (Join-Path $PSScriptRoot '../../devcontainer/Write-DevcontainerChangeLog.ps1')
7 Import-Module (Join-Path $PSScriptRoot '../../lib/Modules/CIHelpers.psm1') -Force
8
9 Mock Write-Host {}
10 Mock Write-CIAnnotation {}
11 Mock Write-CIStepSummary {}
12}
13
14Describe 'Get-DevcontainerFileClassification' -Tag 'Unit' {
15 It 'Classifies <Path> as <Category> with <Impact> impact' -ForEach @(
16 @{ Path = '.devcontainer/scripts/on-create.sh'; Category = 'Lifecycle Scripts'; Impact = 'High' }
17 @{ Path = '.devcontainer/scripts/post-create.sh'; Category = 'Lifecycle Scripts'; Impact = 'Low' }
18 @{ Path = '.devcontainer/Dockerfile'; Category = 'Base Image'; Impact = 'High' }
19 @{ Path = '.devcontainer/Dockerfile.custom'; Category = 'Base Image'; Impact = 'High' }
20 @{ Path = '.devcontainer/base.dockerfile'; Category = 'Base Image'; Impact = 'High' }
21 @{ Path = '.devcontainer/features/custom-feature'; Category = 'Features'; Impact = 'Medium' }
22 @{ Path = '.devcontainer/devcontainer.json'; Category = 'Config'; Impact = 'High' }
23 @{ Path = '.devcontainer/devcontainer-lock.json'; Category = 'Lockfile'; Impact = 'Medium' }
24 @{ Path = '.github/workflows/copilot-setup-steps.yml'; Category = 'Setup Steps'; Impact = 'Medium' }
25 @{ Path = '.devcontainer/something-else.txt'; Category = 'Config'; Impact = 'Medium' }
26 @{ Path = 'unrelated/file.txt'; Category = 'Other'; Impact = 'Unknown' }
27 ) {
28 $result = Get-DevcontainerFileClassification -FilePath $Path
29 $result.Category | Should -Be $Category
30 $result.Impact | Should -Be $Impact
31 }
32}
33
34Describe 'New-DevcontainerChangeSummary' -Tag 'Unit' {
35 Context 'when EventName is workflow_dispatch' {
36 It 'Returns markdown with dispatch message' {
37 $result = New-DevcontainerChangeSummary -CommitSha 'abc123' -BranchName 'main' -EventName 'workflow_dispatch'
38 $result | Should -Match 'workflow_dispatch'
39 $result | Should -Match 'No push range available'
40 }
41 }
42
43 Context 'when BeforeSha is all zeros' {
44 It 'Returns markdown with initial push message' {
45 $result = New-DevcontainerChangeSummary -CommitSha 'abc123' -BranchName 'main' -EventName 'push' -BeforeSha '0000000000000000000000000000000000000000'
46 $result | Should -Match 'Initial push'
47 }
48 }
49
50 Context 'when git diff succeeds with changed files' {
51 BeforeAll {
52 Mock git {
53 $global:LASTEXITCODE = 0
54 return @(
55 '.devcontainer/devcontainer.json'
56 '.devcontainer/scripts/on-create.sh'
57 )
58 } -ParameterFilter { $args -contains 'diff' }
59 }
60
61 It 'Returns markdown with classified file table' {
62 $result = New-DevcontainerChangeSummary -CommitSha 'abc123' -BranchName 'main' -EventName 'push' -BeforeSha 'def456' -RepoRoot '/fake'
63 $result | Should -Match 'devcontainer\.json'
64 $result | Should -Match 'on-create\.sh'
65 $result | Should -Match 'Config'
66 $result | Should -Match 'Lifecycle Scripts'
67 }
68 }
69
70 Context 'when git diff fails (force push)' {
71 BeforeAll {
72 Mock git {
73 $global:LASTEXITCODE = 128
74 return 'fatal: bad object abc123'
75 } -ParameterFilter { $args -contains 'diff' }
76 }
77
78 It 'Returns markdown with force push message' {
79 $result = New-DevcontainerChangeSummary -CommitSha 'abc123' -BranchName 'main' -EventName 'push' -BeforeSha 'def456' -RepoRoot '/fake'
80 $result | Should -Match 'not be reachable'
81 }
82 }
83
84 Context 'when git diff returns empty' {
85 BeforeAll {
86 Mock git {
87 $global:LASTEXITCODE = 0
88 return ''
89 } -ParameterFilter { $args -contains 'diff' }
90 }
91
92 It 'Returns markdown with no changes message' {
93 $result = New-DevcontainerChangeSummary -CommitSha 'abc123' -BranchName 'main' -EventName 'push' -BeforeSha 'def456' -RepoRoot '/fake'
94 $result | Should -Match 'No devcontainer infrastructure files changed'
95 }
96 }
97}
98