Enhance hosts file serialization and entry formatting for proper tab alignment; update tests to reflect changes.

This commit is contained in:
Philip Henning 2025-07-29 23:31:30 +02:00
parent cead0c1066
commit 5a2e0d2623
4 changed files with 111 additions and 32 deletions

View file

@ -161,15 +161,15 @@ class TestHostsParser:
hosts_file = HostsFile()
entry1 = HostEntry(ip_address="127.0.0.1", hostnames=["localhost"])
entry2 = HostEntry(ip_address="192.168.1.1", hostnames=["router"])
hosts_file.add_entry(entry1)
hosts_file.add_entry(entry2)
parser = HostsParser()
content = parser.serialize(hosts_file)
expected = """127.0.0.1 localhost
192.168.1.1 router
expected = """127.0.0.1\tlocalhost
192.168.1.1\trouter
"""
assert content == expected
@ -198,9 +198,8 @@ class TestHostsParser:
expected = """# Header comment 1
# Header comment 2
127.0.0.1 localhost # Loopback
# 10.0.0.1 test
127.0.0.1\tlocalhost\t# Loopback
# 10.0.0.1\ttest
# Footer comment
"""
@ -227,7 +226,7 @@ class TestHostsParser:
# Read back and verify
with open(f.name, 'r') as read_file:
content = read_file.read()
assert content == "127.0.0.1 localhost\n"
assert content == "127.0.0.1\tlocalhost\n"
os.unlink(f.name)
@ -260,7 +259,7 @@ class TestHostsParser:
# Check new content
with open(f.name, 'r') as new_file:
new_content = new_file.read()
assert new_content == "127.0.0.1 localhost\n"
assert new_content == "127.0.0.1\tlocalhost\n"
# Cleanup
os.unlink(backup_path)
@ -344,10 +343,10 @@ class TestHostsParser:
final_content = read_file.read()
# The content should be functionally equivalent
# (though formatting might differ slightly)
assert "127.0.0.1 localhost loopback # Local loopback" in final_content
assert "::1 localhost # IPv6 loopback" in final_content
assert "192.168.1.1 router gateway # Local router" in final_content
assert "# 10.0.0.1 test.local # Test entry (disabled)" in final_content
# (though formatting might differ slightly with tabs)
assert "127.0.0.1\tlocalhost\tloopback\t# Local loopback" in final_content
assert "::1\t\tlocalhost\t# IPv6 loopback" in final_content
assert "192.168.1.1\trouter\t\tgateway\t# Local router" in final_content
assert "# 10.0.0.1\ttest.local\t# Test entry (disabled)" in final_content
os.unlink(f.name)