Skip to content

Commit 7c8e164

Browse files
committed
Explicit handling and future-proofing for arduino-cli#753
1 parent 220875d commit 7c8e164

File tree

4 files changed

+104
-8
lines changed

4 files changed

+104
-8
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2323
- Update .gitattributes so we have consistent line endings
2424
- Change 266 files from CRLF to LF.
2525
- Run tests on push as well as on a pull request so developers can see impact
26+
- `ArduinoBackend` now exposes `config_file_path` instead of `config_dir` so that we can be explicit about [strange behavior in `arduino-cli` that isn't going to change anytime soon](https://github.com/arduino/arduino-cli/issues/753)
2627

2728
### Deprecated
2829

Diff for: lib/arduino_ci/arduino_backend.rb

+41-4
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@ class ArduinoBackend
1717
# @return [String] the only allowable name for the arduino-cli config file.
1818
CONFIG_FILE_NAME = "arduino-cli.yaml".freeze
1919

20+
# Unfortunately we need error messaging around this quirk
21+
# @return [String] The text to use for user apologies regarding the config file
22+
CONFIG_FILE_APOLOGY = "Sorry this is weird, see https://github.com/arduino/arduino-cli/issues/753".freeze
23+
2024
# the actual path to the executable on this platform
2125
# @return [Pathname]
2226
attr_accessor :binary_path
2327

24-
# If a custom config is deired (i.e. for testing), specify it here.
25-
# Note https://github.com/arduino/arduino-cli/issues/753 : the --config-file option
26-
# is really the director that contains the file
28+
# The directory that contains the config file
2729
# @return [Pathname]
28-
attr_accessor :config_dir
30+
attr_reader :config_dir
2931

3032
# @return [String] STDOUT of the most recently-run command
3133
attr_reader :last_out
@@ -62,6 +64,41 @@ def _wrap_run(work_fn, *args, **kwargs)
6264
work_fn.call(*full_cmd, **kwargs)
6365
end
6466

67+
# The config file name to be passed on the command line
68+
#
69+
# Note https://github.com/arduino/arduino-cli/issues/753 : the --config-file option
70+
# is really the directory that contains the file
71+
#
72+
# @return [Pathname]
73+
def config_file_path
74+
@config_dir + CONFIG_FILE_NAME
75+
end
76+
77+
# The config file name to be passed on the command line
78+
#
79+
# Note https://github.com/arduino/arduino-cli/issues/753 : the --config-file option
80+
# is really the directory that contains the file
81+
#
82+
# @param val [Pathname] The config file that will be used
83+
# @return [Pathname]
84+
def config_file_path=(rhs)
85+
path_rhs = Pathname(rhs)
86+
err_text = "Config file basename must be '#{CONFIG_FILE_NAME}'. #{CONFIG_FILE_APOLOGY}"
87+
raise ArgumentError, err_text unless path_rhs.basename.to_s == CONFIG_FILE_NAME
88+
@config_dir = path_rhs.dirname
89+
end
90+
91+
# Get an acceptable filename for use as a config file
92+
#
93+
# Note https://github.com/arduino/arduino-cli/issues/753 : the --config-file option
94+
# is really the directory that contains the file
95+
#
96+
# @param dir [Pathname] the desired directory
97+
# @return [Pathname]
98+
def self.config_file_path_from_dir(dir)
99+
Pathname(dir) + CONFIG_FILE_NAME
100+
end
101+
65102
# build and run the arduino command
66103
def run_and_output(*args, **kwargs)
67104
_wrap_run((proc { |*a, **k| Host.run_and_output(*a, **k) }), *args, **kwargs)

Diff for: spec/arduino_backend_spec.rb

+60-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,32 @@
11
require "spec_helper"
2-
require 'pathname'
2+
require "pathname"
3+
require "fileutils"
4+
require "tmpdir"
35

46
def get_sketch(dir, file)
5-
File.join(File.dirname(__FILE__), dir, file)
7+
Pathname.new(__FILE__).parent + dir + file
68
end
79

10+
# test function for below, to avoid long lines
11+
def bug_753_cmd(backend, config_file)
12+
[
13+
backend.binary_path.to_s,
14+
"--config-file",
15+
config_file.to_s,
16+
"--verbose",
17+
"config",
18+
"dump"
19+
]
20+
end
21+
22+
def with_tmp_file(desired_filename = nil)
23+
Dir.mktmpdir do |tdir|
24+
config_dir = Pathname(tdir)
25+
config_file = config_dir + (desired_filename || ArduinoCI::ArduinoBackend::CONFIG_FILE_NAME)
26+
FileUtils.touch(config_file)
27+
yield(config_dir, config_file)
28+
end
29+
end
830

931
RSpec.describe ArduinoCI::ArduinoBackend do
1032
next if skip_ruby_tests
@@ -27,6 +49,42 @@ def get_sketch(dir, file)
2749
end
2850
end
2951

52+
context "version-specific quirks" do
53+
backend = ArduinoCI::ArduinoInstallation.autolocate!
54+
55+
# https://github.com/arduino/arduino-cli/issues/753
56+
57+
it "suffers from arduino-cli bug 753 - nonstandard filename" do
58+
# foo.yml won't be accepted as a filename
59+
with_tmp_file("foo.yml") do |config_dir, config_file|
60+
expect(config_dir).to exist
61+
expect(config_file).to exist
62+
ret = ArduinoCI::Host.run_and_capture(*bug_753_cmd(backend, config_file))
63+
expect(ret[:out].lines[0]).to include("Config file not found, using default values")
64+
end
65+
end
66+
67+
it "obeys arduino-cli bug 753 workaround" do
68+
# the standard filename will work
69+
with_tmp_file do |config_dir, config_file|
70+
expect(config_dir).to exist
71+
expect(config_file).to exist
72+
ret = ArduinoCI::Host.run_and_capture(*bug_753_cmd(backend, config_file))
73+
expect(ret[:out].lines[0]).to include("Using config file: #{config_file}")
74+
end
75+
end
76+
77+
it "obeys arduino-cli bug 753" do
78+
# the directory alone will work if there is a file with the right name
79+
with_tmp_file do |config_dir, config_file|
80+
expect(config_dir).to exist
81+
expect(config_file).to exist
82+
ret = ArduinoCI::Host.run_and_capture(*bug_753_cmd(backend, config_dir))
83+
expect(ret[:out].lines[0]).to include("Using config file: #{config_file}")
84+
end
85+
end
86+
end
87+
3088
context "board_installed?" do
3189
it "Finds installed boards" do
3290
backend.install_boards("arduino:avr") # we used to assume this was installed... not the case for arduino-cli

Diff for: spec/fake_lib_dir.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class FakeLibDir
1111
def initialize
1212
# we will need to install some dummy libraries into a fake location, so do that on demand
1313
@config_dir = Pathname.new(Dir.pwd).realpath
14-
@config_file = @config_dir + ArduinoCI::ArduinoBackend::CONFIG_FILE_NAME
14+
@config_file = ArduinoCI::ArduinoBackend.config_file_path_from_dir(@config_dir)
1515
@backend = ArduinoCI::ArduinoInstallation.autolocate!
16-
@backend.config_dir = @config_dir
16+
@backend.config_file_path = @config_file
1717
end
1818

1919
# designed to be called by rspec's "around" function

0 commit comments

Comments
 (0)