stages:
  - build

variables:
  TEST_PREFIX_PATH_FILE: 'test_prefix_$CI_JOB_ID.txt'
  CONDA_ENV_NAME: beat_test_env
  CONDA_PREFIX: /local/opt/conda
  PREFIX: $CONDA_PREFIX/envs/$CONDA_ENV_NAME

build:
  stage: build
  except:
    - /^v\d+\.\d+\.\d+([abc]\d*)?$/  # PEP-440 compliant version (tags)
  before_script:
  - ${PREFIX}/bin/python --version
  - docker info
  - git clean -ffdx
  - export BEAT_TEST_PREFIX=`mktemp -d --tmpdir=/var/tmp beat_test_prefix.XXXXXXXXX`
  - echo $BEAT_TEST_PREFIX > $TEST_PREFIX_PATH_FILE
  - export PATH="$CONDA_PREFIX/bin:$PATH"
  - source activate $CONDA_ENV_NAME
  - conda info --envs
  script:
  - ${PREFIX}/bin/python bootstrap-buildout.py
  - ./bin/buildout
  - export COVERAGE_FILE=.coverage.django
  - ./bin/python ${PREFIX}/bin/coverage run --source=${CI_PROJECT_NAME} ./bin/django test --settings=beat.web.settings.ci -v 2
  - export BEAT_CMDLINE_TEST_PLATFORM=django://beat.web.settings.ci
  - export COVERAGE_FILE=.coverage.cmdline
  - export NOSE_WITH_COVERAGE=1
  - export NOSE_COVER_PACKAGE=beat.web
  - ./bin/python ${PREFIX}/bin/coverage run --source=./src/beat.cmdline ./bin/nosetests -sv beat.cmdline
  - unset COVERAGE_FILE
  - ./bin/python ${PREFIX}/bin/coverage combine .coverage.django .coverage.cmdline
  - ./bin/python ${PREFIX}/bin/coverage report
  - ./bin/python ${PREFIX}/bin/sphinx-apidoc --separate -d 2 --output=doc/api ${CI_PROJECT_NAMESPACE} beat/web/*/migrations beat/web/*/tests
  - ./bin/python ${PREFIX}/bin/sphinx-build doc/api html/api
  - ./bin/python ${PREFIX}/bin/sphinx-build doc/admin html/admin
  - ./bin/python ${PREFIX}/bin/sphinx-build doc/user html/user
  after_script:
  - export BEAT_TEST_PREFIX=`cat $TEST_PREFIX_PATH_FILE`
  - echo "Deleting BEAT test prefix $BEAT_TEST_PREFIX"
  - rm -rf $BEAT_TEST_PREFIX $TEST_PREFIX_PATH_FILE
  - unset BEAT_TEST_PREFIX
  tags:
  - docker-build
