summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/chef/provider.rb14
-rw-r--r--lib/chef/resource.rb14
-rw-r--r--lib/chef/run_context/cookbook_compiler.rb2
-rw-r--r--spec/integration/recipes/notifies_spec.rb16
-rw-r--r--spec/integration/recipes/use_partial_spec.rb112
5 files changed, 158 insertions, 0 deletions
diff --git a/lib/chef/provider.rb b/lib/chef/provider.rb
index 3783bd9d5f..973d76325e 100644
--- a/lib/chef/provider.rb
+++ b/lib/chef/provider.rb
@@ -81,6 +81,20 @@ class Chef
# Chef.deprecated(:use_inline_resources, "The use_inline_resources mode is no longer optional and the line enabling it can be removed")
end
+ # Use a partial code fragment. This can be used for code sharing between multiple resources.
+ #
+ # Do not wrap the code fragment in a class or module. It also does not support the use of super
+ # to subclass any methods defined in the fragment, the methods will just be overwritten.
+ #
+ # @param partial [String] the code fragment to eval against the class
+ #
+ def self.use(partial)
+ dirname = ::File.dirname(partial)
+ basename = ::File.basename(partial, ".rb")
+ basename = basename[1..-1] if basename[0] == "_"
+ class_eval IO.read(::File.expand_path("#{dirname}/_#{basename}.rb", ::File.dirname(caller_locations.first.absolute_path)))
+ end
+
# delegate to the resource
#
def_delegators :@new_resource, :property_is_set?
diff --git a/lib/chef/resource.rb b/lib/chef/resource.rb
index bbbe714f3d..d18d6ab158 100644
--- a/lib/chef/resource.rb
+++ b/lib/chef/resource.rb
@@ -1461,6 +1461,20 @@ class Chef
@default_description
end
+ # Use a partial code fragment. This can be used for code sharing between multiple resources.
+ #
+ # Do not wrap the code fragment in a class or module. It also does not support the use of super
+ # to subclass any methods defined in the fragment, the methods will just be overwritten.
+ #
+ # @param partial [String] the code fragment to eval against the class
+ #
+ def self.use(partial)
+ dirname = ::File.dirname(partial)
+ basename = ::File.basename(partial, ".rb")
+ basename = basename[1..-1] if basename[0] == "_"
+ class_eval IO.read(::File.expand_path("#{dirname}/_#{basename}.rb", ::File.dirname(caller_locations.first.absolute_path)))
+ end
+
# The cookbook in which this Resource was defined (if any).
#
# @return Chef::CookbookVersion The cookbook in which this Resource was defined.
diff --git a/lib/chef/run_context/cookbook_compiler.rb b/lib/chef/run_context/cookbook_compiler.rb
index 4dda6aeb2e..36ee34c62e 100644
--- a/lib/chef/run_context/cookbook_compiler.rb
+++ b/lib/chef/run_context/cookbook_compiler.rb
@@ -245,11 +245,13 @@ class Chef
def load_lwrps_from_cookbook(cookbook_name)
files_in_cookbook_by_segment(cookbook_name, :providers).each do |filename|
next unless File.extname(filename) == ".rb"
+ next if File.basename(filename).match?(/^_/)
load_lwrp_provider(cookbook_name, filename)
end
files_in_cookbook_by_segment(cookbook_name, :resources).each do |filename|
next unless File.extname(filename) == ".rb"
+ next if File.basename(filename).match?(/^_/)
load_lwrp_resource(cookbook_name, filename)
end
diff --git a/spec/integration/recipes/notifies_spec.rb b/spec/integration/recipes/notifies_spec.rb
index 6d781922f5..9767cf5de7 100644
--- a/spec/integration/recipes/notifies_spec.rb
+++ b/spec/integration/recipes/notifies_spec.rb
@@ -1,3 +1,19 @@
+#
+# Copyright:: Copyright Chef Software Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
require "spec_helper"
require "support/shared/integration/integration_helper"
require "chef/mixin/shell_out"
diff --git a/spec/integration/recipes/use_partial_spec.rb b/spec/integration/recipes/use_partial_spec.rb
new file mode 100644
index 0000000000..b8e8a27635
--- /dev/null
+++ b/spec/integration/recipes/use_partial_spec.rb
@@ -0,0 +1,112 @@
+#
+# Copyright:: Copyright Chef Software Inc.
+# License:: Apache License, Version 2.0
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+require "spec_helper"
+require "support/shared/integration/integration_helper"
+require "chef/mixin/shell_out"
+
+describe "notifying_block" do
+ include IntegrationSupport
+ include Chef::Mixin::ShellOut
+
+ let(:chef_dir) { File.expand_path("../../../../bin", __FILE__) }
+ let(:chef_client) { "bundle exec chef-client --minimal-ohai" }
+
+ when_the_repository "has a cookbook with partial resources" do
+ before do
+ directory "cookbooks/x" do
+ file "resources/_shared_properties.rb", <<-EOM
+ property :content, String
+ EOM
+ file "resources/_action_helpers.rb", <<-EOM
+ def printit(string)
+ puts "DIDIT: \#{string}"
+ end
+ EOM
+ file "resources/thing.rb", <<-EOM
+ provides :thing
+ use "shared_properties"
+ action_class do
+ use "action_helpers"
+ end
+ action :run do
+ printit(new_resource.content)
+ end
+ EOM
+ file "recipes/default.rb", <<~EOM
+ thing "whatever" do
+ content "stuff"
+ end
+ EOM
+ end
+ file "config/client.rb", <<-EOM
+ local_mode true
+ cookbook_path "#{path_to("cookbooks")}"
+ log_level :warn
+ always_dump_stacktrace true
+ EOM
+ end
+
+ it "should run cleanly and print the output" do
+ result = shell_out("#{chef_client} -c \"#{path_to("config/client.rb")}\" --no-color -F doc -o 'x::default'", cwd: chef_dir)
+ expect(result.stdout).to match(/DIDIT: stuff/)
+ result.error!
+ end
+ end
+
+ when_the_repository "has a cookbook with partial resources done differently" do
+ before do
+ directory "cookbooks/x" do
+ file "partials/_shared_properties.rb", <<-EOM
+ property :content, String
+ EOM
+ file "partials/_action_partials.rb", <<-EOM
+ def printit(string)
+ puts "DIDIT: \#{string}"
+ end
+ EOM
+ # this tests relative pathing, including the underscore and including the trailing .rb all work
+ file "resources/thing.rb", <<-EOM
+ provides :thing
+ use "../partials/_shared_properties.rb"
+ action_class do
+ use "../partials/_action_partials.rb"
+ end
+ action :run do
+ printit(new_resource.content)
+ end
+ EOM
+ file "recipes/default.rb", <<~EOM
+ thing "whatever" do
+ content "stuff"
+ end
+ EOM
+ end
+ file "config/client.rb", <<-EOM
+ local_mode true
+ cookbook_path "#{path_to("cookbooks")}"
+ log_level :warn
+ always_dump_stacktrace true
+ EOM
+ end
+
+ it "should run cleanly and print the output" do
+ result = shell_out("#{chef_client} -c \"#{path_to("config/client.rb")}\" --no-color -F doc -o 'x::default'", cwd: chef_dir)
+ expect(result.stdout).to match(/DIDIT: stuff/)
+ result.error!
+ end
+ end
+end