From 368141c507c91ebca2dad917f945bc47a9bcf6f7 Mon Sep 17 00:00:00 2001 From: Robert Szczepanski Date: Fri, 23 Feb 2024 15:00:37 +0100 Subject: [PATCH] Run tests for all Python versions in a single nox session Internal-tag: [#53659] Signed-off-by: Robert Szczepanski --- .ci.yml | 92 ++++++++++++++--------------- .flake8 | 1 + .github/workflows/pipeline.yml | 102 ++++++++++++++++++++++----------- noxfile.py | 76 ++++++++++++++++++++++-- pyproject.toml | 2 + 5 files changed, 187 insertions(+), 86 deletions(-) diff --git a/.ci.yml b/.ci.yml index effb0d52..1f97b007 100644 --- a/.ci.yml +++ b/.ci.yml @@ -9,10 +9,12 @@ stages: lint: stage: test - image: ubuntu:latest + image: debian:bookworm before_script: - apt-get update -qq - - apt-get install -y --no-install-recommends python3-pip python3-venv + - apt-get install -y --no-install-recommends + python3-pip + python3-venv - python3 -m venv venv - source venv/bin/activate - python3 -m pip install -U pip wheel setuptools @@ -20,67 +22,61 @@ lint: - python3 -m pip install -r requirements.txt - nox -s isort_check black_check flake8 -.run_tests: - stage: test - image: ubuntu:latest +.generate_example: + stage: examples + image: debian:bookworm variables: GIT_SUBMODULE_STRATEGY: normal DEBIAN_FRONTEND: noninteractive + PYTHON_VERSION: "3.12" before_script: + - apt-get update -qq + - apt-get install -y --no-install-recommends + python3-venv + python3-pip - source .github/scripts/setup_env.sh $PYTHON_VERSION - - install_deps - - configure_python_env - - activate_python_env + - python3 -m venv venv + - source venv/bin/activate - python3 -m pip install git+https://github.com/antmicro/tuttest - tuttest README.md | bash - script: - - nox -s tests_with_report - artifacts: - paths: - - cov_html + - cd examples/$EXAMPLE + - tuttest README.md generate | bash - -.generate_example: - stage: examples - image: ubuntu:latest +run_tests: + stage: test + image: debian:bookworm variables: GIT_SUBMODULE_STRATEGY: normal DEBIAN_FRONTEND: noninteractive - PYTHON_VERSION: "3.12" before_script: - - source .github/scripts/setup_env.sh $PYTHON_VERSION - - install_deps - - configure_python_env - - activate_python_env + - apt-get update -qq + - apt-get install -y --no-install-recommends + curl + wget + git + python3-dev + python3-venv + python3-pip + make + ninja-build + gcc-riscv64-unknown-elf + bsdextrautils + verilator + libssl-dev + libreadline-dev + libffi-dev + libbz2-dev + libncurses-dev + libsqlite3-dev + liblzma-dev + - python3 -m venv venv + - source venv/bin/activate + - python3 -m pip install nox - python3 -m pip install git+https://github.com/antmicro/tuttest - tuttest README.md | bash - script: - - cd examples/$EXAMPLE - - tuttest README.md generate | bash - - -tests_py38: - extends: .run_tests - variables: - PYTHON_VERSION: "3.8" - -tests_py39: - extends: .run_tests - variables: - PYTHON_VERSION: "3.9" - -tests_py310: - extends: .run_tests - variables: - PYTHON_VERSION: "3.10" - -tests_py311: - extends: .run_tests - variables: - PYTHON_VERSION: "3.11" - -tests_py312: - extends: .run_tests - variables: - PYTHON_VERSION: "3.12" + - nox -s test_all_in_env generate_hdmi: extends: .generate_example @@ -98,7 +94,7 @@ generate_pwm: EXAMPLE: pwm include: - - project: 'repositories/fpga-topwrap' + - project: 'repositories/topwrap' ref: internal_ci_yaml file: '/internal.yml' diff --git a/.flake8 b/.flake8 index 6148b35f..001711e5 100644 --- a/.flake8 +++ b/.flake8 @@ -15,6 +15,7 @@ exclude = __pycache__, docs/source/conf.py, venv, + build, builds, kenning-pipeline-manager, miniconda3, diff --git a/.github/workflows/pipeline.yml b/.github/workflows/pipeline.yml index 2152882f..1d5d9f31 100644 --- a/.github/workflows/pipeline.yml +++ b/.github/workflows/pipeline.yml @@ -5,72 +5,104 @@ name: Pipeline on: [pull_request, push, workflow_dispatch] +defaults: + run: + shell: bash + jobs: Lint: runs-on: ubuntu-latest - name: "Run Lint checks on Python sources" + container: + image: debian:bookworm + name: "Run lint checks" env: DEBIAN_FRONTEND: noninteractive steps: + - name: Install system packages + run: | + apt-get update -qq + apt-get install -y --no-install-recommends \ + git \ + python3-pip \ + python3-venv + - uses: actions/checkout@v3 with: submodules: true - - name: Install dev requirements + - name: Install Python dependencies run: | - sudo apt-get update -qq - sudo apt-get install -y --no-install-recommends python3-pip python3-venv python3 -m venv venv source venv/bin/activate python3 -m pip install -U pip wheel setuptools + python3 -m pip install -r requirements.txt - name: Run lint checks run: | - python3 -m pip install -r requirements.txt + source venv/bin/activate nox -s isort_check black_check flake8 Tests: runs-on: ubuntu-latest - name: "Test Python ${{ matrix.python-version }}" - - strategy: - fail-fast: false - matrix: - python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] + container: + image: debian:bookworm + name: "Run tests" env: DEBIAN_FRONTEND: noninteractive steps: + - name: Install system packages + run: | + apt-get update -qq + apt-get install -y --no-install-recommends \ + curl \ + wget \ + git \ + python3-dev \ + python3-venv \ + python3-pip \ + make \ + ninja-build \ + gcc-riscv64-unknown-elf \ + bsdextrautils \ + verilator \ + libssl-dev \ + libreadline-dev \ + libffi-dev \ + libbz2-dev \ + libncurses-dev \ + libsqlite3-dev \ + liblzma-dev + - uses: actions/checkout@v3 with: submodules: true - - name: Install dev requirements + - name: Install Python dependencies run: | - source .github/scripts/setup_env.sh ${{ matrix.python-version }} - install_deps - configure_python_env - activate_python_env + python3 -m venv venv + source venv/bin/activate + python3 -m pip install nox python3 -m pip install git+https://github.com/antmicro/tuttest - name: Build run: | - source .github/scripts/setup_env.sh ${{ matrix.python-version }} - activate_python_env + source venv/bin/activate tuttest README.md | bash - - name: Run pytest with nox run: | - source .github/scripts/setup_env.sh ${{ matrix.python-version }} - activate_python_env - nox -s tests + source venv/bin/activate + nox -s test_all_in_env Examples: runs-on: ubuntu-latest - name: 'Example ${{ matrix.example }}' + container: + image: debian:bookworm + name: 'Generate example ${{ matrix.example }}' strategy: fail-fast: false @@ -80,29 +112,31 @@ jobs: - inout - pwm - env: - PYTHON_VERSION: "3.12" - steps: + - name: Install system packages + run: | + apt-get update -qq + apt-get install -y --no-install-recommends \ + git \ + python3-dev \ + python3-venv \ + python3-pip + - uses: actions/checkout@v3 - - name: Install dev requirements + - name: Install Python dependencies run: | - source .github/scripts/setup_env.sh $PYTHON_VERSION - install_deps - configure_python_env - activate_python_env + python3 -m venv venv + source venv/bin/activate python3 -m pip install git+https://github.com/antmicro/tuttest - name: Install fpga-topwrap run: | - source .github/scripts/setup_env.sh $PYTHON_VERSION - activate_python_env + source venv/bin/activate tuttest README.md | bash - - name: Generate sources for example ${{ matrix.example }} setup run: | - source .github/scripts/setup_env.sh $PYTHON_VERSION - activate_python_env + source venv/bin/activate cd examples/${{ matrix.example }} tuttest README.md generate | bash - diff --git a/noxfile.py b/noxfile.py index 330f7f27..efd9fd23 100644 --- a/noxfile.py +++ b/noxfile.py @@ -1,11 +1,17 @@ # Copyright (c) 2023-2024 Antmicro # SPDX-License-Identifier: Apache-2.0 +import os +import shutil +import subprocess +import sys + import nox BLACK_VERSION = "black==23.12.1" FLAKE8_VERSION = "flake8==7.0.0." ISORT_VERSION = "isort==5.13.2" +PYTHON_VERSIONS = ["3.8", "3.9", "3.10", "3.11", "3.12"] @nox.session() @@ -50,15 +56,77 @@ def black_check(session): session.run("black", "--check", ".") -@nox.session +@nox.session(python=PYTHON_VERSIONS) def tests(session: nox.Session) -> None: session.install("-e", ".") session.install("-r", "requirements.txt") - session.run("pytest", "--cov=fpga_topwrap", "tests") + session.run("pytest", "-rs", "--cov=fpga_topwrap", "tests") -@nox.session +@nox.session(python=PYTHON_VERSIONS) def tests_with_report(session: nox.Session) -> None: session.install("-e", ".") session.install("-r", "requirements.txt") - session.run("pytest", "--cov-report", "html:cov_html", "--cov=fpga_topwrap", "tests") + session.run("pytest", "-rs", "--cov-report", "html:cov_html", "--cov=fpga_topwrap", "tests") + + +@nox.session +def test_all_in_env(session: nox.Session) -> None: + project_dir = os.path.dirname(os.path.abspath(__file__)) + pyenv_dir = f"{project_dir}/build/.pyenv" + pyenv_bin = f"{os.path.join(pyenv_dir, 'bin')}" + pyenv_shims = f"{os.path.join(pyenv_dir, 'shims')}" + path = f"{pyenv_bin}:{pyenv_shims}:{os.getenv('PATH')}" + + env = os.environ.copy() + env["PATH"] = path + env["PYENV_ROOT"] = pyenv_dir + + if not shutil.which("pyenv", path=path): + p = subprocess.run( + "curl https://pyenv.run | bash", + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + text=True, + shell=True, + env=env, + ) + session.log(p.stdout.strip("\n")) + + if p.returncode: + session.error() + + session.log("Configuring pyenv") + p = subprocess.run( + "pyenv init -", + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + text=True, + shell=True, + env=env, + ) + session.log(p.stdout.strip("\n")) + + if p.returncode: + session.error() + + for ver in PYTHON_VERSIONS: + if not shutil.which(f"python{ver}", path=path): + session.log(f"Installing Python {ver}") + p = subprocess.run( + f"pyenv install {ver}", + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + text=True, + shell=True, + env=env, + ) + session.log(p.stdout.strip("\n")) + + if p.returncode: + session.error() + + if p.returncode: + session.error() + + session.run("nox", "-s", "tests_with_report", external=True, env=env) diff --git a/pyproject.toml b/pyproject.toml index 4fcdb157..17a28782 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -14,6 +14,7 @@ exclude = ''' | \.pytest_cache | __pycache__ | venv + | build | builds | kenning-pipeline-manager | soc_generator @@ -36,6 +37,7 @@ skip = [ ".pytest_cache", "__pycache__", "venv", + "build", "builds", "kenning-pipeline-manager", "miniconda3",