Skip to content

Commit d815750

Browse files
committed
More flexible config overriding
1 parent d0235b4 commit d815750

File tree

4 files changed

+90
-5
lines changed

4 files changed

+90
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
88
### Added
99
- Provide an `itoa` function. It is present in Arduino's runtime environment but not on most (all?) host systems because itoa is not a portable standard function.
1010
- `to_h` and `to_s` functions for `ci_config.rb`
11+
- `CIConfig::clone`
12+
- Ability to override `CIConfig` from a hash instead of just a file
1113

1214
### Changed
1315
- Simplified the use of `Array.each` with a return statement; it's now simply `Array.find`

lib/arduino_ci/ci_config.rb

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ def load_yaml(path)
127127
yml = YAML.load_file(path)
128128
raise ConfigurationError, "The YAML file at #{path} failed to load" unless yml
129129

130+
apply_configuration(yml)
131+
end
132+
133+
# Load configuration from a hash
134+
# @param yml [Hash] the source data
135+
# @return [ArduinoCI::CIConfig] a reference to self
136+
def apply_configuration(yml)
130137
if yml.include?("packages")
131138
yml["packages"].each do |k, v|
132139
valid_data = validate_data("packages", v, PACKAGE_SCHEMA)
@@ -154,19 +161,35 @@ def load_yaml(path)
154161
self
155162
end
156163

164+
# Create a clone of this configuration and return it
165+
# @return [ArduinoCI::CIConfig] the new settings object
166+
def clone
167+
cloned_config = self.class.new
168+
cloned_config.package_info = deep_clone(@package_info)
169+
cloned_config.platform_info = deep_clone(@platform_info)
170+
cloned_config.compile_info = deep_clone(@compile_info)
171+
cloned_config.unittest_info = deep_clone(@unittest_info)
172+
cloned_config
173+
end
174+
157175
# Override these settings with settings from another file
158176
# @param path [String] the path to the settings yaml file
159177
# @return [ArduinoCI::CIConfig] the new settings object
160178
def with_override(path)
161-
overridden_config = self.class.new
162-
overridden_config.package_info = deep_clone(@package_info)
163-
overridden_config.platform_info = deep_clone(@platform_info)
164-
overridden_config.compile_info = deep_clone(@compile_info)
165-
overridden_config.unittest_info = deep_clone(@unittest_info)
179+
overridden_config = clone
166180
overridden_config.load_yaml(path)
167181
overridden_config
168182
end
169183

184+
# Override these settings with settings from a hash
185+
# @param config_hash [Hash] A configuration hash
186+
# @return [ArduinoCI::CIConfig] the new settings object
187+
def with_override_config(config_hash)
188+
overridden_config = clone
189+
overridden_config.apply_configuration(config_hash)
190+
overridden_config
191+
end
192+
170193
# Get the config file at a given path, if it exists, and pass that to a block.
171194
# Many config files may exist, but only the first match is used
172195
# @param base_dir [String] The directory in which to search for a config file

spec/ci_config_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,46 @@
3939
end
4040
end
4141

42+
context "clone" do
43+
it "creates a copy" do
44+
base = ArduinoCI::CIConfig.new
45+
base.load_yaml(File.join(File.dirname(__FILE__), "yaml", "o2.yaml"))
46+
47+
expect(base.to_h).to eq(
48+
packages: {},
49+
platforms: {
50+
"bogo"=> {
51+
board: "fakeduino:beep:bogo"
52+
},
53+
},
54+
compile: {
55+
libraries: ["zip"],
56+
platforms: ["bogo"]
57+
},
58+
unittest: {
59+
testfiles: {
60+
select: ["*-*.*"],
61+
reject: ["sam-squamsh.*"]
62+
},
63+
libraries: ["def456"],
64+
platforms: ["bogo"]
65+
}
66+
)
67+
end
68+
end
69+
70+
context "clone" do
71+
it "creates a copy" do
72+
base = ArduinoCI::CIConfig.default
73+
orig = base.to_h
74+
clone1 = orig.clone.to_h
75+
clone2 = orig.clone.to_h
76+
77+
expect(orig).to eq(clone1)
78+
expect(clone1).to eq(clone2)
79+
end
80+
end
81+
4282
context "with_override" do
4383
it "loads from yaml" do
4484
override_file = File.join(File.dirname(__FILE__), "yaml", "o1.yaml")

spec/yaml/o2.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
platforms:
2+
bogo:
3+
board: fakeduino:beep:bogo
4+
5+
compile:
6+
libraries:
7+
- "zip"
8+
platforms:
9+
- bogo
10+
11+
unittest:
12+
testfiles:
13+
select:
14+
- "*-*.*"
15+
reject:
16+
- "sam-squamsh.*"
17+
libraries:
18+
- "def456"
19+
platforms:
20+
- bogo

0 commit comments

Comments
 (0)