Skip to content

Commit 58fce6a

Browse files
committed
Explicit handling and future-proofing for arduino-cli#753 - config path
1 parent 360a87f commit 58fce6a

File tree

5 files changed

+134
-10
lines changed

5 files changed

+134
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
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

lib/arduino_ci/arduino_backend.rb

Lines changed: 53 additions & 5 deletions
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
@@ -53,7 +55,7 @@ def _wrap_run(work_fn, *args, **kwargs)
5355
has_env = !args.empty? && args[0].instance_of?(Hash)
5456
env_vars = has_env ? args[0] : {}
5557
actual_args = has_env ? args[1..-1] : args # need to shift over if we extracted args
56-
custom_config = @config_dir.nil? ? [] : ["--config-file", @config_dir.to_s]
58+
custom_config = @config_dir.nil? ? [] : ["--config-file", config_file_cli_param.to_s]
5759
full_args = [binary_path.to_s, "--format", "json"] + custom_config + actual_args
5860
full_cmd = env_vars.empty? ? full_args : [env_vars] + full_args
5961

@@ -62,6 +64,52 @@ 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+
89+
@config_dir = path_rhs.dirname
90+
end
91+
92+
# The config file to be used as a CLI param
93+
#
94+
# Apparently Linux wants the whole path, and OSX wants just the directory as of 0.29.0,
95+
# it's all very annoying. See unit tests.
96+
#
97+
# @return [Pathname] the path to use for a given OS
98+
def config_file_cli_param
99+
OS.linux? ? config_file_path : @config_dir
100+
end
101+
102+
# Get an acceptable filename for use as a config file
103+
#
104+
# Note https://github.com/arduino/arduino-cli/issues/753 : the --config-file option
105+
# is really the directory that contains the file
106+
#
107+
# @param dir [Pathname] the desired directory
108+
# @return [Pathname]
109+
def self.config_file_path_from_dir(dir)
110+
Pathname(dir) + CONFIG_FILE_NAME
111+
end
112+
65113
# build and run the arduino command
66114
def run_and_output(*args, **kwargs)
67115
_wrap_run((proc { |*a, **k| Host.run_and_output(*a, **k) }), *args, **kwargs)

spec/arduino_backend_spec.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
require "spec_helper"
2-
require 'pathname'
32

43
def get_sketch(dir, file)
5-
File.join(File.dirname(__FILE__), dir, file)
4+
Pathname.new(__FILE__).parent + dir + file
65
end
76

8-
97
RSpec.describe ArduinoCI::ArduinoBackend do
108
next if skip_ruby_tests
119

spec/arduino_installation_spec.rb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,37 @@
11
require "spec_helper"
2+
require "pathname"
3+
require "tmpdir"
4+
require "os"
5+
6+
# test function for below, to avoid long lines
7+
def bug_753_cmd(backend, config_file)
8+
[
9+
backend.binary_path.to_s,
10+
"--config-file",
11+
config_file.to_s,
12+
"--verbose",
13+
"config",
14+
"dump"
15+
]
16+
end
17+
18+
def with_tmp_file(desired_filename = nil)
19+
Dir.mktmpdir do |tdir|
20+
config_dir = Pathname(tdir)
21+
config_file = config_dir + (desired_filename || ArduinoCI::ArduinoBackend::CONFIG_FILE_NAME)
22+
File.open(config_file, "w") { |f| f.write("") }
23+
yield(config_dir, config_file)
24+
end
25+
end
26+
27+
def config_success_msg(config_file)
28+
config_file = config_file.gsub('/', '\\') if OS.windows?
29+
"Using config file: #{config_file}"
30+
end
31+
32+
def config_fail_msg
33+
"Config file not found, using default values"
34+
end
235

336
RSpec.describe ArduinoCI::ArduinoInstallation do
437
next if skip_ruby_tests
@@ -35,4 +68,48 @@
3568
end
3669
end
3770

71+
context "installed version-specific quirks" do
72+
backend = ArduinoCI::ArduinoInstallation.autolocate!
73+
74+
# https://github.com/arduino/arduino-cli/issues/753
75+
76+
it "suffers from arduino-cli bug 753 - nonstandard filename" do
77+
# foo.yml won't be accepted as a filename
78+
with_tmp_file("foo.yml") do |config_dir, config_file|
79+
expect(config_dir).to exist
80+
expect(config_file).to exist
81+
ret = ArduinoCI::Host.run_and_capture(*bug_753_cmd(backend, config_file))
82+
if OS.linux?
83+
expect(ret[:out].lines[0]).to include(config_success_msg(config_file))
84+
else
85+
expect(ret[:out].lines[0]).to include(config_fail_msg)
86+
end
87+
end
88+
end
89+
90+
it "obeys arduino-cli bug 753 workaround" do
91+
# the standard filename will work
92+
with_tmp_file do |config_dir, config_file|
93+
expect(config_dir).to exist
94+
expect(config_file).to exist
95+
ret = ArduinoCI::Host.run_and_capture(*bug_753_cmd(backend, config_file))
96+
expect(ret[:out].lines[0]).to include(config_success_msg(config_file))
97+
end
98+
end
99+
100+
it "obeys arduino-cli bug 753" do
101+
# the directory alone will work if there is a file with the right name
102+
with_tmp_file do |config_dir, config_file|
103+
expect(config_dir).to exist
104+
expect(config_file).to exist
105+
ret = ArduinoCI::Host.run_and_capture(*bug_753_cmd(backend, config_dir))
106+
if OS.linux?
107+
expect(ret[:out].lines[0]).to include(config_fail_msg)
108+
else
109+
expect(ret[:out].lines[0]).to include(config_success_msg(config_file))
110+
end
111+
end
112+
end
113+
end
114+
38115
end

spec/fake_lib_dir.rb

Lines changed: 2 additions & 2 deletions
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)