summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/harness/wptrunner/tests/test_hosts.py
blob: 4f122640d58a4297c65f20422739fcfcc5f8db5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.

import unittest
import sys
from cStringIO import StringIO

sys.path.insert(0, "..")

import hosts


class HostsTest(unittest.TestCase):
    def do_test(self, input, expected):
        host_file = hosts.HostsFile.from_file(StringIO(input))
        self.assertEquals(host_file.to_string(), expected)

    def test_simple(self):
        self.do_test("""127.0.0.1    \tlocalhost  alias # comment
# Another comment""",
                     """127.0.0.1 localhost alias # comment
# Another comment
""")

    def test_blank_lines(self):
        self.do_test("""127.0.0.1    \tlocalhost  alias # comment

\r
    \t
# Another comment""",
                     """127.0.0.1 localhost alias # comment
# Another comment
""")

    def test_whitespace(self):
        self.do_test("""    \t127.0.0.1    \tlocalhost  alias # comment     \r
    \t# Another comment""",
                     """127.0.0.1 localhost alias # comment
# Another comment
""")

    def test_alignment(self):
        self.do_test("""127.0.0.1    \tlocalhost  alias
192.168.1.1 another_host    another_alias
""","""127.0.0.1   localhost    alias
192.168.1.1 another_host another_alias
"""
)

    def test_multiple_same_name(self):
        # The semantics are that we overwrite earlier entries with the same name
        self.do_test("""127.0.0.1    \tlocalhost  alias
192.168.1.1 localhost    another_alias""","""192.168.1.1 localhost another_alias
"""
)

if __name__ == "__main__":
    unittest.main()