Skip to content

Commit 189d3ab

Browse files
committed
refactor (compartmentalize) arduino_ci_remote.rb's two main test functions
1 parent e8d8eb0 commit 189d3ab

File tree

2 files changed

+150
-135
lines changed

2 files changed

+150
-135
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88
### Added
99

1010
### Changed
11+
- Finally put some factorization into the `arduino_ci_remote.rb` script: testing unit and testing compilation are now standalone functions
1112

1213
### Deprecated
1314

exe/arduino_ci_remote.rb

Lines changed: 149 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -111,162 +111,166 @@ def display_files(pathname)
111111
non_hidden.each { |p| puts "#{margin}#{p}" }
112112
end
113113

114-
# initialize command and config
115-
config = ArduinoCI::CIConfig.default.from_project_library
116-
@arduino_cmd = ArduinoCI::ArduinoInstallation.autolocate!
117-
inform("Located Arduino binary") { @arduino_cmd.binary_path.to_s }
114+
def perform_unit_tests(config)
115+
cpp_library = ArduinoCI::CppLibrary.new(Pathname.new("."), @arduino_cmd.lib_dir)
116+
117+
# check GCC
118+
compilers = config.compilers_to_use
119+
assure("The set of compilers (#{compilers.length}) isn't empty") { !compilers.empty? }
120+
compilers.each do |gcc_binary|
121+
attempt_multiline("Checking #{gcc_binary} version") do
122+
version = cpp_library.gcc_version(gcc_binary)
123+
next nil unless version
124+
125+
puts version.split("\n").map { |l| " #{l}" }.join("\n")
126+
version
127+
end
128+
inform("libasan availability for #{gcc_binary}") { cpp_library.libasan?(gcc_binary) }
129+
end
118130

119-
# index the existing libraries
120-
attempt("Indexing libraries") { @arduino_cmd.index_libraries } unless @arduino_cmd.libraries_indexed
131+
# Ensure platforms exist for unit test, and save their info in all_platform_info keyed by name
132+
all_platform_info = {}
133+
config.platforms_to_unittest.each { |p| all_platform_info[p] = assured_platform("unittest", p, config) }
121134

122-
# initialize library under test
123-
installed_library_path = attempt("Installing library under test") do
124-
@arduino_cmd.install_local_library(Pathname.new("."))
125-
end
126-
if installed_library_path.exist?
127-
inform("Library installed at") { installed_library_path.to_s }
128-
else
129-
assure_multiline("Library installed successfully") do
130-
# print out the contents of the deepest directory we actually find
131-
@arduino_cmd.lib_dir.ascend do |path_part|
132-
next unless path_part.exist?
133-
134-
break display_files(path_part)
135+
# iterate boards / tests
136+
if !cpp_library.tests_dir.exist?
137+
inform_multiline("Skipping unit tests; no tests dir at #{cpp_library.tests_dir}") do
138+
puts " In case that's an error, this is what was found in the library:"
139+
display_files(cpp_library.tests_dir.parent)
140+
true
141+
end
142+
elsif cpp_library.test_files.empty?
143+
inform_multiline("Skipping unit tests; no test files were found in #{cpp_library.tests_dir}") do
144+
puts " In case that's an error, this is what was found in the tests directory:"
145+
display_files(cpp_library.tests_dir)
146+
true
147+
end
148+
elsif config.platforms_to_unittest.empty?
149+
inform("Skipping unit tests") { "no platforms were requested" }
150+
else
151+
config.platforms_to_unittest.each do |p|
152+
cpp_library.test_files.each do |unittest_path|
153+
unittest_name = unittest_path.basename.to_s
154+
compilers.each do |gcc_binary|
155+
attempt_multiline("Unit testing #{unittest_name} with #{gcc_binary}") do
156+
exe = cpp_library.build_for_test_with_configuration(
157+
unittest_path,
158+
config.aux_libraries_for_unittest,
159+
gcc_binary,
160+
config.gcc_config(p)
161+
)
162+
puts
163+
unless exe
164+
puts "Last command: #{cpp_library.last_cmd}"
165+
puts cpp_library.last_out
166+
puts cpp_library.last_err
167+
next false
168+
end
169+
cpp_library.run_test_file(exe)
170+
end
171+
end
172+
end
135173
end
136-
false
137-
end
138-
end
139-
library_examples = @arduino_cmd.library_examples(installed_library_path)
140-
cpp_library = ArduinoCI::CppLibrary.new(installed_library_path, @arduino_cmd.lib_dir)
141-
142-
# check GCC
143-
compilers = config.compilers_to_use
144-
assure("The set of compilers (#{compilers.length}) isn't empty") { !compilers.empty? }
145-
compilers.each do |gcc_binary|
146-
attempt_multiline("Checking #{gcc_binary} version") do
147-
version = cpp_library.gcc_version(gcc_binary)
148-
next nil unless version
149-
150-
puts version.split("\n").map { |l| " #{l}" }.join("\n")
151-
version
152174
end
153-
inform("libasan availability for #{gcc_binary}") { cpp_library.libasan?(gcc_binary) }
154175
end
155176

156-
# Ensure platforms exist for unit test, and save their info in all_platform_info keyed by name
157-
all_platform_info = {}
158-
config.platforms_to_unittest.each { |p| all_platform_info[p] = assured_platform("unittest", p, config) }
159-
160-
# gather up all required boards for compilation so we can install them up front.
161-
# start with the "platforms to unittest" and add the examples
162-
# while we're doing that, get the aux libraries as well
163-
example_platform_info = {}
164-
board_package_url = {}
165-
aux_libraries = Set.new(config.aux_libraries_for_unittest + config.aux_libraries_for_build)
166-
# while collecting the platforms, ensure they're defined
167-
library_examples.each do |path|
168-
ovr_config = config.from_example(path)
169-
ovr_config.platforms_to_build.each do |platform|
170-
# assure the platform if we haven't already
171-
next if example_platform_info.key?(platform)
172-
173-
platform_info = assured_platform("library example", platform, config)
174-
next if platform_info.nil?
175-
176-
example_platform_info[platform] = all_platform_info[platform] = platform_info
177-
package = platform_info[:package]
178-
board_package_url[package] = ovr_config.package_url(package)
179-
end
180-
aux_libraries.merge(ovr_config.aux_libraries_for_build)
181-
end
177+
def perform_compilation_tests(config)
182178

183-
# with all platform info, we can extract unique packages and their urls
184-
# do that, set the URLs, and download the packages
185-
all_packages = all_platform_info.values.map { |v| v[:package] }.uniq.reject(&:nil?)
179+
# index the existing libraries
180+
attempt("Indexing libraries") { @arduino_cmd.index_libraries } unless @arduino_cmd.libraries_indexed
186181

187-
# inform about builtin packages
188-
all_packages.select { |p| config.package_builtin?(p) }.each do |p|
189-
inform("Using built-in board package") { p }
190-
end
182+
# initialize library under test
183+
installed_library_path = attempt("Installing library under test") do
184+
@arduino_cmd.install_local_library(Pathname.new("."))
185+
end
186+
if installed_library_path.exist?
187+
inform("Library installed at") { installed_library_path.to_s }
188+
else
189+
assure_multiline("Library installed successfully") do
190+
# print out the contents of the deepest directory we actually find
191+
@arduino_cmd.lib_dir.ascend do |path_part|
192+
next unless path_part.exist?
191193

192-
# make sure any non-builtin package has a URL defined
193-
all_packages.reject { |p| config.package_builtin?(p) }.each do |p|
194-
assure("Board package #{p} has a defined URL") { board_package_url[p] }
195-
end
194+
break display_files(path_part)
195+
end
196+
false
197+
end
198+
end
199+
library_examples = @arduino_cmd.library_examples(installed_library_path)
200+
201+
# gather up all required boards for compilation so we can install them up front.
202+
# start with the "platforms to unittest" and add the examples
203+
# while we're doing that, get the aux libraries as well
204+
example_platform_info = {}
205+
board_package_url = {}
206+
aux_libraries = Set.new(config.aux_libraries_for_unittest + config.aux_libraries_for_build)
207+
# while collecting the platforms, ensure they're defined
208+
209+
library_examples.each do |path|
210+
ovr_config = config.from_example(path)
211+
ovr_config.platforms_to_build.each do |platform|
212+
# assure the platform if we haven't already
213+
next if example_platform_info.key?(platform)
214+
215+
platform_info = assured_platform("library example", platform, config)
216+
next if platform_info.nil?
217+
218+
example_platform_info[platform] = platform_info
219+
package = platform_info[:package]
220+
board_package_url[package] = ovr_config.package_url(package)
221+
end
222+
aux_libraries.merge(ovr_config.aux_libraries_for_build)
223+
end
196224

197-
# set up all the board manager URLs.
198-
# we can safely reject nils now, they would be for the builtins
199-
all_urls = all_packages.map { |p| board_package_url[p] }.uniq.reject(&:nil?)
225+
# with all platform info, we can extract unique packages and their urls
226+
# do that, set the URLs, and download the packages
227+
all_packages = example_platform_info.values.map { |v| v[:package] }.uniq.reject(&:nil?)
200228

201-
unless all_urls.empty?
202-
assure("Setting board manager URLs") do
203-
@arduino_cmd.board_manager_urls = all_urls
229+
# inform about builtin packages
230+
all_packages.select { |p| config.package_builtin?(p) }.each do |p|
231+
inform("Using built-in board package") { p }
204232
end
205-
end
206233

207-
all_packages.each do |p|
208-
assure("Installing board package #{p}") do
209-
@arduino_cmd.install_boards(p)
234+
# make sure any non-builtin package has a URL defined
235+
all_packages.reject { |p| config.package_builtin?(p) }.each do |p|
236+
assure("Board package #{p} has a defined URL") { board_package_url[p] }
210237
end
211-
end
212238

213-
aux_libraries.each do |l|
214-
if @arduino_cmd.library_present?(l)
215-
inform("Using pre-existing library") { l.to_s }
216-
else
217-
assure("Installing aux library '#{l}'") { @arduino_cmd.install_library(l) }
218-
end
219-
end
239+
# set up all the board manager URLs.
240+
# we can safely reject nils now, they would be for the builtins
241+
all_urls = all_packages.map { |p| board_package_url[p] }.uniq.reject(&:nil?)
220242

221-
# iterate boards / tests
222-
last_board = nil
223-
if !cpp_library.tests_dir.exist?
224-
inform_multiline("Skipping unit tests; no tests dir at #{cpp_library.tests_dir}") do
225-
puts " In case that's an error, this is what was found in the library:"
226-
display_files(cpp_library.tests_dir.parent)
227-
true
243+
unless all_urls.empty?
244+
assure("Setting board manager URLs") do
245+
@arduino_cmd.board_manager_urls = all_urls
246+
end
228247
end
229-
elsif cpp_library.test_files.empty?
230-
inform_multiline("Skipping unit tests; no test files were found in #{cpp_library.tests_dir}") do
231-
puts " In case that's an error, this is what was found in the tests directory:"
232-
display_files(cpp_library.tests_dir)
233-
true
248+
249+
all_packages.each do |p|
250+
assure("Installing board package #{p}") do
251+
@arduino_cmd.install_boards(p)
252+
end
234253
end
235-
elsif config.platforms_to_unittest.empty?
236-
inform("Skipping unit tests") { "no platforms were requested" }
237-
else
238-
config.platforms_to_unittest.each do |p|
239-
cpp_library.test_files.each do |unittest_path|
240-
unittest_name = unittest_path.basename.to_s
241-
compilers.each do |gcc_binary|
242-
attempt_multiline("Unit testing #{unittest_name} with #{gcc_binary}") do
243-
exe = cpp_library.build_for_test_with_configuration(
244-
unittest_path,
245-
config.aux_libraries_for_unittest,
246-
gcc_binary,
247-
config.gcc_config(p)
248-
)
249-
puts
250-
unless exe
251-
puts "Last command: #{cpp_library.last_cmd}"
252-
puts cpp_library.last_out
253-
puts cpp_library.last_err
254-
next false
255-
end
256-
cpp_library.run_test_file(exe)
257-
end
258-
end
254+
255+
aux_libraries.each do |l|
256+
if @arduino_cmd.library_present?(l)
257+
inform("Using pre-existing library") { l.to_s }
258+
else
259+
assure("Installing aux library '#{l}'") { @arduino_cmd.install_library(l) }
259260
end
260261
end
261-
end
262262

263-
if config.platforms_to_build.empty?
264-
inform("Skipping builds") { "no platforms were requested" }
265-
elsif library_examples.empty?
266-
inform_multiline("Skipping builds; no examples found in #{installed_library_path}") do
267-
display_files(installed_library_path)
263+
last_board = nil
264+
if config.platforms_to_build.empty?
265+
inform("Skipping builds") { "no platforms were requested" }
266+
return
267+
elsif library_examples.empty?
268+
inform_multiline("Skipping builds; no examples found in #{installed_library_path}") do
269+
display_files(installed_library_path)
270+
end
271+
return
268272
end
269-
else
273+
270274
attempt("Setting compiler warning level") { @arduino_cmd.set_pref("compiler.warning_level", "all") }
271275

272276
# switching boards takes time, so iterate board first
@@ -280,7 +284,7 @@ def display_files(pathname)
280284
end
281285

282286
examples_by_platform.each do |platform, example_paths|
283-
board = all_platform_info[platform][:board]
287+
board = example_platform_info[platform][:board]
284288
assure("Switching to board for #{platform} (#{board})") { @arduino_cmd.use_board(board) } unless last_board == board
285289
last_board = board
286290

@@ -297,6 +301,16 @@ def display_files(pathname)
297301
end
298302
end
299303
end
304+
300305
end
301306

307+
# initialize command and config
308+
config = ArduinoCI::CIConfig.default.from_project_library
309+
310+
@arduino_cmd = ArduinoCI::ArduinoInstallation.autolocate!
311+
inform("Located Arduino binary") { @arduino_cmd.binary_path.to_s }
312+
313+
perform_unit_tests(config)
314+
perform_compilation_tests(config)
315+
302316
terminate(true)

0 commit comments

Comments
 (0)