Skip to content

Commit 44e1585

Browse files
authored
Fix NoMethodError when rules.scenes[] or rules.scripts[] target is missing (#541)
Ensure that the returned rule is not nil before calling #tagged? Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
1 parent e6d1bc0 commit 44e1585

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

lib/openhab/core/rules/tagged_array.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def initialize(tag)
2323
#
2424
def [](uid)
2525
rule = $rules.get(uid)
26-
rule.tagged?(@tag) ? rule : nil
26+
rule if rule&.tagged?(@tag)
2727
end
2828
alias_method :include?, :[]
2929
alias_method :key?, :[]

spec/openhab/dsl_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,46 @@
337337
end
338338
end
339339

340+
describe "#rules" do
341+
shared_examples "a tagged rule collection" do |collection_name, factory_method, tag|
342+
it "returns rules tagged with '#{tag}'" do
343+
id = "my_tagged_#{tag.downcase}"
344+
send(factory_method, id:) { nil }
345+
expect(rules.send(collection_name)).to include rules[id]
346+
end
347+
348+
it "does not return rules not tagged with '#{tag}'" do
349+
rule(id: "my_normal_rule") { nil }
350+
expect(rules.send(collection_name)).not_to include rules["my_normal_rule"]
351+
end
352+
353+
describe "#[]" do
354+
it "returns a matching #{tag} by uid" do
355+
id = "my_#{tag.downcase}"
356+
send(factory_method, id:) { nil }
357+
expect(rules.send(collection_name)[id]).to eql rules[id]
358+
end
359+
360+
it "returns nil for rule not tagged with '#{tag}'" do
361+
rule(id: "my_other_rule") { nil }
362+
expect(rules.send(collection_name)["my_other_rule"]).to be_nil
363+
end
364+
365+
it "returns nil for a non-existent uid" do
366+
expect(rules.send(collection_name)["nonexistent"]).to be_nil
367+
end
368+
end
369+
end
370+
371+
describe "#scenes" do
372+
it_behaves_like "a tagged rule collection", :scenes, :scene, "Scene"
373+
end
374+
375+
describe "#scripts" do
376+
it_behaves_like "a tagged rule collection", :scripts, :script, "Script"
377+
end
378+
end
379+
340380
describe "#store_states" do
341381
before do
342382
items.build do

0 commit comments

Comments
 (0)