# This file is used to test the script with Travis CI

# Although sudo is no longer required by arduino-ci-script since the 1.0.0 release, for some reason setting "sudo: false" causes the Travis CI build time to significantly increase so this setting is left as "sudo: required"
sudo: required


env:
  global:
    # The Arduino IDE will be installed at APPLICATION_FOLDER/arduino
    - APPLICATION_FOLDER="${HOME}/arduino-ide"
    - SKETCHBOOK_FOLDER="${HOME}/arduino-sketchbook"

  matrix:
    # Test install_ide with no argument (using full version list). This would cause the Travis CI build to take much longer so I have it disabled
    # - INSTALL_IDE_START_VERSION=""
    # Test install_ide using full version list
    - INSTALL_IDE_START_VERSION="all" VERBOSITY_LEVEL=0 VERBOSE_COMPILATION="false"
    # Test install_ide using custom version list. Test the use of the special version names "oldest" and "newest" in a version list. Test use of "hourly" version name.
    - INSTALL_IDE_START_VERSION='("oldest" "1.8.1" "1.8.2" "newest" "hourly")' VERBOSITY_LEVEL=1 VERBOSE_COMPILATION="true"
    # Allowed to fail
    # Test install_ide using single version
    # test the failure behavior of install_package when a Boards Manager installation is attempted using an IDE version that doesn't support it.
    - INSTALL_IDE_START_VERSION="1.6.3" VERBOSITY_LEVEL=2 VERBOSE_COMPILATION="false"
    # Test install_ide using version range. Test the use of the special version names "oldest" and "newest" in a version range.
    - INSTALL_IDE_START_VERSION="oldest" INSTALL_IDE_END_VERSION="newest" VERBOSITY_LEVEL=0 VERBOSE_COMPILATION="false"


matrix:
  allow_failures:
    # The expected behavior is failure because 1.6.3 doesn't support boards manager installation.
    - env: INSTALL_IDE_START_VERSION="1.6.3" VERBOSITY_LEVEL=2 VERBOSE_COMPILATION="false"


addons:
  apt:
    sources:
    - debian-sid
    packages:
    - shellcheck


before_install:
  # Check for issues in the script
  - bash -c 'shopt -s globstar; shellcheck arduino-ci-script.sh'

  # Check for trailing whitespace in all files in the repository
  - "if grep --line-number --recursive --exclude-dir=.git '[[:blank:]]$' .; then echo 'Trailing whitespace found.'; false; fi"
  # Check for tabs in all files in the repository
  - "if grep --line-number --recursive --exclude-dir=.git $'\t' .; then echo 'Tab found.'; false; fi"
  # Check if all files in the repository end in a newline (https://stackoverflow.com/a/25686825)
  - find . -path ./.git -prune -o -type f -print0 | xargs -0 -L1 bash -c 'if test "$(tail --bytes=1 "$0")"; then echo "No new line at end of $0"; false; fi'

  - source "${TRAVIS_BUILD_DIR}/arduino-ci-script.sh"

  - set_script_verbosity "$VERBOSITY_LEVEL"

  - set_application_folder "$APPLICATION_FOLDER"
  - set_sketchbook_folder "$SKETCHBOOK_FOLDER"

  # Check for board definition errors that don't affect compilation
  - set_board_testing "true"

  # Check for library issues that don't affect compilation
  - set_library_testing "true"

  - install_ide "$INSTALL_IDE_START_VERSION" "$INSTALL_IDE_END_VERSION"

  # Install hardware packages
  # Test package install from this repository (can't do this because the repository isn't a hardware package)
  # - install_package
  # Test manual package install from compressed file download
  - install_package "https://github.com/SpenceKonde/ATTinyCore/archive/master.zip"
  # Test manual package install from Git repository clone
  - install_package "https://github.com/MCUdude/MightyCore.git"
  # Test manual package install from Git repository clone
  - install_package "https://github.com/JChristensen/mighty-1284p.git" "v1.6.3"

  # Test library installation from repository (can't do this because there is no library in this repository)
  # - install_library
  # Test library install from .zip file. A non-GitHub library download must be used because GitHub appends -{branch name} or -{release version} to the .zip downloads and having a library folder installed whos name contains "-" causes arduino 1.5.6 or older to hang.
  - install_library "https://bitbucket.org/teckel12/arduino-new-ping/downloads/NewPing_v1.8.zip"
  # Test library install from .zip file with rename. If the rename doesn't work then any job verifying with Arduino IDE 1.5.6 or older will hang because GitHub changes the folder name to MPU9250-master, which is not a valid folder name on those IDE versions.
  - install_library "https://github.com/brianc118/MPU9250/archive/master.zip" "MPU9250"
  # Test library install from .zip file/folder that has a dot in the name
  - install_library "https://github.com/arduino-libraries/CapacitiveSensor/archive/0.5.1.zip"
  # Test library install from git repo
  - install_library "https://github.com/sfrwmaker/WirelessOregonV2.git"
  # Test library install from git repo with branch
  - install_library "https://github.com/sde1000/NanodeUNIO.git" "master"
  # Test library install from git repo with rename
  - install_library "https://github.com/mikaelpatel/Arduino-Shell.git" "" "ArduinoShell"
  # Test library install from git repo with branch and rename
  - install_library "https://github.com/Avamander/max7456.git" "master" "max_7456"
  # Test set_verbose_output_during_compilation.
  - set_verbose_output_during_compilation "$VERBOSE_COMPILATION"

  # Boards Manager and Library Manager tests are done as late as possible to allow job 3 to do more thorough testing of script verbosity level 2 before the job fails

  # Test Boards Manager package install without URL. Test error handling of attempting to do a Boards Manager installation when the newest installed IDE version doesn't support it (should print a helpful error message and fail instead of hanging).
  - install_package "arduino:sam"
  # Test Boards Manager package install with URL
  - install_package "MiniCore:avr" "https://mcudude.github.io/MiniCore/package_MCUdude_MiniCore_index.json"

  # Test library install from Library Manager
  - install_library "Pushetta:1.0.1"


script:
  # Verify sketches:
  # build_sketch arguments: sketch name, fqbn, IDE version, allow failure
  # IDE version: Use "all" for IDE version argument to verify sketch with all versions of the Arduino IDE, use "newest" for IDE version argument to verify sketch with the newest version of the Arduino IDE

  # Installed package tests:
  # Test board from hardware package installed via Boards Manager without URL
  # Test build_sketch with "newest" special version name
  - build_sketch "${APPLICATION_FOLDER}/arduino/examples/01.Basics/BareMinimum/BareMinimum.ino" "arduino:sam:arduino_due_x_dbg" "false" "newest"
  # Test board from hardware package installed with Boards Manager URL
  # Test build_sketch with specific IDE version
  - build_sketch "${APPLICATION_FOLDER}/arduino/examples/01.Basics/BareMinimum/BareMinimum.ino" "MiniCore:avr:328:variant=modelP,BOD=2v7,LTO=Os,clock=16MHz_external" "false" "1.8.1"
  # Test board from hardware package manually installed from compressed file download
  # Test build_sketch with an IDE version list
  - build_sketch "${APPLICATION_FOLDER}/arduino/examples/01.Basics/BareMinimum/BareMinimum.ino" "ATTinyCore-master:avr:attinyx5:LTO=disable,TimerClockSource=default,chip=85,clock=8internal,bod=disable" "false" '("1.8.1" "1.8.2")'
  # Test board from hardware package manually installed by cloning Git repository
  # Test build_sketch with an IDE version range
  - build_sketch "${APPLICATION_FOLDER}/arduino/examples/01.Basics/BareMinimum/BareMinimum.ino" "MightyCore:avr:1284:pinout=standard,variant=modelP,BOD=2v7,LTO=Os,clock=16MHz_external" "false" "1.8.1" "1.8.2"
  # Test board from hardware package manually installed by cloning Git repository with non-default branch
  - build_sketch "${APPLICATION_FOLDER}/arduino/examples/01.Basics/BareMinimum/BareMinimum.ino" "mighty-1284p:avr:avr_developers" "false" "newest"

  # Installed library tests:
  # Test build_sketch without absolute path
  # Test library installed from .zip with rename
  - cd "${SKETCHBOOK_FOLDER}/libraries/MPU9250/examples/MPU9250/"
  - build_sketch "MPU9250.ino" "arduino:avr:uno" "false" "newest"
  # Test library installed from .zip with dot in the folder name
  - build_sketch "${SKETCHBOOK_FOLDER}/libraries/CapacitiveSensor-0.5.1/examples/CapacitiveSensorSketch/CapacitiveSensorSketch.pde" "arduino:avr:uno" "false" "newest"
  # Test library installed from .zip
  # Test build_sketch with "all" IDE version name
  - build_sketch "${SKETCHBOOK_FOLDER}/libraries/NewPing/examples/NewPingExample/NewPingExample.pde" "arduino:avr:uno" "false" "all"
  # Test library installed from .git
  - build_sketch "${SKETCHBOOK_FOLDER}/libraries/WirelessOregonV2/examples/OregonReceiver/OregonReceiver.ino" "arduino:avr:uno" "false" "newest"
  # Test library installed from .git with branch
  - build_sketch "${SKETCHBOOK_FOLDER}/libraries/NanodeUNIO/examples/NanodeUNIO_test/NanodeUNIO_test.pde" "arduino:avr:uno" "false" "newest"
  # Test library installed from .git with rename
  - build_sketch "${SKETCHBOOK_FOLDER}/libraries/ArduinoShell/examples/ShellBlink/ShellBlink.ino" "arduino:avr:uno" "false" "newest"
  # Test library installed from .git with branch and rename
  - build_sketch "${SKETCHBOOK_FOLDER}/libraries/max_7456/examples/HelloWorld/HelloWorld.ino" "arduino:avr:uno" "false" "newest"
  # Test library installed from Library Manager
  - build_sketch "${SKETCHBOOK_FOLDER}/libraries/Pushetta/examples/simple_notification/simple_notification.ino" "arduino:avr:uno" "false" "newest"

  # build_sketch with version argument tests
  # Test build_sketch with no IDE version argument (should use all installed IDE versions)
  - build_sketch "${APPLICATION_FOLDER}/arduino/examples/01.Basics/BareMinimum/BareMinimum.ino" "arduino:avr:uno" "false"
  # Test build_sketch with "oldest" IDE version name
  - build_sketch "${APPLICATION_FOLDER}/arduino/examples/01.Basics/BareMinimum/BareMinimum.ino" "arduino:avr:uno" "false" "oldest"
  # Test build_sketch allowed to fail (this will fail because WirelessOregonV2 is AVR specific)
  - build_sketch "${SKETCHBOOK_FOLDER}/libraries/WirelessOregonV2/examples/OregonReceiver/OregonReceiver.ino" "arduino:sam:arduino_due_x_dbg" "true" "newest"

  # build_sketch with folder argument tests:
  # Test build_sketch with folder argument with specific IDE version
  - build_sketch "${APPLICATION_FOLDER}/arduino/examples/01.Basics" "arduino:avr:uno" "false" "1.8.1"
  # Test build_sketch with folder argument with "oldest" IDE version name
  - build_sketch "${APPLICATION_FOLDER}/arduino/examples/01.Basics" "arduino:avr:uno" "false" "oldest"
  # Test build_sketch with folder argument with "newest" IDE version name
  - build_sketch "${APPLICATION_FOLDER}/arduino/examples/01.Basics" "arduino:avr:uno" "false" "newest"
  # Test build_sketch with folder argument with "all" IDE version name
  - build_sketch "${APPLICATION_FOLDER}/arduino/examples/01.Basics" "arduino:avr:uno" "false" "all"
  # Test build_sketch with folder argument with no IDE version specified (should use all installed IDE versions)
  - build_sketch "${APPLICATION_FOLDER}/arduino/examples/01.Basics" "arduino:avr:uno" "false"
  # Test build_sketch with folder argument with an IDE version list
  - build_sketch "${APPLICATION_FOLDER}/arduino/examples/01.Basics" "arduino:avr:uno" "false" '("1.8.1" "1.8.2")'
  # Test build_sketch with folder argument with an IDE version range
  - build_sketch "${APPLICATION_FOLDER}/arduino/examples/01.Basics" "arduino:avr:uno" "false" "1.8.1" "1.8.2"
  # Test build_sketch with folder argument required to fail (this will fail because WirelessOregonV2 is AVR specific)
  - build_sketch "${SKETCHBOOK_FOLDER}/libraries/WirelessOregonV2/examples" "arduino:sam:arduino_due_x_dbg" "require" "newest"

  - publish_report_to_gist "$REPORT_GITHUB_TOKEN" "$REPORT_GIST_URL" "true"

  - USER_NAME="$(echo "$TRAVIS_REPO_SLUG" | cut -d'/' -f 1)"
  - REPOSITORY_NAME="$(echo "$TRAVIS_REPO_SLUG" | cut -d'/' -f 2)"
  - publish_report_to_repository "$REPORT_GITHUB_TOKEN" "https://github.com/${USER_NAME}/CI-reports.git" "$REPOSITORY_NAME" "build_$(printf "%05d\n" "${TRAVIS_BUILD_NUMBER}")" "true"

  - display_report


notifications:
  email:
    on_success: always
    on_failure: always
