File: test-admin-commands.py

package info (click to toggle)
mercurial 7.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 44,824 kB
  • sloc: python: 206,444; ansic: 56,415; tcl: 3,715; sh: 1,797; lisp: 1,483; cpp: 864; makefile: 752; javascript: 649; xml: 36
file content (399 lines) | stat: -rw-r--r-- 13,377 bytes parent folder | download | duplicates (3)
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# Test admin commands

import functools
import unittest
from mercurial.i18n import _
from mercurial import error, ui as uimod
from mercurial import registrar
from mercurial.admin import verify


class TestAdminVerifyFindChecks(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.ui = uimod.ui.load()
        self.repo = b"fake-repo"

        def cleanup_table(self):
            self.table = {}
            self.alias_table = {}
            self.pyramid = {}

        self.addCleanup(cleanup_table, self)

    def setUp(self):
        self.table = {}
        self.alias_table = {}
        self.pyramid = {}
        check = registrar.verify_check(self.table, self.alias_table)

        # mock some fake check method for tests purpose
        @check(
            b"test.dummy",
            alias=b"dummy",
            options=[],
        )
        def check_dummy(ui, repo, **options):
            return options

        @check(
            b"test.fake",
            alias=b"fake",
            options=[
                (b'a', False, _(b'a boolean value (default: False)')),
                (b'b', True, _(b'a boolean value (default: True)')),
                (b'c', [], _(b'a list')),
            ],
        )
        def check_fake(ui, repo, **options):
            return options

        # alias in the middle of a hierarchy
        check(
            b"test.noop",
            alias=b"noop",
            options=[],
        )(verify.noop_func)

        @check(
            b"test.noop.deeper",
            alias=b"deeper",
            options=[
                (b'y', True, _(b'a boolean value (default: True)')),
                (b'z', [], _(b'a list')),
            ],
        )
        def check_noop_deeper(ui, repo, **options):
            return options

    # args wrapper utilities
    def find_checks(self, name):
        return verify.find_checks(
            name=name,
            table=self.table,
            alias_table=self.alias_table,
            full_pyramid=self.pyramid,
        )

    def pass_options(self, checks, options):
        return verify.pass_options(
            self.ui,
            checks,
            options,
            table=self.table,
            alias_table=self.alias_table,
            full_pyramid=self.pyramid,
        )

    def get_checks(self, names, options):
        return verify.get_checks(
            self.repo,
            self.ui,
            names=names,
            options=options,
            table=self.table,
            alias_table=self.alias_table,
            full_pyramid=self.pyramid,
        )

    # tests find_checks
    def test_find_checks_empty_name(self):
        with self.assertRaises(error.InputError):
            self.find_checks(name=b"")

    def test_find_checks_wrong_name(self):
        with self.assertRaises(error.InputError):
            self.find_checks(name=b"unknown")

    def test_find_checks_dummy(self):
        name = b"test.dummy"
        found = self.find_checks(name=name)
        self.assertEqual(len(found), 1)
        self.assertIn(name, found)
        meth = found[name]
        self.assertTrue(callable(meth))
        self.assertEqual(len(meth.options), 0)

    def test_find_checks_fake(self):
        name = b"test.fake"
        found = self.find_checks(name=name)
        self.assertEqual(len(found), 1)
        self.assertIn(name, found)
        meth = found[name]
        self.assertTrue(callable(meth))
        self.assertEqual(len(meth.options), 3)

    def test_find_checks_noop(self):
        name = b"test.noop.deeper"
        found = self.find_checks(name=name)
        self.assertEqual(len(found), 1)
        self.assertIn(name, found)
        meth = found[name]
        self.assertTrue(callable(meth))
        self.assertEqual(len(meth.options), 2)

    def test_find_checks_from_aliases(self):
        found = self.find_checks(name=b"dummy")
        self.assertEqual(len(found), 1)
        self.assertIn(b"test.dummy", found)

        found = self.find_checks(name=b"fake")
        self.assertEqual(len(found), 1)
        self.assertIn(b"test.fake", found)

        found = self.find_checks(name=b"deeper")
        self.assertEqual(len(found), 1)
        self.assertIn(b"test.noop.deeper", found)

    def test_find_checks_from_root(self):
        found = self.find_checks(name=b"test")
        self.assertEqual(len(found), 3)
        self.assertIn(b"test.dummy", found)
        self.assertIn(b"test.fake", found)
        self.assertIn(b"test.noop.deeper", found)

    def test_find_checks_from_intermediate(self):
        found = self.find_checks(name=b"test.noop")
        self.assertEqual(len(found), 1)
        self.assertIn(b"test.noop.deeper", found)

    def test_find_checks_from_parent_dot_name(self):
        found = self.find_checks(name=b"noop.deeper")
        self.assertEqual(len(found), 1)
        self.assertIn(b"test.noop.deeper", found)

    # tests pass_options
    def test_pass_options_no_checks_no_options(self):
        checks = {}
        options = []

        with self.assertRaises(error.Error):
            self.pass_options(checks=checks, options=options)

    def test_pass_options_fake_empty_options(self):
        checks = self.find_checks(name=b"test.fake")
        funcs = {
            n: functools.partial(f, self.ui, self.repo)
            for n, f in checks.items()
        }
        options = []
        # should end with default options
        expected_options = {"a": False, "b": True, "c": []}
        func = self.pass_options(checks=funcs, options=options)

        self.assertDictEqual(func[b"test.fake"].keywords, expected_options)

    def test_pass_options_fake_non_existing_options(self):
        checks = self.find_checks(name=b"test.fake")
        funcs = {
            n: functools.partial(f, self.ui, self.repo)
            for n, f in checks.items()
        }

        with self.assertRaises(error.InputError):
            options = [b"test.fake:boom=yes"]
            self.pass_options(checks=funcs, options=options)

    def test_pass_options_fake_unrelated_options(self):
        checks = self.find_checks(name=b"test.fake")
        funcs = {
            n: functools.partial(f, self.ui, self.repo)
            for n, f in checks.items()
        }
        options = [b"test.noop.deeper:y=yes"]

        with self.assertRaises(error.InputError):
            self.pass_options(checks=funcs, options=options)

    def test_pass_options_fake_set_option(self):
        checks = self.find_checks(name=b"test.fake")
        funcs = {
            n: functools.partial(f, self.ui, self.repo)
            for n, f in checks.items()
        }
        options = [b"test.fake:a=yes"]
        expected_options = {"a": True, "b": True, "c": []}
        func = self.pass_options(checks=funcs, options=options)

        self.assertDictEqual(func[b"test.fake"].keywords, expected_options)

    def test_pass_options_fake_set_option_with_alias(self):
        checks = self.find_checks(name=b"test.fake")
        funcs = {
            n: functools.partial(f, self.ui, self.repo)
            for n, f in checks.items()
        }
        options = [b"fake:a=yes"]
        expected_options = {"a": True, "b": True, "c": []}
        func = self.pass_options(checks=funcs, options=options)

        self.assertDictEqual(func[b"test.fake"].keywords, expected_options)

    def test_pass_options_fake_set_all_option(self):
        checks = self.find_checks(name=b"test.fake")
        funcs = {
            n: functools.partial(f, self.ui, self.repo)
            for n, f in checks.items()
        }
        options = [b"test.fake:a=yes", b"test.fake:b=no", b"test.fake:c=0,1,2"]
        expected_options = {"a": True, "b": False, "c": [b"0", b"1", b"2"]}
        func = self.pass_options(checks=funcs, options=options)

        self.assertDictEqual(func[b"test.fake"].keywords, expected_options)

    def test_pass_options_fake_set_all_option_plus_unexisting(self):
        checks = self.find_checks(name=b"test.fake")
        funcs = {
            n: functools.partial(f, self.ui, self.repo)
            for n, f in checks.items()
        }
        options = [
            b"test.fake:a=yes",
            b"test.fake:b=no",
            b"test.fake:c=0,1,2",
            b"test.fake:d=0",
        ]

        with self.assertRaises(error.InputError):
            self.pass_options(checks=funcs, options=options)

    def test_pass_options_fake_duplicate_option(self):
        checks = self.find_checks(name=b"test.fake")
        funcs = {
            n: functools.partial(f, self.ui, self.repo)
            for n, f in checks.items()
        }
        options = [
            b"test.fake:a=yes",
            b"test.fake:a=no",
        ]

        with self.assertRaises(error.InputError):
            self.pass_options(checks=funcs, options=options)

    def test_pass_options_fake_set_malformed_option(self):
        checks = self.find_checks(name=b"test.fake")
        funcs = {
            n: functools.partial(f, self.ui, self.repo)
            for n, f in checks.items()
        }
        options = [
            b"test.fake:ayes",
            b"test.fake:b==no",
            b"test.fake=",
            b"test.fake:",
            b"test.fa=ke:d=0",
            b"test.fa=ke:d=0",
        ]

        for opt in options:
            with self.assertRaises(error.InputError):
                self.pass_options(checks=funcs, options=[opt])

    def test_pass_options_types(self):
        checks = self.find_checks(name=b"test.fake")
        funcs = {
            n: functools.partial(f, self.ui, self.repo)
            for n, f in checks.items()
        }
        # boolean, yes/no
        options = [b"test.fake:a=yes", b"test.fake:b=no"]
        expected_options = {"a": True, "b": False, "c": []}
        func = self.pass_options(checks=funcs, options=options)

        self.assertDictEqual(func[b"test.fake"].keywords, expected_options)

        # boolean, 0/1
        options = [b"test.fake:a=1", b"test.fake:b=0"]
        expected_options = {"a": True, "b": False, "c": []}
        func = self.pass_options(checks=funcs, options=options)

        self.assertDictEqual(func[b"test.fake"].keywords, expected_options)

        # boolean, true/false
        options = [b"test.fake:a=true", b"test.fake:b=false"]
        expected_options = {"a": True, "b": False, "c": []}
        func = self.pass_options(checks=funcs, options=options)

        self.assertDictEqual(func[b"test.fake"].keywords, expected_options)

        # boolean, wrong type
        options = [b"test.fake:a=si"]
        with self.assertRaises(error.InputError):
            self.pass_options(checks=funcs, options=options)

        # lists
        options = [b"test.fake:c=0,1,2"]
        expected_options = {"a": False, "b": True, "c": [b"0", b"1", b"2"]}
        func = self.pass_options(checks=funcs, options=options)

        self.assertDictEqual(func[b"test.fake"].keywords, expected_options)

        options = [b"test.fake:c=x,y,z"]
        expected_options = {"a": False, "b": True, "c": [b"x", b"y", b"z"]}
        func = self.pass_options(checks=funcs, options=options)

        self.assertDictEqual(func[b"test.fake"].keywords, expected_options)

    # tests get_checks
    def test_get_checks_fake(self):
        funcs = self.get_checks(
            names=[b"test.fake"], options=[b"test.fake:a=yes"]
        )
        options = funcs.get(b"test.fake").keywords
        expected_options = {"a": True, "b": True, "c": []}
        self.assertDictEqual(options, expected_options)

    def test_get_checks_multiple_mixed_with_defaults(self):
        funcs = self.get_checks(
            names=[b"test.fake", b"test.noop.deeper", b"test.dummy"],
            options=[
                b"test.noop.deeper:y=no",
                b"test.noop.deeper:z=-1,0,1",
            ],
        )
        options = funcs.get(b"test.fake").keywords
        expected_options = {"a": False, "b": True, "c": []}
        self.assertDictEqual(options, expected_options)

        options = funcs.get(b"test.noop.deeper").keywords
        expected_options = {"y": False, "z": [b"-1", b"0", b"1"]}
        self.assertDictEqual(options, expected_options)

        options = funcs.get(b"test.dummy").keywords
        expected_options = {}
        self.assertDictEqual(options, expected_options)

    def test_broken_pyramid(self):
        """Check that we detect pyramids that can't resolve"""
        table = {}
        alias_table = {}
        pyramid = {}
        check = registrar.verify_check(table, alias_table)

        # Create two checks that clash
        @check(b"test.wrong.intermediate")
        def check_dummy(ui, repo, **options):
            return options

        @check(b"test.wrong.intermediate.thing")
        def check_fake(ui, repo, **options):
            return options

        with self.assertRaises(error.ProgrammingError) as e:
            verify.get_checks(
                self.repo,
                self.ui,
                names=[b"test.wrong.intermediate"],
                options=[],
                table=table,
                alias_table=alias_table,
                full_pyramid=pyramid,
            )
        assert "`verify.noop_func`" in str(e.exception), str(e.exception)


if __name__ == '__main__':
    import silenttestrunner

    silenttestrunner.main(__name__)