image: "python:3.7"

variables:
    # GestioCOF settings
    DBHOST: "postgres"
    REDIS_HOST: "redis"
    REDIS_PASSWD: "dummy"

    # Cached packages
    PIP_CACHE_DIR: "$CI_PROJECT_DIR/vendor/pip"

    # postgres service configuration
    POSTGRES_PASSWORD: "4KZt3nGPLVeWSvtBZPSM3fSzXpzEU4"
    POSTGRES_USER: "cof_gestion"
    POSTGRES_DB: "cof_gestion"

    # psql password authentication
    PGPASSWORD: $POSTGRES_PASSWORD

.test_template:
    before_script:
        - mkdir -p vendor/{pip,apt}
        - apt-get update -q && apt-get -o dir::cache::archives="vendor/apt" install -yqq postgresql-client libldap2-dev libsasl2-dev
        - sed -E 's/^REDIS_HOST.*/REDIS_HOST = "redis"/' cof/settings/secret_example.py > cof/settings/secret.py
        - sed -i.bak -E 's;^REDIS_PASSWD = .*$;REDIS_PASSWD = "";' cof/settings/secret.py
        # Remove the old test database if it has not been done yet
        - psql --username=$POSTGRES_USER --host=$DBHOST -c "DROP DATABASE IF EXISTS test_$POSTGRES_DB"
        - pip install --upgrade -r requirements-prod.txt coverage tblib
        - python --version
    after_script:
        - coverage report
    services:
        - postgres:11.7
        - redis:latest
    cache:
        key: test
        paths:
            - vendor/
    # For GitLab CI to get coverage from build.
    # Keep this disabled for now, as it may kill GitLab...
    # coverage: '/TOTAL.*\s(\d+\.\d+)\%$/'

coftest:
    stage: test
    extends: .test_template
    variables:
        DJANGO_SETTINGS_MODULE: "cof.settings.cof_prod"
    script:
        - coverage run manage.py test gestioncof bda kfet petitscours shared --parallel

bdstest:
    stage: test
    extends: .test_template
    variables:
        DJANGO_SETTINGS_MODULE: "cof.settings.bds_prod"
    script:
        - coverage run manage.py test bds clubs events --parallel

linters:
    stage: test
    before_script:
        - mkdir -p vendor/pip
        - pip install --upgrade black isort flake8
    script:
        - black --check .
        - isort --check --diff .
        # Print errors only
        - flake8 --exit-zero bda bds clubs cof events gestioncof kfet petitscours provisioning shared
    cache:
        key: linters
        paths:
            - vendor/

# Check whether there are some missing migrations.
migration_checks:
    stage: test
    variables:
        DJANGO_SETTINGS_MODULE: "cof.settings.local"
    before_script:
        - mkdir -p vendor/{pip,apt}
        - apt-get update -q && apt-get -o dir::cache::archives="vendor/apt" install -yqq postgresql-client libldap2-dev libsasl2-dev
        - cp cof/settings/secret_example.py cof/settings/secret.py
        - pip install --upgrade -r requirements-devel.txt
        - python --version
    script: python manage.py makemigrations --dry-run --check
    services:
        # this should not be necessary…
        - postgres:11.7
    cache:
        key: migration_checks
        paths:
            - vendor/
