Skip to content

Fix integration tests leaving repositories behind in test accounts #646

Description

Manual cleanup was needed in the psmodule-user, psmodule-test-org, and psmodule-test-org2 test accounts because repositories from the GitHub module test suite were still present after test runs.

Request

What happens

Repository-backed integration tests sometimes leave behind repositories after the run finishes. This appears to happen after failed runs and may also happen after successful runs, which means the test accounts gradually accumulate stale repositories across all of the test owners.

This is operationally noisy, makes it harder to reason about the current test state, and eventually forces manual cleanup of the shared test accounts. The problem appears to date back a long way rather than being limited to a recent change.

What is expected

A test run should clean up the repositories it created regardless of whether the run passes, fails, or is re-run. Repository cleanup should be deterministic and scoped to the current run so that shared test accounts do not accumulate stale repositories over time.

Acceptance criteria

  • No manual repository cleanup is needed after normal test runs in psmodule-user, psmodule-test-org, or psmodule-test-org2
  • Repository-backed tests clean up the repositories created for the current run even when a test fails partway through
  • Cleanup is deterministic and scoped by run ID rather than broad prefix scans over all repositories in an owner
  • The repository lifecycle logic is consistent across setup, per-test execution, and teardown
  • The suite has coverage that would catch regressions where repositories are left behind again

Technical decisions

Authoritative lifecycle scripts: tests/BeforeAll.ps1 and tests/AfterAll.ps1 should remain the authoritative setup and teardown surfaces for per-run test repositories. They already use deterministic names based on {TestName}-{OS}-{TokenType}-{GITHUB_RUN_ID}.

Cleanup strategy: Cleanup should use deterministic name lookups such as Get-GitHubRepository -Name / Get-GitHubRepository -Owner ... -Name rather than listing all repositories and deleting by prefix. That keeps teardown scoped to the exact repositories for the current run and matches the current global setup/teardown approach.

Consistency with test instructions: tests/.github/instructions/tests.instructions.md already describes deterministic per-run repositories and global teardown. Any remaining inline cleanup in individual test files should be aligned with that contract instead of using older prefix-based cleanup patterns.

Primary investigation target: tests/Repositories.Tests.ps1 still contains inline cleanup that lists repositories by prefix in both BeforeAll and AfterAll, while tests/AfterAll.ps1 already uses deterministic cleanup. Start by reconciling those paths and then audit the rest of the repository-backed tests for similar legacy cleanup code.

Failure handling: Cleanup must still run when a test fails mid-file. If per-file setup creates companion resources such as extra repositories or teams, teardown should remove those resources using the same deterministic naming scheme.

Verification approach: Add or update tests around the setup/teardown scripts and the repository-backed test files so the suite asserts the expected cleanup contract instead of relying on manual inspection of the test accounts.


Implementation plan

Cleanup flow

  • Audit repository-backed test files for legacy prefix-based repository cleanup
  • Update tests/Repositories.Tests.ps1 to stop listing repositories by prefix and use deterministic per-run cleanup instead
  • Align any remaining per-file repository cleanup with the same deterministic naming used by tests/BeforeAll.ps1 and tests/AfterAll.ps1
  • Ensure companion resources created by repository tests, including extra repositories and teams, follow the same teardown contract

Failure and rerun behavior

  • Verify cleanup still runs for failed test contexts and workflow reruns that reuse the same GITHUB_RUN_ID
  • Confirm the suite does not delete unrelated repositories owned by the shared test accounts while cleaning up the current run

Tests and documentation

  • Add regression coverage for repository lifecycle cleanup in the test infrastructure scripts or focused tests around them
  • Update test guidance if needed so the repository cleanup contract is explicit and enforced in future test files

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions