diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 00000000..263d1dc2 --- /dev/null +++ b/.rubocop.yml @@ -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' diff --git a/libraries/entry.rb b/libraries/entry.rb index 33012439..3beab198 100644 --- a/libraries/entry.rb +++ b/libraries/entry.rb @@ -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 @@ -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 == '#' @@ -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. diff --git a/libraries/manipulator.rb b/libraries/manipulator.rb index a51d9b1d..93c89100 100644 --- a/libraries/manipulator.rb +++ b/libraries/manipulator.rb @@ -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 = [] @@ -50,9 +50,7 @@ def initialize(node) # @return [Array] # 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. @@ -75,7 +73,7 @@ def add(options = {}) hostname: options[:hostname], aliases: options[:aliases], comment: options[:comment], - priority: options[:priority], + priority: options[:priority] ) @entries << entry @@ -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 diff --git a/providers/entry.rb b/providers/entry.rb index dde469af..ed0d1b32 100644 --- a/providers/entry.rb +++ b/providers/entry.rb @@ -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? @@ -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 @@ -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? @@ -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? diff --git a/spec/unit/entry_spec.rb b/spec/unit/entry_spec.rb index ab3fed31..16438cef 100644 --- a/spec/unit/entry_spec.rb +++ b/spec/unit/entry_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/unit/manipulator_spec.rb b/spec/unit/manipulator_spec.rb index 29b56813..199eac14 100644 --- a/spec/unit/manipulator_spec.rb +++ b/spec/unit/manipulator_spec.rb @@ -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) ] @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -272,7 +272,7 @@ 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 @@ -280,7 +280,7 @@ 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 @@ -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 diff --git a/test/fixtures/cookbooks/fake/recipes/options.rb b/test/fixtures/cookbooks/fake/recipes/options.rb index acee25a3..a6dbb178 100644 --- a/test/fixtures/cookbooks/fake/recipes/options.rb +++ b/test/fixtures/cookbooks/fake/recipes/options.rb @@ -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