Skip to content

Commit

Permalink
Resolve all auto-correctable cookstyle warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Smith <[email protected]>
  • Loading branch information
tas50 committed Sep 11, 2016
1 parent 11302b3 commit b5abe67
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 40 deletions.
20 changes: 20 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Rubocop (cookstyle) todo:

# Configuration parameters: AllowSafeAssignment.
Lint/AssignmentInCondition:
Exclude:
- 'libraries/manipulator.rb'

Lint/ShadowingOuterLocalVariable:
Exclude:
- 'libraries/entry.rb'
- 'libraries/manipulator.rb'

Style/DoubleNegation:
Exclude:
- 'libraries/manipulator.rb'

# Configuration parameters: MinBodyLength.
Style/GuardClause:
Exclude:
- 'libraries/manipulator.rb'
8 changes: 4 additions & 4 deletions libraries/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ def parse(line)
raise ArgumentError, "/etc/hosts has a line without hostname: #{line}"
end

return self.new(
new(
ip_address: entries[0],
hostname: entries[1],
aliases: entries[2..-1],
comment: comment,
priority: priority,
priority: priority
)
end

Expand Down Expand Up @@ -137,7 +137,7 @@ def priority=(new_priority)
def to_line
hosts = [hostname, aliases].flatten.join(' ')

comments = "# #{comment.to_s}".strip
comments = "# #{comment}".strip
comments << " @#{priority}" unless priority.nil? || @calculated_priority
comments = comments.strip
comments = nil if comments == '#'
Expand Down Expand Up @@ -167,7 +167,7 @@ def calculated_priority
return 80 if IPAddr.new('127.0.0.0/8').include?(ip_address) # local
return 60 if ip_address.ipv4? # ipv4
return 20 if ip_address.ipv6? # ipv6
return 00
00
end

# Removes the scopes pieces of the address, because reasons.
Expand Down
12 changes: 5 additions & 7 deletions libraries/manipulator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def initialize(node)
@node = node

# Fail if no hostsfile is found
unless ::File.exists?(hostsfile_path)
raise RuntimeError, "No hostsfile exists at `#{hostsfile_path}'!"
unless ::File.exist?(hostsfile_path)
raise "No hostsfile exists at `#{hostsfile_path}'!"
end

@entries = []
Expand All @@ -50,9 +50,7 @@ def initialize(node)
# @return [Array<IPAddr>]
# the list of IP Addresses
def ip_addresses
@entries.collect do |entry|
entry.ip_address
end.compact || []
@entries.collect(&:ip_address).compact || []
end

# Add a new record to the hostsfile.
Expand All @@ -75,7 +73,7 @@ def add(options = {})
hostname: options[:hostname],
aliases: options[:aliases],
comment: options[:comment],
priority: options[:priority],
priority: options[:priority]
)

@entries << entry
Expand Down Expand Up @@ -264,7 +262,7 @@ def collect_and_flatten(contents)
hostname: entry.hostname,
aliases: entry.aliases,
comment: entry.comment,
priority: !entry.calculated_priority? && entry.priority,
priority: !entry.calculated_priority? && entry.priority
)
end
end
Expand Down
8 changes: 4 additions & 4 deletions providers/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def whyrun_supported?
aliases: new_resource.aliases,
comment: new_resource.comment,
priority: new_resource.priority,
unique: new_resource.unique,
unique: new_resource.unique
)

if hostsfile.content_changed?
Expand All @@ -60,7 +60,7 @@ def whyrun_supported?
aliases: new_resource.aliases,
comment: new_resource.comment,
priority: new_resource.priority,
unique: new_resource.unique,
unique: new_resource.unique
)
hostsfile.save
end
Expand All @@ -80,7 +80,7 @@ def whyrun_supported?
aliases: new_resource.aliases,
comment: new_resource.comment,
priority: new_resource.priority,
unique: new_resource.unique,
unique: new_resource.unique
)

if hostsfile.content_changed?
Expand All @@ -101,7 +101,7 @@ def whyrun_supported?
aliases: new_resource.aliases,
comment: new_resource.comment,
priority: new_resource.priority,
unique: new_resource.unique,
unique: new_resource.unique
)

if hostsfile.content_changed?
Expand Down
16 changes: 8 additions & 8 deletions spec/unit/entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
end

it 'parses aliases' do
expect(Entry).to 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: %w(foo bar), comment: nil, priority: nil)
Entry.parse('1.2.3.4 www.example.com foo bar')
end

Expand All @@ -29,7 +29,7 @@
end

it 'parses aliases and comments' do
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)
expect(Entry).to receive(:new).with(ip_address: '1.2.3.4', hostname: 'www.example.com', aliases: %w(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

Expand All @@ -46,18 +46,18 @@
end

describe '.initialize' do
subject { Entry.new(ip_address: '2.3.4.5', hostname: 'www.example.com', aliases: ['foo', 'bar'], comment: 'This is a comment!', priority: 100) }
subject { Entry.new(ip_address: '2.3.4.5', hostname: 'www.example.com', aliases: %w(foo bar), comment: 'This is a comment!', priority: 100) }

it 'raises an exception if :ip_address is missing' do
expect {
expect do
Entry.new(hostname: 'www.example.com')
}.to raise_error(ArgumentError)
end.to raise_error(ArgumentError)
end

it 'raises an exception if :hostname is missing' do
expect {
expect do
Entry.new(ip_address: '2.3.4.5')
}.to raise_error(ArgumentError)
end.to raise_error(ArgumentError)
end

it 'sets the @ip_address instance variable' do
Expand All @@ -76,7 +76,7 @@

it 'sets the @aliases instance variable' do
expect(subject.aliases).to be_a(Array)
expect(subject.aliases).to eq(['foo', 'bar'])
expect(subject.aliases).to eq(%w(foo bar))
end

it 'sets the @comment instance variable' do
Expand Down
32 changes: 16 additions & 16 deletions spec/unit/manipulator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@

let(:lines) do
[
"127.0.0.1 localhost",
"::1 localhost6",
"1.2.3.4 example.com",
"4.5.6.7 foo.example.com"
'127.0.0.1 localhost',
'::1 localhost6',
'1.2.3.4 example.com',
'4.5.6.7 foo.example.com'
]
end

let(:entries) do
[
Entry.new(ip_address: '127.0.0.1', hostname: 'localhost', to_line: '127.0.0.1 localhost', priority: 10),
Entry.new(ip_address: '::1', hostname: 'localhost6', to_line: '::1 localhost6', priority: 11),
Entry.new(ip_address: '127.0.0.1', hostname: 'localhost', to_line: '127.0.0.1 localhost', priority: 10),
Entry.new(ip_address: '::1', hostname: 'localhost6', to_line: '::1 localhost6', priority: 11),
Entry.new(ip_address: '1.2.3.4', hostname: 'example.com', to_line: '1.2.3.4 example.com', priority: 20),
Entry.new(ip_address: '4.5.6.7', hostname: 'foo.example.com', to_line: '4.5.6.7 foo.example.com', priority: 30)
]
Expand Down Expand Up @@ -77,7 +77,7 @@
context 'when the entry does not exist' do
before do
allow(manipulator).to receive(:find_entry_by_ip_address)
.with(any_args())
.with(any_args)
.and_return(nil)
end

Expand All @@ -92,7 +92,7 @@

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

Expand All @@ -111,7 +111,7 @@

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

Expand All @@ -136,7 +136,7 @@
context 'when the record does not exist' do
before do
allow(manipulator).to receive(:find_entry_by_ip_address)
.with(any_args())
.with(any_args)
.and_return(nil)
allow(manipulator).to receive(:add)
end
Expand All @@ -151,7 +151,7 @@
describe '#remove' do
context 'when the entry does not exist' do
before do
allow(manipulator).to receive(: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 @@ -165,7 +165,7 @@

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

Expand Down Expand Up @@ -272,15 +272,15 @@
expect(manipulator.hostsfile_path).to eq('/etc/hosts')
end
it 'returns C:\Windows\system32\drivers\etc\hosts on a Windows machine' do
windows_attributes = node.merge({ 'platform_family' => 'windows', 'kernel' => { 'os_info' => { 'system_directory' => 'C:\Windows\system32' } } })
windows_attributes = node.merge('platform_family' => 'windows', 'kernel' => { 'os_info' => { 'system_directory' => 'C:\Windows\system32' } })
expect(Manipulator.new(windows_attributes).hostsfile_path).to eq('C:\Windows\system32\drivers\etc\hosts')
end
end

context 'with a custom hostsfile node attribute' do
it 'returns the custom path' do
custom_path = '/custom/path'
expect(Manipulator.new(node.merge({'hostsfile' => { 'path' => custom_path } })).hostsfile_path).to eq(custom_path)
expect(Manipulator.new(node.merge('hostsfile' => { 'path' => custom_path })).hostsfile_path).to eq(custom_path)
end
end
end
Expand All @@ -293,9 +293,9 @@
entry = Entry.new(ip_address: '7.8.9.10', hostname: 'new.example.com')
entries << entry

expect {
expect do
manipulator.remove_existing_hostnames(entry)
}.to_not change(manipulator, :entries)
end.to_not change(manipulator, :entries)
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/cookbooks/fake/recipes/options.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
hostsfile_entry '2.3.4.5' do
hostname 'www.example.com'
aliases ['foo', 'bar']
aliases %w(foo bar)
comment 'This is a comment!'
priority 100
end

0 comments on commit b5abe67

Please sign in to comment.