summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPete Higgins <pete@peterhiggins.org>2020-03-11 18:07:04 -0700
committerPete Higgins <pete@peterhiggins.org>2020-03-23 13:55:01 -0700
commit1bc1c904749997ca8696c575f880d0f50a8c05a6 (patch)
tree0594488043bfdf0ef0c30c43fff0fa0a46c347d2
parent1f629d3a004e45dfd25d06a46f565d52ac192c18 (diff)
downloadchef-1bc1c904749997ca8696c575f880d0f50a8c05a6.tar.gz
Update gem_package to make clear_sources opt-out.
Signed-off-by: Pete Higgins <pete@peterhiggins.org>
-rw-r--r--lib/chef/provider/package/rubygems.rb2
-rw-r--r--lib/chef/resource/gem_package.rb8
-rw-r--r--spec/unit/provider/package/rubygems_spec.rb23
-rw-r--r--spec/unit/resource/gem_package_spec.rb40
4 files changed, 66 insertions, 7 deletions
diff --git a/lib/chef/provider/package/rubygems.rb b/lib/chef/provider/package/rubygems.rb
index 60df50ddd3..512baf6014 100644
--- a/lib/chef/provider/package/rubygems.rb
+++ b/lib/chef/provider/package/rubygems.rb
@@ -555,7 +555,7 @@ class Chef
if new_resource.source.is_a?(String) && new_resource.source =~ /\.gem$/i
name = new_resource.source
else
- src << "--clear-sources" if new_resource.clear_sources
+ src << "--clear-sources" if new_resource.clear_sources?
src += gem_sources.map { |s| "--source=#{s}" }
end
src_str = src.empty? ? "" : " #{src.join(" ")}"
diff --git a/lib/chef/resource/gem_package.rb b/lib/chef/resource/gem_package.rb
index 80eb257fa1..09ce39266e 100644
--- a/lib/chef/resource/gem_package.rb
+++ b/lib/chef/resource/gem_package.rb
@@ -55,6 +55,14 @@ class Chef
property :options, [ String, Hash, Array, nil ],
description: "Options for the gem install, either a Hash or a String. When a hash is given, the options are passed to Gem::DependencyInstaller.new, and the gem will be installed via the gems API. When a String is given, the gem will be installed by shelling out to the gem command. Using a Hash of options with an explicit gem_binary will result in undefined behavior.",
desired_state: false
+
+ def clear_sources?
+ if clear_sources.nil?
+ !!source
+ else
+ clear_sources
+ end
+ end
end
end
end
diff --git a/spec/unit/provider/package/rubygems_spec.rb b/spec/unit/provider/package/rubygems_spec.rb
index 1bafefe5e8..1ef76c05c3 100644
--- a/spec/unit/provider/package/rubygems_spec.rb
+++ b/spec/unit/provider/package/rubygems_spec.rb
@@ -743,7 +743,7 @@ describe Chef::Provider::Package::Rubygems do
let(:gem_binary) { "/foo/bar" }
it "installs the gem with rubygems.org as an added source" do
- expected = "#{gem_binary} install rspec-core -q --no-document -v \"#{target_version}\" --source=#{source} --source=https://www.rubygems.org"
+ expected = "#{gem_binary} install rspec-core -q --no-document -v \"#{target_version}\" --clear-sources --source=#{source} --source=https://www.rubygems.org"
expect(provider).to receive(:shell_out_compacted!).with(expected, env: nil, timeout: 900)
provider.run_action(:install)
expect(new_resource).to be_updated_by_last_action
@@ -754,7 +754,7 @@ describe Chef::Provider::Package::Rubygems do
it "ignores the Chef::Config setting" do
Chef::Config[:rubygems_url] = "https://ignored"
- expected = "#{gem_binary} install rspec-core -q --no-document -v \"#{target_version}\" --source=#{source}"
+ expected = "#{gem_binary} install rspec-core -q --no-document -v \"#{target_version}\" --clear-sources --source=#{source}"
expect(provider).to receive(:shell_out_compacted!).with(expected, env: nil, timeout: 900)
provider.run_action(:install)
expect(new_resource).to be_updated_by_last_action
@@ -767,7 +767,7 @@ describe Chef::Provider::Package::Rubygems do
let(:gem_binary) { "/foo/bar" }
it "installs the gem with an array as an added source" do
- expected = "#{gem_binary} install rspec-core -q --no-document -v \"#{target_version}\" --source=https://mirror1 --source=https://mirror2 --source=https://www.rubygems.org"
+ expected = "#{gem_binary} install rspec-core -q --no-document -v \"#{target_version}\" --clear-sources --source=https://mirror1 --source=https://mirror2 --source=https://www.rubygems.org"
expect(provider).to receive(:shell_out_compacted!).with(expected, env: nil, timeout: 900)
provider.run_action(:install)
expect(new_resource).to be_updated_by_last_action
@@ -778,7 +778,7 @@ describe Chef::Provider::Package::Rubygems do
it "ignores the Chef::Config setting" do
Chef::Config[:rubygems_url] = "https://ignored"
- expected = "#{gem_binary} install rspec-core -q --no-document -v \"#{target_version}\" --source=https://mirror1 --source=https://mirror2"
+ expected = "#{gem_binary} install rspec-core -q --no-document -v \"#{target_version}\" --clear-sources --source=https://mirror1 --source=https://mirror2"
expect(provider).to receive(:shell_out_compacted!).with(expected, env: nil, timeout: 900)
provider.run_action(:install)
expect(new_resource).to be_updated_by_last_action
@@ -786,7 +786,7 @@ describe Chef::Provider::Package::Rubygems do
end
end
- context "when we have cleared sources and an explict source is specified" do
+ context "when clear_sources is set true and an explict source is specified" do
let(:gem_binary) { "/foo/bar" }
let(:source) { "http://mirror.ops.rhcloud.com/mirror/ruby" }
@@ -799,6 +799,19 @@ describe Chef::Provider::Package::Rubygems do
end
end
+ context "when clear_sources is set false and an explict source is specified" do
+ let(:gem_binary) { "/foo/bar" }
+ let(:source) { "http://mirror.ops.rhcloud.com/mirror/ruby" }
+
+ it "installs the gem" do
+ new_resource.clear_sources(false)
+ expected = "#{gem_binary} install rspec-core -q --no-document -v \"#{target_version}\" --source=#{source} --source=https://www.rubygems.org"
+ expect(provider).to receive(:shell_out_compacted!).with(expected, env: nil, timeout: 900)
+ provider.run_action(:install)
+ expect(new_resource).to be_updated_by_last_action
+ end
+ end
+
context "when no version is given" do
let(:target_version) { nil }
let(:options) { "-i /alt/install/location" }
diff --git a/spec/unit/resource/gem_package_spec.rb b/spec/unit/resource/gem_package_spec.rb
index 949396abbc..5ecc2f46ca 100644
--- a/spec/unit/resource/gem_package_spec.rb
+++ b/spec/unit/resource/gem_package_spec.rb
@@ -53,7 +53,7 @@ describe Chef::Resource::GemPackage, "gem_binary" do
end
end
-describe Chef::Resource::GemPackage, "clear_gem_sources" do
+describe Chef::Resource::GemPackage, "clear_sources" do
let(:resource) { Chef::Resource::GemPackage.new("foo") }
it "is nil by default" do
@@ -65,3 +65,41 @@ describe Chef::Resource::GemPackage, "clear_gem_sources" do
expect(resource.clear_sources).to be true
end
end
+
+describe Chef::Resource::GemPackage, "clear_sources?" do
+ let(:resource) { Chef::Resource::GemPackage.new("foo") }
+
+ it "is false when clear_sources is unset" do
+ expect(resource.clear_sources?).to be false
+ end
+
+ it "is false when clear_sources is set false" do
+ resource.clear_sources(false)
+ expect(resource.clear_sources?).to be false
+ end
+
+ it "is true when clear_sources is set true" do
+ resource.clear_sources(true)
+ expect(resource.clear_sources?).to be true
+ end
+
+ context "when a source is set" do
+ before do
+ resource.source("http://mirror.ops.rhcloud.com/mirror/ruby")
+ end
+
+ it "is true when clear_sources is unset" do
+ expect(resource.clear_sources?).to be true
+ end
+
+ it "is false when clear_sources is set false" do
+ resource.clear_sources(false)
+ expect(resource.clear_sources?).to be false
+ end
+
+ it "is true when clear_sources is set true" do
+ resource.clear_sources(true)
+ expect(resource.clear_sources?).to be true
+ end
+ end
+end