summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/tests/cow.py
blob: 75142649c4cde87d0687280adf3a82c01189ab86 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#
# BitBake Tests for Copy-on-Write (cow.py)
#
# SPDX-License-Identifier: GPL-2.0-only
#
# Copyright 2006 Holger Freyther <freyther@handhelds.org>
# Copyright (C) 2020  Agilent Technologies, Inc.
#

import io
import re
import sys
import unittest
import contextlib
import collections

from bb.COW import COWDictBase, COWSetBase, COWDictMeta, COWSetMeta


class COWTestCase(unittest.TestCase):
    """
    Test case for the COW module from mithro
    """

    def setUp(self):
        self._track_warnings = False
        self._warning_file = io.StringIO()
        self._unhandled_warnings = collections.deque()
        COWDictBase.__warn__ = self._warning_file

    def tearDown(self):
        COWDictBase.__warn__ = sys.stderr
        if self._track_warnings:
            self._checkAllWarningsRead()

    def trackWarnings(self):
        self._track_warnings = True

    def _collectWarnings(self):
        self._warning_file.seek(0)
        for warning in self._warning_file:
            self._unhandled_warnings.append(warning.rstrip("\n"))
        self._warning_file.truncate(0)
        self._warning_file.seek(0)

    def _checkAllWarningsRead(self):
        self._collectWarnings()
        self.assertSequenceEqual(self._unhandled_warnings, [])

    @contextlib.contextmanager
    def checkReportsWarning(self, expected_warning):
        self._checkAllWarningsRead()
        yield
        self._collectWarnings()
        warning = self._unhandled_warnings.popleft()
        self.assertEqual(warning, expected_warning)

    def checkStrOutput(self, obj, expected_levels, expected_keys):
        if obj.__class__ is COWDictMeta:
            expected_class_name = "COWDict"
        elif obj.__class__ is COWSetMeta:
            expected_class_name = "COWSet"
        else:
            self.fail("obj is of unknown type {0}".format(type(obj)))
        s = str(obj)
        regex = re.compile(r"<(\w+) Level: (\d+) Current Keys: (\d+)>")
        match = regex.match(s)
        self.assertIsNotNone(match, "bad str output: '{0}'".format(s))
        class_name = match.group(1)
        self.assertEqual(class_name, expected_class_name)
        levels = int(match.group(2))
        self.assertEqual(levels, expected_levels, "wrong # levels in str: '{0}'".format(s))
        keys = int(match.group(3))
        self.assertEqual(keys, expected_keys, "wrong # keys in str: '{0}'".format(s))

    def testGetSet(self):
        """
        Test and set
        """
        a = COWDictBase.copy()

        self.assertEqual(False, 'a' in a)

        a['a'] = 'a'
        a['b'] = 'b'
        self.assertEqual(True, 'a' in a)
        self.assertEqual(True, 'b' in a)
        self.assertEqual('a', a['a'])
        self.assertEqual('b', a['b'])

    def testCopyCopy(self):
        """
        Test the copy of copies
        """

        # create two COW dict 'instances'
        b = COWDictBase.copy()
        c = COWDictBase.copy()

        # assign some keys to one instance, some keys to another
        b['a'] = 10
        b['c'] = 20
        c['a'] = 30

        # test separation of the two instances
        self.assertEqual(False, 'c' in c)
        self.assertEqual(30, c['a'])
        self.assertEqual(10, b['a'])

        # test copy
        b_2 = b.copy()
        c_2 = c.copy()

        self.assertEqual(False, 'c' in c_2)
        self.assertEqual(10, b_2['a'])

        b_2['d'] = 40
        self.assertEqual(False, 'd' in c_2)
        self.assertEqual(True, 'd' in b_2)
        self.assertEqual(40, b_2['d'])
        self.assertEqual(False, 'd' in b)
        self.assertEqual(False, 'd' in c)

        c_2['d'] = 30
        self.assertEqual(True, 'd' in c_2)
        self.assertEqual(True, 'd' in b_2)
        self.assertEqual(30, c_2['d'])
        self.assertEqual(40, b_2['d'])
        self.assertEqual(False, 'd' in b)
        self.assertEqual(False, 'd' in c)

        # test copy of the copy
        c_3 = c_2.copy()
        b_3 = b_2.copy()
        b_3_2 = b_2.copy()

        c_3['e'] = 4711
        self.assertEqual(4711, c_3['e'])
        self.assertEqual(False, 'e' in c_2)
        self.assertEqual(False, 'e' in b_3)
        self.assertEqual(False, 'e' in b_3_2)
        self.assertEqual(False, 'e' in b_2)

        b_3['e'] = 'viel'
        self.assertEqual('viel', b_3['e'])
        self.assertEqual(4711, c_3['e'])
        self.assertEqual(False, 'e' in c_2)
        self.assertEqual(True, 'e' in b_3)
        self.assertEqual(False, 'e' in b_3_2)
        self.assertEqual(False, 'e' in b_2)

    def testCow(self):
        self.trackWarnings()

        c = COWDictBase.copy()
        c['123'] = 1027
        c['other'] = 4711
        c['d'] = {'abc': 10, 'bcd': 20}

        copy = c.copy()

        self.assertEqual(1027, c['123'])
        self.assertEqual(4711, c['other'])
        self.assertEqual({'abc': 10, 'bcd': 20}, c['d'])
        self.assertEqual(1027, copy['123'])
        self.assertEqual(4711, copy['other'])
        with self.checkReportsWarning("Warning: Doing a copy because d is a mutable type."):
            self.assertEqual({'abc': 10, 'bcd': 20}, copy['d'])

        # cow it now
        copy['123'] = 1028
        copy['other'] = 4712
        copy['d']['abc'] = 20

        self.assertEqual(1027, c['123'])
        self.assertEqual(4711, c['other'])
        self.assertEqual({'abc': 10, 'bcd': 20}, c['d'])
        self.assertEqual(1028, copy['123'])
        self.assertEqual(4712, copy['other'])
        self.assertEqual({'abc': 20, 'bcd': 20}, copy['d'])

    def testOriginalTestSuite(self):
        # This test suite is a port of the original one from COW.py
        self.trackWarnings()

        a = COWDictBase.copy()
        self.checkStrOutput(a, 1, 0)

        a['a'] = 'a'
        a['b'] = 'b'
        a['dict'] = {}
        self.checkStrOutput(a, 1, 4)  # 4th member is dict__mutable__

        b = a.copy()
        self.checkStrOutput(b, 2, 0)
        b['c'] = 'b'
        self.checkStrOutput(b, 2, 1)

        with self.checkReportsWarning("Warning: If you aren't going to change any of the values call with True."):
            self.assertListEqual(list(a.iteritems()),
                                 [('a', 'a'),
                                  ('b', 'b'),
                                  ('dict', {})
                                  ])

        with self.checkReportsWarning("Warning: If you aren't going to change any of the values call with True."):
            b_gen = b.iteritems()
        self.assertTupleEqual(next(b_gen), ('a', 'a'))
        self.assertTupleEqual(next(b_gen), ('b', 'b'))
        self.assertTupleEqual(next(b_gen), ('c', 'b'))
        with self.checkReportsWarning("Warning: Doing a copy because dict is a mutable type."):
            self.assertTupleEqual(next(b_gen), ('dict', {}))
        with self.assertRaises(StopIteration):
            next(b_gen)

        b['dict']['a'] = 'b'
        b['a'] = 'c'

        self.checkStrOutput(a, 1, 4)
        self.checkStrOutput(b, 2, 3)

        with self.checkReportsWarning("Warning: If you aren't going to change any of the values call with True."):
            self.assertListEqual(list(a.iteritems()),
                                 [('a', 'a'),
                                  ('b', 'b'),
                                  ('dict', {})
                                  ])

        with self.checkReportsWarning("Warning: If you aren't going to change any of the values call with True."):
            b_gen = b.iteritems()
        self.assertTupleEqual(next(b_gen), ('a', 'c'))
        self.assertTupleEqual(next(b_gen), ('b', 'b'))
        self.assertTupleEqual(next(b_gen), ('c', 'b'))
        self.assertTupleEqual(next(b_gen), ('dict', {'a': 'b'}))
        with self.assertRaises(StopIteration):
            next(b_gen)

        with self.assertRaises(KeyError):
            print(b["dict2"])

        a['set'] = COWSetBase()
        a['set'].add("o1")
        a['set'].add("o1")
        a['set'].add("o2")
        self.assertSetEqual(set(a['set'].itervalues()), {"o1", "o2"})
        self.assertSetEqual(set(b['set'].itervalues()), {"o1", "o2"})

        b['set'].add('o3')
        self.assertSetEqual(set(a['set'].itervalues()), {"o1", "o2"})
        self.assertSetEqual(set(b['set'].itervalues()), {"o1", "o2", "o3"})

        a['set2'] = set()
        a['set2'].add("o1")
        a['set2'].add("o1")
        a['set2'].add("o2")

        # We don't expect 'a' to change anymore
        def check_a():
            with self.checkReportsWarning("Warning: If you aren't going to change any of the values call with True."):
                a_gen = a.iteritems()
                self.assertTupleEqual(next(a_gen), ('a', 'a'))
            self.assertTupleEqual(next(a_gen), ('b', 'b'))
            self.assertTupleEqual(next(a_gen), ('dict', {}))
            self.assertTupleEqual(next(a_gen), ('set2', {'o1', 'o2'}))
            a_sub_set = next(a_gen)
            self.assertEqual(a_sub_set[0], 'set')
            self.checkStrOutput(a_sub_set[1], 1, 2)
            self.assertSetEqual(set(a_sub_set[1].itervalues()), {'o1', 'o2'})

        check_a()

        b_gen = b.iteritems(readonly=True)
        self.assertTupleEqual(next(b_gen), ('a', 'c'))
        self.assertTupleEqual(next(b_gen), ('b', 'b'))
        self.assertTupleEqual(next(b_gen), ('c', 'b'))
        self.assertTupleEqual(next(b_gen), ('dict', {'a': 'b'}))
        self.assertTupleEqual(next(b_gen), ('set2', {'o1', 'o2'}))
        b_sub_set = next(b_gen)
        self.assertEqual(b_sub_set[0], 'set')
        self.checkStrOutput(b_sub_set[1], 2, 1)
        self.assertSetEqual(set(b_sub_set[1].itervalues()), {'o1', 'o2', 'o3'})

        del b['b']
        with self.assertRaises(KeyError):
            print(b['b'])
        self.assertFalse('b' in b)

        check_a()

        b.__revertitem__('b')
        check_a()
        self.assertEqual(b['b'], 'b')
        self.assertTrue('b' in b)

        b.__revertitem__('dict')
        check_a()

        b_gen = b.iteritems(readonly=True)
        self.assertTupleEqual(next(b_gen), ('a', 'c'))
        self.assertTupleEqual(next(b_gen), ('b', 'b'))
        self.assertTupleEqual(next(b_gen), ('c', 'b'))
        self.assertTupleEqual(next(b_gen), ('dict', {}))
        self.assertTupleEqual(next(b_gen), ('set2', {'o1', 'o2'}))
        b_sub_set = next(b_gen)
        self.assertEqual(b_sub_set[0], 'set')
        self.checkStrOutput(b_sub_set[1], 2, 1)
        self.assertSetEqual(set(b_sub_set[1].itervalues()), {'o1', 'o2', 'o3'})

        self.checkStrOutput(a, 1, 6)
        self.checkStrOutput(b, 2, 3)

    def testSetMethods(self):
        s = COWSetBase()
        with self.assertRaises(TypeError):
            print(s.iteritems())
        with self.assertRaises(TypeError):
            print(s.iterkeys())