summaryrefslogtreecommitdiffstats
path: root/python/mozbuild/mozbuild/test/test_dotproperties.py
blob: 4e7a4377999d040f04779ede657a49494959e466 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# -*- coding: utf-8 -*-

import os
import unittest

import mozpack.path as mozpath
from mozunit import main
from six import StringIO

from mozbuild.dotproperties import DotProperties

test_data_path = mozpath.abspath(mozpath.dirname(__file__))
test_data_path = mozpath.join(test_data_path, "data")


class TestDotProperties(unittest.TestCase):
    def test_get(self):
        contents = StringIO(
            """
key=value
"""
        )
        p = DotProperties(contents)
        self.assertEqual(p.get("missing"), None)
        self.assertEqual(p.get("missing", "default"), "default")
        self.assertEqual(p.get("key"), "value")

    def test_update(self):
        contents = StringIO(
            """
old=old value
key=value
"""
        )
        p = DotProperties(contents)
        self.assertEqual(p.get("old"), "old value")
        self.assertEqual(p.get("key"), "value")

        new_contents = StringIO(
            """
key=new value
"""
        )
        p.update(new_contents)
        self.assertEqual(p.get("old"), "old value")
        self.assertEqual(p.get("key"), "new value")

    def test_get_list(self):
        contents = StringIO(
            """
list.0=A
list.1=B
list.2=C

order.1=B
order.0=A
order.2=C
"""
        )
        p = DotProperties(contents)
        self.assertEqual(p.get_list("missing"), [])
        self.assertEqual(p.get_list("list"), ["A", "B", "C"])
        self.assertEqual(p.get_list("order"), ["A", "B", "C"])

    def test_get_list_with_shared_prefix(self):
        contents = StringIO(
            """
list.0=A
list.1=B
list.2=C

list.sublist.1=E
list.sublist.0=D
list.sublist.2=F

list.sublist.second.0=G

list.other.0=H
"""
        )
        p = DotProperties(contents)
        self.assertEqual(p.get_list("list"), ["A", "B", "C"])
        self.assertEqual(p.get_list("list.sublist"), ["D", "E", "F"])
        self.assertEqual(p.get_list("list.sublist.second"), ["G"])
        self.assertEqual(p.get_list("list.other"), ["H"])

    def test_get_dict(self):
        contents = StringIO(
            """
A.title=title A

B.title=title B
B.url=url B

C=value
"""
        )
        p = DotProperties(contents)
        self.assertEqual(p.get_dict("missing"), {})
        self.assertEqual(p.get_dict("A"), {"title": "title A"})
        self.assertEqual(p.get_dict("B"), {"title": "title B", "url": "url B"})
        with self.assertRaises(ValueError):
            p.get_dict("A", required_keys=["title", "url"])
        with self.assertRaises(ValueError):
            p.get_dict("missing", required_keys=["key"])
        # A key=value pair is considered to root an empty dict.
        self.assertEqual(p.get_dict("C"), {})
        with self.assertRaises(ValueError):
            p.get_dict("C", required_keys=["missing_key"])

    def test_get_dict_with_shared_prefix(self):
        contents = StringIO(
            """
A.title=title A
A.subdict.title=title A subdict

B.title=title B
B.url=url B
B.subdict.title=title B subdict
B.subdict.url=url B subdict
"""
        )
        p = DotProperties(contents)
        self.assertEqual(p.get_dict("A"), {"title": "title A"})
        self.assertEqual(p.get_dict("B"), {"title": "title B", "url": "url B"})
        self.assertEqual(p.get_dict("A.subdict"), {"title": "title A subdict"})
        self.assertEqual(
            p.get_dict("B.subdict"),
            {"title": "title B subdict", "url": "url B subdict"},
        )

    def test_get_dict_with_value_prefix(self):
        contents = StringIO(
            """
A.default=A
A.default.B=B
A.default.B.ignored=B ignored
A.default.C=C
A.default.C.ignored=C ignored
"""
        )
        p = DotProperties(contents)
        self.assertEqual(p.get("A.default"), "A")
        # This enumerates the properties.
        self.assertEqual(p.get_dict("A.default"), {"B": "B", "C": "C"})
        # They can still be fetched directly.
        self.assertEqual(p.get("A.default.B"), "B")
        self.assertEqual(p.get("A.default.C"), "C")

    def test_unicode(self):
        contents = StringIO(
            """
# Danish.
# ####  ~~ Søren Munk Skrøder, sskroeder - 2009-05-30 @ #mozmae

# Korean.
A.title=한메일

# Russian.
list.0 = test
list.1 = Яндекс
"""
        )
        p = DotProperties(contents)
        self.assertEqual(p.get_dict("A"), {"title": "한메일"})
        self.assertEqual(p.get_list("list"), ["test", "Яндекс"])

    def test_valid_unicode_from_file(self):
        # The contents of valid.properties is identical to the contents of the
        # test above.  This specifically exercises reading from a file.
        p = DotProperties(os.path.join(test_data_path, "valid.properties"))
        self.assertEqual(p.get_dict("A"), {"title": "한메일"})
        self.assertEqual(p.get_list("list"), ["test", "Яндекс"])

    def test_bad_unicode_from_file(self):
        # The contents of bad.properties is not valid Unicode; see the comments
        # in the file itself for details.
        with self.assertRaises(UnicodeDecodeError):
            DotProperties(os.path.join(test_data_path, "bad.properties"))


if __name__ == "__main__":
    main()