Commit acd1dc2c authored by Adrian DC's avatar Adrian DC
Browse files

ci(gitlab-ci, tests): implement coverage initial jobs and tests

Signed-off-by: default avatarAdrian DC <radian.dc@gmail.com>
parent b83320ef
Loading
Loading
Loading
Loading
Loading
+3
Changes for .gitignore: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -5,6 +5,9 @@ build/
*.pyc
__pycache__/

# Coverage files
coverage-reports/

# Distribution files
.eggs/
*.egg-info/
+171 −1
Changes for .gitlab-ci.yml: 171 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ stages:
  - development
  - prepare
  - build
  - test
  - quality
  - deploy

@@ -70,8 +71,11 @@ images:
    # Rehost standard images
    - |
      for base_rename in \
          'python:3.8' \
          'python:3.12' \
          'python:3.12-alpine' \
          'sonarsource/sonar-scanner-cli:latest' \
          'tobix/pywine:3.10' \
      ; do
        base=$(echo "${base_rename}" | cut -d'|' -f1 | tr -d ' ')
        rename=$(echo "${base_rename}" | cut -d'|' -f2 | tr -d ' ')
@@ -95,6 +99,10 @@ images:
          'build                 | python:3.12-alpine | build' \
          'codestyle             | python:3.12-alpine | codestyle' \
          'deploy                | python:3.12-alpine | deploy' \
          'test-coverage-linux   | python:3.12        | test-coverage-linux' \
          'test-coverage-windows | tobix/pywine:3.10  | test-coverage-windows' \
          'test-oldest           | python:3.8         | test-linux' \
          'test-latest           | python:3.12        | test-linux' \
      ; do
        name=$(echo "${name_base_container}" | cut -d'|' -f1 | tr -d ' ')
        base=$(echo "${name_base_container}" | cut -d'|' -f2 | tr -d ' ')
@@ -342,6 +350,150 @@ readme:
  rules:
    - if: $CI_LOCAL

# =============================================================================
# Stage: test (templates)

.unit-tests-rules:
  rules:
    - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
      changes:
        - 'CHANGELOG.md'
        - 'requirements/**/*'
        - 'src/**/*'
    - if: $CI_COMMIT_TAG || $CI_PIPELINE_SOURCE == "trigger" || $CI_PIPELINE_SOURCE == "web" || $CI_COMMIT_REF_NAME != $CI_DEFAULT_BRANCH
      when: on_success

.unit-tests-template:
  stage: test
  timeout: 10m
  variables:
    DEBUG_UPDATES_DISABLE: 'true'
    FORCE_COLOR: 1
    PYTHONUNBUFFERED: 1
    TEMP: .tmp
    TERM: ansi
  before_script:
    - mkdir -p ./.tmp/
    - |
      if type pipx >/dev/null 2>&1; then
        pipx uninstall $(ls -1 ./dist/*.whl | cut -d'/' -f3 | cut -d'-' -f1) || true
        pipx install --force ./dist/*.whl
      else
        pip3 install ./dist/*.whl
        pip3 install --force-reinstall --no-deps --upgrade ./dist/*.whl
        pip3 show gitlab-projects-settings
      fi
  script:
    # Prepare tests
    - export SUITE=$(echo "${SUITE}" | sed 's/,/\\|/g')
    - if [ ! -z "${SUITE}" ]; then ls -1d ./tests/* | grep -q "\/\(${SUITE}\)" || (set +x; echo ''; echo -e '\033[1;31m[ERROR] The requested SUITE filter does not match any suite\033[0m'; echo ''; exit 1); fi
    # Run tests
    - for path in $(ls -1d ./tests/* | grep "\/\(${SUITE}\)"); do (cd "${path}/" && time sh ./test.sh 2>&1); done
    # Finish tests
    - |
      (
        set +x
        echo ''
        echo ' [INFO] Unit tests finished successfully'
        echo ''
      )

.coverage-template:
  extends:
    - .unit-tests-template
    - .unit-tests-rules
  variables:
    COVERAGE_COMMON: ${CI_PROJECT_DIR}/coverage-reports/coverage-common.xml
    COVERAGE_FOLDER: ${CI_PROJECT_DIR}/coverage-reports
    COVERAGE_RCFILE: ${CI_PROJECT_DIR}/.coveragerc
    PYTHONDONTWRITEBYTECODE: 1
    PYTHONPATH: ${CI_PROJECT_DIR}
  before_script:
    - mkdir -p "${COVERAGE_FOLDER}/"
    - chmod 777 "${COVERAGE_FOLDER}"
    - |
      if [ -z "${SUITE}" ]; then
        coverage erase
      fi
    - coverage run --append --module --source=src src --version >/dev/null
    - chmod 666 "${COVERAGE_FILE}"
    - echo 'coverage run --append --module --source=src src "${@}"' | tee /usr/local/bin/gitlab-projects-settings >/dev/null
    - echo 'coverage run --append --source=src "${@}"' | tee /usr/local/bin/python3-coverage >/dev/null
    - chmod a+x /usr/local/bin/gitlab-projects-settings /usr/local/bin/python3-coverage
  after_script:
    - coverage xml -o "${COVERAGE_XML}"
    - coverage report --show-missing --skip-covered
    - sed -i 's#\(<source>\).*\(</source>\)#\1.\2#g' "${COVERAGE_XML}"
    - |
      set +x
      if [ ! -z "${CI_LOCAL}" ]; then
        common=$(mktemp -d)
        cp "${COVERAGE_FOLDER}/.coverage-"* "${common}/"
        COVERAGE_FILE="${common}/.coverage" coverage combine "${common}/.coverage-"*
        COVERAGE_FILE="${common}/.coverage" coverage report --ignore-errors --show-missing --skip-covered
        COVERAGE_FILE="${common}/.coverage" coverage xml -o "${COVERAGE_COMMON}"
        sed -i 's#\(<source>\).*\(</source>\)#\1.\2#g' "${COVERAGE_COMMON}"
        rm -rf "${common}"
      fi
  rules:
    - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH
      changes:
        - 'CHANGELOG.md'
        - 'requirements/**/*'
        - 'src/**/*'
        - 'tests/**/*'
    - if: $CI_COMMIT_TAG || $CI_PIPELINE_SOURCE == "trigger" || $CI_PIPELINE_SOURCE == "web" || $CI_COMMIT_REF_NAME != $CI_DEFAULT_BRANCH
      when: on_success
  artifacts:
    paths:
      - coverage-reports/

# =============================================================================
# Stage: test (coverage)

coverage:linux:
  extends:
    - .coverage-template
  image: registry.gitlab.com/radiandevcore/tools/gitlab-projects-settings/gitlab-projects-settings:test-coverage-linux
  needs: []
  variables:
    COVERAGE_FILE: ${CI_PROJECT_DIR}/coverage-reports/.coverage-linux
    COVERAGE_XML: ${CI_PROJECT_DIR}/coverage-reports/coverage-linux.xml
  before_script:
    - !reference [.coverage-template, before_script]

coverage:windows:
  extends:
    - .coverage-template
  image: registry.gitlab.com/radiandevcore/tools/gitlab-projects-settings/gitlab-projects-settings:test-coverage-windows
  needs: []
  variables:
    COVERAGE_FILE: ${CI_PROJECT_DIR}/coverage-reports/.coverage-windows
    COVERAGE_XML: ${CI_PROJECT_DIR}/coverage-reports/coverage-windows.xml
  before_script:
    - !reference [.coverage-template, before_script]
  script:
    - cd ./tests/windows/ && sh ./test.sh 2>&1

# =============================================================================
# Stage: test (Python)

py3.8:
  extends:
    - .unit-tests-template
    - .unit-tests-rules
  image: registry.gitlab.com/radiandevcore/tools/gitlab-projects-settings/gitlab-projects-settings:test-oldest
  before_script:
    - !reference [.unit-tests-template, before_script]

py3.12:
  extends:
    - .unit-tests-template
    - .unit-tests-rules
  image: registry.gitlab.com/radiandevcore/tools/gitlab-projects-settings/gitlab-projects-settings:test-latest
  before_script:
    - !reference [.unit-tests-template, before_script]

# =============================================================================
# Stage: quality

@@ -350,7 +502,9 @@ sonarcloud:
  image:
    name: registry.gitlab.com/radiandevcore/tools/gitlab-projects-settings/sonarsource/sonar-scanner-cli:latest
    entrypoint: ['']
  needs: []
  needs:
    - job: coverage:linux
    - job: coverage:windows
  variables:
    GIT_DEPTH: 0
    SONAR_USER_HOME: ${CI_PROJECT_DIR}/.sonar
@@ -385,6 +539,14 @@ deploy:release:
  image: registry.gitlab.com/radiandevcore/tools/gitlab-projects-settings/gitlab-projects-settings:deploy
  needs:
    - job: build
    - job: coverage:linux
      artifacts: false
    - job: coverage:windows
      artifacts: false
    - job: py3.8
      artifacts: false
    - job: py3.12
      artifacts: false
  variables:
    TWINE_PASSWORD: ${TWINE_PASSWORD}
  before_script:
@@ -424,6 +586,14 @@ deploy:trial:
  image: registry.gitlab.com/radiandevcore/tools/gitlab-projects-settings/gitlab-projects-settings:deploy
  needs:
    - job: build
    - job: coverage:linux
      artifacts: false
    - job: coverage:windows
      artifacts: false
    - job: py3.8
      artifacts: false
    - job: py3.12
      artifacts: false
  variables:
    TWINE_PASSWORD_TEST: ${TWINE_PASSWORD_TEST}
  script:
+4 −1
Changes for containers/codestyle/Dockerfile: 4 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -8,8 +8,11 @@ RUN apk add -q \
# Install requirements
COPY --from='requirements' ./runtime.txt /tmp/
COPY --from='requirements' ./quality.txt /tmp/
COPY --from='requirements' ./tests.txt /tmp/
RUN export PIP_DISABLE_PIP_VERSION_CHECK=1 \
 && pip3 install -q -r /tmp/runtime.txt --no-cache-dir --upgrade \
 && pip3 install -q -r /tmp/quality.txt --no-cache-dir --upgrade \
 && pip3 install -q -r /tmp/tests.txt --no-cache-dir --upgrade \
 && rm -f /tmp/runtime.txt \
 && rm -f /tmp/quality.txt
 && rm -f /tmp/quality.txt \
 && rm -f /tmp/tests.txt
+19
Changes for containers/test-coverage-linux/Dockerfile: 19 added lines, 0 removed lines.
Original line number Diff line number Diff line
ARG IMAGE_BASE=python:3
FROM $IMAGE_BASE

# Install dependencies
RUN apt-get update -qq \
 && apt-get install -qq -y sudo time 2>&1 | grep 'Setting up' \
 && rm -rf /var/lib/apt/lists/*

# Install requirements
COPY --from='requirements' ./runtime.txt /tmp/
COPY --from='requirements' ./coverage.txt /tmp/
COPY --from='requirements' ./tests.txt /tmp/
RUN export PIP_DISABLE_PIP_VERSION_CHECK=1 \
 && pip3 install -q -r /tmp/runtime.txt --no-cache-dir --upgrade \
 && pip3 install -q -r /tmp/coverage.txt --no-cache-dir --upgrade \
 && pip3 install -q -r /tmp/tests.txt --no-cache-dir --upgrade \
 && rm -f /tmp/runtime.txt \
 && rm -f /tmp/coverage.txt \
 && rm -f /tmp/tests.txt
+22
Changes for containers/test-coverage-windows/Dockerfile: 22 added lines, 0 removed lines.
Original line number Diff line number Diff line
ARG IMAGE_BASE=tobix/pywine:latest
FROM $IMAGE_BASE

# Install dependencies
RUN echo 'wine pip3 "${@}"' | tee /usr/local/bin/pip3 >/dev/null \
 && chmod a+x /usr/local/bin/pip3 \
 && export PIP_DISABLE_PIP_VERSION_CHECK=1 \
 && pip3 install -q --no-cache-dir --upgrade wheel 2>/dev/null

# Install requirements
COPY --from='requirements' ./runtime.txt /tmp/
COPY --from='requirements' ./coverage.txt /tmp/
COPY --from='requirements' ./tests.txt /tmp/
RUN export PIP_DISABLE_PIP_VERSION_CHECK=1 \
 && pip3 install -q -r /tmp/runtime.txt --no-cache-dir --upgrade \
 && pip3 install -q -r /tmp/coverage.txt --no-cache-dir --upgrade \
 && pip3 install -q -r /tmp/tests.txt --no-cache-dir --upgrade \
 && rm -f /tmp/runtime.txt \
 && rm -f /tmp/coverage.txt \
 && rm -f /tmp/tests.txt \
 && echo 'wine coverage "${@}"' | tee /usr/local/bin/coverage >/dev/null \
 && chmod a+x /usr/local/bin/coverage
Loading