summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2015-01-12 11:54:24 -0800
committerLamont Granquist <lamont@scriptkiddie.org>2015-01-12 11:54:24 -0800
commit153764d26ed639dfd58945fdc5c4cc5ae51ef5e2 (patch)
tree4495f50be76d4352a0296a7a4df3a841bfa840a4
parentc1869a22ecec29aaafdd09e19e5291b1946e6498 (diff)
parentd108ffac0bf93ec89af499e3e34778b8d6da6efb (diff)
downloadchef-153764d26ed639dfd58945fdc5c4cc5ae51ef5e2.tar.gz
Merge pull request #2730 from opscode/lcg/chef-gem-compile-time-flag
add a compile_time flag to chef_gem resource
-rw-r--r--CHANGELOG.md1
-rw-r--r--lib/chef/resource/chef_gem.rb23
-rw-r--r--spec/unit/resource/chef_gem_spec.rb60
3 files changed, 78 insertions, 6 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 293d039bbc..050d60a3c9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -27,6 +27,7 @@
* Added deprecation warnings around the use of command attribute in script resources
* Audit mode feature added - see the RELEASE_NOTES for details
* shell_out now sets `LANGUAGE` and `LANG` to the `Chef::Config[:internal_locale]` in addition to `LC_ALL` forcing
+* chef_gem supports a compile_time flag and will warn if it is not set (behavior will change in the future)
## 12.0.3
* [**Phil Dibowitz**](https://github.com/jaymzh):
diff --git a/lib/chef/resource/chef_gem.rb b/lib/chef/resource/chef_gem.rb
index b4ead11f1d..126e3d56c3 100644
--- a/lib/chef/resource/chef_gem.rb
+++ b/lib/chef/resource/chef_gem.rb
@@ -28,6 +28,7 @@ class Chef
def initialize(name, run_context=nil)
super
@resource_name = :chef_gem
+ @compile_time = nil
@gem_binary = RbConfig::CONFIG['bindir'] + "/gem"
end
@@ -40,13 +41,29 @@ class Chef
@gem_binary
end
+ def compile_time(arg=nil)
+ set_or_return(
+ :compile_time,
+ arg,
+ :kind_of => [ TrueClass, FalseClass ]
+ )
+ end
+
def after_created
# Chef::Resource.run_action: Caveat: this skips Chef::Runner.run_action, where notifications are handled
# Action could be an array of symbols, but probably won't (think install + enable for a package)
- Array(@action).each do |action|
- self.run_action(action)
+ if compile_time.nil?
+ Chef::Log.warn "The chef_gem installation at compile time is deprecated and this behavior will change in the future."
+ Chef::Log.warn "Please set `compile_time false` on the resource to use the new behavior and suppress this warning,"
+ Chef::Log.warn "or you may set `compile_time true` on the resource if compile_time behavior is necessary."
+ end
+
+ if compile_time || compile_time.nil?
+ Array(action).each do |action|
+ self.run_action(action)
+ end
+ Gem.clear_paths
end
- Gem.clear_paths
end
end
end
diff --git a/spec/unit/resource/chef_gem_spec.rb b/spec/unit/resource/chef_gem_spec.rb
index 480856d19f..657713d54f 100644
--- a/spec/unit/resource/chef_gem_spec.rb
+++ b/spec/unit/resource/chef_gem_spec.rb
@@ -32,16 +32,70 @@ describe Chef::Resource::ChefGem, "initialize" do
end
describe Chef::Resource::ChefGem, "gem_binary" do
+ let(:resource) { Chef::Resource::ChefGem.new("foo") }
+
before(:each) do
expect(RbConfig::CONFIG).to receive(:[]).with('bindir').and_return("/opt/chef/embedded/bin")
- @resource = Chef::Resource::ChefGem.new("foo")
end
it "should raise an exception when gem_binary is set" do
- expect { @resource.gem_binary("/lol/cats/gem") }.to raise_error(ArgumentError)
+ expect { resource.gem_binary("/lol/cats/gem") }.to raise_error(ArgumentError)
+ end
+
+ it "should set the gem_binary based on computing it from RbConfig" do
+ expect(resource.gem_binary).to eql("/opt/chef/embedded/bin/gem")
end
it "should set the gem_binary based on computing it from RbConfig" do
- expect(@resource.gem_binary).to eql("/opt/chef/embedded/bin/gem")
+ expect(resource.compile_time).to be nil
+ end
+
+ context "when building the resource" do
+ let(:node) do
+ Chef::Node.new.tap {|n| n.normal[:tags] = [] }
+ end
+
+ let(:run_context) do
+ Chef::RunContext.new(node, {}, nil)
+ end
+
+ let(:recipe) do
+ Chef::Recipe.new("hjk", "test", run_context)
+ end
+
+ let(:resource) { Chef::Resource::ChefGem.new("foo", run_context) }
+
+ before do
+ expect(Chef::Resource::ChefGem).to receive(:new).and_return(resource)
+ end
+
+ it "runs the install at compile-time by default", :chef_lt_13_only do
+ expect(resource).to receive(:run_action).with(:install)
+ expect(Chef::Log).to receive(:warn).at_least(:once)
+ recipe.chef_gem "foo"
+ end
+
+ # the default behavior will change in Chef-13
+ it "does not runs the install at compile-time by default", :chef_gte_13_only do
+ expect(resource).not_to receive(:run_action).with(:install)
+ expect(Chef::Log).not_to receive(:warn)
+ recipe.chef_gem "foo"
+ end
+
+ it "compile_time true installs at compile-time" do
+ expect(resource).to receive(:run_action).with(:install)
+ expect(Chef::Log).not_to receive(:warn)
+ recipe.chef_gem "foo" do
+ compile_time true
+ end
+ end
+
+ it "compile_time false does not install at compile-time" do
+ expect(resource).not_to receive(:run_action).with(:install)
+ expect(Chef::Log).not_to receive(:warn)
+ recipe.chef_gem "foo" do
+ compile_time false
+ end
+ end
end
end