spec/unit/berkshelf/location_spec.rb in berkshelf-2.0.18 vs spec/unit/berkshelf/location_spec.rb in berkshelf-3.0.0.beta1

- old
+ new

@@ -1,155 +1,128 @@ require 'spec_helper' describe Berkshelf::Location do - # Since this is a module, we need to test the implementation via a class - let(:klass) { Class.new { include Berkshelf::Location } } + describe "ClassMethods" do + describe "::init" do + let(:dependency) { double('dependency') } - describe '.set_location_key' do - before do - @original = Berkshelf::CookbookSource.class_variable_get :@@location_keys - Berkshelf::CookbookSource.class_variable_set :@@location_keys, {} - end + it 'returns an instance of PathLocation given a path: option key' do + result = described_class.init(dependency, path: '/Users/reset/code') + expect(result).to be_a(Berkshelf::PathLocation) + end - after do - Berkshelf::CookbookSource.class_variable_set :@@location_keys, @original - end + it 'returns an instance of GitLocation given a git: option key' do + result = described_class.init(dependency, git: 'git://github.com/something.git') + expect(result).to be_a(Berkshelf::GitLocation) + end - it 'adds the given location key CookbookSource.location_keys' do - klass.set_location_key(:reset) - - expect(Berkshelf::CookbookSource.location_keys).to have(1).item - expect(Berkshelf::CookbookSource.location_keys).to include(:reset) - expect(Berkshelf::CookbookSource.location_keys[:reset]).to eq(klass) + context 'given two location_keys' do + it 'raises an InternalError' do + expect { + described_class.init(dependency, git: :value, path: :value) + }.to raise_error(Berkshelf::InternalError) + end + end end end +end - describe '.location_key' do - before do - @original = Berkshelf::CookbookSource.class_variable_get :@@location_keys - Berkshelf::CookbookSource.class_variable_set :@@location_keys, {} - end +describe Berkshelf::Location::Base do + describe "ClassMethods" do + subject { Class.new(described_class) } - after do - Berkshelf::CookbookSource.class_variable_set :@@location_keys, @original - end + describe "::set_location_key" do + before do + @original = Berkshelf::Dependency.class_variable_get :@@location_keys + Berkshelf::Dependency.class_variable_set :@@location_keys, {} + end - it "returns the class' registered location key" do - klass.set_location_key(:reset) - expect(klass.location_key).to eq(:reset) - end - end + after do + Berkshelf::Dependency.class_variable_set :@@location_keys, @original + end - describe '.set_valid_options' do - before do - @original = Berkshelf::CookbookSource.class_variable_get :@@valid_options - Berkshelf::CookbookSource.class_variable_set :@@valid_options, [] - end + it 'adds the given location key Berkshelf::Dependency.location_keys' do + subject.set_location_key(:reset) - after do - Berkshelf::CookbookSource.class_variable_set :@@valid_options, @original + expect(Berkshelf::Dependency.location_keys).to have(1).item + expect(Berkshelf::Dependency.location_keys).to include(:reset) + expect(Berkshelf::Dependency.location_keys[:reset]).to eq(subject) + end end - it 'adds the given symbol to the list of valid options on CookbookSource' do - klass.set_valid_options(:mundo) + describe "::location_key" do + before do + @original = Berkshelf::Dependency.class_variable_get :@@location_keys + Berkshelf::Dependency.class_variable_set :@@location_keys, {} + end - expect(Berkshelf::CookbookSource.valid_options).to have(1).item - expect(Berkshelf::CookbookSource.valid_options).to include(:mundo) - end + after do + Berkshelf::Dependency.class_variable_set :@@location_keys, @original + end - it 'adds parameters to the list of valid options on the CookbookSource' do - klass.set_valid_options(:riot, :arenanet) - - expect(Berkshelf::CookbookSource.valid_options).to have(2).items - expect(Berkshelf::CookbookSource.valid_options).to include(:riot) - expect(Berkshelf::CookbookSource.valid_options).to include(:arenanet) + it "returns the class' registered location key" do + subject.set_location_key(:reset) + expect(subject.location_key).to eq(:reset) + end end - end - describe '.solve_for_constraint' do - let(:constraint) { '~> 0.101.2' } - let(:versions) do - { - '0.101.2' => 'http://cookbooks.opscode.com/api/v1/cookbooks/nginx/versions/0_101_2', - '0.101.0' => 'http://cookbooks.opscode.com/api/v1/cookbooks/nginx/versions/0_101_0', - '0.100.2' => 'http://cookbooks.opscode.com/api/v1/cookbooks/nginx/versions/0_100_2', - '0.100.0' => 'http://cookbooks.opscode.com/api/v1/cookbooks/nginx/versions/0_100_0' - } - end + describe "::set_valid_options" do + before do + @original = Berkshelf::Dependency.class_variable_get :@@valid_options + Berkshelf::Dependency.class_variable_set :@@valid_options, [] + end - it 'returns an array with a string containing the version of the solution at index 0' do - result = klass.solve_for_constraint(constraint, versions) - expect(result[0]).to eq('0.101.2') - end + after do + Berkshelf::Dependency.class_variable_set :@@valid_options, @original + end - it 'returns an array containing a URI at index 0' do - result = klass.solve_for_constraint(constraint, versions) - expect(result[1]).to match(URI.regexp) - end + it 'adds the given symbol to the list of valid options on Berkshelf::Dependency' do + subject.set_valid_options(:mundo) - it 'returns the best match for the constraint and versions given' do - expect(klass.solve_for_constraint(constraint, versions)[0].to_s).to eql('0.101.2') - end + expect(Berkshelf::Dependency.valid_options).to have(1).item + expect(Berkshelf::Dependency.valid_options).to include(:mundo) + end - context 'given a solution can not be found for constraint' do - it 'returns nil' do - result = klass.solve_for_constraint(Solve::Constraint.new('>= 1.0'), versions) - expect(result).to be_nil + it 'adds parameters to the list of valid options on the Berkshelf::Dependency' do + subject.set_valid_options(:riot, :arenanet) + + expect(Berkshelf::Dependency.valid_options).to have(2).items + expect(Berkshelf::Dependency.valid_options).to include(:riot) + expect(Berkshelf::Dependency.valid_options).to include(:arenanet) end end end - describe ';init' do - let(:name) { 'artifact' } - let(:constraint) { double('constraint') } + let(:name) { "nginx" } + let(:constraint) { double('constraint') } + let(:dependency) { double('dependency', name: name, version_constraint: constraint) } + subject { Class.new(Berkshelf::Location::Base).new(dependency) } - it 'returns an instance of SiteLocation given a site: option key' do - result = Berkshelf::Location.init(name, constraint, site: 'http://site/value') - expect(result).to be_a(Berkshelf::SiteLocation) + describe "#download" do + context "when #do_download is not defined" do + it "raises a AbstractFunction" do + expect { subject.download }.to raise_error(Berkshelf::AbstractFunction) + end end - it 'returns an instance of PathLocation given a path: option key' do - result = Berkshelf::Location.init(name, constraint, path: '/Users/reset/code') - expect(result).to be_a(Berkshelf::PathLocation) - end + context "when #do_download is defined" do + let(:cached) { double('cached') } + before { subject.stub(do_download: cached) } - it 'returns an instance of GitLocation given a git: option key' do - result = Berkshelf::Location.init(name, constraint, git: 'git://github.com/something.git') - expect(result).to be_a(Berkshelf::GitLocation) - end + it "validates the returned cached cookbook" do + subject.should_receive(:validate_cached).with(cached).and_return(true) + subject.download + end - it 'returns an instance of SiteLocation when no option key is given that matches a registered location_key' do - result = Berkshelf::Location.init(name, constraint) - expect(result).to be_a(Berkshelf::SiteLocation) - end + it "returns the cached cookbook if valid" do + subject.stub(validate_cached: true) - context 'given two location_keys' do - it 'raises an InternalError' do - expect { - Berkshelf::Location.init(name, constraint, git: :value, path: :value) - }.to raise_error(Berkshelf::InternalError) + expect(subject.download).to eq(cached) end end end - - - let(:name) { 'nginx' } - let(:constraint) { double('constraint') } - - subject do - Class.new { include Berkshelf::Location }.new(name, constraint) - end - - describe '#download' do - it 'raises a AbstractFunction if not defined' do - expect { - subject.download(double('destination')) - }.to raise_error(Berkshelf::AbstractFunction) - end - end - describe '#validate_cached' do let(:cached) { double('cached-cb', cookbook_name: name, version: '0.1.0') } it 'raises a CookbookValidationFailure error if the version constraint does not satisfy the cached version' do constraint.should_receive(:satisfies?).with(cached.version).and_return(false) @@ -172,10 +145,10 @@ end it "warns about the MismatchedCookbookName if the cached_cookbook's name does not match the location's" do constraint.should_receive(:satisfies?).with(cached.version).and_return(true) cached.stub(:cookbook_name) { "artifact" } - msg = Berkshelf::MismatchedCookbookName.new(subject, cached).to_s + msg = Berkshelf::MismatchedCookbookName.new(dependency, cached).to_s Berkshelf.ui.should_receive(:warn).with(msg) subject.validate_cached(cached) end end