-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathBeforeAll.ps1
More file actions
102 lines (89 loc) · 4.68 KB
/
Copy pathBeforeAll.ps1
File metadata and controls
102 lines (89 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
[CmdletBinding()]
param()
LogGroup 'BeforeAll - Global Test Setup' {
$authCases = . "$PSScriptRoot/Data/AuthCases.ps1"
$id = $env:GITHUB_RUN_ID
if (-not $id) {
throw 'GITHUB_RUN_ID environment variable is not set. Refusing to create or clean up test repositories with a non-deterministic name.'
}
if (-not $env:Settings) {
throw 'Settings environment variable is not set. Process-PSModule must populate it with the test suite configuration.'
}
# Derive the list of OS names from the Settings JSON provided by Process-PSModule.
try {
$settings = $env:Settings | ConvertFrom-Json
} catch {
throw "Settings environment variable does not contain valid JSON. Process-PSModule must populate it with a valid test suite configuration. $_"
}
$osNames = @($settings.TestSuites.Module.OSName | Sort-Object -Unique)
if (-not $osNames) {
throw 'Settings JSON must contain TestSuites.Module.OSName with at least one OS name.'
}
$invalidOsNames = @($osNames | Where-Object { -not $_ -or -not $_.ToString().Trim() })
if ($invalidOsNames.Count -gt 0) {
throw 'Settings JSON contains one or more null or empty values in TestSuites.Module.OSName.'
}
Write-Host "Creating test repositories for OSes: $($osNames -join ', ')"
# Source the single authoritative list of test-file repositories so setup and teardown
# always operate on the same set. See tests/Data/TestRepos.ps1.
$testRepos = . "$PSScriptRoot/Data/TestRepos.ps1"
$testNames = $testRepos.TestNames
$testNamesWithExtraRepos = $testRepos.TestNamesWithExtraRepos
foreach ($authCase in $authCases) {
$authCase.GetEnumerator() | ForEach-Object { Set-Variable -Name $_.Key -Value $_.Value }
if ($TokenType -eq 'GITHUB_TOKEN') {
Write-Host "Skipping setup for $AuthType-$TokenType (uses existing repository)"
continue
}
$context = Connect-GitHubAccount @connectParams -PassThru -Silent
if ($AuthType -eq 'APP') {
$context = Connect-GitHubApp @connectAppParams -PassThru -Default -Silent
}
Write-Host ($context | Format-List | Out-String)
foreach ($os in $osNames) {
foreach ($testName in $testNames) {
$repoPrefix = "$testName-$os-$TokenType"
$repoName = "$repoPrefix-$id"
LogGroup "Repository setup - $AuthType-$TokenType - $os - $testName" {
# Clean up repos from a previous attempt of the same run (re-runs).
# Use deterministic name lookups instead of listing all repos to reduce API calls.
$cleanupRepoNames = @($repoName)
if ($OwnerType -eq 'organization' -and $testName -in $testNamesWithExtraRepos) {
$cleanupRepoNames += "$repoName-2", "$repoName-3"
}
foreach ($cleanupRepoName in $cleanupRepoNames) {
switch ($OwnerType) {
'user' {
Get-GitHubRepository -Name $cleanupRepoName -ErrorAction SilentlyContinue |
Remove-GitHubRepository -Confirm:$false
}
'organization' {
Get-GitHubRepository -Owner $Owner -Name $cleanupRepoName -ErrorAction SilentlyContinue |
Remove-GitHubRepository -Confirm:$false
}
}
}
# Provision the primary per-test-file repository.
$repoParams = @{
Name = $repoName
AddReadme = $true
License = 'mit'
Gitignore = 'VisualStudio'
}
switch ($OwnerType) {
'user' { Set-GitHubRepository @repoParams }
'organization' { Set-GitHubRepository @repoParams -Organization $Owner }
}
# Provision extra repositories needed by Secrets/Variables SelectedRepository tests.
# Only organization owners need them — those tests are skipped for user owners.
if ($OwnerType -eq 'organization' -and $testName -in $testNamesWithExtraRepos) {
foreach ($suffix in 2, 3) {
Set-GitHubRepository -Organization $Owner -Name "$repoName-$suffix"
}
}
}
}
}
Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount -Silent
}
}