Enhance hosts file serialization and entry formatting for proper tab alignment; update tests to reflect changes.
This commit is contained in:
parent
cead0c1066
commit
5a2e0d2623
4 changed files with 111 additions and 32 deletions
|
@ -67,7 +67,7 @@ class TestHostEntry:
|
|||
comment="Loopback"
|
||||
)
|
||||
line = entry.to_hosts_line()
|
||||
assert line == "127.0.0.1 localhost local # Loopback"
|
||||
assert line == "127.0.0.1\tlocalhost\tlocal\t# Loopback"
|
||||
|
||||
def test_to_hosts_line_inactive(self):
|
||||
"""Test conversion to hosts file line format for inactive entry."""
|
||||
|
@ -77,7 +77,7 @@ class TestHostEntry:
|
|||
is_active=False
|
||||
)
|
||||
line = entry.to_hosts_line()
|
||||
assert line == "# 192.168.1.1 router"
|
||||
assert line == "# 192.168.1.1\trouter"
|
||||
|
||||
def test_from_hosts_line_simple(self):
|
||||
"""Test parsing simple hosts file line."""
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue