Skip to content

Commit 99a47f3

Browse files
Rewrite SharedTestRepositories docs in idiomatic comment-based help
1 parent 40e14a4 commit 99a47f3

1 file changed

Lines changed: 61 additions & 38 deletions

File tree

tests/Data/SharedTestRepositories.ps1

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,70 @@
1-
# Idempotent get-or-create helpers for the shared run-scoped test repositories.
2-
#
3-
# Background
4-
# ----------
5-
# `tests/BeforeAll.ps1` provisions one repository per (OS, TokenType, RunID) for the
6-
# happy-path workflow run. Several *.Tests.ps1 files then read that repository via
7-
# their per-context `BeforeAll`. Before this helper existed, each test file simply
8-
# threw if the repository was missing, which broke the GitHub Actions
9-
# **Re-run failed jobs** path: `AfterAll-ModuleLocal` deletes the shared repository
10-
# at the end of every attempt, and a partial rerun does not re-execute the
11-
# successful `BeforeAll-ModuleLocal` job, so the leaf jobs landed on a non-existent
12-
# repository (issue #590).
13-
#
14-
# These helpers move ownership of "ensure the shared repository exists" from a
15-
# fragile precondition-check into a declarative get-or-create that any leaf job
16-
# can call. On the happy path the repositories already exist (created by
17-
# `BeforeAll.ps1`) and the helpers are a single `Get-GitHubRepository` call. On a
18-
# partial rerun where the repositories were torn down, the helpers recreate them
19-
# transparently so the leaf job can proceed.
20-
#
21-
# Functions defined here:
22-
# - Initialize-SharedTestRepository primary `Test-{OS}-{TokenType}-{RunID}` repository (with readme/license/gitignore for release tests)
23-
# - Initialize-SharedTestRepositoryExtras org-only `-2` and `-3` companion repositories used by Secrets/Variables tests
1+
<#
2+
.SYNOPSIS
3+
Idempotent get-or-create helpers for the shared run-scoped test repositories.
4+
5+
.DESCRIPTION
6+
The integration test suite uses a small set of GitHub repositories whose names are
7+
scoped to the current `GITHUB_RUN_ID` and shared across the leaf jobs in the
8+
`Test-ModuleLocal` matrix. Any leaf job that depends on these repositories calls
9+
the helpers in this file from its `BeforeAll` block to ensure the repositories
10+
are present before the tests execute.
11+
12+
The helpers are declarative: they fetch the repository if it already exists and
13+
create it if it does not. This keeps test setup independent of the order in which
14+
jobs run, makes individual tests safe to execute in isolation, and lets the
15+
suite recover when shared infrastructure is missing for any reason.
16+
17+
Naming and scope are owned by the caller. The helpers do not list, rename, or
18+
delete repositories, and they do not widen ownership beyond the names they are
19+
given.
20+
21+
.NOTES
22+
Functions:
23+
- Initialize-SharedTestRepository Primary `Test-{OS}-{TokenType}-{RunID}` repository, initialized with a readme, license, and gitignore so a default branch with content is available.
24+
- Initialize-SharedTestRepositoryExtras Companion `-2` and `-3` repositories used by organization-scoped Secrets/Variables `SelectedRepository` tests.
25+
#>
2426

2527
function Initialize-SharedTestRepository {
28+
<#
29+
.SYNOPSIS
30+
Returns the named shared test repository, creating it if it does not exist.
31+
32+
.DESCRIPTION
33+
Looks up the repository by name in the given owner scope and returns it if
34+
found. If it is not found, creates it with a readme, MIT license, and
35+
VisualStudio gitignore so release-related tests have a default branch with
36+
content to operate on. If creation races with a parallel caller, the
37+
repository is re-fetched and returned.
38+
#>
2639
[CmdletBinding()]
2740
[OutputType([object])]
2841
param(
42+
# Login of the user or organization that owns the repository.
2943
[Parameter(Mandatory)]
3044
[string] $Owner,
3145

46+
# Whether $Owner is a user account or an organization. Determines which
47+
# Get-/New-GitHubRepository parameter set is used.
3248
[Parameter(Mandatory)]
3349
[ValidateSet('user', 'organization')]
3450
[string] $OwnerType,
3551

52+
# Repository name within the owner scope.
3653
[Parameter(Mandatory)]
3754
[string] $Name
3855
)
3956

4057
$repo = switch ($OwnerType) {
41-
'user' {
42-
Get-GitHubRepository -Name $Name -ErrorAction SilentlyContinue
43-
}
44-
'organization' {
45-
Get-GitHubRepository -Owner $Owner -Name $Name -ErrorAction SilentlyContinue
46-
}
58+
'user' { Get-GitHubRepository -Name $Name -ErrorAction SilentlyContinue }
59+
'organization' { Get-GitHubRepository -Owner $Owner -Name $Name -ErrorAction SilentlyContinue }
4760
}
4861

4962
if ($repo) {
5063
return $repo
5164
}
5265

53-
Write-Host "Shared test repository '$Name' not found for owner '$Owner' ($OwnerType). Creating it now (self-heal path for partial reruns / issue #590)."
66+
Write-Host "Provisioning shared test repository '$Owner/$Name' ($OwnerType)."
5467

55-
# The primary shared repository is initialized with readme/license/gitignore so
56-
# release tests have a default branch with content available for tag operations.
5768
$createParams = @{
5869
Name = $Name
5970
AddReadme = $true
@@ -67,9 +78,9 @@ function Initialize-SharedTestRepository {
6778
'organization' { New-GitHubRepository @createParams -Organization $Owner }
6879
}
6980
} catch {
70-
# Another leaf job in the same matrix may have created the repository
71-
# between our Get and our New. Re-fetch to recover from that race.
72-
Write-Host "Create attempt for '$Name' failed ($($_.Exception.Message)). Re-fetching in case a parallel job created it."
81+
# A parallel caller may have created the repository between the lookup above
82+
# and this create call. Re-fetch and treat the existing repository as success.
83+
Write-Host "Create attempt for '$Name' failed ($($_.Exception.Message)). Re-fetching in case a parallel caller created it."
7384
$repo = switch ($OwnerType) {
7485
'user' { Get-GitHubRepository -Name $Name -ErrorAction SilentlyContinue }
7586
'organization' { Get-GitHubRepository -Owner $Owner -Name $Name -ErrorAction SilentlyContinue }
@@ -83,27 +94,39 @@ function Initialize-SharedTestRepository {
8394
}
8495

8596
function Initialize-SharedTestRepositoryExtras {
97+
<#
98+
.SYNOPSIS
99+
Returns the `-2` and `-3` companion repositories for an organization-scoped base name, creating any that are missing.
100+
101+
.DESCRIPTION
102+
Some Secrets and Variables tests exercise `SelectedRepositories` visibility,
103+
which requires more than one repository in the same organization. This helper
104+
ensures the two companion repositories exist alongside the primary shared
105+
repository and returns them in `-2`, `-3` order.
106+
#>
86107
[CmdletBinding()]
87108
[OutputType([object[]])]
88109
param(
110+
# Organization that owns the companion repositories.
89111
[Parameter(Mandatory)]
90112
[string] $Owner,
91113

114+
# Name of the primary shared repository. Companions are derived as
115+
# "$BaseName-2" and "$BaseName-3".
92116
[Parameter(Mandatory)]
93117
[string] $BaseName
94118
)
95119

96-
# Extras are only used for organization-scoped Secrets/Variables SelectedRepository tests.
97120
$extras = @()
98121
foreach ($suffix in 2, 3) {
99122
$extraName = "$BaseName-$suffix"
100123
$extra = Get-GitHubRepository -Owner $Owner -Name $extraName -ErrorAction SilentlyContinue
101124
if (-not $extra) {
102-
Write-Host "Shared extra test repository '$extraName' not found for owner '$Owner'. Creating it now (self-heal path for partial reruns / issue #590)."
125+
Write-Host "Provisioning shared test repository '$Owner/$extraName'."
103126
try {
104127
$extra = New-GitHubRepository -Organization $Owner -Name $extraName
105128
} catch {
106-
Write-Host "Create attempt for '$extraName' failed ($($_.Exception.Message)). Re-fetching in case a parallel job created it."
129+
Write-Host "Create attempt for '$extraName' failed ($($_.Exception.Message)). Re-fetching in case a parallel caller created it."
107130
$extra = Get-GitHubRepository -Owner $Owner -Name $extraName -ErrorAction SilentlyContinue
108131
if (-not $extra) {
109132
throw

0 commit comments

Comments
 (0)