spec/unit/berkshelf/downloader_spec.rb in berkshelf-1.4.6 vs spec/unit/berkshelf/downloader_spec.rb in berkshelf-2.0.0.beta

- old
+ new

@@ -1,210 +1,190 @@ require 'spec_helper' -module Berkshelf - describe Downloader do - describe "ClassMethods" do - subject { Downloader } +describe Berkshelf::Downloader do + let(:locations) do + [ + { + type: :chef_api, + value: double('capi'), + options: double('capi_opts') + }, + { + type: :chef_api, + value: double('capi2'), + options: double('capi_opts2') + } + ] + end - describe "::initialize" do - context "when no value for locations is given" do - it "sets the @locations instance variable to a blank array" do - downloader = subject.new(double('store')) + describe '.initialize' do + context 'when no value for locations is given' do + it 'sets the @locations instance variable to a blank array' do + downloader = Berkshelf::Downloader.new(double('store')) + locations = downloader.instance_variable_get(:@locations) - downloader.instance_variable_get(:@locations).should be_a(Array) - downloader.instance_variable_get(:@locations).should have(0).items - end - end + expect(locations).to be_a(Array) + expect(locations).to be_empty + end + end - context "when an explicit value of locations is given" do - let(:locations) do - [ - { - type: :chef_api, - value: double('capi'), - options: double('capi_opts') - }, - { - type: :chef_api, - value: double('capi2'), - options: double('capi_opts2') - } - ] - end - - it "sets the @locations instance variable to the given locations" do - downloader = subject.new(double('store'), locations: locations) - - downloader.instance_variable_get(:@locations).should eql(locations) - end - end + context 'when an explicit value of locations is given' do + it 'sets the @locations instance variable to the given locations' do + downloader = Berkshelf::Downloader.new(double('store'), locations: locations) + expect(downloader.instance_variable_get(:@locations)).to eq(locations) end end + end - subject { Downloader.new(CookbookStore.new(tmp_path)) } - describe "#download" do - let(:source) { double('source', name: "artifact", version_constraint: "= 0.10.0") } - let(:location) { double('location') } - let(:cached_cookbook) { double('cached') } - context "when the source has a location" do - before(:each) do - source.stub(:location).and_return(location) - location.should_receive(:download).with(subject.storage_path).and_return(cached_cookbook) - source.should_receive(:cached_cookbook=).with(cached_cookbook) - end + let!(:cookbook_store) { Berkshelf::CookbookStore.new(tmp_path) } + subject { Berkshelf::Downloader.new(cookbook_store) } - it "sends 'download' to the source's location and sets the source's cached_cookbook to the result" do - subject.download(source).should be_true - end + describe '#download' do + let(:source) { double('source', name: 'artifact', version_constraint: '= 0.10.0') } + let(:location) { double('location') } + let(:cached_cookbook) { double('cached') } - it "returns an Array containing the cached_cookbook and location used to download" do - result = subject.download(source) + context 'when the source has a location' do + before do + source.stub(:location).and_return(location) + location.should_receive(:download).with(subject.storage_path).and_return(cached_cookbook) + source.should_receive(:cached_cookbook=).with(cached_cookbook) + end - result.should be_a(Array) - result[0].should eql(cached_cookbook) - result[1].should eql(location) - end + it "sends :download to the source's location and sets the source's cached_cookbook to the result" do + expect(subject.download(source)).to be_true end - context "when the source does not have a location" do - before(:each) do - source.stub(:location).and_return(nil) - subject.stub(:locations).and_return([{type: :chef_api, value: :config, options: Hash.new}]) - end + it 'returns an Array containing the cached_cookbook and location used to download' do + result = subject.download(source) - it "sends the 'download' message to the default location" do - Location.should_receive(:init).with(source.name, source.version_constraint, chef_api: :config).and_return(location) - location.should_receive(:download).with(subject.storage_path).and_return(cached_cookbook) - source.should_receive(:cached_cookbook=).with(cached_cookbook) - - subject.download(source) - end + expect(result).to be_a(Array) + expect(result[0]).to eq(cached_cookbook) + expect(result[1]).to eq(location) end end - describe "#locations" do - let(:type) { :site } - let(:value) { double('value') } - let(:options) { double('options') } - - it "returns an array of Hashes representing locations" do - subject.add_location(type, value, options) - - subject.locations.each { |l| l.should be_a(Hash) } + context 'when the source does not have a location' do + before do + source.stub(:location).and_return(nil) + subject.stub(:locations).and_return([{ type: :chef_api, value: :config, options: {} }]) end - context "when no locations are explicitly added" do - subject { Downloader.new(double('store')) } + it 'sends the :download message to the default location' do + Berkshelf::Location.should_receive(:init).with(source.name, source.version_constraint, chef_api: :config).and_return(location) + location.should_receive(:download).with(subject.storage_path).and_return(cached_cookbook) + source.should_receive(:cached_cookbook=).with(cached_cookbook) - it "returns an array of default locations" do - subject.locations.should eql(Downloader::DEFAULT_LOCATIONS) - end + subject.download(source) end + end + end - context "when locations are explicitly added" do - let(:locations) do - [ - { - type: :chef_api, - value: double('capi'), - options: double('capi_opts') - }, - { - type: :chef_api, - value: double('capi2'), - options: double('capi_opts2') - } - ] - end + describe '#locations' do + let(:type) { :site } + let(:value) { double('value') } + let(:options) { double('options') } - subject { Downloader.new(double('store'), locations: locations) } + it 'returns an array of Hashes representing locations' do + subject.add_location(type, value, options) - it "contains only the locations passed to the initializer" do - subject.locations.should eql(locations) - end + subject.locations.each do |location| + expect(location).to be_a(Hash) + end + end - it "does not include the array of default locations" do - subject.locations.should_not include(Downloader::DEFAULT_LOCATIONS) - end + context 'when no locations are explicitly added' do + subject { Berkshelf::Downloader.new(double('store')) } + + it 'returns an array of default locations' do + expect(subject.locations).to eq(Berkshelf::Downloader::DEFAULT_LOCATIONS) end end - describe "#add_location" do - let(:type) { :site } - let(:value) { double('value') } - let(:options) { double('options') } + context 'when locations are explicitly added' do + subject { Berkshelf::Downloader.new(double('store'), locations: locations) } - it "adds a hash to the end of the array of locations" do - subject.add_location(type, value, options) + it 'contains only the locations passed to the initializer' do + expect(subject.locations).to eq(locations) + end - subject.locations.should have(1).item + it 'does not include the array of default locations' do + expect(subject.locations).to_not include(Berkshelf::Downloader::DEFAULT_LOCATIONS) end + end + end - it "adds a hash with a type, value, and options key" do - subject.add_location(type, value, options) + describe '#add_location' do + let(:type) { :site } + let(:value) { double('value') } + let(:options) { double('options') } - subject.locations.last.should have_key(:type) - subject.locations.last.should have_key(:value) - subject.locations.last.should have_key(:options) - end + it 'adds a hash to the end of the array of locations' do + subject.add_location(type, value, options) + expect(subject.locations).to have(1).item + end - it "sets the value of the given 'value' to the value of the key 'value'" do - subject.add_location(type, value, options) + it 'adds a hash with a type, value, and options key' do + subject.add_location(type, value, options) + location = subject.locations.last - subject.locations.last[:value].should eql(value) + [:type, :value, :options].each do |key| + expect(location).to have_key(key) end + end - it "sets the value of the given 'type' to the value of the key 'type'" do - subject.add_location(type, value, options) + it 'sets the value of the given :value to the value of the key :value' do + subject.add_location(type, value, options) + expect(subject.locations.last[:value]).to eq(value) + end - subject.locations.last[:type].should eql(type) - end + it 'sets the value of the given :type to the value of the key :type' do + subject.add_location(type, value, options) + expect(subject.locations.last[:type]).to eq(type) + end - it "sets the value of the given 'options' to the value of the key 'options'" do - subject.add_location(type, value, options) + it 'sets the value of the given :options to the value of the key :options' do + subject.add_location(type, value, options) + expect(subject.locations.last[:options]).to eq(options) + end - subject.locations.last[:options].should eql(options) - end + it 'raises a DuplicateLocationDefined error if a location of the given type and value was already added' do + subject.add_location(type, value, options) - it "raises a DuplicateLocationDefined error if a location of the given type and value was already added" do + expect { subject.add_location(type, value, options) + }.to raise_error(Berkshelf::DuplicateLocationDefined) + end - lambda { - subject.add_location(type, value, options) - }.should raise_error(DuplicateLocationDefined) - end + context 'adding multiple locations' do + let(:type_2) { :site } + let(:value_2) { double('value_2') } + let(:options_2) { double('options_2') } - context "adding multiple locations" do - let(:type_2) { :site } - let(:value_2) { double('value_2') } - let(:options_2) { double('options_2') } + it 'adds locations in the order they are added' do + subject.add_location(type, value, options) + subject.add_location(type_2, value_2, options_2) - it "adds locations in the order they are added" do - subject.add_location(type, value, options) - subject.add_location(type_2, value_2, options_2) + expect(subject.locations).to have(2).items - subject.locations.should have(2).items - - subject.locations[0][:value].should eql(value) - subject.locations[1][:value].should eql(value_2) - end + expect(subject.locations[0][:value]).to eql(value) + expect(subject.locations[1][:value]).to eql(value_2) end end + end - describe "#has_location?" do - let(:type) { :site } - let(:value) { double('value') } + describe '#has_location?' do + let(:type) { :site } + let(:value) { double('value') } - it "returns true if a source of the given type and value was already added" do - subject.add_location(type, value) + it 'returns true if a source of the given type and value was already added' do + subject.add_location(type, value) + expect(subject.has_location?(type, value)).to be_true + end - subject.has_location?(type, value).should be_true - end - - it "returns false if a source of the given type and value was not added" do - subject.has_location?(type, value).should be_false - end + it 'returns false if a source of the given type and value was not added' do + expect(subject.has_location?(type, value)).to be_false end end end