diff --git a/libraries/entry.rb b/libraries/entry.rb index 57ddf7b9..2315feeb 100644 --- a/libraries/entry.rb +++ b/libraries/entry.rb @@ -136,14 +136,36 @@ def priority=(new_priority) # @return [String] # the string representation of this entry def to_line - hosts = [hostname, aliases].flatten.join(' ') + hosts = [hostname, aliases].flatten + + # Windows only allows 9 hostname entries per line + # It does support multiple lines for an ip to cover that + if Chef::Platform.windows? && hosts.size > 9 + comments = entry_comment(comment, priority) + results = [] + until hosts.empty? + hosts_set = hosts[0..8].join(' ') + hosts = hosts[9..-1] || [] + results << [ip_address, hosts_set, comments].compact.join("\t").strip + end + results.join("\n") + else + hosts = hosts.join(' ') + comments = entry_comment(comment, priority) + + [ip_address, hosts, comments].compact.join("\t").strip + end + end + # Returns formatted comments of a entry + # + # @return [String] + def entry_comment(comment, priority) comments = "# #{comment}".strip comments << " @#{priority}" unless priority.nil? || @calculated_priority comments = comments.strip comments = nil if comments == '#' - - [ip_address, hosts, comments].compact.join("\t").strip + comments end # Returns true if priority is calculated diff --git a/spec/unit/entry_spec.rb b/spec/unit/entry_spec.rb index 2111ddad..1f9ac618 100644 --- a/spec/unit/entry_spec.rb +++ b/spec/unit/entry_spec.rb @@ -164,5 +164,79 @@ expect(subject.to_line).to eq("2.3.4.5\twww.example.com\t# This is a comment! @10") end end + + context 'windows' do + subject { HostsFile::Entry.new(ip_address: '2.3.4.5', hostname: 'www.example.com', comment: 'This is a comment!', priority: 10) } + + before do + allow(Chef::Platform).to receive(:windows?).and_return(true) + end + + it 'prints 9 hosts on a single line' do + subject.aliases = ['www.example2.com', + 'www.example3.com', + 'www.example4.com', + 'www.example5.com', + 'www.example6.com', + 'www.example7.com', + 'www.example8.com', + 'www.example9.com'] + + expect(subject.to_line).to eq("2.3.4.5\twww.example.com www.example2.com www.example3.com www.example4.com www.example5.com www.example6.com www.example7.com www.example8.com www.example9.com\t# This is a comment! @10") + end + + it 'prints more than 9 hosts on multiple lines' do + subject.aliases = ['www.example2.com', + 'www.example3.com', + 'www.example4.com', + 'www.example5.com', + 'www.example6.com', + 'www.example7.com', + 'www.example8.com', + 'www.example9.com', + 'www.example10.com', + 'www.example11.com', + 'www.example12.com'] + + expect(subject.to_line).to eq("2.3.4.5\twww.example.com www.example2.com www.example3.com www.example4.com www.example5.com www.example6.com www.example7.com www.example8.com www.example9.com\t# This is a comment! @10\n2.3.4.5\twww.example10.com www.example11.com www.example12.com\t# This is a comment! @10") + end + end + + context 'linux' do + subject { HostsFile::Entry.new(ip_address: '2.3.4.5', hostname: 'www.example.com', comment: 'This is a comment!', priority: 10) } + + before do + allow(Chef::Platform).to receive(:windows?).and_return(false) + end + + it 'prints 9 hosts on a single line' do + subject.aliases = ['www.example2.com', + 'www.example3.com', + 'www.example4.com', + 'www.example5.com', + 'www.example6.com', + 'www.example7.com', + 'www.example8.com', + 'www.example9.com'] + + expect(subject.to_line).to eq("2.3.4.5\twww.example.com www.example2.com www.example3.com www.example4.com www.example5.com www.example6.com www.example7.com www.example8.com www.example9.com\t# This is a comment! @10") + end + + it 'prints more than 9 hosts on a single line' do + subject.aliases = ['www.example2.com', + 'www.example3.com', + 'www.example4.com', + 'www.example5.com', + 'www.example6.com', + 'www.example7.com', + 'www.example8.com', + 'www.example9.com', + 'www.example10.com', + 'www.example11.com', + 'www.example12.com'] + + expect(subject.to_line).to eq("2.3.4.5\twww.example.com www.example2.com www.example3.com www.example4.com www.example5.com www.example6.com www.example7.com www.example8.com www.example9.com www.example10.com www.example11.com www.example12.com\t# This is a comment! @10") + end + end end end