summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@chef.io>2020-01-30 15:38:43 -0800
committerGitHub <noreply@github.com>2020-01-30 15:38:43 -0800
commitb4aca3a306e46d5a8c3eadada213c15698cf8553 (patch)
treee7b722572b930287045cbf371ef051b5836ca27b
parent348731e64803ec553a719f89893209712daacdd5 (diff)
parentda82e9c737a594e619943c8f32e6368e6bb85fce (diff)
downloadchef-b4aca3a306e46d5a8c3eadada213c15698cf8553.tar.gz
Add chef-sugar include_recipe? helper (#9304)
Add chef-sugar include_recipe? helper
-rw-r--r--chef-utils/README.md1
-rw-r--r--chef-utils/lib/chef-utils/dsl/introspection.rb13
-rw-r--r--chef-utils/spec/unit/dsl/introspection_spec.rb21
3 files changed, 33 insertions, 2 deletions
diff --git a/chef-utils/README.md b/chef-utils/README.md
index 3a44cf7330..8116f84c5e 100644
--- a/chef-utils/README.md
+++ b/chef-utils/README.md
@@ -146,6 +146,7 @@ Architecture Helpers allow you to determine the processor architecture of your n
* `systemd?` - if the init system is systemd
* `kitchen?` - if ENV['TEST_KITCHEN'] is set
* `ci?` - if ENV['CI'] is set
+* `include_recipe?(recipe_name)` - if the `recipe_name` is in the run list, the expanded run list, or has been `include_recipe`'d.
### Service Helpers
diff --git a/chef-utils/lib/chef-utils/dsl/introspection.rb b/chef-utils/lib/chef-utils/dsl/introspection.rb
index 6fb06107d2..c5dcdc0c20 100644
--- a/chef-utils/lib/chef-utils/dsl/introspection.rb
+++ b/chef-utils/lib/chef-utils/dsl/introspection.rb
@@ -1,5 +1,5 @@
#
-# Copyright:: Copyright 2018-2019, Chef Software Inc.
+# Copyright:: Copyright 2018-2020, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -97,6 +97,17 @@ module ChefUtils
end
end
+ # Determine if the current node includes the given recipe name.
+ #
+ # @param [String] recipe_name
+ #
+ # @return [Boolean]
+ #
+ def includes_recipe?(recipe_name, node = __getnode)
+ node.recipe?(recipe_name)
+ end
+ alias_method :include_recipe?, :includes_recipe?
+
extend self
end
end
diff --git a/chef-utils/spec/unit/dsl/introspection_spec.rb b/chef-utils/spec/unit/dsl/introspection_spec.rb
index d45a3c6000..8457f06630 100644
--- a/chef-utils/spec/unit/dsl/introspection_spec.rb
+++ b/chef-utils/spec/unit/dsl/introspection_spec.rb
@@ -1,5 +1,5 @@
#
-# Copyright:: Copyright 2018-2019, Chef Software Inc.
+# Copyright:: Copyright 2018-2020, Chef Software Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -165,4 +165,23 @@ RSpec.describe ChefUtils::DSL::Introspection do
end
end
end
+
+ context "#include_recipe?" do
+ it "is true when the recipe has been seen by the node" do
+ expect(node).to receive(:recipe?).with("myrecipe").and_return(true)
+ expect(ChefUtils.include_recipe?("myrecipe", node)).to be true
+ end
+ it "is false when the recipe has not been seen by the node" do
+ expect(node).to receive(:recipe?).with("myrecipe").and_return(false)
+ expect(ChefUtils.include_recipe?("myrecipe", node)).to be false
+ end
+ it "the alias is true when the recipe has been seen by the node" do
+ expect(node).to receive(:recipe?).with("myrecipe").and_return(true)
+ expect(ChefUtils.includes_recipe?("myrecipe", node)).to be true
+ end
+ it "the alias is false when the recipe has not been seen by the node" do
+ expect(node).to receive(:recipe?).with("myrecipe").and_return(false)
+ expect(ChefUtils.includes_recipe?("myrecipe", node)).to be false
+ end
+ end
end