diff options
author | Kartik Null Cating-Subramanian <ksubramanian@chef.io> | 2016-02-04 12:22:11 -0500 |
---|---|---|
committer | Kartik Null Cating-Subramanian <ksubramanian@chef.io> | 2016-02-04 12:52:29 -0500 |
commit | cece7b1513904d9ecc0653a7c144848239c9fd1b (patch) | |
tree | c7e8a8877425bb90cd5e40469eb904bd94480eed /spec | |
parent | 8045115ca8137bcd85a362776c2b623fe8e7a713 (diff) | |
download | chef-cece7b1513904d9ecc0653a7c144848239c9fd1b.tar.gz |
Refactor rubygems_spec test and make it fastjdm/rubygems-test
Diffstat (limited to 'spec')
-rw-r--r-- | spec/unit/provider/package/rubygems_spec.rb | 566 |
1 files changed, 312 insertions, 254 deletions
diff --git a/spec/unit/provider/package/rubygems_spec.rb b/spec/unit/provider/package/rubygems_spec.rb index 79b2afe3c1..7f81656eb3 100644 --- a/spec/unit/provider/package/rubygems_spec.rb +++ b/spec/unit/provider/package/rubygems_spec.rb @@ -327,396 +327,454 @@ end describe Chef::Provider::Package::Rubygems do let(:target_version) { nil } + let(:gem_name) { "rspec-core" } + let(:gem_binary) { nil } + let(:bindir) { "/usr/bin/ruby" } + let(:options) { nil } + let(:source) { nil } + + let(:new_resource) do + new_resource = Chef::Resource::GemPackage.new(gem_name) + new_resource.version(target_version) + new_resource.gem_binary(gem_binary) if gem_binary + new_resource.options(options) if options + new_resource.source(source) if source + new_resource + end - before(:each) do - @node = Chef::Node.new - @new_resource = Chef::Resource::GemPackage.new("rspec-core") - @spec_version = @new_resource.version(target_version) - @events = Chef::EventDispatch::Dispatcher.new - @run_context = Chef::RunContext.new(@node, {}, @events) + let (:current_resource) { nil } + + let(:provider) do + run_context = Chef::RunContext.new(Chef::Node.new, {}, Chef::EventDispatch::Dispatcher.new) + provider = Chef::Provider::Package::Rubygems.new(new_resource, run_context) + if current_resource + allow(provider).to receive(:load_current_resource) + provider.current_resource = current_resource + end + provider + end + + let(:gem_dep) { Gem::Dependency.new(gem_name, target_version) } + before(:each) do # We choose detect omnibus via RbConfig::CONFIG['bindir'] in Chef::Provider::Package::Rubygems.new - allow(RbConfig::CONFIG).to receive(:[]).with("bindir").and_return("/usr/bin/ruby") + allow(RbConfig::CONFIG).to receive(:[]).with("bindir").and_return(bindir) # Rubygems uses this interally allow(RbConfig::CONFIG).to receive(:[]).with("arch").and_call_original - @provider = Chef::Provider::Package::Rubygems.new(@new_resource, @run_context) end describe "when new_resource version is nil" do let(:target_version) { nil } it "target_version_already_installed? should return false so that we can search for candidates" do - @provider.load_current_resource - expect(@provider.target_version_already_installed?(@provider.current_resource.version, @new_resource.version)).to be_falsey + provider.load_current_resource + expect(provider.target_version_already_installed?(provider.current_resource.version, new_resource.version)).to be_falsey end end - describe "when new_resource version is current rspec version" do - let(:target_version) { RSpec::Core::Version::STRING } + describe "when new_resource version is an rspec version" do + let(:current_version) { RSpec::Core::Version::STRING } + let(:target_version) { current_version } it "triggers a gem configuration load so a later one will not stomp its config values" do + _ = provider # ugly, is there a better way? expect(Gem.instance_variable_get(:@configuration)).not_to be_nil end it "uses the CurrentGemEnvironment implementation when no gem_binary_path is provided" do - expect(@provider.gem_env).to be_a_kind_of(Chef::Provider::Package::Rubygems::CurrentGemEnvironment) + expect(provider.gem_env).to be_a_kind_of(Chef::Provider::Package::Rubygems::CurrentGemEnvironment) end - it "uses the AlternateGemEnvironment implementation when a gem_binary_path is provided" do - @new_resource.gem_binary("/usr/weird/bin/gem") - provider = Chef::Provider::Package::Rubygems.new(@new_resource, @run_context) - expect(provider.gem_env.gem_binary_location).to eq("/usr/weird/bin/gem") - end + context "when a gem_binary_path is provided" do + let(:gem_binary) { "/usr/weird/bin/gem" } - it "recognizes chef as omnibus" do - allow(RbConfig::CONFIG).to receive(:[]).with("bindir").and_return("/opt/chef/embedded/bin") - provider = Chef::Provider::Package::Rubygems.new(@new_resource, @run_context) - expect(provider.is_omnibus?).to be true - end + it "uses the AlternateGemEnvironment implementation when a gem_binary_path is provided" do + expect(provider.gem_env.gem_binary_location).to eq(gem_binary) + end - it "recognizes opscode as omnibus" do - allow(RbConfig::CONFIG).to receive(:[]).with("bindir").and_return("/opt/opscode/embedded/bin") - provider = Chef::Provider::Package::Rubygems.new(@new_resource, @run_context) - expect(provider.is_omnibus?).to be true - end + context "when you try to use a hash of install options" do + let(:options) { {:fail => :burger } } - it "recognizes chefdk as omnibus" do - allow(RbConfig::CONFIG).to receive(:[]).with("bindir").and_return("/opt/chefdk/embedded/bin") - provider = Chef::Provider::Package::Rubygems.new(@new_resource, @run_context) - expect(provider.is_omnibus?).to be true + it "smites you" do + expect {provider}.to raise_error(ArgumentError) + end + end end - it "searches for a gem binary when running on Omnibus on Unix" do - platform_mock :unix do - allow(RbConfig::CONFIG).to receive(:[]).with("bindir").and_return("/opt/chef/embedded/bin") - allow(ENV).to receive(:[]).with("PATH").and_return("/usr/bin:/usr/sbin:/opt/chef/embedded/bin") - allow(File).to receive(:exists?).with("/usr/bin/gem").and_return(false) - allow(File).to receive(:exists?).with("/usr/sbin/gem").and_return(true) - allow(File).to receive(:exists?).with("/opt/chef/embedded/bin/gem").and_return(true) # should not get here - provider = Chef::Provider::Package::Rubygems.new(@new_resource, @run_context) - expect(provider.gem_env.gem_binary_location).to eq("/usr/sbin/gem") + context "when in omnibus opscode" do + let(:bindir) { "/opt/opscode/embedded/bin" } + + it "recognizes opscode as omnibus" do + expect(provider.is_omnibus?).to be true end end - it "searches for a gem binary when running on Omnibus on Windows" do - platform_mock :windows do - allow(RbConfig::CONFIG).to receive(:[]).with("bindir").and_return("d:/opscode/chef/embedded/bin") - allow(ENV).to receive(:[]).with("PATH").and_return('C:\windows\system32;C:\windows;C:\Ruby186\bin;d:\opscode\chef\embedded\bin') - allow(File).to receive(:exists?).with('C:\\windows\\system32\\gem').and_return(false) - allow(File).to receive(:exists?).with('C:\\windows\\gem').and_return(false) - allow(File).to receive(:exists?).with('C:\\Ruby186\\bin\\gem').and_return(true) - allow(File).to receive(:exists?).with('d:\\opscode\\chef\\bin\\gem').and_return(false) # should not get here - allow(File).to receive(:exists?).with('d:\\opscode\\chef\\embedded\\bin\\gem').and_return(false) # should not get here - provider = Chef::Provider::Package::Rubygems.new(@new_resource, @run_context) - expect(provider.gem_env.gem_binary_location).to eq('C:\Ruby186\bin\gem') + context "when in omnibus chefdk" do + let(:bindir) { "/opt/chefdk/embedded/bin" } + + it "recognizes chefdk as omnibus" do + expect(provider.is_omnibus?).to be true end end - it "smites you when you try to use a hash of install options with an explicit gem binary" do - @new_resource.gem_binary("/foo/bar") - @new_resource.options(:fail => :burger) - expect {Chef::Provider::Package::Rubygems.new(@new_resource, @run_context)}.to raise_error(ArgumentError) + context "when in omnibus chef" do + let(:bindir) { "/opt/chef/embedded/bin" } + + it "recognizes chef as omnibus" do + expect(provider.is_omnibus?).to be true + end + + it "searches for a gem binary when running on Omnibus on Unix" do + platform_mock :unix do + allow(ENV).to receive(:[]).with("PATH").and_return("/usr/bin:/usr/sbin:/opt/chef/embedded/bin") + allow(File).to receive(:exists?).with("/usr/bin/gem").and_return(false) + allow(File).to receive(:exists?).with("/usr/sbin/gem").and_return(true) + allow(File).to receive(:exists?).with("/opt/chef/embedded/bin/gem").and_return(true) # should not get here + expect(provider.gem_env.gem_binary_location).to eq("/usr/sbin/gem") + end + end + + context "when on Windows" do + let(:bindir) { "d:/opscode/chef/embedded/bin" } + + it "searches for a gem binary when running on Omnibus on Windows" do + platform_mock :windows do + allow(ENV).to receive(:[]).with("PATH").and_return('C:\windows\system32;C:\windows;C:\Ruby186\bin;d:\opscode\chef\embedded\bin') + allow(File).to receive(:exists?).with('C:\\windows\\system32\\gem').and_return(false) + allow(File).to receive(:exists?).with('C:\\windows\\gem').and_return(false) + allow(File).to receive(:exists?).with('C:\\Ruby186\\bin\\gem').and_return(true) + allow(File).to receive(:exists?).with('d:\\opscode\\chef\\bin\\gem').and_return(false) # should not get here + allow(File).to receive(:exists?).with('d:\\opscode\\chef\\embedded\\bin\\gem').and_return(false) # should not get here + expect(provider.gem_env.gem_binary_location).to eq('C:\Ruby186\bin\gem') + end + end + end end it "converts the new resource into a gem dependency" do - expect(@provider.gem_dependency).to eq(Gem::Dependency.new("rspec-core", @spec_version)) - @new_resource.version("~> 1.2.0") - expect(@provider.gem_dependency).to eq(Gem::Dependency.new("rspec-core", "~> 1.2.0")) + expect(provider.gem_dependency).to eq(gem_dep) + end + + context "when the new resource is not the current version" do + let(:target_version) { "~> 9000.0.2" } + + it "converts the new resource into a gem dependency" do + expect(provider.gem_dependency).to eq(gem_dep) + end end describe "when determining the currently installed version" do + before do + provider.load_current_resource + end it "sets the current version to the version specified by the new resource if that version is installed" do - @provider.load_current_resource - expect(@provider.current_resource.version).to eq(@spec_version) + expect(provider.current_resource.version).to eq(current_version) end - it "sets the current version to the highest installed version if the requested version is not installed" do - @new_resource.version("9000.0.2") - @provider.load_current_resource - expect(@provider.current_resource.version).to eq(@spec_version) + context "if the requested version is not installed" do + let(:target_version) { "9000.0.2" } + + it "sets the current version to the highest installed version if the requested version is not installed" do + expect(provider.current_resource.version).to eq(current_version) + end end - it "leaves the current version at nil if the package is not installed" do - @new_resource.package_name("no-such-gem-should-exist-with-this-name") - @provider.load_current_resource - expect(@provider.current_resource.version).to be_nil + context "if the package is not currently installed" do + let(:gem_name) { "no-such-gem-should-exist-with-this-name" } + + it "leaves the current version at nil" do + expect(provider.current_resource.version).to be_nil + end end end describe "when determining the candidate version to install" do + before do + provider.load_current_resource + end it "does not query for available versions when the current version is the target version" do - @provider.current_resource = @new_resource.dup - expect(@provider.candidate_version).to be_nil + expect(provider.candidate_version).to be_nil end - it "determines the candidate version by querying the remote gem servers" do - @new_resource.source("http://mygems.example.com") - @provider.load_current_resource - @provider.current_resource.version("0.0.1") - version = Gem::Version.new(@spec_version) - expect(@provider.gem_env).to receive(:candidate_version_from_remote). - with(Gem::Dependency.new("rspec-core", @spec_version), "http://mygems.example.com"). - and_return(version) - expect(@provider.candidate_version).to eq(@spec_version) + context "when the current version is not the target version" do + let(:target_version) { "9000.0.2" } + let(:source) { "http://mygems.example.com" } + + 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). + and_return(Gem::Version.new(target_version)) + expect(provider.candidate_version).to eq(target_version) + end end - it "parses the gem's specification if the requested source is a file" do - @new_resource.package_name("chef-integration-test") - @new_resource.source(CHEF_SPEC_DATA + "/gems/chef-integration-test-0.1.0.gem") - @new_resource.version(">= 0") - @provider.load_current_resource - expect(@provider.candidate_version).to eq("0.1.0") + context "when the requested source is a file" do + let (:gem_name) { "chef-integration-test" } + let (:source) { CHEF_SPEC_DATA + "/gems/chef-integration-test-0.1.0.gem" } + let (:target_version) { ">= 0" } + + it "parses the gem's specification" do + expect(provider.candidate_version).to eq("0.1.0") + end end end describe "when installing a gem" do + let(:target_version) { "9000.0.2" } + let(:current_version) { nil } + let(:candidate_version) { "9000.0.2" } + let(:current_resource) do + current_resource = Chef::Resource::GemPackage.new(gem_name) + current_resource.version(current_version) + current_resource + end + before do - @current_resource = Chef::Resource::GemPackage.new("rspec-core") - @provider.current_resource = @current_resource - @gem_dep = Gem::Dependency.new("rspec-core", @spec_version) - allow(@provider).to receive(:load_current_resource) - version = Gem::Version.new(@spec_version) - allow(@provider.gem_env).to receive(:candidate_version_from_remote). - with(@gem_dep). + version = Gem::Version.new(candidate_version) + args = [gem_dep] + args << source if source + allow(provider.gem_env).to receive(:candidate_version_from_remote). + with(*args). 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 - version = Gem::Version.new(@spec_version) - expect(@provider.gem_env).to receive(:candidate_version_from_remote). - with(Gem::Dependency.new("rspec-core", @spec_version)). - and_return(version) - - expect(@provider.gem_env).to receive(:install).with(@gem_dep, :sources => nil) - @provider.run_action(:install) - expect(@new_resource).to be_updated_by_last_action + expect(provider.gem_env).to receive(:install).with(gem_dep, :sources => nil) + provider.run_action(:install) + expect(new_resource).to be_updated_by_last_action end - it "installs the gem via the gems api when a remote source is provided" do - version = Gem::Version.new(@spec_version) - expect(@provider.gem_env).to receive(:candidate_version_from_remote). - with(@gem_dep, "http://gems.example.org"). - and_return(version) + context "when a remote source is provided" do + let(:source) { "http://gems.example.org" } - @new_resource.source("http://gems.example.org") - sources = ["http://gems.example.org"] - expect(@provider.gem_env).to receive(:install).with(@gem_dep, :sources => sources) - @provider.run_action(:install) - expect(@new_resource).to be_updated_by_last_action + it "installs the gem via the gems api" do + 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 end - it "installs the gem from file via the gems api when no explicit options are used" do - @new_resource.source(CHEF_SPEC_DATA + "/gems/chef-integration-test-0.1.0.gem") - expect(@provider.gem_env).to receive(:install).with(CHEF_SPEC_DATA + "/gems/chef-integration-test-0.1.0.gem") - @provider.run_action(:install) - expect(@new_resource).to be_updated_by_last_action + context "when source is a path" do + let(:source) { CHEF_SPEC_DATA + "/gems/chef-integration-test-0.1.0.gem" } + + it "installs the gem from file via the gems api" do + expect(provider.gem_env).to receive(:install).with(source) + provider.run_action(:install) + expect(new_resource).to be_updated_by_last_action + end end - it "installs the gem from file via the gems api when the package is a path and the source is nil" do - @new_resource = Chef::Resource::GemPackage.new(CHEF_SPEC_DATA + "/gems/chef-integration-test-0.1.0.gem") - @provider = Chef::Provider::Package::Rubygems.new(@new_resource, @run_context) - @provider.current_resource = @current_resource - expect(@new_resource.source).to eq(CHEF_SPEC_DATA + "/gems/chef-integration-test-0.1.0.gem") - expect(@provider.gem_env).to receive(:install).with(CHEF_SPEC_DATA + "/gems/chef-integration-test-0.1.0.gem") - @provider.run_action(:install) - expect(@new_resource).to be_updated_by_last_action + context "when the gem name is a file path and source is nil" do + let(:gem_name) { CHEF_SPEC_DATA + "/gems/chef-integration-test-0.1.0.gem" } + + it "installs the gem from file via the gems api" do + expect(new_resource.source).to eq(gem_name) + expect(provider.gem_env).to receive(:install).with(gem_name) + provider.run_action(:install) + expect(new_resource).to be_updated_by_last_action + end end # this catches 'gem_package "foo"' when "./foo" is a file in the cwd, and instead of installing './foo' it fetches the remote gem 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 - version = Gem::Version.new(@spec_version) - expect(@provider.gem_env).to receive(:candidate_version_from_remote). - with(@gem_dep). - and_return(version) - allow(::File).to receive(:exists?).and_return(true) - @new_resource.package_name("rspec-core") - expect(@provider.gem_env).to receive(:install).with(@gem_dep, :sources => nil) - @provider.run_action(:install) - expect(@new_resource).to be_updated_by_last_action + new_resource.package_name("rspec-core") + expect(provider.gem_env).to receive(:install).with(gem_dep, :sources => nil) + provider.run_action(:install) + expect(new_resource).to be_updated_by_last_action end - it "installs the gem by shelling out when options are provided as a String" do - version = Gem::Version.new(@spec_version) - expect(@provider.gem_env).to receive(:candidate_version_from_remote). - with(@gem_dep). - and_return(version) + context "when options are provided as a String" do + let(:options) { "-i /alt/install/location" } - @new_resource.options("-i /alt/install/location") - expected ="gem install rspec-core -q --no-rdoc --no-ri -v \"#{@spec_version}\" -i /alt/install/location" - expect(@provider).to receive(:shell_out!).with(expected, env: nil, timeout: 900) - @provider.run_action(:install) - expect(@new_resource).to be_updated_by_last_action + it "installs the gem by shelling out when options are provided as a String" do + expected ="gem install rspec-core -q --no-rdoc --no-ri -v \"#{target_version}\" #{options}" + expect(provider).to receive(:shell_out!).with(expected, env: nil, timeout: 900) + provider.run_action(:install) + expect(new_resource).to be_updated_by_last_action + end end - it "installs the gem with rubygems.org as an added source" do - version = Gem::Version.new(@spec_version) - expect(@provider.gem_env).to receive(:candidate_version_from_remote). - with(@gem_dep, "http://mirror.ops.rhcloud.com/mirror/ruby"). - and_return(version) + context "when another source and binary are provided" do + let(:source) { "http://mirror.ops.rhcloud.com/mirror/ruby" } + let(:gem_binary) { "/foo/bar" } - @new_resource.gem_binary("/foo/bar") - @new_resource.source("http://mirror.ops.rhcloud.com/mirror/ruby") - expected ="/foo/bar install rspec-core -q --no-rdoc --no-ri -v \"#{@spec_version}\" --source=#{@new_resource.source} --source=https://rubygems.org" - expect(@provider).to receive(:shell_out!).with(expected, env: nil, timeout: 900) - @provider.run_action(:install) - expect(@new_resource).to be_updated_by_last_action + it "installs the gem with rubygems.org as an added source" do + expected ="#{gem_binary} install rspec-core -q --no-rdoc --no-ri -v \"#{target_version}\" --source=#{source} --source=https://rubygems.org" + expect(provider).to receive(:shell_out!).with(expected, env: nil, timeout: 900) + provider.run_action(:install) + expect(new_resource).to be_updated_by_last_action + end end - it "installs the gem with cleared sources and explict source when specified" do - version = Gem::Version.new(@spec_version) - expect(@provider.gem_env).to receive(:candidate_version_from_remote). - with(@gem_dep, "http://mirror.ops.rhcloud.com/mirror/ruby"). - and_return(version) + context "when we have cleared sources and an explict source is specified" do + let(:gem_binary) { "/foo/bar" } + let(:source) { "http://mirror.ops.rhcloud.com/mirror/ruby" } - @new_resource.gem_binary("/foo/bar") - @new_resource.source("http://mirror.ops.rhcloud.com/mirror/ruby") - @new_resource.clear_sources(true) - expected ="/foo/bar install rspec-core -q --no-rdoc --no-ri -v \"#{@spec_version}\" --clear-sources --source=#{@new_resource.source}" - expect(@provider).to receive(:shell_out!).with(expected, env: nil, timeout: 900) - @provider.run_action(:install) - expect(@new_resource).to be_updated_by_last_action + it "installs the gem" do + new_resource.clear_sources(true) + expected ="#{gem_binary} install rspec-core -q --no-rdoc --no-ri -v \"#{target_version}\" --clear-sources --source=#{source}" + expect(provider).to receive(:shell_out!).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 } - - #it "installs the gem by shelling out when options are provided but no version is given" do - #@new_resource.options("-i /alt/install/location") - #expected ="gem install \"rspec-core\" -q --no-rdoc --no-ri -v \"#{@provider.candidate_version}\" -i /alt/install/location" - #expect(@provider).to receive(:shell_out!).with(expected, env: nil, timeout: 900) - #@provider.run_action(:install) - #expect(@new_resource).to be_updated_by_last_action - #end - #end - - it "installs the gem via the gems api when options are given as a Hash" do - version = Gem::Version.new(@spec_version) - expect(@provider.gem_env).to receive(:candidate_version_from_remote). - with(@gem_dep). - and_return(version) + context "when no version is given" do + let(:target_version) { nil } + let(:options) { "-i /alt/install/location" } - @new_resource.options(:install_dir => "/alt/install/location") - expect(@provider.gem_env).to receive(:install).with(@gem_dep, :sources => nil, :install_dir => "/alt/install/location") - @provider.run_action(:install) - expect(@new_resource).to be_updated_by_last_action + it "installs the gem by shelling out when options are provided but no version is given" do + expected ="gem install rspec-core -q --no-rdoc --no-ri -v \"#{candidate_version}\" #{options}" + expect(provider).to receive(:shell_out!).with(expected, env: nil, timeout: 900) + provider.run_action(:install) + expect(new_resource).to be_updated_by_last_action + end end - describe "at a specific version" do - before do - @gem_dep = Gem::Dependency.new("rspec-core", @spec_version) + context "when options are given as a Hash" 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 => nil }.merge(options)) + provider.run_action(:install) + expect(new_resource).to be_updated_by_last_action end + end + + describe "at a specific version" 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 => nil) - @provider.run_action(:install) - expect(@new_resource).to be_updated_by_last_action + expect(provider.gem_env).to receive(:install).with(gem_dep, :sources => nil) + provider.run_action(:install) + expect(new_resource).to be_updated_by_last_action end end - describe "at version specified with comparison operator" do - it "skips install if current version satisifies requested version" do - @current_resource.version("2.3.3") - @new_resource.version(">=2.3.0") - expect(@provider.gem_env).not_to receive(:install) - @provider.run_action(:install) + describe "at version specified with comparison operator" do + context "if current version satisifies requested version" do + let(:target_version) { ">=2.3.0" } + let(:current_version) { "2.3.3" } + + it "skips the install" do + expect(provider.gem_env).not_to receive(:install) + provider.run_action(:install) + end end - it "allows user to specify gem version with fuzzy operator" do - @current_resource.version("2.3.3") - @new_resource.version("~>2.3.0") + context "if the fuzzy operator is used" do + let(:target_version) { "~>2.3.0" } + let(:current_version) { "2.3.3" } - expect(@provider.gem_env).not_to receive(:install) - @provider.run_action(:install) + it "it matches an existing gem" do + expect(provider.gem_env).not_to receive(:install) + provider.run_action(:install) + end end end end describe "in an alternate gem environment" do + let(:gem_binary) { "/usr/weird/bin/gem" } + it "installs the gem by shelling out to gem install" do - @new_resource.gem_binary("/usr/weird/bin/gem") - expect(@provider).to receive(:shell_out!).with("/usr/weird/bin/gem install rspec-core -q --no-rdoc --no-ri -v \"#{@spec_version}\"", env: nil, timeout: 900) - @provider.run_action(:install) - expect(@new_resource).to be_updated_by_last_action + expect(provider).to receive(:shell_out!).with("#{gem_binary} install rspec-core -q --no-rdoc --no-ri -v \"#{target_version}\"", env: nil, timeout: 900) + provider.run_action(:install) + expect(new_resource).to be_updated_by_last_action end - it "installs the gem from file by shelling out to gem install" do - @new_resource.gem_binary("/usr/weird/bin/gem") - @new_resource.source(CHEF_SPEC_DATA + "/gems/chef-integration-test-0.1.0.gem") - @new_resource.version(">= 0") - expect(@provider).to receive(:shell_out!).with("/usr/weird/bin/gem install #{CHEF_SPEC_DATA}/gems/chef-integration-test-0.1.0.gem -q --no-rdoc --no-ri -v \">= 0\"", env: nil, timeout: 900) - @provider.run_action(:install) - expect(@new_resource).to be_updated_by_last_action + context "when source is a path" do + let(:source) { CHEF_SPEC_DATA + "/gems/chef-integration-test-0.1.0.gem" } + let(:target_version) { ">= 0" } + + it "installs the gem by shelling out to gem install" do + expect(provider).to receive(:shell_out!).with("#{gem_binary} install #{source} -q --no-rdoc --no-ri -v \"#{target_version}\"", env: nil, timeout: 900) + provider.run_action(:install) + expect(new_resource).to be_updated_by_last_action + end end - it "installs the gem from file by shelling out to gem install when the package is a path and the source is nil" do - @new_resource = Chef::Resource::GemPackage.new(CHEF_SPEC_DATA + "/gems/chef-integration-test-0.1.0.gem") - @provider = Chef::Provider::Package::Rubygems.new(@new_resource, @run_context) - @provider.current_resource = @current_resource - @new_resource.gem_binary("/usr/weird/bin/gem") - @new_resource.version(">= 0") - expect(@new_resource.source).to eq(CHEF_SPEC_DATA + "/gems/chef-integration-test-0.1.0.gem") - expect(@provider).to receive(:shell_out!).with("/usr/weird/bin/gem install #{CHEF_SPEC_DATA}/gems/chef-integration-test-0.1.0.gem -q --no-rdoc --no-ri -v \">= 0\"", env: nil, timeout: 900) - @provider.run_action(:install) - expect(@new_resource).to be_updated_by_last_action + context "when the package is a path and source is nil" do + let(:gem_name) { CHEF_SPEC_DATA + "/gems/chef-integration-test-0.1.0.gem" } + let(:target_version) { ">= 0" } + + it "installs the gem from file by shelling out to gem install when the package is a path and the source is nil" do + expect(new_resource.source).to eq(gem_name) + expect(provider).to receive(:shell_out!).with("#{gem_binary} install #{gem_name} -q --no-rdoc --no-ri -v \"#{target_version}\"", env: nil, timeout: 900) + provider.run_action(:install) + expect(new_resource).to be_updated_by_last_action + end end end end describe "when uninstalling a gem" do - before do - @new_resource = Chef::Resource::GemPackage.new("rspec") - @current_resource = @new_resource.dup - @current_resource.version("1.2.3") - @provider.new_resource = @new_resource - @provider.current_resource = @current_resource + let(:gem_name) { "rspec" } + let(:current_version) { "1.2.3" } + let(:target_version) { nil } + + let(:current_resource) do + current_resource = Chef::Resource::GemPackage.new(gem_name) + current_resource.version(current_version) + current_resource end describe "in the current gem environment" do it "uninstalls via the api when no explicit options are used" do # pre-reqs for action_remove to actually remove the package: - expect(@provider.new_resource.version).to be_nil - expect(@provider.current_resource.version).not_to be_nil + expect(provider.new_resource.version).to be_nil + expect(provider.current_resource.version).not_to be_nil # the behavior we're testing: - expect(@provider.gem_env).to receive(:uninstall).with("rspec", nil) - @provider.action_remove + expect(provider.gem_env).to receive(:uninstall).with("rspec", nil) + provider.action_remove end - it "uninstalls via the api when options are given as a Hash" do - # pre-reqs for action_remove to actually remove the package: - expect(@provider.new_resource.version).to be_nil - expect(@provider.current_resource.version).not_to be_nil - # the behavior we're testing: - @new_resource.options(:install_dir => "/alt/install/location") - expect(@provider.gem_env).to receive(:uninstall).with("rspec", nil, :install_dir => "/alt/install/location") - @provider.action_remove + context "when options are given as a Hash" do + let(:options) { { :install_dir => "/alt/install/location" } } + + it "uninstalls via the api" do + # pre-reqs for action_remove to actually remove the package: + expect(provider.new_resource.version).to be_nil + expect(provider.current_resource.version).not_to be_nil + # the behavior we're testing: + expect(provider.gem_env).to receive(:uninstall).with("rspec", nil, options) + provider.action_remove + end end - it "uninstalls via the gem command when options are given as a String" do - @new_resource.options("-i /alt/install/location") - expect(@provider).to receive(:shell_out!).with("gem uninstall rspec -q -x -I -a -i /alt/install/location", env: nil, timeout: 900) - @provider.action_remove + context "when options are given as a String" do + let(:options) { "-i /alt/install/location" } + + it "uninstalls via the gem command" do + expect(provider).to receive(:shell_out!).with("gem uninstall rspec -q -x -I -a #{options}", env: nil, timeout: 900) + provider.action_remove + end end - it "uninstalls a specific version of a gem when a version is provided" do - @new_resource.version("1.2.3") - expect(@provider.gem_env).to receive(:uninstall).with("rspec", "1.2.3") - @provider.action_remove + context "when a version is provided" do + let(:target_version) { "1.2.3" } + + it "uninstalls a specific version of a gem" do + expect(provider.gem_env).to receive(:uninstall).with("rspec", "1.2.3") + provider.action_remove + end end end describe "in an alternate gem environment" do + let(:gem_binary) { "/usr/weird/bin/gem" } + it "uninstalls via the gem command" do - @new_resource.gem_binary("/usr/weird/bin/gem") - expect(@provider).to receive(:shell_out!).with("/usr/weird/bin/gem uninstall rspec -q -x -I -a", env: nil, timeout: 900) - @provider.action_remove + expect(provider).to receive(:shell_out!).with("#{gem_binary} uninstall rspec -q -x -I -a", env: nil, timeout: 900) + provider.action_remove end end end |