Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions libraries/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
74 changes: 74 additions & 0 deletions spec/unit/entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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