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