spec/unit/project_spec.rb in omnibus-3.2.2 vs spec/unit/project_spec.rb in omnibus-4.0.0.beta.1

- old
+ new

@@ -1,92 +1,127 @@ require 'spec_helper' require 'ohai' module Omnibus describe Project do - let(:project) { Project.load(project_path('sample')) } + subject do + described_class.new.evaluate do + name 'sample' + friendly_name 'Sample Project' + install_dir '/sample' + maintainer 'Sample Devs' + homepage 'http://example.com/' - subject { project } + build_version '1.0' + build_iteration 1 + extra_package_file '/path/to/sample_dir' + extra_package_file '/path/to/file.conf' + + resources_path 'sample/project/resources' + end + end + it_behaves_like 'a cleanroom setter', :name, %|name 'chef'| it_behaves_like 'a cleanroom setter', :friendly_name, %|friendly_name 'Chef'| - it_behaves_like 'a cleanroom setter', :msi_parameters, %|msi_parameters {}| - it_behaves_like 'a cleanroom setter', :package_name, %|package_name 'chef.package'| - it_behaves_like 'a cleanroom setter', :install_dir, %|install_dir '/opt/chef'| - it_behaves_like 'a cleanroom setter', :install_path, %|install_path '/opt/chef'| it_behaves_like 'a cleanroom setter', :maintainer, %|maintainer 'Chef Software, Inc'| it_behaves_like 'a cleanroom setter', :homepage, %|homepage 'https://getchef.com'| it_behaves_like 'a cleanroom setter', :description, %|description 'Installs the thing'| - it_behaves_like 'a cleanroom setter', :replaces, %|replaces 'old-chef'| + it_behaves_like 'a cleanroom setter', :replace, %|replace 'old-chef'| it_behaves_like 'a cleanroom setter', :conflict, %|conflict 'puppet'| it_behaves_like 'a cleanroom setter', :build_version, %|build_version '1.2.3'| it_behaves_like 'a cleanroom setter', :build_iteration, %|build_iteration 1| - it_behaves_like 'a cleanroom setter', :mac_pkg_identifier, %|mac_pkg_identifier 'com.getchef'| it_behaves_like 'a cleanroom setter', :package_user, %|package_user 'chef'| it_behaves_like 'a cleanroom setter', :package_group, %|package_group 'chef'| it_behaves_like 'a cleanroom setter', :override, %|override :chefdk, source: 'foo.com'| it_behaves_like 'a cleanroom setter', :resources_path, %|resources_path '/path'| it_behaves_like 'a cleanroom setter', :package_scripts_path, %|package_scripts_path '/path/scripts'| it_behaves_like 'a cleanroom setter', :dependency, %|dependency 'libxslt-dev'| it_behaves_like 'a cleanroom setter', :runtime_dependency, %|runtime_dependency 'libxslt'| it_behaves_like 'a cleanroom setter', :exclude, %|exclude 'hamlet'| it_behaves_like 'a cleanroom setter', :config_file, %|config_file '/path/to/config.rb'| it_behaves_like 'a cleanroom setter', :extra_package_file, %|extra_package_file '/path/to/asset'| - it_behaves_like 'a cleanroom setter', :dependencies, %|dependencies 'a', 'b', 'c'| - it_behaves_like 'a cleanroom getter', :files_path - describe 'basics' do - it 'should return a name' do - expect(project.name).to eq('sample') + it 'returns a name' do + expect(subject.name).to eq('sample') end - it 'should return an install_dir' do - expect(project.install_dir).to eq('/sample') + it 'returns an install_dir' do + expect(subject.install_dir).to eq('/sample') end - it 'should return a maintainer' do - expect(project.maintainer).to eq('Sample Devs') + it 'returns a maintainer' do + expect(subject.maintainer).to eq('Sample Devs') end - it 'should return a homepage' do - expect(project.homepage).to eq('http://example.com/') + it 'returns a homepage' do + expect(subject.homepage).to eq('http://example.com/') end - it 'should return a build version' do - expect(project.build_version).to eq('1.0') + it 'returns a build version' do + expect(subject.build_version).to eq('1.0') end - it 'should return a build iteration' do - expect(project.build_iteration).to eq('1') + it 'returns a build iteration' do + expect(subject.build_iteration).to eq(1) end - it 'should return an array of files and dirs' do - expect(project.extra_package_files).to eq(['/path/to/sample_dir', '/path/to/file.conf']) + it 'returns an array of files and dirs' do + expect(subject.extra_package_files).to eq(['/path/to/sample_dir', '/path/to/file.conf']) end - it 'should return friendly_name' do - expect(project.friendly_name).to eq('Sample Project') + it 'returns a friendly_name' do + expect(subject.friendly_name).to eq('Sample Project') end - it 'should return resources_path' do - expect(project.resources_path).to include('sample/project/resources') + it 'returns a resources_path' do + expect(subject.resources_path).to include('sample/project/resources') end end - describe '#package_user' do - it 'returns root by default' do - subject.instance_variable_set(:@package_user, nil) - expect(subject.package_user).to eq('root') + describe '#install_dir' do + it 'removes duplicate slashes' do + subject.install_dir('///opt//chef') + expect(subject.install_dir).to eq('/opt/chef') end + + it 'converts Windows slashes to Ruby ones' do + subject.install_dir('C:\\chef\\chefdk') + expect(subject.install_dir).to eq('C:/chef/chefdk') + end + + it 'removes trailing slashes' do + subject.install_dir('/opt/chef//') + expect(subject.install_dir).to eq('/opt/chef') + end + + it 'is a DSL method' do + expect(subject).to have_exposed_method(:install_dir) + end end - describe '#package_group' do - it 'returns root by default' do - subject.instance_variable_set(:@package_group, nil) - expect(subject.package_group).to eq('root') + describe '#default_root' do + context 'on Windows' do + before { stub_ohai(platform: 'windows', version: '2012') } + + it 'returns C:/' do + expect(subject.default_root).to eq('C:') + end end + + context 'on non-Windows' do + before { stub_ohai(platform: 'ubuntu', version: '12.04') } + + it 'returns /opt' do + expect(subject.default_root).to eq('/opt') + end + end + + it 'is a DSL method' do + expect(subject).to have_exposed_method(:default_root) + end end describe '#dirty!' do it 'dirties the cache' do subject.instance_variable_set(:@dirty, nil) @@ -111,117 +146,219 @@ expect(subject).to_not be_dirty end end describe '#<=>' do + let(:chefdk) { described_class.new.tap { |p| p.name('chefdk') } } + let(:chef) { described_class.new.tap { |p| p.name('chef') } } + let(:ruby) { described_class.new.tap { |p| p.name('ruby') } } + it 'compares projects by name' do - list = [ - project, - Project.load(project_path('chefdk')), - ] - expect(list.sort.map(&:name)).to eq(%w(chefdk sample)) + list = [chefdk, chef, ruby] + expect(list.sort.map(&:name)).to eq(%w(chef chefdk ruby)) end end - describe '#iteration' do + describe '#build_iteration' do let(:fauxhai_options) { Hash.new } before { stub_ohai(fauxhai_options) } context 'when on RHEL' do let(:fauxhai_options) { { platform: 'redhat', version: '6.4' } } - it 'should return a RHEL iteration' do - expect(project.iteration).to eq('1.el6') + it 'returns a RHEL iteration' do + expect(subject.build_iteration).to eq(1) end end context 'when on Debian' do let(:fauxhai_options) { { platform: 'debian', version: '7.2' } } - it 'should return a Debian iteration' do - expect(project.iteration).to eq('1') + it 'returns a Debian iteration' do + expect(subject.build_iteration).to eq(1) end end context 'when on FreeBSD' do let(:fauxhai_options) { { platform: 'freebsd', version: '9.1' } } - it 'should return a FreeBSD iteration' do - expect(project.iteration).to eq('1.freebsd.9.amd64') + it 'returns a FreeBSD iteration' do + expect(subject.build_iteration).to eq(1) end end context 'when on Windows' do - let(:fauxhai_options) { { platform: 'windows', version: '2008R2' } } + before { stub_ohai(platform: 'windows', version: '2008R2') } before { stub_const('File::ALT_SEPARATOR', '\\') } - it 'should return a Windows iteration' do - expect(project.iteration).to eq('1.windows') + it 'returns a Windows iteration' do + expect(subject.build_iteration).to eq(1) end end context 'when on OS X' do let(:fauxhai_options) { { platform: 'mac_os_x', version: '10.8.2' } } - it 'should return a generic iteration' do - expect(project.iteration).to eq('1') + it 'returns a generic iteration' do + expect(subject.build_iteration).to eq(1) end end end describe '#overrides' do - let(:project) { Project.load(project_path('chefdk')) } + before { subject.overrides.clear } - before { project.overrides.clear } + it 'sets all the things through #overrides' do + subject.override(:thing, version: '6.6.6') + expect(subject.override(:zlib)).to be_nil + end + it 'retrieves the things set through #overrides' do + subject.override(:thing, version: '6.6.6') + expect(subject.override(:thing)[:version]).to eq('6.6.6') + end + end - it 'should set all the things through #overrides' do - project.override(:thing, version: '6.6.6') - expect(project.override(:zlib)).to be_nil + describe '#ohai' do + before { stub_ohai(platform: 'ubuntu', version: '12.04') } + + it 'is a DSL method' do + expect(subject).to have_exposed_method(:ohai) end - it 'retrieves the things set through #overrides' do - project.override(:thing, version: '6.6.6') - expect(project.override(:thing)[:version]).to eq('6.6.6') + it 'delegates to the Ohai class' do + expect(subject.ohai).to be(Ohai) end end + describe '#packager' do + it 'returns a packager object' do + expect(subject.packager).to be_a(Packager::Base) + end + + it 'calls Packager#for_current_system' do + expect(Packager).to receive(:for_current_system) + .and_call_original + subject.packager + end + end + + describe '#package' do + it 'raises an exception when a block is not given' do + expect { subject.package(:foo) }.to raise_error(InvalidValue) + end + + it 'adds the block to the list' do + block = Proc.new {} + subject.package(:foo, &block) + + expect(subject.packagers[:foo]).to include(block) + end + + it 'allows for multiple invocations, keeping order' do + block_1, block_2 = Proc.new {}, Proc.new {} + subject.package(:foo, &block_1) + subject.package(:foo, &block_2) + + expect(subject.packagers[:foo]).to eq([block_1, block_2]) + end + end + + describe '#packagers' do + it 'returns a Hash' do + expect(subject.packagers).to be_a(Hash) + end + + it 'has a default Hash value of an empty array' do + expect(subject.packagers[:foo]).to be_a(Array) + expect(subject.packagers[:bar]).to_not be(subject.packagers[:foo]) + end + end + + describe '#compressor' do + it 'returns a compressor object' do + expect(subject.compressor).to be_a(Compressor::Base) + end + + it 'calls Compressor#for_current_system' do + expect(Compressor).to receive(:for_current_system) + .and_call_original + + subject.compressor + end + + it 'passes in the current compressors' do + subject.compress(:dmg) + subject.compress(:tgz) + + expect(Compressor).to receive(:for_current_system) + .with([:dmg, :tgz]) + .and_call_original + + subject.compressor + end + end + + describe '#compress' do + it 'does not raises an exception when a block is not given' do + expect { subject.compress(:foo) }.to_not raise_error + end + + it 'adds the compressor to the list' do + subject.compress(:foo) + expect(subject.compressors).to include(:foo) + end + + it 'adds the block to the list' do + block = Proc.new {} + subject.compress(:foo, &block) + + expect(subject.compressors[:foo]).to include(block) + end + + it 'allows for multiple invocations, keeping order' do + block_1, block_2 = Proc.new {}, Proc.new {} + subject.compress(:foo, &block_1) + subject.compress(:foo, &block_2) + + expect(subject.compressors[:foo]).to eq([block_1, block_2]) + end + end + + describe '#compressors' do + it 'returns a Hash' do + expect(subject.compressors).to be_a(Hash) + end + + it 'has a default Hash value of an empty array' do + expect(subject.compressors[:foo]).to be_a(Array) + expect(subject.compressors[:bar]).to_not be(subject.compressors[:foo]) + end + end + describe '#shasum' do context 'when a filepath is given' do let(:path) { '/project.rb' } let(:file) { double(File) } - subject do - project = described_class.new(path) - project.name('project') - project.install_dir('/opt/project') - project.build_version('1.0.0') - project - end - before do + subject.instance_variable_set(:@filepath, path) + allow(File).to receive(:exist?) .with(path) .and_return(true) allow(File).to receive(:open) .with(path) .and_return(file) end it 'returns the correct shasum' do - expect(subject.shasum).to eq('8270d9078b577d3bedc2353ba3dc33fda1f8e69db3b7c0b449183a3e0e560d09') + expect(subject.shasum).to eq('2cb8bdd11c766caa11a37607e84ffb51af3ae3da16931988f12f7fc9de98d68e') end end context 'when a filepath is not given' do - subject do - project = described_class.new - project.name('project') - project.install_dir('/opt/project') - project.build_version('1.0.0') - project - end + before { subject.send(:remove_instance_variable, :@filepath) } it 'returns the correct shasum' do - expect(subject.shasum).to eq('545571a6041129f1224741a700c776b960cb093d4260ff6ca78b6a34bc130b45') + expect(subject.shasum).to eq('3cc6bd98da4d643b79c71be2c93761a458b442e2931f7d421636f526d0c1e8bf') end end end - end end