# Gitlab CI config
#
# Note: When updating, please also update .travis.yml
stages:
  - test

# cache the pip download directory in all jobs
variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/pip-cache"

cache:
  paths:
    - "$CI_PROJECT_DIR/pip-cache"

.test_template: &test_template
  stage: test
  image: $CI_DOCKER_REGISTRY/esp32-ci-env$BOT_DOCKER_IMAGE_TAG
  tags:
    - host_test
  dependencies: []

host_tests:
  <<: *test_template
  script:
    - ./test/ci/multirun_with_pyenv.sh python setup.py build
    - ./test/ci/multirun_with_pyenv.sh python test/test_imagegen.py
    - ./test/ci/multirun_with_pyenv.sh python test/test_espsecure.py
    - ./test/ci/multirun_with_pyenv.sh python test/test_espefuse_host.py esp32
    - ./test/ci/multirun_with_pyenv.sh python test/test_espefuse_host.py esp32s2
    - ./test/ci/multirun_with_pyenv.sh python test/test_espefuse_host.py esp32s3beta2
    - ./test/ci/multirun_with_pyenv.sh python test/test_espefuse_host.py esp32c3

check_python_style:
  <<: *test_template
  script:
    # this step installs any 'dev' dependencies (ie flake8) that might be missing in the CI image.
    # The runner should cache the downloads, so still quite fast.
    - ./test/ci/multirun_with_pyenv.sh pip install --user -e .[dev]
    - ./test/ci/multirun_with_pyenv.sh python -m flake8

# Check all the scripts can run when installed under all Python versions
check_installation_can_run:
  <<: *test_template
  script:
    - ./test/ci/multirun_with_pyenv.sh python setup.py install
    - cd /
    - ${CI_PROJECT_DIR}/test/ci/multirun_with_pyenv.sh esptool.py --help
    - ${CI_PROJECT_DIR}/test/ci/multirun_with_pyenv.sh espefuse.py --help
    - ${CI_PROJECT_DIR}/test/ci/multirun_with_pyenv.sh espsecure.py --help

# Check the stub can build and that a stub built in CI has the same contents
# as the one embedded in esptool
check_stub_build:
  <<: *test_template
  artifacts:
    when: on_failure
    paths:
      - flasher_stub/build/
    expire_in: 1 week
  variables:
    # using this directory as the CI image probably already has an ESP32, ESP32S2, ESP32S3 and ESP32C3 toolchain downloaded into here
    TOOLCHAIN_DIR: "/root/.espressif/dist"

    ESP8266_BINDIR: "${TOOLCHAIN_DIR}/xtensa-lx106-elf/bin"
    ESP32_BINDIR: "${TOOLCHAIN_DIR}/xtensa-esp32-elf/bin"
    ESP32S2_BINDIR: "${TOOLCHAIN_DIR}/xtensa-esp32s2-elf/bin"
    ESP32S3_BINDIR: "${TOOLCHAIN_DIR}/xtensa-esp32s3-elf/bin"
    ESP32C3_BINDIR: "${TOOLCHAIN_DIR}/riscv32-esp-elf/bin"

    TOOLCHAIN_PATHS: "${ESP8266_BINDIR}:${ESP32_BINDIR}:${ESP32S2_BINDIR}:${ESP32S3_BINDIR}:${ESP32C3_BINDIR}"
  script:
    - ./test/ci/setup_ci_build_env.sh
    - make -C flasher_stub V=1 PATH="${TOOLCHAIN_PATHS}:${PATH}"
    - cd flasher_stub && python ./compare_stubs.py

target_esptool_test:
  timeout: 2 hours 30 minutes
  stage: test
  image: $CI_DOCKER_REGISTRY/rpi-test-env$BOT_DOCKER_IMAGE_TAG
  tags:
    - esptool_target
    - internet
  script:
    - ${CI_PROJECT_DIR}/test/ci/multirun_with_pyenv.sh python -m pip install -e .
    - ./test/ci/run_test_on_chips_parallel.sh
  artifacts:
    reports:
      junit: test/report.xml
    when: always
    paths:
      - test/*.out
    expire_in: 1 week
  after_script:
    - tail -n +1 test/*.out
