summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2015-01-07 09:14:50 -0800
committerLamont Granquist <lamont@scriptkiddie.org>2015-01-07 09:14:50 -0800
commita13aa2378e121742f81772e1d1f5c11f92350aca (patch)
treea593ddc3a30b959b0f4ee41837d1b2ceeeb1e731
parent005f75e158ce702bb28fd6199e9654b3d1115d3b (diff)
downloadchef-lcg/chef-gem-compile-time-flag.tar.gz
add a compile_time flag to chef_gem resourcelcg/chef-gem-compile-time-flag
the default is still the same, but will warn that users should start being explicit. setting compile_time false will become the new default in chef-13 and is encouraged to avoid the compile_time arms race.
-rw-r--r--lib/chef/resource/chef_gem.rb23
-rw-r--r--spec/unit/resource/chef_gem_spec.rb60
2 files changed, 77 insertions, 6 deletions
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