summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaxime Brugidou <m.brugidou@criteo.com>2017-03-28 11:55:09 +0200
committerMaxime Brugidou <m.brugidou@criteo.com>2017-03-28 13:12:23 +0200
commit0a943849f8b8866b7d38f3e76906aa14770ab051 (patch)
tree75dbb247da1d7dadb4be81ba9025077bf9cacccb
parentb640eb8f3087b6c4ae56272ffb35b5918f0865d2 (diff)
downloadchef-0a943849f8b8866b7d38f3e76906aa14770ab051.tar.gz
Add unit test for gem installer
Change-Id: Ibf4a53b9f687882f84932433cc3d45e084e3a29e Signed-off-by: Maxime Brugidou <m.brugidou@criteo.com>
-rw-r--r--spec/unit/cookbook/gem_installer_spec.rb70
1 files changed, 70 insertions, 0 deletions
diff --git a/spec/unit/cookbook/gem_installer_spec.rb b/spec/unit/cookbook/gem_installer_spec.rb
new file mode 100644
index 0000000000..69b714d977
--- /dev/null
+++ b/spec/unit/cookbook/gem_installer_spec.rb
@@ -0,0 +1,70 @@
+require "spec_helper"
+require "bundler/dsl"
+
+describe Chef::Cookbook::GemInstaller do
+ let(:cookbook_collection) do
+ {
+ test: double(
+ :cookbook,
+ metadata: double(
+ :metadata,
+ gems: [["httpclient"], ["nokogiri"]]
+ )
+ ),
+ test2: double(
+ :cookbook,
+ metadata: double(
+ :metadata,
+ gems: [["httpclient", ">= 2.0"]]
+ )
+ ),
+ test3: double(
+ :cookbook,
+ metadata: double(
+ :metadata,
+ gems: [["httpclient", ">= 1.0"]]
+ )
+ ),
+ }
+ end
+
+ let(:gem_installer) do
+ described_class.new(cookbook_collection, Chef::EventDispatch::Dispatcher.new)
+ end
+
+ let(:gemfile) do
+ StringIO.new
+ end
+
+ let(:shell_out) do
+ double(:shell_out, stdout: "")
+ end
+
+ let(:bundler_dsl) do
+ b = Bundler::Dsl.new
+ b.instance_eval(gemfile.string)
+ b
+ end
+
+ before(:each) do
+ # Prepare mocks: using a StringIO instead of a File
+ expect(Dir).to receive(:mktmpdir).and_yield("")
+ expect(File).to receive(:open).and_yield(gemfile)
+ expect(gemfile).to receive(:path).and_return("")
+ expect(IO).to receive(:read).and_return("")
+ expect(gem_installer).to receive(:shell_out!).and_return(shell_out)
+
+ end
+
+ it "generates a valid Gemfile" do
+ expect { gem_installer.install }.to_not raise_error
+
+ expect { bundler_dsl }.to_not raise_error
+ end
+
+ it "generate a Gemfile with all constraints" do
+ expect { gem_installer.install }.to_not raise_error
+
+ expect(bundler_dsl.dependencies.find { |d| d.name == "httpclient" }.requirements_list.length).to eql(2)
+ end
+end