microsoft/hve-core
Publicmirrored fromhttps://github.com/microsoft/hve-coreAvailable
.github/skills/shared/pr-reference/tests/shared.Tests.ps1
164lines · modecode
| 1 | #Requires -Modules Pester |
| 2 | # Copyright (c) Microsoft Corporation. |
| 3 | # SPDX-License-Identifier: MIT |
| 4 | |
| 5 | BeforeAll { |
| 6 | Import-Module (Join-Path $PSScriptRoot '../scripts/shared.psm1') -Force |
| 7 | } |
| 8 | |
| 9 | Describe 'Get-RepositoryRoot' { |
| 10 | Context 'Default (fallback) mode' { |
| 11 | It 'Returns a valid directory when in a git repository' { |
| 12 | $result = Get-RepositoryRoot |
| 13 | $result | Should -Not -BeNullOrEmpty |
| 14 | Test-Path -Path $result -PathType Container | Should -BeTrue |
| 15 | } |
| 16 | |
| 17 | It 'Returns path containing .git directory' { |
| 18 | $result = Get-RepositoryRoot |
| 19 | Test-Path -Path (Join-Path $result '.git') | Should -BeTrue |
| 20 | } |
| 21 | |
| 22 | It 'Falls back to current directory when git fails' { |
| 23 | Mock git { $global:LASTEXITCODE = 128; return $null } -ModuleName shared |
| 24 | $result = Get-RepositoryRoot |
| 25 | $result | Should -Be $PWD.Path |
| 26 | } |
| 27 | |
| 28 | It 'Falls back to current directory when git returns empty' { |
| 29 | Mock git { $global:LASTEXITCODE = 0; return '' } -ModuleName shared |
| 30 | $result = Get-RepositoryRoot |
| 31 | $result | Should -Be $PWD.Path |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | Context 'Strict mode' { |
| 36 | It 'Returns a valid directory when in a git repository' { |
| 37 | $result = Get-RepositoryRoot -Strict |
| 38 | $result | Should -Not -BeNullOrEmpty |
| 39 | Test-Path -Path $result -PathType Container | Should -BeTrue |
| 40 | } |
| 41 | |
| 42 | It 'Throws when repository root cannot be determined' { |
| 43 | Mock git { $global:LASTEXITCODE = 0; return '' } -ModuleName shared |
| 44 | { Get-RepositoryRoot -Strict } | Should -Throw '*Unable to determine repository root*' |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | Describe 'Resolve-DefaultBranch' { |
| 50 | Context 'Successful resolution' { |
| 51 | It 'Returns a branch reference' { |
| 52 | $result = Resolve-DefaultBranch |
| 53 | $result | Should -Not -BeNullOrEmpty |
| 54 | $result | Should -BeOfType [string] |
| 55 | } |
| 56 | |
| 57 | It 'Returns origin-prefixed branch name' { |
| 58 | $result = Resolve-DefaultBranch |
| 59 | $result | Should -Match '^origin/' |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | Context 'Fallback behavior' { |
| 64 | It 'Falls back to origin/main when symbolic-ref fails' { |
| 65 | Mock git { $global:LASTEXITCODE = 1; return $null } -ModuleName shared |
| 66 | $result = Resolve-DefaultBranch |
| 67 | $result | Should -Be 'origin/main' |
| 68 | } |
| 69 | |
| 70 | It 'Falls back to origin/main when symbolic-ref returns empty' { |
| 71 | Mock git { $global:LASTEXITCODE = 0; return '' } -ModuleName shared |
| 72 | $result = Resolve-DefaultBranch |
| 73 | $result | Should -Be 'origin/main' |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | Describe 'Build-PathspecExclusions' { |
| 79 | Context 'Extension exclusions' { |
| 80 | It 'Returns pathspec for single extension' { |
| 81 | $result = Build-PathspecExclusions -Extensions @('yml') |
| 82 | $result | Should -Contain ':!*.yml' |
| 83 | } |
| 84 | |
| 85 | It 'Returns pathspecs for multiple extensions' { |
| 86 | $result = Build-PathspecExclusions -Extensions @('yml', 'json', 'png') |
| 87 | $result.Count | Should -Be 3 |
| 88 | $result | Should -Contain ':!*.yml' |
| 89 | $result | Should -Contain ':!*.json' |
| 90 | $result | Should -Contain ':!*.png' |
| 91 | } |
| 92 | |
| 93 | It 'Strips leading dots from extensions' { |
| 94 | $result = Build-PathspecExclusions -Extensions @('.yml', '.json') |
| 95 | $result | Should -Contain ':!*.yml' |
| 96 | $result | Should -Contain ':!*.json' |
| 97 | } |
| 98 | |
| 99 | It 'Returns empty array for empty extensions input' { |
| 100 | $result = Build-PathspecExclusions -Extensions @() |
| 101 | $result.Count | Should -Be 0 |
| 102 | } |
| 103 | |
| 104 | It 'Skips empty extension strings' { |
| 105 | $result = Build-PathspecExclusions -Extensions @('yml', '', 'json') |
| 106 | $result.Count | Should -Be 2 |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | Context 'Path exclusions' { |
| 111 | It 'Returns pathspec for single path' { |
| 112 | $result = Build-PathspecExclusions -Paths @('docs/') |
| 113 | $result | Should -Contain ':!docs/**' |
| 114 | } |
| 115 | |
| 116 | It 'Returns pathspecs for multiple paths' { |
| 117 | $result = Build-PathspecExclusions -Paths @('docs/', '.github/skills/') |
| 118 | $result.Count | Should -Be 2 |
| 119 | $result | Should -Contain ':!docs/**' |
| 120 | $result | Should -Contain ':!.github/skills/**' |
| 121 | } |
| 122 | |
| 123 | It 'Strips trailing slashes from paths' { |
| 124 | $result = Build-PathspecExclusions -Paths @('docs/') |
| 125 | $result | Should -Contain ':!docs/**' |
| 126 | } |
| 127 | |
| 128 | It 'Handles paths without trailing slash' { |
| 129 | $result = Build-PathspecExclusions -Paths @('docs') |
| 130 | $result | Should -Contain ':!docs/**' |
| 131 | } |
| 132 | |
| 133 | It 'Returns empty array for empty paths input' { |
| 134 | $result = Build-PathspecExclusions -Paths @() |
| 135 | $result.Count | Should -Be 0 |
| 136 | } |
| 137 | |
| 138 | It 'Skips empty path strings' { |
| 139 | $result = Build-PathspecExclusions -Paths @('docs/', '', '.github/') |
| 140 | $result.Count | Should -Be 2 |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | Context 'Combined exclusions' { |
| 145 | It 'Returns pathspecs for both extensions and paths' { |
| 146 | $result = Build-PathspecExclusions -Extensions @('yml') -Paths @('docs/') |
| 147 | $result.Count | Should -Be 2 |
| 148 | $result | Should -Contain ':!*.yml' |
| 149 | $result | Should -Contain ':!docs/**' |
| 150 | } |
| 151 | |
| 152 | It 'Returns empty array when both inputs are empty' { |
| 153 | $result = Build-PathspecExclusions -Extensions @() -Paths @() |
| 154 | $result.Count | Should -Be 0 |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | Context 'Default parameters' { |
| 159 | It 'Returns empty array when called without parameters' { |
| 160 | $result = Build-PathspecExclusions |
| 161 | $result.Count | Should -Be 0 |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |