stages:
  - test
  - build
  - coverage_report

variables:
  BUILD_ENV_DOCKER_IMAGE: docker-registry.ebrains.eu/esd/tmp:latest

default:
  before_script:
    - chmod +x dedal/utils/bootstrap.sh
    - ./dedal/utils/bootstrap.sh
    - pip install -e .[test]

build-wheel:
  stage: build
  tags:
    - docker-runner
  image: python:latest
  before_script:
    - python -m pip install --upgrade pip setuptools wheel build
  script:
    - python -m build --sdist --wheel
  artifacts:
    paths:
      - dist/*.whl
      - dist/*.tar.gz
    expire_in: 1 week

unit_tests:
  stage: test
  tags:
    - docker-runner
  image: ubuntu:22.04
  script:
    - coverage run -m pytest -s --tb=short --junitxml=test-results.xml ./dedal/tests/unit_tests
    - mv .coverage .coverage.unit  # Rename to avoid overwriting
  artifacts:
    when: always
    reports:
      junit: test-results.xml
    paths:
      - test-results.xml
      - .dedal.log
      - .generate_cache.log
      - .coverage.unit
    expire_in: 1 week

integration_tests:
  stage: test
  tags:
    - docker-runner
  image: ubuntu:22.04
  script:
    - coverage run -m pytest -s --tb=short --junitxml=test-results.xml ./dedal/tests/integration_tests
    - mv .coverage .coverage.integration  # Rename to avoid overwriting
  needs: ["unit_tests"]
  artifacts:
    when: always
    reports:
      junit: test-results.xml
    paths:
      - test-results.xml
      - .dedal.log
      - .generate_cache.log
      - .coverage.integration
    expire_in: 1 week

merge_coverage:
  stage: coverage_report
  tags:
    - docker-runner
  image: ubuntu:22.04
  script:
    - coverage combine .coverage.unit .coverage.integration
    - coverage report
    - coverage xml -o coverage.xml
    - coverage html -d coverage_html
  artifacts:
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage.xml
    paths:
      - coverage.xml
      - coverage_html
    expire_in: 1 week
  coverage: '/TOTAL.*?(\d+\%)$/'

