Skip to content

Commit

Permalink
Update specs and test harness
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed Jun 24, 2014
1 parent 85a7c54 commit 958629d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Berksfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
site :opscode
source 'https://api.berkshelf.com'
metadata

group :integration do
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source 'https://rubygems.org'

gem 'berkshelf', '~> 3.0.0.beta'
gem 'chefspec', '~> 3.2'
gem 'berkshelf', '~> 3.1'
gem 'chefspec', '~> 4.0'
gem 'foodcritic', '~> 3.0'

group :integration do
Expand Down
18 changes: 9 additions & 9 deletions spec/unit/entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,36 @@
let(:entry) { double('entry') }

before do
Entry.stub(:new).and_return(entry)
allow(Entry).to receive(:new).and_return(entry)
end

it 'parses just an ip_address and hostname' do
Entry.should_receive(:new).with(ip_address: '1.2.3.4', hostname: 'www.example.com', aliases: [], comment: nil, priority: nil)
expect(Entry).to receive(:new).with(ip_address: '1.2.3.4', hostname: 'www.example.com', aliases: [], comment: nil, priority: nil)
Entry.parse('1.2.3.4 www.example.com')
end

it 'parses aliases' do
Entry.should_receive(:new).with(ip_address: '1.2.3.4', hostname: 'www.example.com', aliases: ['foo', 'bar'], comment: nil, priority: nil)
expect(Entry).to receive(:new).with(ip_address: '1.2.3.4', hostname: 'www.example.com', aliases: ['foo', 'bar'], comment: nil, priority: nil)
Entry.parse('1.2.3.4 www.example.com foo bar')
end

it 'parses a comment' do
Entry.should_receive(:new).with(ip_address: '1.2.3.4', hostname: 'www.example.com', aliases: [], comment: 'This is a comment!', priority: nil)
expect(Entry).to receive(:new).with(ip_address: '1.2.3.4', hostname: 'www.example.com', aliases: [], comment: 'This is a comment!', priority: nil)
Entry.parse('1.2.3.4 www.example.com # This is a comment!')
end

it 'parses aliases and comments' do
Entry.should_receive(:new).with(ip_address: '1.2.3.4', hostname: 'www.example.com', aliases: ['foo', 'bar'], comment: 'This is a comment!', priority: nil)
expect(Entry).to receive(:new).with(ip_address: '1.2.3.4', hostname: 'www.example.com', aliases: ['foo', 'bar'], comment: 'This is a comment!', priority: nil)
Entry.parse('1.2.3.4 www.example.com foo bar # This is a comment!')
end

it 'parses priorities with comments' do
Entry.should_receive(:new).with(ip_address: '1.2.3.4', hostname: 'www.example.com', aliases: [], comment: 'This is a comment!', priority: '40')
expect(Entry).to receive(:new).with(ip_address: '1.2.3.4', hostname: 'www.example.com', aliases: [], comment: 'This is a comment!', priority: '40')
Entry.parse('1.2.3.4 www.example.com # This is a comment! @40')
end

it 'parses priorities' do
Entry.should_receive(:new).with(ip_address: '1.2.3.4', hostname: 'www.example.com', aliases: [], comment: nil, priority: '40')
expect(Entry).to receive(:new).with(ip_address: '1.2.3.4', hostname: 'www.example.com', aliases: [], comment: nil, priority: '40')
Entry.parse('1.2.3.4 www.example.com # @40')
end
end
Expand Down Expand Up @@ -99,7 +99,7 @@
end

it 'calls calculated_priority for @priority' do
Entry.any_instance.should_receive(:calculated_priority)
expect_any_instance_of(Entry).to receive(:calculated_priority)
Entry.new(ip_address: '2.3.4.5', hostname: 'www.example.com')
end
end
Expand All @@ -124,7 +124,7 @@

it 'sets @calculated_priority to false' do
subject.priority = 50
expect(subject.instance_variable_get(:@calculated_priority)).to be_false
expect(subject.instance_variable_get(:@calculated_priority)).to be_falsey
end
end

Expand Down
68 changes: 39 additions & 29 deletions spec/unit/manipulator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
let(:header) { manipulator.hostsfile_header }

before do
File.stub(:exists?).and_return(true)
File.stub(:readlines).and_return(lines)
allow(File).to receive(:exists?).and_return(true)
allow(File).to receive(:readlines).and_return(lines)
manipulator.instance_variable_set(:@entries, entries)
end

Expand All @@ -37,12 +37,12 @@
end

it 'raises a fatal error if the hostsfile does not exist' do
File.stub(:exists?).and_return(false)
allow(File).to receive(:exists?).and_return(false)
expect { Manipulator.new(node) }.to raise_error(RuntimeError)
end

it 'sends the line to be parsed by the Entry class' do
lines.each { |l| Entry.should_receive(:parse).with(l) }
lines.each { |l| allow(Entry).to receive(:parse).with(l) }
Manipulator.new(node)
end
end
Expand All @@ -58,10 +58,10 @@

let(:options) { { ip_address: '1.2.3.4', hostname: 'example.com', aliases: nil, comment: 'Some comment', priority: 5 } }

before { Entry.stub(:new).and_return(entry) }
before { allow(Entry).to receive(:new).and_return(entry) }

it 'creates a new entry object' do
Entry.should_receive(:new).with(options)
allow(Entry).to receive(:new).with(options)
manipulator.add(options)
end

Expand All @@ -74,7 +74,9 @@
describe '#update' do
context 'when the entry does not exist' do
before do
manipulator.stub(:find_entry_by_ip_address).with(any_args()).and_return(nil)
allow(manipulator).to receive(:find_entry_by_ip_address)
.with(any_args())
.and_return(nil)
end

it 'does nothing' do
Expand All @@ -87,11 +89,13 @@
let(:entry) { double('entry', :hostname= => nil, :aliases= => nil, :comment= => nil, :priority= => nil) }

before do
manipulator.stub(:find_entry_by_ip_address).with(any_args()).and_return(entry)
allow(manipulator).to receive(:find_entry_by_ip_address)
.with(any_args())
.and_return(entry)
end

it 'updates the hostname' do
entry.should_receive(:hostname=).with('seth.com')
allow(entry).to receive(:hostname=).with('seth.com')
manipulator.update(ip_address: '1.2.3.4', hostname: 'seth.com')
end
end
Expand All @@ -104,35 +108,39 @@
let(:entry) { double('entry', options.merge(:aliases= => nil, :comment= => nil)) }

before do
manipulator.stub(:find_entry_by_ip_address).with(any_args()).and_return(entry)
allow(manipulator).to receive(:find_entry_by_ip_address)
.with(any_args())
.and_return(entry)
end

it 'updates the hostname' do
entry.should_receive(:hostname=).with('example.com')
allow(entry).to receive(:hostname=).with('example.com')
manipulator.append(options)
end

it 'updates the aliases' do
entry.should_receive(:aliases=).with(['www.example.com'])
entry.should_receive(:hostname=).with('example.com')
allow(entry).to receive(:aliases=).with(['www.example.com'])
allow(entry).to receive(:hostname=).with('example.com')
manipulator.append(options.merge(aliases: 'www.example.com'))
end

it 'updates the comment' do
entry.should_receive(:comment=).with('Some comment, This is a new comment!')
entry.should_receive(:hostname=).with('example.com')
allow(entry).to receive(:comment=).with('Some comment, This is a new comment!')
allow(entry).to receive(:hostname=).with('example.com')
manipulator.append(options.merge(comment: 'This is a new comment!'))
end
end

context 'when the record does not exist' do
before do
manipulator.stub(:find_entry_by_ip_address).with(any_args()).and_return(nil)
manipulator.stub(:add)
allow(manipulator).to receive(:find_entry_by_ip_address)
.with(any_args())
.and_return(nil)
allow(manipulator).to receive(:add)
end

it 'delegates to #add' do
manipulator.should_receive(:add).with(options).once
allow(manipulator).to receive(:add).with(options).once
manipulator.append(options)
end
end
Expand All @@ -141,7 +149,7 @@
describe '#remove' do
context 'when the entry does not exist' do
before do
manipulator.stub(:find_entry_by_ip_address).with(any_args()).and_return(nil)
allow(manipulator).to receive(:find_entry_by_ip_address).with(any_args()).and_return(nil)
end

it 'does nothing' do
Expand All @@ -154,7 +162,9 @@
let(:entry) { entries[0] }

before do
manipulator.stub(:find_entry_by_ip_address).with(any_args()).and_return(entry)
allow(manipulator).to receive(:find_entry_by_ip_address)
.with(any_args())
.and_return(entry)
end

it 'removes the entry' do
Expand All @@ -171,7 +181,7 @@
before do
manipulator.class.send(:public, :new_content)
manipulator.class.send(:public, :hostsfile_header)
manipulator.stub(:unique_entries).and_return(entries)
allow(manipulator).to receive(:unique_entries).and_return(entries)
end

it 'starts with comment header' do
Expand All @@ -189,20 +199,20 @@
end

before do
File.stub(:read).and_return(current_content)
manipulator.stub(:unique_entries).and_return(entries)
allow(File).to receive(:read).and_return(current_content)
allow(manipulator).to receive(:unique_entries).and_return(entries)
end

context 'when content has not changed' do
it 'returns false' do
expect(manipulator.content_changed?).to be_false
expect(manipulator.content_changed?).to be_falsey
end
end

context 'when content has changed' do
it 'returns true' do
manipulator.remove('4.5.6.7')
expect(manipulator.content_changed?).to be_true
expect(manipulator.content_changed?).to be_truthy
end
end
end
Expand All @@ -223,9 +233,9 @@
let(:file) { double('file', content: nil, run_action: nil) }

before do
Chef::Resource::File.stub(:new).and_return(file)
manipulator.stub(:unique_entries).and_return(entries)
node.stub(:run_context)
allow(Chef::Resource::File).to receive(:new).and_return(file)
allow(manipulator).to receive(:unique_entries).and_return(entries)
allow(node).to receive(:run_context)
end

it 'writes out the new file' do
Expand All @@ -251,7 +261,7 @@
describe '#hostsfile_path' do
before do
manipulator.class.send(:public, :hostsfile_path)
File.stub(:exists?).and_return(true)
allow(File).to receive(:exists?).and_return(true)
end

context 'with no node attribute specified' do
Expand Down

0 comments on commit 958629d

Please sign in to comment.