|
| 1 | +#!/usr/bin/env ruby |
| 2 | +require 'arduino_ci' |
| 3 | +require 'set' |
| 4 | + |
| 5 | +WIDTH = 80 |
| 6 | + |
| 7 | +@failure_count = 0 |
| 8 | + |
| 9 | +# terminate after printing any debug info. TODO: capture debug info |
| 10 | +def terminate |
| 11 | + puts "Failures: #{@failure_count}" |
| 12 | + retcode = @failure_count.zero? ? 0 : 1 |
| 13 | + exit(retcode) |
| 14 | +end |
| 15 | + |
| 16 | +# make a nice status line for an action and react to the action |
| 17 | +def perform_action(message, on_fail_msg, abort_on_fail) |
| 18 | + line = "#{message}..." |
| 19 | + print line |
| 20 | + result = yield |
| 21 | + mark = result ? "✓" : "X" |
| 22 | + puts mark.rjust(WIDTH - line.length, " ") |
| 23 | + unless result |
| 24 | + puts on_fail_msg unless on_fail_msg.nil? |
| 25 | + @failure_count += 1 |
| 26 | + # print out error messaging here if we've captured it |
| 27 | + terminate if abort_on_fail |
| 28 | + end |
| 29 | + result |
| 30 | +end |
| 31 | + |
| 32 | +# Make a nice status for something that defers any failure code until script exit |
| 33 | +def attempt(message, &block) |
| 34 | + perform_action(message, nil, false, &block) |
| 35 | +end |
| 36 | + |
| 37 | +# Make a nice status for something that kills the script immediately on failure |
| 38 | +def assure(message, &block) |
| 39 | + perform_action(message, "This may indicate a problem with ArduinoCI!", true, &block) |
| 40 | +end |
| 41 | + |
| 42 | +# initialize command and config |
| 43 | +config = ArduinoCI::CIConfig.default.from_project_library |
| 44 | +arduino_cmd = ArduinoCI::ArduinoInstallation.autolocate! |
| 45 | + |
| 46 | +# initialize library under test |
| 47 | +installed_library_path = assure("Installing library under test") { arduino_cmd.install_local_library(".") } |
| 48 | +library_examples = arduino_cmd.library_examples(installed_library_path) |
| 49 | + |
| 50 | +# gather up all required boards so we can install them up front. |
| 51 | +# start with the "platforms to unittest" and add the examples |
| 52 | +# while we're doing that, get the aux libraries as well |
| 53 | +all_platforms = {} |
| 54 | +aux_libraries = Set.new(config.aux_libraries_for_unittest + config.aux_libraries_for_build) |
| 55 | +config.platforms_to_unittest.each { |p| all_platforms[p] = config.platform_definition(p) } |
| 56 | +library_examples.each do |path| |
| 57 | + ovr_config = config.from_example(path) |
| 58 | + ovr_config.platforms_to_build.each { |p| all_platforms[p] = config.platform_definition(p) } |
| 59 | + aux_libraries.merge(ovr_config.aux_libraries_for_build) |
| 60 | +end |
| 61 | + |
| 62 | +# with all platform info, we can extract unique packages and their urls |
| 63 | +# do that, set the URLs, and download the packages |
| 64 | +all_packages = all_platforms.values.map { |v| v[:package] }.uniq.reject(&:nil?) |
| 65 | +all_urls = all_packages.map { |p| config.package_url(p) }.uniq.reject(&:nil?) |
| 66 | +assure("Setting board manager URLs") do |
| 67 | + arduino_cmd.set_pref("boardsmanager.additional.urls", all_urls.join(",")) |
| 68 | +end |
| 69 | + |
| 70 | +all_packages.each do |p| |
| 71 | + assure("Installing board package #{p}") do |
| 72 | + arduino_cmd.install_boards(p) |
| 73 | + end |
| 74 | +end |
| 75 | + |
| 76 | +aux_libraries.each do |l| |
| 77 | + assure("Installing aux library '#{l}'") { arduino_command.install_library(l) } |
| 78 | +end |
| 79 | + |
| 80 | +attempt("Setting compiler warning level") { arduino_cmd.set_pref("compiler.warning_level", "all") } |
| 81 | + |
| 82 | +library_examples.each do |example_path| |
| 83 | + ovr_config = config.from_example(example_path) |
| 84 | + ovr_config.platforms_to_build.each do |p| |
| 85 | + board = all_platforms[p][:board] |
| 86 | + assure("Switching to board for #{p} (#{board})") { arduino_cmd.use_board(board) } |
| 87 | + example_name = File.basename(example_path) |
| 88 | + attempt("Verifying #{example_name}") { arduino_cmd.verify_sketch(example_path) } |
| 89 | + end |
| 90 | +end |
| 91 | + |
| 92 | +config.platforms_to_test.each do |p| |
| 93 | + board = all_platforms[p][:board] |
| 94 | + assure("Switching to board for #{p} (#{board})") { arduino_cmd.use_board(board) } |
| 95 | + cpp_library.test_files.each do |unittest_path| |
| 96 | + unittest_name = File.basename(unittest_path) |
| 97 | + attempt("Unit testing #{unittest_name}") { cpp_library.test(unittest_path) } |
| 98 | + end |
| 99 | +end |
0 commit comments