diff --git a/tests/AfterAll.ps1 b/tests/AfterAll.ps1 new file mode 100644 index 000000000..54364bb17 --- /dev/null +++ b/tests/AfterAll.ps1 @@ -0,0 +1,49 @@ +[CmdletBinding()] +param() + +LogGroup 'AfterAll - Global Test Teardown' { + $authCases = . "$PSScriptRoot/Data/AuthCases.ps1" + + $id = $env:GITHUB_RUN_ID + $prefix = 'Test' + + # Derive the list of OS names from the Settings JSON provided by Process-PSModule. + $settings = $env:Settings | ConvertFrom-Json + $osNames = @($settings.TestSuites.Module.OSName | Sort-Object -Unique) + Write-Host "Cleaning up test repositories for OSes: $($osNames -join ', ')" + + foreach ($authCase in $authCases) { + $authCase.GetEnumerator() | ForEach-Object { Set-Variable -Name $_.Key -Value $_.Value } + + if ($TokenType -eq 'GITHUB_TOKEN') { + Write-Host "Skipping teardown for $AuthType-$TokenType (uses existing repository)" + continue + } + + LogGroup "Teardown - $AuthType-$TokenType" { + $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) { + $repoPrefix = "$prefix-$os-$TokenType" + $repoName = "$repoPrefix-$id" + + LogGroup "Repository cleanup - $AuthType-$TokenType - $os" { + switch ($OwnerType) { + 'user' { + Get-GitHubRepository | Where-Object { $_.Name -like "$repoName*" } | Remove-GitHubRepository -Confirm:$false + } + 'organization' { + Get-GitHubRepository -Organization $Owner | Where-Object { $_.Name -like "$repoName*" } | Remove-GitHubRepository -Confirm:$false + } + } + } + } + + Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount -Silent + } + } +} diff --git a/tests/BeforeAll.ps1 b/tests/BeforeAll.ps1 new file mode 100644 index 000000000..1edf8883a --- /dev/null +++ b/tests/BeforeAll.ps1 @@ -0,0 +1,66 @@ +[CmdletBinding()] +param() + +LogGroup 'BeforeAll - Global Test Setup' { + $authCases = . "$PSScriptRoot/Data/AuthCases.ps1" + $id = $env:GITHUB_RUN_ID + + # Derive the list of OS names from the Settings JSON provided by Process-PSModule. + $settings = $env:Settings | ConvertFrom-Json + $osNames = @($settings.TestSuites.Module.OSName | Sort-Object -Unique) + Write-Host "Creating test repositories for OSes: $($osNames -join ', ')" + + 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) { + $repoPrefix = "Test-$os-$TokenType" + $repoName = "$repoPrefix-$id" + + LogGroup "Repository setup - $AuthType-$TokenType - $os" { + # Clean up repos from a previous attempt of the same run (re-runs). + switch ($OwnerType) { + 'user' { + Get-GitHubRepository | Where-Object { $_.Name -like "$repoName*" } | Remove-GitHubRepository -Confirm:$false + } + 'organization' { + Get-GitHubRepository -Organization $Owner | Where-Object { $_.Name -like "$repoName*" } | Remove-GitHubRepository -Confirm:$false + } + } + + # Create the primary shared repository (with readme, license, gitignore for release tests). + $repoParams = @{ + Name = $repoName + AddReadme = $true + License = 'MIT' + Gitignore = 'VisualStudio' + } + switch ($OwnerType) { + 'user' { + New-GitHubRepository @repoParams + } + 'organization' { + New-GitHubRepository @repoParams -Organization $Owner + } + } + + # Create extra repositories needed by Secrets/Variables SelectedRepository tests. + foreach ($suffix in 2, 3) { + $extraName = "$repoName-$suffix" + switch ($OwnerType) { + 'user' { + New-GitHubRepository -Name $extraName + } + 'organization' { + New-GitHubRepository -Organization $Owner -Name $extraName + } diff --git a/tests/Emojis.Tests.ps1 b/tests/Emojis.Tests.ps1 index ff3100a2e..92d5308f4 100644 --- a/tests/Emojis.Tests.ps1 +++ b/tests/Emojis.Tests.ps1 @@ -28,26 +28,18 @@ Describe 'Emojis' { LogGroup 'Context' { Write-Host ($context | Format-List | Out-String) } - } - AfterAll { - Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount -Silent - Write-Host ('-' * 60) - } - - # Tests for APP goes here - if ($AuthType -eq 'APP') { - It 'Connect-GitHubApp - Connects as a GitHub App to ' { + if ($AuthType -eq 'APP') { $context = Connect-GitHubApp @connectAppParams -PassThru -Default -Silent LogGroup 'Context' { Write-Host ($context | Format-List | Out-String) } } } + AfterAll { + Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount -Silent + Write-Host ('-' * 60) + } - # Tests for runners goes here - if ($Type -eq 'GitHub Actions') {} - - # Tests for IAT UAT and PAT goes here It 'Get-GitHubEmoji - Gets a list of all emojis' { $emojis = Get-GitHubEmoji LogGroup 'emojis' { diff --git a/tests/Enterprise.Tests.ps1 b/tests/Enterprise.Tests.ps1 index 98f3cd03c..195c7a785 100644 --- a/tests/Enterprise.Tests.ps1 +++ b/tests/Enterprise.Tests.ps1 @@ -19,11 +19,7 @@ [CmdletBinding()] param() -BeforeAll { - # DEFAULTS ACROSS ALL TESTS -} - -Describe 'Template' { +Describe 'Enterprise' { $authCases = . "$PSScriptRoot/Data/AuthCases.ps1" Context 'As using on ' -ForEach $authCases { diff --git a/tests/Environments.Tests.ps1 b/tests/Environments.Tests.ps1 index 762eab550..41f3ed8f1 100644 --- a/tests/Environments.Tests.ps1 +++ b/tests/Environments.Tests.ps1 @@ -20,9 +20,9 @@ param() BeforeAll { - $testName = 'EnvironmentsTests' + $testName = 'Environments' $os = $env:RUNNER_OS - $guid = [guid]::NewGuid().ToString() + $id = $env:GITHUB_RUN_ID } Describe 'Environments' { @@ -40,34 +40,17 @@ Describe 'Environments' { Write-Host ($context | Format-List | Out-String) } } - $repoPrefix = "$testName-$os-$TokenType" - $repoName = "$repoPrefix-$guid" - $environmentName = "$testName-$os-$TokenType-$guid" + $repoPrefix = "Test-$os-$TokenType" + $repoName = "$repoPrefix-$id" + $environmentName = "$testName-$os-$TokenType-$id" - switch ($OwnerType) { - 'user' { - Get-GitHubRepository | Where-Object { $_.Name -like "$repoPrefix*" } | Remove-GitHubRepository -Confirm:$false - $repo = New-GitHubRepository -Name $repoName -Confirm:$false - } - 'organization' { - Get-GitHubRepository -Organization $Owner | Where-Object { $_.Name -like "$repoPrefix*" } | Remove-GitHubRepository -Confirm:$false - $repo = New-GitHubRepository -Organization $owner -Name $repoName -Confirm:$false - } - } - LogGroup "Repository - [$repoName]" { + LogGroup "Using Repository - [$repoName]" { + $repo = Get-GitHubRepository -Owner $Owner -Name $repoName Write-Host ($repo | Select-Object * | Out-String) } } AfterAll { - switch ($OwnerType) { - 'user' { - Get-GitHubRepository | Where-Object { $_.Name -like "$repoPrefix*" } | Remove-GitHubRepository -Confirm:$false - } - 'organization' { - Get-GitHubRepository -Organization $Owner | Where-Object { $_.Name -like "$repoPrefix*" } | Remove-GitHubRepository -Confirm:$false - } - } Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount -Silent Write-Host ('-' * 60) } diff --git a/tests/GitHub.Tests.ps1 b/tests/GitHub.Tests.ps1 index dab477d5a..ea7a186a3 100644 --- a/tests/GitHub.Tests.ps1 +++ b/tests/GitHub.Tests.ps1 @@ -567,36 +567,27 @@ Describe 'API' { Write-Host ($context | Format-List | Out-String) } $context | Should -Not -BeNullOrEmpty + if ($AuthType -eq 'APP') { + $context = Connect-GitHubApp @connectAppParams -PassThru -Default -Silent + LogGroup 'Context - Installation' { + Write-Host ($context | Format-List | Out-String) + } + } } AfterAll { Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount -Silent Write-Host ('-' * 60) } - # Tests for APP goes here - if ($AuthType -eq 'APP') { - It 'Invoke-GitHubAPI - Gets the app details' { - { - $app = Invoke-GitHubAPI -ApiEndpoint '/app' - LogGroup 'App' { - Write-Host ($app | Format-List | Out-String) - } - } | Should -Not -Throw - } - - It 'Connect-GitHubApp - Connects as a GitHub App to ' { - $context = Connect-GitHubApp @connectAppParams -PassThru -Default -Silent - LogGroup 'Context' { - Write-Host ($context | Format-List | Out-String) + It 'Invoke-GitHubAPI - Gets the app details' -Skip:($AuthType -ne 'APP') { + { + $app = Invoke-GitHubAPI -ApiEndpoint '/app' + LogGroup 'App' { + Write-Host ($app | Format-List | Out-String) } - $context | Should -Not -BeNullOrEmpty - } + } | Should -Not -Throw } - # Tests for runners goes here - if ($Type -eq 'GitHub Actions') {} - - # Tests for IAT UAT and PAT goes here Context 'API' { It 'Invoke-GitHubAPI - Gets the rate limits directly using APIEndpoint' { { diff --git a/tests/Organizations.Tests.ps1 b/tests/Organizations.Tests.ps1 index 41e6d7221..9f6a4a80d 100644 --- a/tests/Organizations.Tests.ps1 +++ b/tests/Organizations.Tests.ps1 @@ -20,9 +20,9 @@ param() BeforeAll { - $testName = 'MsxOrgTests' + $testName = 'Organizations' $os = $env:RUNNER_OS - $number = Get-Random + $id = $env:GITHUB_RUN_ID } Describe 'Organizations' { @@ -35,11 +35,11 @@ Describe 'Organizations' { Write-Host ($context | Select-Object * | Out-String) } $orgPrefix = "$testName-$os-" - $orgName = "$orgPrefix$number" + $orgName = "$orgPrefix$id" if ($AuthType -eq 'APP') { LogGroup 'Pre-test Cleanup - App Installations' { - Get-GitHubAppInstallation -Context $context | Where-Object { $_.Target.Name -like "$orgPrefix*" } | + Get-GitHubAppInstallation -Context $context | Where-Object { $_.Target.Name -like "$orgName*" } | Uninstall-GitHubApp -Confirm:$false } diff --git a/tests/README.md b/tests/README.md index dba4a4440..3b5d937cb 100644 --- a/tests/README.md +++ b/tests/README.md @@ -42,3 +42,107 @@ Events: Secrets: - TEST_APP_ORG_CLIENT_ID - TEST_APP_ORG_PRIVATE_KEY + +## Auth cases + +[AuthCases.ps1](../tests/Data/AuthCases.ps1) defines 7 auth cases. Each test file iterates over all cases, skipping those +that don't apply (e.g., `repository` and `enterprise` owner types skip repo-dependent tests). + +| # | AuthType | TokenType | Owner | OwnerType | +|---|----------|---------------|--------------------|--------------| +| 1 | PAT | USER_FG_PAT | psmodule-user | user | +| 2 | PAT | ORG_FG_PAT | psmodule-test-org2 | organization | +| 3 | PAT | PAT | psmodule-user | user | +| 4 | IAT | GITHUB_TOKEN | PSModule | repository | +| 5 | App | APP_ORG | psmodule-test-org | organization | +| 6 | App | APP_ENT | psmodule-test-org3 | organization | +| 7 | App | APP_ENT | msx | enterprise | + +Cases 4 (`repository`) and 7 (`enterprise`) skip repo creation. Cases 1 and 3 share the same user owner (`psmodule-user`) +but have different `$TokenType` values, so repo names are unique. + +## Setup and teardown + +Shared test infrastructure is provisioned once per workflow run using `BeforeAll.ps1` and torn down using `AfterAll.ps1`. +For generic guidance on setup/teardown scripts, see the +[Process-PSModule documentation](https://github.com/PSModule/Process-PSModule#setup-and-teardown-scripts). + +### `BeforeAll.ps1` — global setup + +Runs once before all parallel test files. For each auth case (except `GITHUB_TOKEN`): + +1. Connects using the auth case credentials +2. Cleans up stale repos from previous failed runs (matching `Test-$os-$TokenType-*`) +3. Creates a primary shared repository per OS: `Test-{OS}-{TokenType}-{GITHUB_RUN_ID}` + - Includes `AddReadme`, `License` (MIT), and `Gitignore` (VisualStudio) for release tests + - For `user` owners: `New-GitHubRepository -Name $repoName` + - For `organization` owners: `New-GitHubRepository -Organization $Owner -Name $repoName` +4. Creates two extra repositories per OS (`-2`, `-3` suffix) for Secrets/Variables SelectedRepository tests + +### `AfterAll.ps1` — global teardown + +Runs once after all parallel test files complete. For each auth case (except `GITHUB_TOKEN`): + +1. Connects using the auth case credentials +2. Removes all repositories matching the `Test-{OS}-{TokenType}-*` prefix + +## Test file pattern + +Each test file follows this pattern: + +```powershell +BeforeAll { + $testName = 'TestName' + $os = $env:RUNNER_OS + $id = $env:GITHUB_RUN_ID +} + +Describe 'TestName' { + $authCases = . "$PSScriptRoot/Data/AuthCases.ps1" + + Context 'As using on ' -ForEach $authCases { + BeforeAll { + # Connect + $context = Connect-GitHubAccount @connectParams -PassThru -Silent + if ($AuthType -eq 'APP') { + $context = Connect-GitHubApp @connectAppParams -PassThru -Default -Silent + } + + # Reference the shared repo (NOT New-GitHubRepository) + $repoPrefix = "Test-$os-$TokenType" + $repoName = "$repoPrefix-$id" + $repo = Get-GitHubRepository -Owner $Owner -Name $repoName + } + + AfterAll { + Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount -Silent + } + + It 'Should do something' -Skip:($OwnerType -in ('repository', 'enterprise')) { + # Test logic using $repo, $Owner, $repoName + } + } +} +``` + +### Key conventions + +- **`$id = $env:GITHUB_RUN_ID`** — not `[guid]::NewGuid()` or `Get-Random`. This makes the repo name deterministic + per workflow run so shared infrastructure can be referenced by name. +- **`Get-GitHubRepository`** — test files fetch the shared repo, they do not create repos. +- **`-Skip:($OwnerType -in ('repository', 'enterprise'))`** — standard skip condition for repo-dependent tests. +- **`Disconnect-GitHubAccount`** — every context disconnects all sessions in `AfterAll`. +- Test-specific ephemeral resources (releases, secrets, variables, environments, teams) are still created and cleaned up + within each test file. Only repositories are shared. +- **Exception:** `Repositories.Tests.ps1` creates and deletes its own repos because it tests repository CRUD operations. + +## Naming conventions + +| Resource | Pattern | Example | +|------------|----------------------------------------------|----------------------------------| +| Repo | `Test-{OS}-{TokenType}-{RunID}` | `Test-Linux-USER_FG_PAT-1234` | +| Extra repo | `Test-{OS}-{TokenType}-{RunID}-{N}` | `Test-Linux-USER_FG_PAT-1234-1` | +| Secret | `{TestName}_{OS}_{TokenType}_{RunID}` | `Secrets_Linux_PAT_1234` | +| Variable | `{TestName}_{OS}_{TokenType}_{RunID}` | `Variables_Linux_PAT_1234` | +| Team | `{TestName}_{OS}_{TokenType}_{RunID}_{Name}` | `Teams_Linux_APP_ORG_1234_Pull` | +| Env | `{TestName}-{OS}-{TokenType}-{RunID}` | `Secrets-Linux-PAT-1234` | diff --git a/tests/Releases.Tests.ps1 b/tests/Releases.Tests.ps1 index 3dd9f843f..1b5a5090a 100644 --- a/tests/Releases.Tests.ps1 +++ b/tests/Releases.Tests.ps1 @@ -20,9 +20,9 @@ param() BeforeAll { - $testName = 'ReleasesTests' + $testName = 'Releases' $os = $env:RUNNER_OS - $guid = [guid]::NewGuid().ToString() + $id = $env:GITHUB_RUN_ID } Describe 'Releases' { @@ -40,40 +40,16 @@ Describe 'Releases' { Write-Host ($context | Format-Table | Out-String) } } - $repoPrefix = "$testName-$os-$TokenType" - $repoName = "$repoPrefix-$guid" + $repoPrefix = "Test-$os-$TokenType" + $repoName = "$repoPrefix-$id" - $params = @{ - Name = $repoName - Context = $context - AddReadme = $true - License = 'mit' - Gitignore = 'VisualStudio' - } - switch ($OwnerType) { - 'user' { - Get-GitHubRepository | Where-Object { $_.Name -like "$repoPrefix*" } | Remove-GitHubRepository -Confirm:$false - $repo = New-GitHubRepository @params - } - 'organization' { - Get-GitHubRepository -Organization $Owner | Where-Object { $_.Name -like "$repoPrefix*" } | Remove-GitHubRepository -Confirm:$false - $repo = New-GitHubRepository @params -Organization $owner - } - } - LogGroup "Repository - [$repoName]" { + LogGroup "Using Repository - [$repoName]" { + $repo = Get-GitHubRepository -Owner $Owner -Name $repoName Write-Host ($repo | Select-Object * | Out-String) } } AfterAll { - switch ($OwnerType) { - 'user' { - Get-GitHubRepository | Where-Object { $_.Name -like "$repoPrefix*" } | Remove-GitHubRepository -Confirm:$false - } - 'organization' { - Get-GitHubRepository -Organization $Owner | Where-Object { $_.Name -like "$repoPrefix*" } | Remove-GitHubRepository -Confirm:$false - } - } Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount -Silent Write-Host ('-' * 60) } diff --git a/tests/Repositories.Tests.ps1 b/tests/Repositories.Tests.ps1 index ed482b054..23522bd0d 100644 --- a/tests/Repositories.Tests.ps1 +++ b/tests/Repositories.Tests.ps1 @@ -20,9 +20,9 @@ param() BeforeAll { - $testName = 'RepositoriesTests' + $testName = 'Repositories' $os = $env:RUNNER_OS - $guid = [guid]::NewGuid().ToString() + $id = $env:GITHUB_RUN_ID } Describe 'Repositories' { @@ -41,20 +41,20 @@ Describe 'Repositories' { } } $repoPrefix = "$testName-$os-$TokenType" - $repoName = "$repoPrefix-$guid" + $repoName = "$repoPrefix-$id" switch ($OwnerType) { 'user' { - Get-GitHubRepository | Where-Object { $_.Name -like "$repoPrefix*" } | Remove-GitHubRepository -Confirm:$false + Get-GitHubRepository | Where-Object { $_.Name -like "$repoName*" } | Remove-GitHubRepository -Confirm:$false } 'organization' { - Get-GitHubTeam -Organization $owner | Where-Object { $_.Name -like "$repoPrefix*" } | Remove-GitHubTeam -Confirm:$false + Get-GitHubTeam -Organization $owner | Where-Object { $_.Name -like "$repoName*" } | Remove-GitHubTeam -Confirm:$false $teamAdmin = New-GitHubTeam -Organization $owner -Name "$repoName-admin" $teamMaintain = New-GitHubTeam -Organization $owner -Name "$repoName-maintain" $teamPush = New-GitHubTeam -Organization $owner -Name "$repoName-push" $teamTriage = New-GitHubTeam -Organization $owner -Name "$repoName-triage" $teamPull = New-GitHubTeam -Organization $owner -Name "$repoName-pull" - Get-GitHubRepository -Organization $Owner | Where-Object { $_.Name -like "$repoPrefix*" } | Remove-GitHubRepository -Confirm:$false + Get-GitHubRepository -Organization $Owner | Where-Object { $_.Name -like "$repoName*" } | Remove-GitHubRepository -Confirm:$false } } } @@ -62,11 +62,11 @@ Describe 'Repositories' { AfterAll { switch ($OwnerType) { 'user' { - Get-GitHubRepository | Where-Object { $_.Name -like "$repoPrefix*" } | Remove-GitHubRepository -Confirm:$false + Get-GitHubRepository | Where-Object { $_.Name -like "$repoName*" } | Remove-GitHubRepository -Confirm:$false } 'organization' { - Get-GitHubTeam -Organization $owner | Where-Object { $_.Name -like "$repoPrefix*" } | Remove-GitHubTeam -Confirm:$false - Get-GitHubRepository -Organization $Owner | Where-Object { $_.Name -like "$repoPrefix*" } | Remove-GitHubRepository -Confirm:$false + Get-GitHubTeam -Organization $owner | Where-Object { $_.Name -like "$repoName*" } | Remove-GitHubTeam -Confirm:$false + Get-GitHubRepository -Organization $Owner | Where-Object { $_.Name -like "$repoName*" } | Remove-GitHubRepository -Confirm:$false } } Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount -Silent @@ -600,10 +600,10 @@ Describe 'Repositories' { It 'Remove-GitHubRepository - Removes all repositories' -Skip:($OwnerType -in ('repository', 'enterprise')) { switch ($OwnerType) { 'user' { - Get-GitHubRepository | Where-Object { $_.Name -like "$repoPrefix*" } | Remove-GitHubRepository -Confirm:$false + Get-GitHubRepository | Where-Object { $_.Name -like "$repoName*" } | Remove-GitHubRepository -Confirm:$false } 'organization' { - Get-GitHubRepository -Organization $Owner | Where-Object { $_.Name -like "$repoPrefix*" } | + Get-GitHubRepository -Organization $Owner | Where-Object { $_.Name -like "$repoName*" } | Remove-GitHubRepository -Confirm:$false } } diff --git a/tests/Secrets.Tests.ps1 b/tests/Secrets.Tests.ps1 index e3937cfc1..d8af30896 100644 --- a/tests/Secrets.Tests.ps1 +++ b/tests/Secrets.Tests.ps1 @@ -20,9 +20,9 @@ param() BeforeAll { - $testName = 'SecretsTests' + $testName = 'Secrets' $os = $env:RUNNER_OS - $guid = [guid]::NewGuid().ToString() -replace '-', '_' + $id = $env:GITHUB_RUN_ID } Describe 'Secrets' { @@ -40,26 +40,24 @@ Describe 'Secrets' { Write-Host ($context | Format-List | Out-String) } } - $repoPrefix = "$testName-$os-$TokenType" - $repoName = "$repoPrefix-$guid" + $repoPrefix = "Test-$os-$TokenType" + $repoName = "$repoPrefix-$id" $secretPrefix = "$testName`_$os`_$TokenType" - $secretName = "$secretPrefix`_$guid" + $secretName = "$secretPrefix`_$id" $orgSecretName = "$secretName`_ORG" - $environmentName = "$testName-$os-$TokenType-$guid" + $environmentName = "$testName-$os-$TokenType-$id" switch ($OwnerType) { 'user' { - Get-GitHubRepository | Where-Object { $_.Name -like "$repoPrefix*" } | Remove-GitHubRepository -Confirm:$false - $repo = New-GitHubRepository -Name "$repoName-1" - $repo2 = New-GitHubRepository -Name "$repoName-2" - $repo3 = New-GitHubRepository -Name "$repoName-3" + $repo = Get-GitHubRepository -Name $repoName + $repo2 = Get-GitHubRepository -Name "$repoName-2" + $repo3 = Get-GitHubRepository -Name "$repoName-3" } 'organization' { - Get-GitHubRepository -Organization $Owner | Where-Object { $_.Name -like "$repoPrefix*" } | Remove-GitHubRepository -Confirm:$false - Get-GitHubSecret -Owner $Owner | Where-Object { $_.Name -like "$secretPrefix*" } | Remove-GitHubSecret -Confirm:$false - $repo = New-GitHubRepository -Organization $owner -Name "$repoName-1" - $repo2 = New-GitHubRepository -Organization $owner -Name "$repoName-2" - $repo3 = New-GitHubRepository -Organization $owner -Name "$repoName-3" + Get-GitHubSecret -Owner $Owner | Where-Object { $_.Name -like "$secretName*" } | Remove-GitHubSecret -Confirm:$false + $repo = Get-GitHubRepository -Owner $Owner -Name $repoName + $repo2 = Get-GitHubRepository -Owner $Owner -Name "$repoName-2" + $repo3 = Get-GitHubRepository -Owner $Owner -Name "$repoName-3" LogGroup "Org secret - [$orgSecretName]" { $params = @{ Owner = $owner @@ -83,20 +81,12 @@ Describe 'Secrets' { AfterAll { switch ($OwnerType) { - 'user' { - Get-GitHubRepository | Where-Object { $_.Name -like "$repoPrefix*" } | Remove-GitHubRepository -Confirm:$false - } 'organization' { LogGroup 'Secrets to remove' { $orgSecrets = Get-GitHubSecret -Owner $owner | Where-Object { $_.Name -like "$secretName*" } Write-Host "$($orgSecrets | Format-List | Out-String)" $orgSecrets | Remove-GitHubSecret } - LogGroup 'Repos to remove' { - $reposToRemove = Get-GitHubRepository -Organization $Owner | Where-Object { $_.Name -like "$repoPrefix*" } - Write-Host "$($reposToRemove | Format-List | Out-String)" - $reposToRemove | Remove-GitHubRepository -Confirm:$false - } } } Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount -Silent diff --git a/tests/TEMPLATE.ps1 b/tests/TEMPLATE.ps1 index b73dce38e..9af0f57b2 100644 --- a/tests/TEMPLATE.ps1 +++ b/tests/TEMPLATE.ps1 @@ -20,7 +20,9 @@ param() BeforeAll { - # DEFAULTS ACROSS ALL TESTS + $testName = 'Template' + $os = $env:RUNNER_OS + $id = $env:GITHUB_RUN_ID } Describe 'Template' { @@ -32,25 +34,25 @@ Describe 'Template' { LogGroup 'Context' { Write-Host ($context | Format-List | Out-String) } + if ($AuthType -eq 'APP') { + $context = Connect-GitHubApp @connectAppParams -PassThru -Default -Silent + LogGroup 'Context - Installation' { + Write-Host ($context | Format-List | Out-String) + } + } + + # Reference the shared repo (NOT New-GitHubRepository) + $repoPrefix = "Test-$os-$TokenType" + $repoName = "$repoPrefix-$id" + $repo = Get-GitHubRepository -Owner $Owner -Name $repoName } AfterAll { Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount -Silent Write-Host ('-' * 60) } - # Tests for APP goes here - if ($AuthType -eq 'APP') { - It 'Connect-GitHubApp - Connects as a GitHub App to ' { - $context = Connect-GitHubApp @connectAppParams -PassThru -Default -Silent - LogGroup 'Context' { - Write-Host ($context | Format-List | Out-String) - } - } + It 'Should do something' -Skip:($OwnerType -in ('repository', 'enterprise')) { + # Test logic using $repo, $Owner, $repoName } - - # Tests for runners goes here - if ($Type -eq 'GitHub Actions') {} - - # Tests for IAT UAT and PAT goes here } } diff --git a/tests/Teams.Tests.ps1 b/tests/Teams.Tests.ps1 index 5132228d9..f19d8ca05 100644 --- a/tests/Teams.Tests.ps1 +++ b/tests/Teams.Tests.ps1 @@ -20,9 +20,9 @@ param() BeforeAll { - $testName = 'TeamsTests' + $testName = 'Teams' $os = $env:RUNNER_OS - $guid = [guid]::NewGuid().ToString() -replace '-', '_' + $id = $env:GITHUB_RUN_ID } Describe 'Teams' { @@ -41,11 +41,11 @@ Describe 'Teams' { } } $teamPrefix = "$testName`_$os`_$TokenType" - $teamName = "$teamPrefix`_$guid" + $teamName = "$teamPrefix`_$id" switch ($OwnerType) { 'organization' { - Get-GitHubTeam -Organization $owner | Where-Object { $_.Name -like "$teamPrefix*" } | Remove-GitHubTeam -Confirm:$false + Get-GitHubTeam -Organization $owner | Where-Object { $_.Name -like "$teamName*" } | Remove-GitHubTeam -Confirm:$false } } } @@ -53,7 +53,7 @@ Describe 'Teams' { AfterAll { switch ($OwnerType) { 'organization' { - $teamsToRemove = Get-GitHubTeam -Organization $owner | Where-Object { $_.Name -like "$teamPrefix*" } + $teamsToRemove = Get-GitHubTeam -Organization $owner | Where-Object { $_.Name -like "$teamName*" } LogGroup 'Teams to remove' { Write-Host "$($teamsToRemove | Format-List | Out-String)" } diff --git a/tests/Users.Tests.ps1 b/tests/Users.Tests.ps1 index f4cb7cc32..d3d7247da 100644 --- a/tests/Users.Tests.ps1 +++ b/tests/Users.Tests.ps1 @@ -24,23 +24,18 @@ Describe 'Users' { LogGroup 'Context' { Write-Host ($context | Format-List | Out-String) } - } - AfterAll { - Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount -Silent - Write-Host ('-' * 60) - } - - # Tests for APP goes here - if ($AuthType -eq 'APP') { - It 'Connect-GitHubApp - Connects as a GitHub App to ' { + if ($AuthType -eq 'APP') { $context = Connect-GitHubApp @connectAppParams -PassThru -Default -Silent LogGroup 'Context' { Write-Host ($context | Format-List | Out-String) } } } + AfterAll { + Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount -Silent + Write-Host ('-' * 60) + } - # Tests for IAT UAT and PAT goes here It 'Get-GitHubUser - Get the specified user' { { Get-GitHubUser -Name 'Octocat' } | Should -Not -Throw } diff --git a/tests/Variables.Tests.ps1 b/tests/Variables.Tests.ps1 index ba063b04e..f33a36bc9 100644 --- a/tests/Variables.Tests.ps1 +++ b/tests/Variables.Tests.ps1 @@ -20,9 +20,9 @@ param() BeforeAll { - $testName = 'VariablesTests' + $testName = 'Variables' $os = $env:RUNNER_OS - $guid = [guid]::NewGuid().ToString() -replace '-', '_' + $id = $env:GITHUB_RUN_ID } Describe 'Variables' { @@ -30,42 +30,34 @@ Describe 'Variables' { Context 'As using on ' -ForEach $authCases { BeforeAll { - LogGroup 'Current Contexts' { - $context = Connect-GitHubAccount @connectParams -PassThru -Silent - Write-Host (Get-GitHubContext -ListAvailable | Format-List | Out-String) - } + $context = Connect-GitHubAccount @connectParams -PassThru -Silent LogGroup 'Context' { Write-Host ($context | Format-List | Out-String) } if ($AuthType -eq 'APP') { - LogGroup 'Current Contexts' { - Write-Host (Get-GitHubContext -ListAvailable | Format-List | Out-String) - } LogGroup 'Context - Installation' { $context = Connect-GitHubApp @connectAppParams -PassThru -Default -Silent Write-Host ($context | Format-List | Out-String) } } - $repoPrefix = "$testName-$os-$TokenType" - $repoName = "$repoPrefix-$guid" + $repoPrefix = "Test-$os-$TokenType" + $repoName = "$repoPrefix-$id" $variablePrefix = "$testName`_$os`_$TokenType" - $variableName = "$variablePrefix`_$guid" + $variableName = "$variablePrefix`_$id" $orgVariableName = "$variableName`_ORG" - $environmentName = "$testName-$os-$TokenType-$guid" + $environmentName = "$testName-$os-$TokenType-$id" switch ($OwnerType) { 'user' { - Get-GitHubRepository | Where-Object { $_.Name -like "$repoPrefix*" } | Remove-GitHubRepository -Confirm:$false - $repo = New-GitHubRepository -Name "$repoName-1" - $repo2 = New-GitHubRepository -Name "$repoName-2" - $repo3 = New-GitHubRepository -Name "$repoName-3" + $repo = Get-GitHubRepository -Name $repoName + $repo2 = Get-GitHubRepository -Name "$repoName-2" + $repo3 = Get-GitHubRepository -Name "$repoName-3" } 'organization' { - Get-GitHubRepository -Organization $Owner | Where-Object { $_.Name -like "$repoPrefix*" } | Remove-GitHubRepository -Confirm:$false - Get-GitHubVariable -Owner $Owner | Where-Object { $_.Name -like "$variablePrefix*" } | Remove-GitHubVariable -Confirm:$false - $repo = New-GitHubRepository -Organization $owner -Name "$repoName-1" - $repo2 = New-GitHubRepository -Organization $owner -Name "$repoName-2" - $repo3 = New-GitHubRepository -Organization $owner -Name "$repoName-3" + Get-GitHubVariable -Owner $Owner | Where-Object { $_.Name -like "$variableName*" } | Remove-GitHubVariable -Confirm:$false + $repo = Get-GitHubRepository -Owner $Owner -Name $repoName + $repo2 = Get-GitHubRepository -Owner $Owner -Name "$repoName-2" + $repo3 = Get-GitHubRepository -Owner $Owner -Name "$repoName-3" LogGroup "Org variable - [$orgVariableName]" { $params = @{ Owner = $owner @@ -88,20 +80,12 @@ Describe 'Variables' { AfterAll { switch ($OwnerType) { - 'user' { - Get-GitHubRepository | Where-Object { $_.Name -like "$repoPrefix*" } | Remove-GitHubRepository -Confirm:$false - } 'organization' { - $variablesToRemove = Get-GitHubVariable -Owner $owner | Where-Object { $_.Name -like "$variablePrefix*" } - LogGroup 'Secrets to remove' { + $variablesToRemove = Get-GitHubVariable -Owner $owner | Where-Object { $_.Name -like "$variableName*" } + LogGroup 'Variables to remove' { Write-Host "$($variablesToRemove | Format-List | Out-String)" } $variablesToRemove | Remove-GitHubVariable - LogGroup 'Repos to remove' { - $reposToRemove = Get-GitHubRepository -Organization $Owner | Where-Object { $_.Name -like "$repoPrefix*" } - Write-Host "$($reposToRemove | Format-List | Out-String)" - $reposToRemove | Remove-GitHubRepository -Confirm:$false - } } } Get-GitHubContext -ListAvailable | Disconnect-GitHubAccount -Silent