Skip to content

Commit 40e14a4

Browse files
Self-heal shared test repositories in leaf-test BeforeAll blocks
1 parent fc43af8 commit 40e14a4

5 files changed

Lines changed: 34 additions & 30 deletions

File tree

tests/Actions.Tests.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ BeforeAll {
2222
if (-not $id) {
2323
throw 'GITHUB_RUN_ID is required for Actions tests because it is used to build repository-scoped names for OIDC operations.'
2424
}
25+
. "$PSScriptRoot/Data/SharedTestRepositories.ps1"
2526
}
2627

2728
Describe 'Actions' {
@@ -61,10 +62,9 @@ Describe 'Actions' {
6162
if ($OwnerType -in ('repository', 'enterprise')) {
6263
$repo = $null
6364
} else {
64-
$repo = Get-GitHubRepository -Owner $Owner -Name $repoName
65-
if (-not $repo) {
66-
throw "Shared test repository '$repoName' was not found for owner '$Owner' (OwnerType: '$OwnerType'). Ensure the repository was provisioned and the repository name is correct."
67-
}
65+
# Declarative get-or-create so partial reruns (issue #590) can rebuild
66+
# the shared repository if AfterAll already tore it down.
67+
$repo = Initialize-SharedTestRepository -Owner $Owner -OwnerType $OwnerType -Name $repoName
6868
Write-Host ($repo | Select-Object * | Out-String)
6969
}
7070
}

tests/Environments.Tests.ps1

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ BeforeAll {
2626
if (-not $id) {
2727
throw 'GITHUB_RUN_ID is required for Environments tests.'
2828
}
29+
. "$PSScriptRoot/Data/SharedTestRepositories.ps1"
2930
}
3031

3132
Describe 'Environments' {
@@ -48,9 +49,12 @@ Describe 'Environments' {
4849
$environmentName = "$testName-$os-$TokenType-$id"
4950

5051
LogGroup "Using Repository - [$repoName]" {
51-
$repo = Get-GitHubRepository -Owner $Owner -Name $repoName
52-
if (($OwnerType -notin ('repository', 'enterprise')) -and (-not $repo)) {
53-
throw "Shared test repository '$repoName' was not found for owner '$Owner'. Ensure the repository was created before running the environment tests."
52+
if ($OwnerType -in ('repository', 'enterprise')) {
53+
$repo = $null
54+
} else {
55+
# Declarative get-or-create so partial reruns (issue #590) can rebuild
56+
# the shared repository if AfterAll already tore it down.
57+
$repo = Initialize-SharedTestRepository -Owner $Owner -OwnerType $OwnerType -Name $repoName
5458
}
5559
Write-Host ($repo | Select-Object * | Out-String)
5660
}

tests/Releases.Tests.ps1

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ BeforeAll {
2626
if (-not $id) {
2727
throw 'GITHUB_RUN_ID must be set for run-scoped release tests.'
2828
}
29+
. "$PSScriptRoot/Data/SharedTestRepositories.ps1"
2930
}
3031

3132
Describe 'Releases' {
@@ -47,9 +48,12 @@ Describe 'Releases' {
4748
$repoName = "$repoPrefix-$id"
4849

4950
LogGroup "Using Repository - [$repoName]" {
50-
$repo = Get-GitHubRepository -Owner $Owner -Name $repoName
51-
if (($OwnerType -notin ('repository', 'enterprise')) -and (-not $repo)) {
52-
throw "Expected shared test repository '$Owner/$repoName' was not found. Get-GitHubRepository returned no result, so release tests cannot continue."
51+
if ($OwnerType -in ('repository', 'enterprise')) {
52+
$repo = $null
53+
} else {
54+
# Declarative get-or-create so partial reruns (issue #590) can rebuild
55+
# the shared repository if AfterAll already tore it down.
56+
$repo = Initialize-SharedTestRepository -Owner $Owner -OwnerType $OwnerType -Name $repoName
5357
}
5458
Write-Host ($repo | Select-Object * | Out-String)
5559
}

tests/Secrets.Tests.ps1

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ BeforeAll {
2626
if (-not $id) {
2727
throw 'GITHUB_RUN_ID is required for Secrets tests because secret cleanup uses run-scoped wildcard names.'
2828
}
29+
. "$PSScriptRoot/Data/SharedTestRepositories.ps1"
2930
}
3031

3132
Describe 'Secrets' {
@@ -52,19 +53,16 @@ Describe 'Secrets' {
5253

5354
switch ($OwnerType) {
5455
'user' {
55-
$repo = Get-GitHubRepository -Name $repoName
56-
if (-not $repo) {
57-
throw "Shared test repository '$repoName' was not found. Ensure BeforeAll.ps1 provisioned it."
58-
}
56+
# Declarative get-or-create so partial reruns (issue #590) can rebuild
57+
# the shared repository if AfterAll already tore it down.
58+
$repo = Initialize-SharedTestRepository -Owner $Owner -OwnerType 'user' -Name $repoName
5959
}
6060
'organization' {
6161
Get-GitHubSecret -Owner $Owner | Where-Object { $_.Name -like "$secretName*" } | Remove-GitHubSecret -Confirm:$false
62-
$repo = Get-GitHubRepository -Owner $Owner -Name $repoName
63-
$repo2 = Get-GitHubRepository -Owner $Owner -Name "$repoName-2"
64-
$repo3 = Get-GitHubRepository -Owner $Owner -Name "$repoName-3"
65-
if (-not $repo -or -not $repo2 -or -not $repo3) {
66-
throw "One or more shared test repositories ('$repoName', '$repoName-2', '$repoName-3') not found for owner '$Owner'. Ensure BeforeAll.ps1 provisioned them."
67-
}
62+
$repo = Initialize-SharedTestRepository -Owner $Owner -OwnerType 'organization' -Name $repoName
63+
$extras = Initialize-SharedTestRepositoryExtras -Owner $Owner -BaseName $repoName
64+
$repo2 = $extras[0]
65+
$repo3 = $extras[1]
6866
LogGroup "Org secret - [$orgSecretName]" {
6967
$params = @{
7068
Owner = $owner

tests/Variables.Tests.ps1

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ BeforeAll {
2626
if (-not $id) {
2727
throw 'GITHUB_RUN_ID is not set. Variables tests refuse to run without a scoped run ID to avoid deleting variables from other runs.'
2828
}
29+
. "$PSScriptRoot/Data/SharedTestRepositories.ps1"
2930
}
3031

3132
Describe 'Variables' {
@@ -52,19 +53,16 @@ Describe 'Variables' {
5253

5354
switch ($OwnerType) {
5455
'user' {
55-
$repo = Get-GitHubRepository -Name $repoName
56-
if (-not $repo) {
57-
throw "Shared test repository '$repoName' was not found. Ensure BeforeAll.ps1 provisioned it."
58-
}
56+
# Declarative get-or-create so partial reruns (issue #590) can rebuild
57+
# the shared repository if AfterAll already tore it down.
58+
$repo = Initialize-SharedTestRepository -Owner $Owner -OwnerType 'user' -Name $repoName
5959
}
6060
'organization' {
6161
Get-GitHubVariable -Owner $Owner | Where-Object { $_.Name -like "$variableName*" } | Remove-GitHubVariable -Confirm:$false
62-
$repo = Get-GitHubRepository -Owner $Owner -Name $repoName
63-
$repo2 = Get-GitHubRepository -Owner $Owner -Name "$repoName-2"
64-
$repo3 = Get-GitHubRepository -Owner $Owner -Name "$repoName-3"
65-
if (-not $repo -or -not $repo2 -or -not $repo3) {
66-
throw "One or more shared test repositories ('$repoName', '$repoName-2', '$repoName-3') not found for owner '$Owner'. Ensure BeforeAll.ps1 provisioned them."
67-
}
62+
$repo = Initialize-SharedTestRepository -Owner $Owner -OwnerType 'organization' -Name $repoName
63+
$extras = Initialize-SharedTestRepositoryExtras -Owner $Owner -BaseName $repoName
64+
$repo2 = $extras[0]
65+
$repo3 = $extras[1]
6866
LogGroup "Org variable - [$orgVariableName]" {
6967
$params = @{
7068
Owner = $owner

0 commit comments

Comments
 (0)