diff options
author | Tim Smith <tsmith@chef.io> | 2020-03-27 13:55:16 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-27 13:55:16 -0700 |
commit | 2b2c2ef217181bb2833ccf0620258d1df21f7fcb (patch) | |
tree | 54d7b53d1086ee34160eadd44a8b9252e7426c4b | |
parent | b296966dfc75ebd6696851beb77b7410ec111091 (diff) | |
parent | dd9e9cd97682a528875c16d774048d6d48cd07f0 (diff) | |
download | chef-2b2c2ef217181bb2833ccf0620258d1df21f7fcb.tar.gz |
Merge pull request #9480 from chef/fix-clear-sources
Fix unintuitive behavior of sources on gem resources
-rw-r--r-- | chef-config/lib/chef-config/config.rb | 4 | ||||
-rw-r--r-- | lib/chef/cookbook/gem_installer.rb | 2 | ||||
-rw-r--r-- | lib/chef/provider/package/rubygems.rb | 31 | ||||
-rw-r--r-- | lib/chef/resource/gem_package.rb | 10 | ||||
-rw-r--r-- | spec/unit/cookbook/gem_installer_spec.rb | 4 | ||||
-rw-r--r-- | spec/unit/provider/package/rubygems_spec.rb | 237 | ||||
-rw-r--r-- | spec/unit/resource/gem_package_spec.rb | 6 |
7 files changed, 253 insertions, 41 deletions
diff --git a/chef-config/lib/chef-config/config.rb b/chef-config/lib/chef-config/config.rb index 1ee27949bc..9c8614559d 100644 --- a/chef-config/lib/chef-config/config.rb +++ b/chef-config/lib/chef-config/config.rb @@ -1219,10 +1219,10 @@ module ChefConfig default :ruby_encoding, Encoding::UTF_8 # can be set to a string or array of strings for URIs to set as rubygems sources - default :rubygems_url, "https://www.rubygems.org" + default :rubygems_url, nil # globally sets the default of the clear_sources property on the gem_package and chef_gem resources - default :clear_gem_sources, false + default :clear_gem_sources, nil # If installed via an omnibus installer, this gives the path to the # "embedded" directory which contains all of the software packaged with diff --git a/lib/chef/cookbook/gem_installer.rb b/lib/chef/cookbook/gem_installer.rb index cf0177d1d5..172c1e6448 100644 --- a/lib/chef/cookbook/gem_installer.rb +++ b/lib/chef/cookbook/gem_installer.rb @@ -57,7 +57,7 @@ class Chef begin Dir.mktmpdir("chef-gem-bundle") do |dir| File.open("#{dir}/Gemfile", "w+") do |tf| - Array(Chef::Config[:rubygems_url] || "https://www.rubygems.org").each do |s| + Array(Chef::Config[:rubygems_url] || "https://rubygems.org").each do |s| tf.puts "source '#{s}'" end cookbook_gems.each do |gem_name, args| diff --git a/lib/chef/provider/package/rubygems.rb b/lib/chef/provider/package/rubygems.rb index 60df50ddd3..fd93affd97 100644 --- a/lib/chef/provider/package/rubygems.rb +++ b/lib/chef/provider/package/rubygems.rb @@ -483,9 +483,23 @@ class Chef end end + ## + # If `include_default_source` is nil, return true if the global + # `rubygems_url` was set or if `clear_sources` and `source` on the + # resource are not set. + # If `include_default_source` is not nil, it has been set explicitly on + # the resource and that value should be used. + def include_default_source? + if new_resource.include_default_source.nil? + !!Chef::Config[:rubygems_url] || !(new_resource.source || new_resource.clear_sources) + else + new_resource.include_default_source + end + end + def gem_sources srcs = [ new_resource.source ] - srcs << Chef::Config[:rubygems_url] if new_resource.include_default_source + srcs << (Chef::Config[:rubygems_url] || "https://rubygems.org") if include_default_source? srcs.flatten.compact end @@ -550,12 +564,25 @@ class Chef new_resource.gem_binary || "gem" end + ## + # If `clear_sources` is nil, clearing sources is implied if a `source` + # was added or if the global rubygems URL is set. If `clear_sources` + # is not nil, it has been set explicitly on the resource and its value + # should be used. + def clear_sources? + if new_resource.clear_sources.nil? + !!(new_resource.source || Chef::Config[:rubygems_url]) + else + new_resource.clear_sources + end + end + def install_via_gem_command(name, version) src = [] 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 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 a8e16841dc..5cd83d25fd 100644 --- a/lib/chef/resource/gem_package.rb +++ b/lib/chef/resource/gem_package.rb @@ -30,7 +30,7 @@ class Chef # the source can either be a path to a package source like: # source /var/tmp/mygem-1.2.3.4.gem # or it can be a url rubygems source like: - # https://www.rubygems.org + # https://rubygems.org # the default has to be nil in order for the magical wiring up of the name property to # the source pathname to work correctly. # @@ -41,16 +41,16 @@ class Chef property :source, [ String, Array ], description: "Optional. The URL, or list of URLs, at which the gem package is located. This list is added to the source configured in Chef::Config[:rubygems_url] (see also include_default_source) to construct the complete list of rubygems sources. Users in an 'airgapped' environment should set Chef::Config[:rubygems_url] to their local RubyGems mirror." - property :clear_sources, [ TrueClass, FalseClass ], + property :clear_sources, [ TrueClass, FalseClass, nil ], description: "Set to 'true' to download a gem from the path specified by the source property (and not from RubyGems).", default: lazy { Chef::Config[:clear_gem_sources] }, desired_state: false property :gem_binary, String, desired_state: false, - description: "The path of a gem binary to use for the installation. By default, the same version of Ruby that is used by the #{Chef::Dist::CLIENT} will be installed." + description: "The path of a gem binary to use for the installation. By default, the same version of Ruby that is used by the #{Chef::Dist::CLIENT} will be installed." - property :include_default_source, [ TrueClass, FalseClass ], + property :include_default_source, [ TrueClass, FalseClass, nil ], description: "Set to 'false' to not include 'Chef::Config[:rubygems_url]'' in the sources.", - default: true, introduced: "13.0" + default: nil, introduced: "13.0" 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.", diff --git a/spec/unit/cookbook/gem_installer_spec.rb b/spec/unit/cookbook/gem_installer_spec.rb index 5cb995e33b..2733dfc862 100644 --- a/spec/unit/cookbook/gem_installer_spec.rb +++ b/spec/unit/cookbook/gem_installer_spec.rb @@ -78,7 +78,7 @@ describe Chef::Cookbook::GemInstaller do it "generates a valid Gemfile when Chef::Config[:rubygems_url] is set to a String" do expect(gem_installer).to receive(:shell_out!).and_return(shell_out) - Chef::Config[:rubygems_url] = "https://www.rubygems.org" + Chef::Config[:rubygems_url] = "https://rubygems.org" expect { gem_installer.install }.to_not raise_error expect(bundler_dsl.dependencies.find { |d| d.name == "httpclient" }.requirements_list.length).to eql(2) @@ -86,7 +86,7 @@ describe Chef::Cookbook::GemInstaller do it "generates a valid Gemfile when Chef::Config[:rubygems_url] is set to an Array" do expect(gem_installer).to receive(:shell_out!).and_return(shell_out) - Chef::Config[:rubygems_url] = [ "https://www.rubygems.org" ] + Chef::Config[:rubygems_url] = [ "https://rubygems.org" ] expect { gem_installer.install }.to_not raise_error diff --git a/spec/unit/provider/package/rubygems_spec.rb b/spec/unit/provider/package/rubygems_spec.rb index 1bafefe5e8..54b721ed40 100644 --- a/spec/unit/provider/package/rubygems_spec.rb +++ b/spec/unit/provider/package/rubygems_spec.rb @@ -363,7 +363,7 @@ describe Chef::Provider::Package::Rubygems do let(:bindir) { "/usr/bin" } let(:options) { nil } let(:source) { nil } - let(:include_default_source) { true } + let(:include_default_source) { nil } let(:new_resource) do new_resource = Chef::Resource::GemPackage.new(gem_name) @@ -585,7 +585,7 @@ describe Chef::Provider::Package::Rubygems do it "determines the candidate version by querying the remote gem servers" do expect(provider.gem_env).to receive(:candidate_version_from_remote) - .with(gem_dep, source, Chef::Config[:rubygems_url]) + .with(gem_dep, source) .and_return(Gem::Version.new(target_version)) expect(provider.candidate_version).to eq(target_version) end @@ -605,7 +605,7 @@ describe Chef::Provider::Package::Rubygems do it "determines the candidate version by querying the remote gem servers" do expect(provider.gem_env).to receive(:candidate_version_from_remote) - .with(gem_dep, *[source, Chef::Config[:rubygems_url] ].flatten) + .with(gem_dep, *source) .and_return(Gem::Version.new(target_version)) expect(provider.candidate_version).to eq(target_version) end @@ -645,13 +645,13 @@ describe Chef::Provider::Package::Rubygems do before do expected_source = [ source ] - expected_source << Chef::Config[:rubygems_url] if include_default_source + expected_source << "https://rubygems.org" if provider.include_default_source? allow(provider.gem_env).to receive(:candidate_version_from_remote).with(gem_dep, *expected_source.flatten.compact).and_return(version) end describe "in the current gem environment" do it "installs the gem via the gems api when no explicit options are used" do - expect(provider.gem_env).to receive(:install).with(gem_dep, sources: [ Chef::Config[:rubygems_url] ]) + expect(provider.gem_env).to receive(:install).with(gem_dep, sources: [ "https://rubygems.org" ]) provider.run_action(:install) expect(new_resource).to be_updated_by_last_action end @@ -660,7 +660,7 @@ describe Chef::Provider::Package::Rubygems do let(:source) { "http://gems.example.org" } it "installs the gem via the gems api" do - expect(provider.gem_env).to receive(:install).with(gem_dep, sources: [source, Chef::Config[:rubygems_url]]) + expect(provider.gem_env).to receive(:install).with(gem_dep, sources: [source]) provider.run_action(:install) expect(new_resource).to be_updated_by_last_action end @@ -691,7 +691,7 @@ describe Chef::Provider::Package::Rubygems do it "installs the gem via the gems api, when the package has no file separator characters in it, but a matching file exists in cwd" do allow(::File).to receive(:exist?).and_return(true) new_resource.package_name("rspec-core") - expect(provider.gem_env).to receive(:install).with(gem_dep, sources: [ Chef::Config[:rubygems_url] ]) + expect(provider.gem_env).to receive(:install).with(gem_dep, sources: [ "https://rubygems.org" ]) provider.run_action(:install) expect(new_resource).to be_updated_by_last_action end @@ -700,14 +700,14 @@ describe Chef::Provider::Package::Rubygems do let(:options) { "-i /alt/install/location" } it "installs the gem by shelling out when options are provided as a String" do - expected = "gem install rspec-core -q --no-document -v \"#{target_version}\" --source=https://www.rubygems.org #{options}" + expected = "gem install rspec-core -q --no-document -v \"#{target_version}\" --source=https://rubygems.org #{options}" 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 it "unmockening needs_nodocument?" do - expected = "gem install rspec-core -q --no-document -v \"#{target_version}\" --source=https://www.rubygems.org #{options}" + expected = "gem install rspec-core -q --no-document -v \"#{target_version}\" --source=https://rubygems.org #{options}" expect(provider).to receive(:needs_nodocument?).and_call_original stub_const("Gem::VERSION", "3.0.0") expect(provider).to receive(:shell_out_compacted!).with(expected, env: nil, timeout: 900) @@ -716,7 +716,7 @@ describe Chef::Provider::Package::Rubygems do end it "when the rubygems_version is old it uses the old flags" do - expected = "gem install rspec-core -q --no-rdoc --no-ri -v \"#{target_version}\" --source=https://www.rubygems.org #{options}" + expected = "gem install rspec-core -q --no-rdoc --no-ri -v \"#{target_version}\" --source=https://rubygems.org #{options}" expect(provider).to receive(:needs_nodocument?).and_call_original stub_const("Gem::VERSION", "2.8.0") expect(provider).to receive(:shell_out_compacted!).with(expected, env: nil, timeout: 900) @@ -728,10 +728,10 @@ describe Chef::Provider::Package::Rubygems do context "when the Chef::Config[:rubygems_url] option is provided" do let(:gem_binary) { "/foo/bar" } - it "installs the gem with rubygems.org as an added source" do + it "installs the gem" do Chef::Config[:rubygems_url] = "https://mirror1" - expect(provider.gem_env).to receive(:candidate_version_from_remote).with(gem_dep, Chef::Config[:rubygems_url]).and_return(version) - expected = "#{gem_binary} install rspec-core -q --no-document -v \"#{target_version}\" --source=https://mirror1" + expect(provider.gem_env).to receive(:candidate_version_from_remote).with(gem_dep, "https://mirror1").and_return(version) + expected = "#{gem_binary} install rspec-core -q --no-document -v \"#{target_version}\" --clear-sources --source=https://mirror1" 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 @@ -742,19 +742,30 @@ describe Chef::Provider::Package::Rubygems do let(:source) { "http://mirror.ops.rhcloud.com/mirror/ruby" } 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" + it "installs the gem" do + 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 end + context "with include_default_source true" do + let(:include_default_source) { true } + + it "ignores the Chef::Config setting" do + expected = "#{gem_binary} install rspec-core -q --no-document -v \"#{target_version}\" --clear-sources --source=#{source} --source=https://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 "with include_default_source false" do let(:include_default_source) { false } 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,18 +778,29 @@ 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" 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 + context "with include_default_source true" do + let(:include_default_source) { true } + + it "installs the gem with rubygems as a source" do + expected = "#{gem_binary} install rspec-core -q --no-document -v \"#{target_version}\" --clear-sources --source=https://mirror1 --source=https://mirror2 --source=https://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 "with include_default_source false" do let(:include_default_source) { false } 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,13 +808,26 @@ 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" } it "installs the gem" do new_resource.clear_sources(true) - expected = "#{gem_binary} install rspec-core -q --no-document -v \"#{target_version}\" --clear-sources --source=#{source} --source=https://www.rubygems.org" + 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 + 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}" 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 @@ -804,7 +839,7 @@ describe Chef::Provider::Package::Rubygems do let(:options) { "-i /alt/install/location" } it "installs the gem by shelling out when options are provided but no version is given" do - expected = "gem install rspec-core -q --no-document -v \"#{candidate_version}\" --source=https://www.rubygems.org #{options}" + expected = "gem install rspec-core -q --no-document -v \"#{candidate_version}\" --source=https://rubygems.org #{options}" 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 @@ -815,7 +850,7 @@ describe Chef::Provider::Package::Rubygems do let(:options) { { install_dir: "/alt/install/location" } } it "installs the gem via the gems api when options are given as a Hash" do - expect(provider.gem_env).to receive(:install).with(gem_dep, { sources: [ Chef::Config[:rubygems_url] ] }.merge(options)) + expect(provider.gem_env).to receive(:install).with(gem_dep, { sources: [ "https://rubygems.org" ] }.merge(options)) provider.run_action(:install) expect(new_resource).to be_updated_by_last_action end @@ -825,7 +860,7 @@ describe Chef::Provider::Package::Rubygems do let(:target_version) { "9000.0.2" } it "installs the gem via the gems api" do - expect(provider.gem_env).to receive(:install).with(gem_dep, sources: [ Chef::Config[:rubygems_url] ] ) + expect(provider.gem_env).to receive(:install).with(gem_dep, sources: [ "https://rubygems.org" ] ) provider.run_action(:install) expect(new_resource).to be_updated_by_last_action end @@ -868,7 +903,7 @@ describe Chef::Provider::Package::Rubygems do let(:gem_binary) { "/usr/weird/bin/gem" } it "installs the gem by shelling out to gem install" do - expect(provider).to receive(:shell_out_compacted!).with("#{gem_binary} install rspec-core -q --no-document -v \"#{target_version}\" --source=https://www.rubygems.org", env: nil, timeout: 900) + expect(provider).to receive(:shell_out_compacted!).with("#{gem_binary} install rspec-core -q --no-document -v \"#{target_version}\" --source=https://rubygems.org", env: nil, timeout: 900) provider.run_action(:install) expect(new_resource).to be_updated_by_last_action end @@ -876,7 +911,7 @@ describe Chef::Provider::Package::Rubygems do it "unmockening needs_nodocument?" do expect(provider).to receive(:needs_nodocument?).and_call_original expect(provider.gem_env).to receive(:shell_out!).with("#{gem_binary} --version").and_return(instance_double(Mixlib::ShellOut, stdout: "3.0.0\n")) - expect(provider).to receive(:shell_out_compacted!).with("#{gem_binary} install rspec-core -q --no-document -v \"#{target_version}\" --source=https://www.rubygems.org", env: nil, timeout: 900) + expect(provider).to receive(:shell_out_compacted!).with("#{gem_binary} install rspec-core -q --no-document -v \"#{target_version}\" --source=https://rubygems.org", env: nil, timeout: 900) provider.run_action(:install) expect(new_resource).to be_updated_by_last_action end @@ -884,7 +919,7 @@ describe Chef::Provider::Package::Rubygems do it "when the rubygems_version is old it uses the old flags" do expect(provider).to receive(:needs_nodocument?).and_call_original expect(provider.gem_env).to receive(:shell_out!).with("#{gem_binary} --version").and_return(instance_double(Mixlib::ShellOut, stdout: "2.8.0\n")) - expect(provider).to receive(:shell_out_compacted!).with("#{gem_binary} install rspec-core -q --no-rdoc --no-ri -v \"#{target_version}\" --source=https://www.rubygems.org", env: nil, timeout: 900) + expect(provider).to receive(:shell_out_compacted!).with("#{gem_binary} install rspec-core -q --no-rdoc --no-ri -v \"#{target_version}\" --source=https://rubygems.org", env: nil, timeout: 900) provider.run_action(:install) expect(new_resource).to be_updated_by_last_action end @@ -979,3 +1014,153 @@ describe Chef::Provider::Package::Rubygems do end end end + +describe Chef::Provider::Package::Rubygems, "clear_sources?" do + let(:new_resource) do + Chef::Resource::GemPackage.new("foo") + end + + let(:provider) do + run_context = Chef::RunContext.new(Chef::Node.new, {}, Chef::EventDispatch::Dispatcher.new) + Chef::Provider::Package::Rubygems.new(new_resource, run_context) + end + + it "is false when clear_sources is unset" do + expect(provider.clear_sources?).to be false + end + + it "is false when clear_sources is set false" do + new_resource.clear_sources(false) + expect(provider.clear_sources?).to be false + end + + it "is true when clear_sources is set true" do + new_resource.clear_sources(true) + expect(provider.clear_sources?).to be true + end + + context "when a source is set" do + before do + new_resource.source("http://mirror.ops.rhcloud.com/mirror/ruby") + end + + it "is true when clear_sources is unset" do + expect(provider.clear_sources?).to be true + end + + it "is false when clear_sources is set false" do + new_resource.clear_sources(false) + expect(provider.clear_sources?).to be false + end + + it "is true when clear_sources is set true" do + new_resource.clear_sources(true) + expect(provider.clear_sources?).to be true + end + end + + context "when Chef::Config[:rubygems_url] is set" do + before do + Chef::Config.rubygems_url = "https://example.com/" + end + + it "is true when clear_sources is unset" do + expect(provider.clear_sources?).to be true + end + + it "is false when clear_sources is set false" do + new_resource.clear_sources(false) + expect(provider.clear_sources?).to be false + end + + it "is true when clear_sources is set true" do + new_resource.clear_sources(true) + expect(provider.clear_sources?).to be true + end + end +end + +describe Chef::Provider::Package::Rubygems, "include_default_source?" do + let(:new_resource) do + Chef::Resource::GemPackage.new("foo") + end + + let(:provider) do + run_context = Chef::RunContext.new(Chef::Node.new, {}, Chef::EventDispatch::Dispatcher.new) + Chef::Provider::Package::Rubygems.new(new_resource, run_context) + end + + it "is true when include_default_source is unset" do + expect(provider.include_default_source?).to be true + end + + it "is false when include_default_source is set false" do + new_resource.include_default_source(false) + expect(provider.include_default_source?).to be false + end + + it "is true when include_default_source is set true" do + new_resource.include_default_source(true) + expect(provider.include_default_source?).to be true + end + + context "when a source is set" do + before do + new_resource.source("http://mirror.ops.rhcloud.com/mirror/ruby") + end + + it "is false when include_default_source is unset" do + expect(provider.include_default_source?).to be false + end + + it "is false when include_default_source is set false" do + new_resource.include_default_source(false) + expect(provider.include_default_source?).to be false + end + + it "is true when include_default_source is set true" do + new_resource.include_default_source(true) + expect(provider.include_default_source?).to be true + end + end + + context "when Chef::Config[:rubygems_url] is set" do + before do + Chef::Config.rubygems_url = "https://example.com/" + end + + it "is true when include_default_source is unset" do + expect(provider.include_default_source?).to be true + end + + it "is false when include_default_source is set false" do + new_resource.include_default_source(false) + expect(provider.include_default_source?).to be false + end + + it "is true when include_default_source is set true" do + new_resource.include_default_source(true) + expect(provider.include_default_source?).to be true + end + end + + context "when clear_sources is set" do + before do + new_resource.clear_sources(true) + end + + it "is false when include_default_source is unset" do + expect(provider.include_default_source?).to be false + end + + it "is false when include_default_source is set false" do + new_resource.include_default_source(false) + expect(provider.include_default_source?).to be false + end + + it "is true when include_default_source is set true" do + new_resource.include_default_source(true) + expect(provider.include_default_source?).to be true + end + end +end diff --git a/spec/unit/resource/gem_package_spec.rb b/spec/unit/resource/gem_package_spec.rb index aba6ab1032..b572fd0d40 100644 --- a/spec/unit/resource/gem_package_spec.rb +++ b/spec/unit/resource/gem_package_spec.rb @@ -53,11 +53,11 @@ 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 false by default" do - expect(resource.clear_sources).to be false + it "is nil by default" do + expect(resource.clear_sources).to be_nil end it "sets the default of clear_sources to the config value" do |