---
# Cirrus CI configuration - FreeBSD testing only
# Other platforms and tests are now covered by GitHub Actions

env:
  LC_ALL: C.UTF-8
  LANG: C.UTF-8
  PIP_CACHE_DIR: ${CIRRUS_WORKING_DIR}/.cache/pip
  PRE_COMMIT_HOME: ${CIRRUS_WORKING_DIR}/.cache/pre-commit
  CIRRUS_ARTIFACT_URL: https://api.cirrus-ci.com/v1/artifact/build/${CIRRUS_BUILD_ID}
  # Coveralls configuration
  CI_NAME: cirrus-ci
  CI_BRANCH: ${CIRRUS_BRANCH}
  CI_PULL_REQUEST: ${CIRRUS_PR}
  CI_BUILD_NUMBER: ${CIRRUS_BUILD_ID}
  CI_BUILD_URL: https://cirrus-ci.com/build/${CIRRUS_BUILD_ID}
  COVERALLS_PARALLEL: "true"
  COVERALLS_FLAG_NAME: ${CIRRUS_TASK_NAME}
  # Project-specific
  VALIDATE_PYPROJECT_CACHE_REMOTE: tests/.cache

# ---- Templates ----

.task_template: &task-template
  debug_information_script:
    - echo "$(which python) -- $(python -VV)"
    - echo "$(which pip) -- $(pip -VV)"
    - python -c 'import os, sys; print(os.name, sys.platform, getattr(sys, "abiflags", None))'
  prepare_script:  # avoid git failing with setuptools-scm
    - git config --global user.email "you@example.com"
    - git config --global user.name "Your Name"
  pip_cache:
    folder: "${CIRRUS_WORKING_DIR}/.cache/pip"
    fingerprint_script: echo "${CIRRUS_OS}-${CIRRUS_TASK_NAME}"
    reupload_on_changes: true

.test_template: &test-template
  # Requires pip, tox, and pipx to be installed via OS/pip
  alias: test
  depends_on: [build]
  <<: *task-template
  test_files_cache:
    folder: ${VALIDATE_PYPROJECT_CACHE_REMOTE}
    fingerprint_script: echo $CIRRUS_BUILD_ID
    populate_script: python tools/cache_urls_for_tests.py
    reupload_on_changes: true
  download_artifact_script: &download-artifact
    - curl -L -O ${CIRRUS_ARTIFACT_URL}/build/upload/dist.tar.gz
    - tar xzf dist.tar.gz
    - rm dist.tar.gz
  test_script: >
    tox --installpkg dist/*.whl --
    -n 5 --randomly-seed=42 -rfEx --durations 10 --color yes
  submit_coverage_script:
    - pipx run coverage xml -o coverage.xml
    - pipx run 'coveralls<4' --submit coverage.xml
      # ^-- https://github.com/TheKevJames/coveralls-python/issues/434

# Deep clone script for POSIX environments (required for setuptools-scm)
.clone_script: &clone |
  if [ -z "$CIRRUS_PR" ]; then
    git clone --recursive --branch=$CIRRUS_BRANCH https://x-access-token:${CIRRUS_REPO_CLONE_TOKEN}@github.com/${CIRRUS_REPO_FULL_NAME}.git $CIRRUS_WORKING_DIR
    git reset --hard $CIRRUS_CHANGE_IN_REPO
  else
    git clone --recursive https://x-access-token:${CIRRUS_REPO_CLONE_TOKEN}@github.com/${CIRRUS_REPO_FULL_NAME}.git $CIRRUS_WORKING_DIR
    git fetch origin pull/$CIRRUS_PR/head:pull/$CIRRUS_PR
    git reset --hard $CIRRUS_CHANGE_IN_REPO
  fi

# ---- CI Pipeline ----

build_task:
  name: build and check (Linux - 3.12)
  alias: build
  container: {image: "python:3.12-trixie"}
  clone_script: *clone
  <<: *task-template
  install_script: pip install tox tox-uv
  build_script:
    - tox -e clean,lint,typecheck,build
    - tar czf dist.tar.gz dist
  upload_artifacts:
    path: dist.tar.gz

freebsd_task:
  name: test (freebsd - 3.11)
  freebsd_instance: {image_family: freebsd-14-3}
  install_script:
    - pkg remove -y python lang/python
    - pkg install -y git python311 py311-pip py311-gdbm py311-sqlite3 py311-tox py311-tomli py311-pipx
    - ln -s /usr/local/bin/python3.11 /usr/local/bin/python
  <<: *test-template
  depends_on: [build]

finalize_task:
  container: {image: "python:3.12-trixie"}
  depends_on: [test]
  <<: *task-template
  install_script: pip install 'coveralls<4'
    # ^-- https://github.com/TheKevJames/coveralls-python/issues/434
  finalize_coverage_script: coveralls --finish
