diff --git a/README.md b/README.md index 4f5b5df..e2194fe 100644 --- a/README.md +++ b/README.md @@ -165,7 +165,7 @@ cd eks_automation python -m pytest tests/ -v -m integration ``` -Note: Integration tests will create temporary repositories in your GitHub organization. These repositories will be archived (not deleted) after the tests complete. +Note: Integration tests will create temporary repositories in your GitHub organization. These repositories will be archived (not deleted) after the tests complete. To implement full cleanup, the test code would need to be modified to delete repositories instead of archiving them. ## Resources diff --git a/template_automation/tests/conftest.py b/template_automation/tests/conftest.py index 769a94b..4ae8ce7 100644 --- a/template_automation/tests/conftest.py +++ b/template_automation/tests/conftest.py @@ -1,6 +1,49 @@ -import pytest import os -import json +import pytest +from github import Github +import time + +@pytest.fixture(scope="session") +def github_client(): + """Create a GitHub client for integration tests.""" + token = os.environ.get("GITHUB_TOKEN") + if not token: + pytest.skip("GITHUB_TOKEN environment variable not set") + + api_url = os.environ.get("GITHUB_API", "https://api.github.com") + return Github(base_url=api_url, login_or_token=token) + +@pytest.fixture(scope="session") +def cleanup_mode(): + """Determine if repositories should be deleted or just archived.""" + return os.environ.get("INTEGRATION_TEST_DELETE_REPOS", "").lower() in ("true", "1", "yes") + +@pytest.fixture +def test_repo(github_client, cleanup_mode, request): + """Create a test repository and clean it up after the test.""" + org_name = os.environ.get("GITHUB_ORG") + if not org_name: + pytest.skip("GITHUB_ORG environment variable not set") + + # Create a unique repo name for this test + repo_name = f"test-repo-{pytest.config.getoption('--timestamp', default='')}-{id(request)}" + + org = github_client.get_organization(org_name) + repo = org.create_repo( + repo_name, + description="Temporary repository for integration testing", + private=True + ) + + yield repo + + # Clean up after the test + if cleanup_mode: + # Delete the repository + repo.delete() + else: + # Archive the repository (the original behavior) + repo.edit(archived=True) @pytest.fixture def github_client_params(): @@ -71,4 +114,9 @@ def mock_reference_response(): "sha": "test-commit-sha", "type": "commit" } - } \ No newline at end of file + } + +def pytest_addoption(parser): + """Add custom command line options.""" + timestamp = int(time.time()) + parser.addoption("--timestamp", action="store", default=str(timestamp)) \ No newline at end of file diff --git a/template_automation/tests/integration/test_github_operations.py b/template_automation/tests/integration/test_github_operations.py new file mode 100644 index 0000000..caf95fb --- /dev/null +++ b/template_automation/tests/integration/test_github_operations.py @@ -0,0 +1,16 @@ +import pytest +import os + +@pytest.mark.integration +def test_repository_operations(test_repo, cleanup_mode): + """Test basic repository operations.""" + # Your test code here that uses the test_repo + + # This is just an example verification + assert test_repo.name.startswith("test-repo-") + + # Log what will happen to this repository + if cleanup_mode: + print(f"Repository {test_repo.name} will be DELETED after this test") + else: + print(f"Repository {test_repo.name} will be ARCHIVED after this test") diff --git a/template_automation/tests/pytest.ini b/template_automation/tests/pytest.ini new file mode 100644 index 0000000..948fe8b --- /dev/null +++ b/template_automation/tests/pytest.ini @@ -0,0 +1,5 @@ +[pytest] +markers = + integration: marks tests as integration tests (requires GitHub API access) +addopts = --timestamp=%(timestamp)s +python_functions = test_*