Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(templates): Migrate shell scripts for cookiecutter e2e tests to Nox #1563

Merged
merged 11 commits into from
Mar 31, 2023
61 changes: 27 additions & 34 deletions .github/workflows/cookiecutter-e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,32 @@ env:

jobs:
lint:
name: Lint ${{ matrix.cookiecutter }}, Replay ${{ matrix.replay }} on ${{ matrix.python-version }} ${{ matrix.python-version }} / ${{ matrix.os }}
name: Cookiecutter E2E ${{ matrix.python-version }} ${{ matrix.python-version }} / ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
include:
- { cookiecutter: "tap-template", replay: "tap-graphql-jwt.json", python-version: "3.9", os: "ubuntu-latest" }
- { cookiecutter: "tap-template", replay: "tap-rest-api_key-github.json", python-version: "3.9", os: "ubuntu-latest" }
- { cookiecutter: "tap-template", replay: "tap-rest-basic_auth.json", python-version: "3.9", os: "ubuntu-latest" }
- { cookiecutter: "tap-template", replay: "tap-rest-bearer_token.json", python-version: "3.9", os: "ubuntu-latest" }
- { cookiecutter: "tap-template", replay: "tap-rest-custom.json", python-version: "3.9", os: "ubuntu-latest" }
- { cookiecutter: "tap-template", replay: "tap-rest-jwt.json", python-version: "3.9", os: "ubuntu-latest" }
- { cookiecutter: "tap-template", replay: "tap-rest-oauth2.json", python-version: "3.9", os: "ubuntu-latest" }
- { cookiecutter: "tap-template", replay: "tap-other-custom.json", python-version: "3.9", os: "ubuntu-latest" }
- { cookiecutter: "tap-template", replay: "tap-sql-custom.json", python-version: "3.9", os: "ubuntu-latest" }
- { cookiecutter: "target-template", replay: "target-per_record.json", python-version: "3.9", os: "ubuntu-latest" }
- { python-version: "3.10", os: "ubuntu-latest" }

steps:
- name: Check out the repository
uses: actions/checkout@v3.4.0
uses: actions/checkout@v3.3.0

- name: Install Poetry & Tox
env:
PIP_CONSTRAINT: .github/workflows/constraints.txt
run: |
pipx install poetry
poetry --version
pipx install tox
- name: Setup Python ${{ matrix.python-version }}
uses: actions/[email protected]
with:
python-version: ${{ matrix.python-version }}
architecture: x64
cache: 'pip'
cache-dependency-path: 'poetry.lock'

- name: Upgrade pip
env:
Expand All @@ -52,32 +52,25 @@ jobs:
pip install pip
pip --version

- name: Install Poetry, Tox & Cookiecutter
- name: Install Nox
env:
PIP_CONSTRAINT: .github/workflows/constraints.txt
run: |
pipx install poetry
pipx install cookiecutter
pipx install tox
pipx install --pip-args=--constraint=.github/workflows/constraints.txt nox
pipx inject --pip-args=--constraint=.github/workflows/constraints.txt nox nox-poetry
nox --version

- name: Build cookiecutter project
env:
CC_TEMPLATE: cookiecutter/${{ matrix.cookiecutter }}
REPLAY_FILE: e2e-tests/cookiecutters/${{ matrix.replay }}
- name: Run Nox
run: |
chmod +x ./e2e-tests/cookiecutters/test_cookiecutter.sh
./e2e-tests/cookiecutters/test_cookiecutter.sh $CC_TEMPLATE $REPLAY_FILE 0

- uses: actions/upload-artifact@v3
nox --python=${{ matrix.python-version }} --session=test_cookiecutter
- name: Upload build artifacts
if: always()
uses: actions/upload-artifact@v3
with:
name: ${{ matrix.replay }}
path: |
${{ env.CC_TEST_OUTPUT }}/
!${{ env.CC_TEST_OUTPUT }}/.mypy_cache/

- name: Run lint
env:
REPLAY_FILE: e2e-tests/cookiecutters/${{ matrix.replay }}
run: |
cd $CC_TEST_OUTPUT
poetry run tox -e lint
/tmp/tap-*
/tmp/target-*
!/tmp/tap-*/.mypy_cache/
!/tmp/target-*/.mypy_cache/
!/tmp/tap-*/.tox/
!/tmp/target-*/.tox/
58 changes: 0 additions & 58 deletions e2e-tests/cookiecutters/test_cookiecutter.sh

This file was deleted.

73 changes: 73 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

from __future__ import annotations

import glob
import os
import shutil
import sys
import tempfile
from pathlib import Path
from textwrap import dedent

Expand All @@ -27,6 +29,7 @@
"mypy",
"tests",
"doctest",
"test_cookiecutter",
)
test_dependencies = [
"coverage[toml]",
Expand Down Expand Up @@ -170,3 +173,73 @@ def docs_serve(session: Session) -> None:
shutil.rmtree(build_dir)

session.run("sphinx-autobuild", *args)


@nox.parametrize("replay_file_path", glob.glob("./e2e-tests/cookiecutters/*.json"))
@session(python=main_python_version)
def test_cookiecutter(session: Session, replay_file_path) -> None:
"""Uses the tap template to build an empty cookiecutter.

Runs the lint task on the created test project.
"""
args = session.posargs or ["1"]

cc_build_path = tempfile.gettempdir()
folder_base_path = "./cookiecutter"

target_folder = (
"tap-template"
if Path(replay_file_path).name.startswith("tap")
else "target-template"
)
tap_template = Path(folder_base_path + "/" + target_folder).resolve()
replay_file = Path(replay_file_path).resolve()

if not Path(tap_template).exists():
return

if not Path(replay_file).is_file():
return

sdk_dir = Path(Path(tap_template).parent).parent
cc_output_dir = Path(replay_file_path).name.replace(".json", "")
cc_test_output = cc_build_path + "/" + cc_output_dir

if Path(cc_test_output).exists():
session.run("rm", "-fr", cc_test_output, external=True)

session.install(".")
session.install("cookiecutter", "pythonsed")

session.run(
"cookiecutter",
"--replay-file",
str(replay_file),
str(tap_template),
"-o",
cc_build_path,
)
session.chdir(cc_test_output)

session.run(
"pythonsed",
"-i.bak",
's|singer-sdk =.*|singer-sdk = \\{ path = "'
+ str(sdk_dir)
+ '", develop = true \\}|',
"pyproject.toml",
)
session.run("poetry", "lock", external=True)
session.run("poetry", "install", external=True)

for path in glob.glob(f"{Path.cwd()}/*", recursive=True):
if Path(path).name.startswith("tap") or Path(
path,
).name.startswith("target"):
library_name = Path(path).name

for argument in ["black", "isort", "flake8", "mypy"]:
session.run("poetry", "run", argument, library_name, external=True)

if int(args[0]) == 1:
session.run("poetry", "run", "tox", "-e", "lint", external=True)