Skip to content

Commit

Permalink
Clear up new RuboCop offences
Browse files Browse the repository at this point in the history
  • Loading branch information
hartmantis committed Nov 4, 2014
1 parent 81c699f commit 5e041de
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 81 deletions.
27 changes: 17 additions & 10 deletions libraries/provider_chef_dk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def action_install
metadata.yolo && Chef::Log.warn('Using a ChefDk package not ' \
'officially supported on this platform')
remote_file.run_action(:create)
global_shell_init(:create).write_file if new_resource.global_shell_init
global_shell_init(:create).write_file
package.run_action(:install)
new_resource.installed = true
end
Expand All @@ -70,7 +70,7 @@ def action_install
# Uninstall the ChefDk package and delete the cached file
#
def action_remove
global_shell_init(:delete).write_file if new_resource.global_shell_init
global_shell_init(:delete).write_file
package.run_action(:remove)
remote_file.run_action(:delete)
# A full uninstall would also delete the omnijack gem, but ehhh...
Expand Down Expand Up @@ -125,11 +125,11 @@ def global_shell_init(action = nil)
matcher = /^eval "\$\(chef shell-init bash\)"$/
line = 'eval "$(chef shell-init bash)"'
@global_shell_init ||= Chef::Util::FileEdit.new(bashrc_file)
return @global_shell_init unless new_resource.global_shell_init
case action
when :create
@global_shell_init.insert_line_if_no_match(matcher, line)
when :delete
@global_shell_init.search_file_delete_line(matcher)
when :create then @global_shell_init.insert_line_if_no_match(matcher,
line)
when :delete then @global_shell_init.search_file_delete_line(matcher)
end
@global_shell_init
end
Expand Down Expand Up @@ -162,14 +162,21 @@ def download_path
#
def metadata
require 'omnijack'
@metadata ||= Omnijack::Project::ChefDk.new(
platform: node['platform'],
@metadata ||= Omnijack::Project::ChefDk.new(metadata_params).metadata
end

#
# Construct the hash of parameters for Omnijack to get the right metadata
#
# @return [Hash]
#
def metadata_params
{ platform: node['platform'],
platform_version: node['platform_version'],
machine_arch: node['kernel']['machine'],
version: new_resource.version,
prerelease: new_resource.prerelease,
nightlies: new_resource.nightlies
).metadata
nightlies: new_resource.nightlies }
end

#
Expand Down
169 changes: 101 additions & 68 deletions spec/libraries/provider_chef_dk_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@
provider.action_install
end

it 'calls the bashrc create logic' do
expect_any_instance_of(described_class).to receive(:global_shell_init)
.with(:create)
expect(gsi).to receive(:write_file)
provider.action_install
end

it 'installs the package file' do
expect(package).to receive(:run_action).with(:install)
provider.action_install
Expand All @@ -93,22 +100,6 @@
provider.action_install
end

it 'does not modify any bashrc' do
expect(gsi).not_to receive(:write_file)
provider.action_install
end

context 'overridden global shell init' do
let(:global_shell_init) { true }

it 'modifies bashrc' do
expect_any_instance_of(described_class).to receive(:global_shell_init)
.with(:create)
expect(gsi).to receive(:write_file)
provider.action_install
end
end

context 'a "yolo" package' do
let(:yolo) { true }

Expand All @@ -135,22 +126,13 @@
.and_return(gsi)
end

it 'does not modify any bashrc' do
expect(gsi).not_to receive(:write_file)
it 'calls the bashrc delete logic' do
expect_any_instance_of(described_class).to receive(:global_shell_init)
.with(:delete)
expect(gsi).to receive(:write_file)
provider.action_remove
end

context 'overridden global shell init' do
let(:global_shell_init) { true }

it 'modifies bashrc' do
expect_any_instance_of(described_class).to receive(:global_shell_init)
.with(:delete)
expect(gsi).to receive(:write_file)
provider.action_remove
end
end

it 'deletes the package remote file' do
expect(remote_file).to receive(:run_action).with(:delete)
provider.action_remove
Expand Down Expand Up @@ -265,52 +247,92 @@
end
end

context 'no action (default)' do
it_behaves_like 'any instance'
shared_examples_for 'no action' do
it 'does not call insert_line_if_no_match' do
expect_any_instance_of(Chef::Util::FileEdit)
.not_to receive(:insert_line_if_no_match)
end

it 'does nothing to the file' do
it 'does not call search_file_delete_line' do
expect_any_instance_of(Chef::Util::FileEdit)
.not_to receive(:search_file_delete_line)
end

it 'will do nothing to the file' do
@fakebashrc.write('some stuff')
@fakebashrc.seek(0)
res
@fakebashrc.seek(0)
expect(@fakebashrc.read).to eq('')
expect(@fakebashrc.read).to eq('some stuff')
end
end

context 'no action (default)' do
context 'global shell init disabled (default)' do
it_behaves_like 'any instance'
it_behaves_like 'no action'
end

context 'global shell init enabled' do
let(:global_shell_init) { true }

it_behaves_like 'any instance'
it_behaves_like 'no action'
end
end

context 'a create action' do
let(:action) { :create }

it_behaves_like 'any instance'

it 'calls insert_line_if_no_match' do
expect_any_instance_of(Chef::Util::FileEdit)
.to receive(:insert_line_if_no_match)
res
context 'global shell init disabled (default)' do
it_behaves_like 'any instance'
it_behaves_like 'no action'
end

it 'will write to the file' do
res.write_file
@fakebashrc.seek(0)
expected = "eval \"$(chef shell-init bash)\"\n"
expect(@fakebashrc.read).to eq(expected)
context 'global shell init enabled' do
let(:global_shell_init) { true }

it_behaves_like 'any instance'

it 'calls insert_line_if_no_match' do
expect_any_instance_of(Chef::Util::FileEdit)
.to receive(:insert_line_if_no_match)
res
end

it 'will write to the file' do
res.write_file
@fakebashrc.seek(0)
expected = "eval \"$(chef shell-init bash)\"\n"
expect(@fakebashrc.read).to eq(expected)
end
end
end

context 'a delete action' do
let(:action) { :delete }

it_behaves_like 'any instance'

it 'calls search_file_delete_line' do
expect_any_instance_of(Chef::Util::FileEdit)
.to receive(:search_file_delete_line)
res
context 'global shell init disabled (default)' do
it_behaves_like 'any instance'
it_behaves_like 'no action'
end

it 'will delete from the file' do
@fakebashrc.write("eval \"$(chef shell-init bash)\"\n")
@fakebashrc.seek(0)
res.write_file
@fakebashrc.seek(0)
expect(@fakebashrc.read).to eq('')
context 'global shell init enabled' do
let(:global_shell_init) { true }

it 'calls search_file_delete_line' do
expect_any_instance_of(Chef::Util::FileEdit)
.to receive(:search_file_delete_line)
res
end

it 'will delete from the file' do
@fakebashrc.write("eval \"$(chef shell-init bash)\"\n")
@fakebashrc.seek(0)
res.write_file
@fakebashrc.seek(0)
expect(@fakebashrc.read).to eq('')
end
end
end
end
Expand Down Expand Up @@ -348,57 +370,68 @@
describe '#metadata' do
before(:each) do
require 'omnijack'
allow_any_instance_of(described_class).to receive(:metadata_params)
.and_return(some: 'things')
allow_any_instance_of(Omnijack::Project::ChefDk).to receive(:metadata)
.and_return('SOME METADATA')
end

it 'fetches and returns the metadata instance' do
expect(Omnijack::Project::ChefDk).to receive(:new).with(some: 'things')
.and_call_original
expect(provider.send(:metadata)).to eq('SOME METADATA')
end
end

describe '#metadata_params' do
context 'Ubuntu' do
let(:platform) { { platform: 'ubuntu', version: '14.04' } }

it 'fetches and returns the metadata instance' do
expect(Omnijack::Project::ChefDk).to receive(:new).with(
it 'returns the correct params hash' do
expected = {
platform: 'ubuntu',
platform_version: '14.04',
machine_arch: 'x86_64',
version: nil,
prerelease: nil,
nightlies: nil
).and_call_original
expect(provider.send(:metadata)).to eq('SOME METADATA')
}
expect(provider.send(:metadata_params)).to eq(expected)
end
end

context 'Mac OS X' do
let(:platform) { { platform: 'mac_os_x', version: '10.9.2' } }

it 'fetches and returns the metadata instance' do
expect(Omnijack::Project::ChefDk).to receive(:new).with(
it 'returns the correct params hash' do
expected = {
platform: 'mac_os_x',
platform_version: '10.9.2',
machine_arch: 'x86_64',
version: nil,
prerelease: nil,
nightlies: nil
).and_call_original
expect(provider.send(:metadata)).to eq('SOME METADATA')
}
expect(provider.send(:metadata_params)).to eq(expected)
end
end

context 'Windows' do
let(:platform) { { platform: 'windows', version: '2012' } }

it 'fetches and returns the metadata instance' do
expect(Omnijack::Project::ChefDk).to receive(:new).with(
it 'returns the correct params hash' do
expected = {
platform: 'windows',
platform_version: '6.2.9200',
machine_arch: 'x86_64',
version: nil,
prerelease: nil,
nightlies: nil
).and_call_original
expect(provider.send(:metadata)).to eq('SOME METADATA')
}
expect(provider.send(:metadata_params)).to eq(expected)
end
end

end

describe '#omnijack_gem' do
Expand Down
6 changes: 3 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@
end

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
Coveralls::SimpleCov::Formatter,
SimpleCov::Formatter::HTMLFormatter,
SimpleCov::Formatter::Console
Coveralls::SimpleCov::Formatter,
SimpleCov::Formatter::HTMLFormatter,
SimpleCov::Formatter::Console
]
SimpleCov.minimum_coverage 90
SimpleCov.start
Expand Down

0 comments on commit 5e041de

Please sign in to comment.