|
| 1 | +require "arduino_ci/version" |
| 2 | + |
| 3 | +require 'singleton' |
| 4 | + |
| 5 | +# Cross-platform way of finding an executable in the $PATH. |
| 6 | +# via https://stackoverflow.com/a/5471032/2063546 |
| 7 | +# which('ruby') #=> /usr/bin/ruby |
| 8 | +def which(cmd) |
| 9 | + exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : [''] |
| 10 | + ENV['PATH'].split(File::PATH_SEPARATOR).each do |path| |
| 11 | + exts.each do |ext| |
| 12 | + exe = File.join(path, "#{cmd}#{ext}") |
| 13 | + return exe if File.executable?(exe) && !File.directory?(exe) |
| 14 | + end |
| 15 | + end |
| 16 | + nil |
| 17 | +end |
| 18 | + |
| 19 | +# ArduinoCI contains classes for automated testing of Arduino code on the command line |
| 20 | +# @author Ian Katz <[email protected]> |
| 21 | +module ArduinoCI |
| 22 | + |
| 23 | + # Wrap the Arduino executable. This requires, in some cases, a faked display. |
| 24 | + class ArduinoCmd |
| 25 | + |
| 26 | + # create as many ArduinoCmds as you like, but we need one and only one display manager |
| 27 | + class DisplayMgr |
| 28 | + include Singleton |
| 29 | + attr_reader :enabled |
| 30 | + |
| 31 | + def initialize |
| 32 | + @existing = existing_display? |
| 33 | + @enabled = false |
| 34 | + @pid = nil |
| 35 | + end |
| 36 | + |
| 37 | + # attempt to determine if the machine is running a graphical display (i.e. not Travis) |
| 38 | + def existing_display? |
| 39 | + return true if RUBY_PLATFORM.include? "darwin" |
| 40 | + return true if ENV["DISPLAY"].nil? |
| 41 | + return true if ENV["DISPLAY"].include? ":" |
| 42 | + false |
| 43 | + end |
| 44 | + |
| 45 | + # enable a virtual display |
| 46 | + def enable |
| 47 | + return @enabled = true if @existing # silent no-op if built in display |
| 48 | + return unless @pid.nil? |
| 49 | + |
| 50 | + @enabled = true |
| 51 | + @pid = fork do |
| 52 | + puts "Forking Xvfb" |
| 53 | + system("Xvfb", ":1", "-ac", "-screen", "0", "1280x1024x16") |
| 54 | + puts "Xvfb unexpectedly quit!" |
| 55 | + end |
| 56 | + sleep(3) # TODO: test a connection to the X server? |
| 57 | + end |
| 58 | + |
| 59 | + # disable the virtual display |
| 60 | + def disable |
| 61 | + return @enabled = false if @existing # silent no-op if built in display |
| 62 | + return if @pid.nil? |
| 63 | + |
| 64 | + begin |
| 65 | + Process.kill 9, @pid |
| 66 | + ensure |
| 67 | + Process.wait @pid |
| 68 | + @pid = nil |
| 69 | + end |
| 70 | + puts "Xvfb killed" |
| 71 | + end |
| 72 | + |
| 73 | + # Enable a virtual display for the duration of the given block |
| 74 | + def with_display |
| 75 | + enable |
| 76 | + begin |
| 77 | + yield environment |
| 78 | + ensure |
| 79 | + disable |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + def environment |
| 84 | + return nil unless @existing || @enabled |
| 85 | + return {} if @existing |
| 86 | + { DISPLAY => ":1.0" } |
| 87 | + end |
| 88 | + |
| 89 | + # On finalize, ensure child process is ended |
| 90 | + def self.finalize |
| 91 | + disable |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + class << self |
| 96 | + protected :new |
| 97 | + |
| 98 | + # attempt to find a workable Arduino executable across platforms |
| 99 | + def guess_executable_location |
| 100 | + osx_place = "/Applications/Arduino.app/Contents/MacOS/Arduino" |
| 101 | + places = { |
| 102 | + "arduino" => !which("arduino").nil?, |
| 103 | + osx_place => (File.exist? osx_place), |
| 104 | + } |
| 105 | + places.each { |k, v| return k if v } |
| 106 | + nil |
| 107 | + end |
| 108 | + |
| 109 | + def autolocate |
| 110 | + ret = new |
| 111 | + ret.path = guess_executable_location |
| 112 | + ret |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + attr_accessor :path |
| 117 | + |
| 118 | + def initialize |
| 119 | + @display_mgr = DisplayMgr::instance |
| 120 | + end |
| 121 | + |
| 122 | + end |
| 123 | + |
| 124 | +end |
0 commit comments