diff --git a/setup.cfg b/setup.cfg index adf8c25d..42de93f1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -35,3 +35,4 @@ filterwarnings = error markers= repo_suffix: Allows to use an additional suffix for the e2e test repo. + code_path: Allows to place the code in a subdirectory for the e2e test repo. diff --git a/tests/end_to_end/conftest.py b/tests/end_to_end/conftest.py index 50ee5087..ea8f9aaa 100644 --- a/tests/end_to_end/conftest.py +++ b/tests/end_to_end/conftest.py @@ -41,7 +41,7 @@ def _(command, *args, env, **kwargs): @contextlib.contextmanager def _cd(tmp_path: pathlib.Path, path: str): full_path = tmp_path / path - full_path.mkdir(exist_ok=True) + full_path.mkdir(exist_ok=True, parents=True) old_path = pathlib.Path.cwd() if old_path == full_path: yield full_path @@ -191,18 +191,30 @@ def gh_other_username(gh_other): @pytest.fixture -def git_repo(cd, git, action_ref): +def git_repo(cd, git, action_ref, code_path): with cd("repo") as repo: git("init", "-b", "main") + # Copy .github + shutil.copytree( + pathlib.Path(__file__).parent / "repo" / ".github", + repo / ".github", + dirs_exist_ok=True, + ) + # Copy everything else shutil.copytree( pathlib.Path(__file__).parent / "repo", - repo, + repo / code_path, dirs_exist_ok=True, + ignore=shutil.ignore_patterns(".github"), ) # Rewrite the specific version of the action we run in the workflow files. for file in (repo / ".github/workflows").iterdir(): - file: pathlib.Path - file.write_text(file.read_text().replace("__ACTION_REF__", action_ref)) + content = ( + file.read_text() + .replace("__ACTION_REF__", action_ref) + .replace("__ACTION_COVERAGE_PATH__", str(code_path)) + ) + file.write_text(content) git("add", ".") git("commit", "-m", "initial commit") @@ -221,6 +233,12 @@ def repo_name(request): return name +@pytest.fixture +def code_path(request): + mark = request.node.get_closest_marker("code_path") + return pathlib.Path(*mark.args) if mark else pathlib.Path(".") + + @pytest.fixture def repo_full_name(repo_name, gh_me_username): return f"{gh_me_username}/{repo_name}" @@ -378,9 +396,9 @@ def _(*, workflow_name, triggering_user, gh): @pytest.fixture -def add_coverage_line(git): +def add_coverage_line(git, code_path): def f(line): - csv_file = pathlib.Path("tests/cases.csv") + csv_file = pathlib.Path(code_path / "tests/cases.csv") csv_file.write_text(csv_file.read_text() + line + "\n") git("add", str(csv_file)) diff --git a/tests/end_to_end/repo/.github/workflows/ci.yml b/tests/end_to_end/repo/.github/workflows/ci.yml index dbdc8bb8..18c7daf2 100644 --- a/tests/end_to_end/repo/.github/workflows/ci.yml +++ b/tests/end_to_end/repo/.github/workflows/ci.yml @@ -4,7 +4,7 @@ on: pull_request: push: branches: - - 'main' + - "main" concurrency: group: CI @@ -17,7 +17,6 @@ jobs: pull-requests: write contents: write steps: - - uses: actions/checkout@v3 - name: Install poetry @@ -25,12 +24,14 @@ jobs: - uses: actions/setup-python@v4 with: - python-version: '3.10' - cache: 'poetry' + python-version: "3.10" + cache: "poetry" - run: poetry install + working-directory: __ACTION_COVERAGE_PATH__ - run: poetry run pytest + working-directory: __ACTION_COVERAGE_PATH__ - name: Coverage comment id: coverage_comment @@ -39,6 +40,7 @@ jobs: GITHUB_TOKEN: ${{ github.token }} ANNOTATE_MISSING_LINES: true ANNOTATION_TYPE: notice + COVERAGE_PATH: __ACTION_COVERAGE_PATH__ - name: Store Pull Request comment to be posted uses: actions/upload-artifact@v3 diff --git a/tests/end_to_end/repo/.github/workflows/coverage-comment.yml b/tests/end_to_end/repo/.github/workflows/coverage-comment.yml index af0387de..9db6642d 100644 --- a/tests/end_to_end/repo/.github/workflows/coverage-comment.yml +++ b/tests/end_to_end/repo/.github/workflows/coverage-comment.yml @@ -21,3 +21,4 @@ jobs: with: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_PR_RUN_ID: ${{ github.event.workflow_run.id }} + COVERAGE_PATH: __ACTION_COVERAGE_PATH__ diff --git a/tests/end_to_end/test_all.py b/tests/end_to_end/test_all.py index 81482cb0..4b445295 100644 --- a/tests/end_to_end/test_all.py +++ b/tests/end_to_end/test_all.py @@ -5,6 +5,7 @@ @pytest.mark.repo_suffix("public") +@pytest.mark.code_path("subdir") def test_public_repo( gh_create_repo, wait_for_run_to_start, @@ -28,7 +29,7 @@ def test_public_repo( run_id = wait_for_run_to_start(sha1=get_sha1(), branch="main", gh=gh_me) # AAAaand it's started. Now let's wait for it to end. - # Also, raise if it doesn't end succefully. That half of the job. + # Also, raise if it doesn't end successfully. That half of the job. gh_me("run", "watch", run_id, "--exit-status") # Now to the other half: maybe it did nothing successfully, so let's check