stages:
  - test
  - deploy

before_script:
  - apt-get update --yes
  # - apt-get install --yes build-essential cmake graphviz python3-pyqt5 xvfb xdg-utils lcov
  - apt-get install --yes graphviz python3-pyqt5 xvfb xdg-utils
  - apt-get install -y libxcb-cursor-dev
  - python -m pip install --upgrade pip
  - python --version
  - pip install .[$QT_API]
  - git fetch --tags
  # - export CXXFLAGS='--coverage'
  # Install without dependencies to make sure that requirements.txt is up-to-date
  - pip install --no-deps -ve .
  - pip show b_asic
  - export QT_API=$QT_API
  # Install test dependencies
  - pip install .[test]
  - export PYTEST_QT_API=$QT_API

.run-test:
  stage: test
  script:
    - pytest --cov=b_asic --cov-report=xml:cov.xml --cov-report=term --cov-branch --color=yes test --timeout=30 --durations=10
    # - lcov --capture --directory . --output-file coverage.info
    # - lcov --output-file coverage.info --extract coverage.info $PWD/src/'*' $PWD/b_asic/'*'
    # - lcov --list coverage.info
    # - find . -name '*.gc*' -delete
  artifacts:
    paths:
      - result_images/
    when: always
    reports:
      coverage_report:
        coverage_format: cobertura
        path: cov.xml
  coverage: /(?i)total.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/

run-test-3.10-pyqt6:
  variables:
    QT_API: pyqt6
  image: python:3.10
  extends: ".run-test"

run-test-3.10-pyside6:
  variables:
    QT_API: pyside6
  image: python:3.10
  extends: ".run-test"

run-test-3.11-pyqt6:
  variables:
    QT_API: pyqt6
  image: python:3.11
  extends: ".run-test"

run-test-3.11-pyside6:
  variables:
    QT_API: pyside6
  image: python:3.11
  extends: ".run-test"

run-test-3.12-pyqt6:
  variables:
    QT_API: pyqt6
  image: python:3.12
  extends: ".run-test"

run-test-3.12-pyside6:
  variables:
    QT_API: pyside6
  image: python:3.12
  extends: ".run-test"

run-test-3.13-pyqt6:
  variables:
    QT_API: pyqt6
  image: python:3.13
  extends: ".run-test"

run-test-3.13-pyside6:
  variables:
    QT_API: pyside6
  image: python:3.13
  extends: ".run-test"

run-vhdl-tests:
  variables:
    QT_API: pyqt6
  image: python:3.10
  stage: test
  script:
    - pytest
    - pip install vunit_hdl
    - apt install -y ghdl
    - cd b_asic/codegen/testbench
    - python test.py

run-doc-test:
  variables:
    QT_API: pyside6
  image: python:3.10
  stage: test
  script:
    - pip install .[doc]
    - sphinx-build -b html docs_sphinx public
    # Run linting on doc-strings
    - pip install black velin
    - velin . --check --black
    - pip install ruff
    - ruff check b_asic --output-format=gitlab > ruff.json
  artifacts:
    name: "${CI_PROJECT_NAME}_code_quality"
    when: always
    reports:
      codequality: ruff.json

pages:
  variables:
    QT_API: pyqt6
  stage: deploy
  image: python:3.10
  script:
    - apt-get install --yes optipng
    - pip install .[doc]
    - sphinx-build -b html docs_sphinx public
  artifacts:
    paths:
      - public
  only:
    - master