# Python Django
# Test a Django project on multiple versions of Python.
# Add steps that analyze code, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/languages/python

trigger:
- master

pool:
  vmImage: 'Ubuntu-16.04'
strategy:
  matrix:
    Python36:
      PYTHON_VERSION: '3.6'
    Python37:
      PYTHON_VERSION: '3.7'
  maxParallel: 3

steps:
- task: UsePythonVersion@0
  inputs:
    versionSpec: '$(PYTHON_VERSION)'
    architecture: 'x64'

- task: PythonScript@0
  displayName: 'Export project path'
  inputs:
    scriptSource: 'inline'
    script: |
      """Search all subdirectories for `manage.py`."""
      from glob import iglob
      from os import path
      # Python >= 3.5
      manage_py = next(iglob(path.join('**', 'manage.py'), recursive=True), None)
      if not manage_py:
          raise SystemExit('Could not find a Django project')
      project_location = path.dirname(path.abspath(manage_py))
      print('Found Django project in', project_location)
      print('##vso[task.setvariable variable=projectRoot]{}'.format(project_location))

- script: |
    python -m pip install --upgrade pip setuptools wheel pipenv
    pipenv install --system
    pip install unittest-xml-reporting
  displayName: 'Install prerequisites'

- script: |
    python -m pip install flake8
    flake8 --max-line-length 120 --exclude rarfile.py,migrations
  displayName: 'Run lint tests'

- script: |
    pushd '$(projectRoot)'
    python manage.py test --testrunner xmlrunner.extra.djangotestrunner.XMLTestRunner --no-input
  condition: succeededOrFailed()
  displayName: 'Run tests'

- script: |
    pip install coverage
    coverage run --source='.' --omit=*migrations*,comic\rarfile.py  manage.py test
    coverage xml
    coverage html
  displayName: 'Run Coverage'


- task: PublishTestResults@2
  inputs:
    testResultsFiles: "**/TEST-*.xml"
    testRunTitle: 'Python $(PYTHON_VERSION)'

- task: PublishCodeCoverageResults@1
  inputs:
    codeCoverageTool: Cobertura
    summaryFileLocation: '$(System.DefaultWorkingDirectory)/**/coverage.xml'
    reportDirectory: '$(System.DefaultWorkingDirectory)/**/htmlcov'

- script: docker build -t cbwebreaderacr.azurecr.io/cbwebreader .  # add options to this command to meet your needs

- script: |
    docker build -t cbwebreaderacr.azurecr.io/cbwebreader .
    docker login -u cbwebreaderacr -p 1/os6ps5UYEL5I9ZCOZaoKbqcVhNfBqw cbwebreaderacr.azurecr.io
    docker push cbwebreaderacr.azurecr.io/cbwebreader