trigger:
  branches:
    exclude: [ l10n_master ]

stages:
- stage: lint
  jobs:
  - job: docs
    pool:
      vmImage: ubuntu-latest
    steps:
    - template: templates/install-dependencies.yml
      parameters:
        platform: ubuntu-latest
        useCache: ${{ eq('$(USE_CACHE)', 'true') }}

    - script: |
        sudo apt-get install -y pandoc
      displayName: Install pandoc

    - script: |
        cd docs
        # rstcheck -r source
      displayName: rstcheck (disabled for now)  # TODO: rstcheck

    - script: |
        cd docs
        make -e SPHINXOPTS="-W --keep-going -T -q" html
      displayName: Sphinx sanity check

    - script: |
        set -e
        cd docs
        rm -rf build
        sudo apt-get install -y gettext
        python tools/translation_patch.py
        make -e SPHINXOPTS="-W --keep-going -T -q -D language='zh'" html
      displayName: Sphinx sanity check (Chinese)

    - script: |
        cd docs
        python tools/chineselink.py check
      displayName: Translation up-to-date

  - job: python
    pool:
      vmImage: ubuntu-latest
    steps:
    - template: templates/install-dependencies.yml
      parameters:
        platform: ubuntu-latest
        useCache: ${{ eq('$(USE_CACHE)', 'true') }}

    - script: |
        # pylint requires newer typing extension. Override requirements in tensorflow
        python -m pip install "typing-extensions>=3.10"
      displayName: Resolve dependency version

    - script: |
        python -m pylint --rcfile pylintrc nni
      displayName: pylint

    - script: |
        set -e
        python -m flake8 nni --count --select=E9,F63,F72,F82 --show-source --statistics
        EXCLUDES=examples/trials/mnist-nas/*/mnist*.py,examples/trials/nas_cifar10/src/cifar10/general_child.py
        python -m flake8 examples --count --exclude=$EXCLUDES --select=E9,F63,F72,F82 --show-source --statistics
      displayName: flake8

    - script: |
        python -m pyright nni
      displayName: Type Check

  - job: typescript
    pool:
      vmImage: ubuntu-latest
    steps:
    - template: templates/install-dependencies.yml
      parameters:
        platform: ubuntu-latest
        useCache: ${{ eq('$(USE_CACHE)', 'true') }}

    - script: |
        set -e
        yarn --cwd ts/nni_manager
        yarn --cwd ts/nni_manager eslint
      displayName: ESLint (NNI Manager)

    - script: |
        set -e
        yarn --cwd ts/webui
        yarn --cwd ts/webui sanity-check
      displayName: Sanity check (WebUI)


  # To reduce debug cost, steps are sorted differently on each platform,
  # so that a bug in any module will cause at least one platform to fail quickly.
- stage: test
  jobs:
  - job: ubuntu_latest
    pool:
      vmImage: ubuntu-latest

    steps:
    - template: templates/install-dependencies.yml
      parameters:
        platform: ubuntu-latest
        useCache: ${{ eq('$(USE_CACHE)', 'true') }}

    - script: |
        python setup.py develop
        echo "##vso[task.setvariable variable=PATH]${HOME}/.local/bin:${PATH}"
      displayName: Install NNI

    - script: |
        set -e
        mkdir -p coverage
        cd test
        python -m pytest ut --cov-config=.coveragerc \
          --ignore=ut/compression/v1/test_pruners.py \
          --ignore=ut/compression/v1/test_compressor_tf.py \
          --ignore=ut/compression/v1/test_compressor_torch.py \
          --ignore=ut/compression/v1/test_model_speedup.py
        python -m pytest ut/compression/v1/test_pruners.py --cov-config=.coveragerc --cov-append
        python -m pytest ut/compression/v1/test_compressor_tf.py --cov-config=.coveragerc --cov-append
        python -m pytest ut/compression/v1/test_compressor_torch.py --cov-config=.coveragerc --cov-append
        python -m pytest ut/compression/v1/test_model_speedup.py --cov-config=.coveragerc --cov-append
        cp coverage.xml ../coverage/python.xml
      displayName: Python unit test

    - script: |
        yarn --cwd ts/nni_manager test
        cp ts/nni_manager/coverage/cobertura-coverage.xml coverage/typescript.xml
      displayName: TypeScript unit test

    - task: PublishTestResults@2
      condition: succeededOrFailed()
      inputs:
        testResultsFiles: '$(System.DefaultWorkingDirectory)/**/test-*.xml'
        testRunTitle: 'Publish test results for Python $(python.version)'
      displayName: Publish test results

    - task: PublishCodeCoverageResults@1
      inputs:
        codeCoverageTool: Cobertura
        summaryFileLocation: coverage/*
      displayName: Publish code coverage results

    - script: |
        cd test
        python nni_test/nnitest/run_tests.py --config config/pr_tests.yml
      displayName: Simple integration test

  - job: ubuntu_legacy
    pool:
      vmImage: ubuntu-18.04

    steps:
    - template: templates/install-dependencies.yml
      parameters:
        platform: ubuntu-legacy
        useCache: ${{ eq('$(USE_CACHE)', 'true') }}

    - script: |
        python setup.py develop
        echo "##vso[task.setvariable variable=PATH]${HOME}/.local/bin:${PATH}"
      displayName: Install NNI

    - script: |
        cd test
        python nni_test/nnitest/run_tests.py --config config/pr_tests.yml
      displayName: Simple integration test

    - script: |
        cd test
        python -m pytest ut --ignore=ut/retiarii/test_convert_basic.py \
          --ignore=ut/retiarii/test_convert_operators.py \
          --ignore=ut/retiarii/test_convert_pytorch.py
      displayName: Python unit test

    - script: |
        yarn --cwd ts/nni_manager test
      displayName: TypeScript unit test

  - job: windows
    pool:
      vmImage: windows-latest
    timeoutInMinutes: 75

    steps:
    - template: templates/install-dependencies.yml
      parameters:
        platform: windows
        useCache: ${{ eq('$(USE_CACHE)', 'true') }}

    - script: |
        python setup.py develop --no-user
      displayName: Install NNI

    - script: |
        cd test
        python -m pytest ut
      displayName: Python unit test

    - script: |
        yarn --cwd ts/nni_manager test
      displayName: TypeScript unit test

    - script: |
        cd test
        python nni_test/nnitest/run_tests.py --config config/pr_tests.yml
      displayName: Simple integration test

  - job: macos
    pool:
      vmImage: macOS-latest

    steps:
    - template: templates/install-dependencies.yml
      parameters:
        platform: macos
        useCache: ${{ eq('$(USE_CACHE)', 'true') }}

    - script: |
        python setup.py develop
        echo "##vso[task.setvariable variable=PATH]${HOME}/.local/bin:${PATH}"
      displayName: Install NNI

    - script: |
        CI=true yarn --cwd ts/nni_manager test --exclude test/core/nnimanager.test.ts
      displayName: TypeScript unit test

    - script: |
        cd test
        python -m pytest ut
      displayName: Python unit test

    - script: |
        cd test
        python nni_test/nnitest/run_tests.py --config config/pr_tests.yml
      displayName: Simple integration test
