spec/unit/project_spec.rb in omnibus-3.1.1 vs spec/unit/project_spec.rb in omnibus-3.2.0.rc.1

- old
+ new

@@ -1,23 +1,49 @@ require 'spec_helper' require 'ohai' module Omnibus describe Project do - let(:project) do - Omnibus::Project.load(project_path('sample')) - end + let(:project) { Project.load(project_path('sample')) } subject { project } + it_behaves_like 'a cleanroom setter', :name, 'chef' + it_behaves_like 'a cleanroom setter', :friendly_name, 'Chef' + it_behaves_like 'a cleanroom setter', :msi_parameters, { foo: 'bar' } + it_behaves_like 'a cleanroom setter', :package_name, 'chef.package' + it_behaves_like 'a cleanroom setter', :install_dir, '/opt/chef' + it_behaves_like 'a cleanroom setter', :install_path, '/opt/chef' + it_behaves_like 'a cleanroom setter', :maintainer, 'Chef Software, Inc' + it_behaves_like 'a cleanroom setter', :homepage, 'https://getchef.com' + it_behaves_like 'a cleanroom setter', :description, 'Installs the thing' + it_behaves_like 'a cleanroom setter', :replaces, 'old-chef' + it_behaves_like 'a cleanroom setter', :conflict, 'puppet' + it_behaves_like 'a cleanroom setter', :build_version, '1.2.3' + it_behaves_like 'a cleanroom setter', :build_iteration, 1 + it_behaves_like 'a cleanroom setter', :mac_pkg_identifier, 'com.getchef' + it_behaves_like 'a cleanroom setter', :package_user, 'chef' + it_behaves_like 'a cleanroom setter', :package_group, 'chef' + it_behaves_like 'a cleanroom setter', :override, 'foo' + it_behaves_like 'a cleanroom setter', :resources_path, '/path' + it_behaves_like 'a cleanroom setter', :package_scripts_path, '/path/scripts' + it_behaves_like 'a cleanroom setter', :dependency, 'libxslt-dev' + it_behaves_like 'a cleanroom setter', :runtime_dependency, 'libxslt' + it_behaves_like 'a cleanroom setter', :exclude, 'hamlet' + it_behaves_like 'a cleanroom setter', :config_file, '/path/to/config.rb' + it_behaves_like 'a cleanroom setter', :extra_package_file, '/path/to/asset' + it_behaves_like 'a cleanroom setter', :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') end - it 'should return an install path' do - expect(project.install_path).to eq('/sample') + it 'should return an install_dir' do + expect(project.install_dir).to eq('/sample') end it 'should return a maintainer' do expect(project.maintainer).to eq('Sample Devs') end @@ -45,26 +71,49 @@ it 'should return friendly_name' do expect(project.resources_path).to eq('sample/project/resources') end end + describe '#dirty!' do + it 'dirties the cache' do + subject.instance_variable_set(:@dirty, nil) + subject.dirty! + expect(subject).to be_dirty + end + end + + describe '#dirty?' do + it 'returns true by default' do + subject.instance_variable_set(:@dirty, nil) + expect(subject).to_not be_dirty + end + + it 'returns true when the cache is dirty' do + subject.instance_variable_set(:@dirty, true) + expect(subject).to be_dirty + end + + it 'returns false when the cache is not dirty' do + subject.instance_variable_set(:@dirty, false) + expect(subject).to_not be_dirty + end + end + describe '#<=>' do it 'compares projects by name' do list = [ project, - Omnibus::Project.load(project_path('chefdk')), + Project.load(project_path('chefdk')), ] expect(list.sort.map(&:name)).to eq(%w(chefdk sample)) end end describe '#iteration' do let(:fauxhai_options) { Hash.new } - before do - stub_ohai(Fauxhai.mock(fauxhai_options).data) - end + 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') @@ -100,37 +149,139 @@ end end end describe '#overrides' do - let(:project) { Omnibus::Project.load(project_path('chefdk')) } + let(:project) { Project.load(project_path('chefdk')) } - it 'should set an override for the zlib version' do - expect(project.overrides[:zlib][:version]).to eq('1.2.8') - end + before { project.overrides.clear } - it 'should access the zlib version through the #override method as well' do - expect(project.override(:zlib)[:version]).to eq('1.2.8') - end it 'should set all the things through #overrides' do - project.overrides(thing: { version: '6.6.6' }) + project.override(:thing, version: '6.6.6') expect(project.override(:zlib)).to be_nil end - it 'should retrieve the things set through #overrides' do - project.overrides(thing: { version: '6.6.6' }) + 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') end + end - it 'should not set other things through setting a single #override' do - project.override(:thing, version: '6.6.6') - expect(project.override(:zlib)[:version]).to eq('1.2.8') + describe '#platform_version_for_package' do + before { described_class.send(:public, :platform_version_for_package) } + + shared_examples 'a version manipulator' do |platform, version, family, expected| + context "on #{platform}-#{version} (#{family})" do + before do + allow(Ohai).to receive(:ohai).and_return( + 'platform' => platform, + 'platform_version' => version, + 'platform_family' => family, + ) + end + + it 'returns the correct value' do + expect(subject.platform_version_for_package).to eq(expected) + end + end end - it 'should retrieve the things set through #overrides' do - project.override(:thing, version: '6.6.6') - expect(project.override(:thing)[:version]).to eq('6.6.6') + it_behaves_like 'a version manipulator', 'arch', '2009.02', 'arch', '2009.02' + it_behaves_like 'a version manipulator', 'arch', '2014.06.01', 'arch', '2014.06' + it_behaves_like 'a version manipulator', 'debian', '7.1', 'debian', '7' + it_behaves_like 'a version manipulator', 'debian', '6.9', 'debian', '6' + it_behaves_like 'a version manipulator', 'ubuntu', '10.04', 'debian', '10.04' + it_behaves_like 'a version manipulator', 'ubuntu', '10.04.04', 'debian', '10.04' + it_behaves_like 'a version manipulator', 'fedora', '11.5', 'fedora', '11' + it_behaves_like 'a version manipulator', 'freebsd', '10.0', 'fedora', '10' + it_behaves_like 'a version manipulator', 'rhel', '6.5', 'rhel', '6' + it_behaves_like 'a version manipulator', 'centos', '5.9.6', 'rhel', '5' + it_behaves_like 'a version manipulator', 'aix', '7.1', 'aix', '7.1' + it_behaves_like 'a version manipulator', 'gentoo', '2004.3', 'aix', '2004.3' + it_behaves_like 'a version manipulator', 'mac_os_x', '10.9.1', 'mac_os_x', '10.9' + it_behaves_like 'a version manipulator', 'openbsd', '5.4.4', 'openbsd', '5.4' + it_behaves_like 'a version manipulator', 'slackware', '12.0.1', 'slackware', '12.0' + it_behaves_like 'a version manipulator', 'solaris', '5.9', 'solaris2', '5.9' + it_behaves_like 'a version manipulator', 'suse', '5.9', 'suse', '5.9' + it_behaves_like 'a version manipulator', 'omnios', 'r151010', 'omnios', 'r151010' + it_behaves_like 'a version manipulator', 'smartos', '20120809T221258Z', 'smartos', '20120809T221258Z' + it_behaves_like 'a version manipulator', 'windows', '6.1.7600', 'windows', '7' + it_behaves_like 'a version manipulator', 'windows', '6.1.7601', 'windows', '2008r2' + it_behaves_like 'a version manipulator', 'windows', '6.2.9200', 'windows', '8' + it_behaves_like 'a version manipulator', 'windows', '6.3.9200', 'windows', '8.1' + + context 'given an unknown platform' do + before do + allow(Ohai).to receive(:ohai).and_return( + 'platform' => 'bacon', + 'platform_version' => '1.crispy', + 'platform_family' => 'meats', + ) + end + + it 'raises an exception' do + expect { subject.platform_version_for_package } + .to raise_error(UnknownPlatformFamily) + end end + + context 'given an unknown windows platform version' do + before do + allow(Ohai).to receive(:ohai).and_return( + 'platform' => 'windows', + 'platform_version' => '1.2.3', + 'platform_family' => 'windows', + ) + end + + it 'raises an exception' do + expect { subject.platform_version_for_package } + .to raise_error(UnknownPlatformVersion) + end + 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 + 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') + 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 + + it 'returns the correct shasum' do + expect(subject.shasum).to eq('545571a6041129f1224741a700c776b960cb093d4260ff6ca78b6a34bc130b45') + end + end + end + end end