# Python CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-python/ for more details
#
version: 2
jobs:
  python3.8: &DEFAULT
    docker:
      - image: circleci/python:3.8

    environment:
      TOX_ENV: py38
      RUN_CHECK: 1

    working_directory: ~/repo

    steps:
      - checkout

      # Download and cache dependencies
#      - restore_cache:
#          keys:
#          - v1-dependencies-{{ .Environment.TOX_ENV }}-{{ checksum "requirements_dev.txt" }}

      - run:
          name: install dependencies
          command: |
            # pip install -U pip virtualenv --user
            if ! which virtualenv; then
              pip install 'virtualenv<=20.0.21' --user
            fi
            export PATH="~/.local/bin:$PATH"

            virtualenv venv
            . venv/bin/activate

            pip install codecov
            pip install tox

            if [[ $RUN_CHECK == 1 ]]; then
              pip install -U -r requirements_dev.txt
            fi

            if [[ $(python -c "import sys; print(sys.stdin.encoding)" |grep None) ]]; then
              export PYTHONIOENCODING=utf-8
            fi
#
#      - save_cache:
#          paths:
#            - ./venv
#          key: v1-dependencies-{{ .Environment.TOX_ENV }}-{{ checksum "requirements_dev.txt" }}

      - run:
          name: run tests
          command: |
            . venv/bin/activate

            if [[ $RUN_CHECK == 1 ]]; then
              pre-commit run --all-files
              mypy pypinyin
            fi


            tox -e $TOX_ENV

            python setup.py install
            pypinyin hello
            echo hello | pypinyin
            pypinyin < setup.cfg

            codecov

      - store_artifacts:
          path: test-reports
          destination: test-reports

  python3.9:
    <<: *DEFAULT
    docker:
      - image: circleci/python:3.9
    environment:
      TOX_ENV: py39

  python3.7:
    <<: *DEFAULT
    docker:
      - image: circleci/python:3.7
    environment:
      TOX_ENV: py37

  python3.6:
    <<: *DEFAULT
    docker:
      - image: circleci/python:3.6
    environment:
      TOX_ENV: py36

  python3.5:
    <<: *DEFAULT
    docker:
      - image: circleci/python:3.5
    environment:
      TOX_ENV: py35

  python3.4:
    <<: *DEFAULT
    docker:
      - image: circleci/python:3.4
    environment:
      TOX_ENV: py34

  python2.7:
    <<: *DEFAULT
    docker:
      - image: circleci/python:2.7
    environment:
      TOX_ENV: py27

  # python2.6:
  #   <<: *DEFAULT
  #   docker:
  #     - image: python:2.6
  #   environment:
  #     TOX_ENV: py33

  pypy2:
    <<: *DEFAULT
    docker:
      - image: pypy:2
    environment:
      TOX_ENV: pypy

  pypy3:
    <<: *DEFAULT
    docker:
      - image: pypy:3
    environment:
      TOX_ENV: pypy3


workflows:
  version: 2
  testing:
    jobs:
      - python3.9
      - python3.8
      - python3.7
      - python3.6
      - python3.5
      - python3.4
      - python2.7
      - pypy2
      - pypy3
