File: test_parser.py

package info (click to toggle)
gitlabracadabra 2.8.0
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,520 kB
  • sloc: python: 12,305; javascript: 663; makefile: 4
file content (348 lines) | stat: -rw-r--r-- 10,782 bytes parent folder | download | duplicates (2)
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
#
# Copyright (C) 2019-2025 Mathieu Parent <math.parent@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import pytest

from gitlabracadabra.objects.group import GitLabracadabraGroup
from gitlabracadabra.objects.project import GitLabracadabraProject
from gitlabracadabra.parser import GitlabracadabraParser
from gitlabracadabra.tests import patch_open
from gitlabracadabra.tests.case import TestCase


class TestParser(TestCase):
    def test_from_yaml_file(self):
        contents_map = {
            "gitlabracadabra.yml": """
                message:
                  hello: world
                """,
        }
        with patch_open(contents_map):
            p = GitlabracadabraParser.from_yaml_file("gitlabracadabra.yml")
            assert dict(p._items()) == {"message": {"hello": "world"}}

    def test_include_str(self):
        contents_map = {
            "gitlabracadabra.yml": """
                include:
                  - included.yml
                """,
            "included.yml": """
                message:
                  hello: world
                """,
        }
        with patch_open(contents_map):
            p = GitlabracadabraParser.from_yaml_file("gitlabracadabra.yml")
            assert dict(p._items()) == {"message": {"hello": "world"}}

    def test_include_local(self):
        contents_map = {
            "gitlabracadabra.yml": """
                include:
                  - local: included.yml
                """,
            "included.yml": """
                message:
                  hello: world
                """,
        }
        with patch_open(contents_map):
            p = GitlabracadabraParser.from_yaml_file("gitlabracadabra.yml")
            assert dict(p._items()) == {"message": {"hello": "world"}}

    def test_include_recursion(self):
        contents_map = {
            "gitlabracadabra.yml": """
                include:
                  - included.yml
                """,
            "included.yml": """
                include:
                  - gitlabracadabra.yml
                """,
        }
        with patch_open(contents_map), pytest.raises(ValueError) as cm:
            GitlabracadabraParser.from_yaml_file("gitlabracadabra.yml")

        assert str(cm.value) == "gitlabracadabra.yml: nesting too deep in `include`"

    def test_include_forbidden_relative_path(self):
        contents_map = {
            "gitlabracadabra.yml": """
                include:
                  - ../included.yml
                """,
        }
        with patch_open(contents_map), pytest.raises(ValueError) as cm:
            GitlabracadabraParser.from_yaml_file("gitlabracadabra.yml")

        assert str(cm.value) == "gitlabracadabra.yml: forbidden path for `include`: ../included.yml"

    def test_include_forbidden_absolute_path(self):
        contents_map = {
            "gitlabracadabra.yml": """
                include:
                  - /included.yml
                """,
        }
        with patch_open(contents_map), pytest.raises(ValueError) as cm:
            GitlabracadabraParser.from_yaml_file("gitlabracadabra.yml")

        assert str(cm.value) == "gitlabracadabra.yml: forbidden path for `include`: /included.yml"

    def test_extend(self):
        p = GitlabracadabraParser.from_yaml(
            "-",
            """
            .hidden-key:
              param1: value1
              param2: value2
            shown-key:
              extends: .hidden-key
              param3: value3
            another-key:
              param4: value4
            """,
        )
        d = dict(p._items())

        assert ".hidden-key" not in d
        assert "shown-key" in d
        assert "another-key" in d
        assert d["another-key"] == {"param4": "value4"}

    def test_extend_not_found(self):
        p = GitlabracadabraParser.from_yaml(
            "-",
            """
            key2:
              extends:
                - key1
            """,
        )
        with pytest.raises(ValueError) as cm:
            dict(p._items())

        assert str(cm.value) == "- (`key1` from `key2`): key1 not found"

    def test_extend_recursion(self):
        p = GitlabracadabraParser.from_yaml(
            "-",
            """
            key1:
              extends: key3
              foo: bar
            key2:
              extends: key1
              foo2: bar2
            key3:
              extends: key2
              foo3: bar3
            """,
        )
        with pytest.raises(ValueError) as cm:
            dict(p._items())

        assert str(cm.value) == "- (key1): nesting too deep in `extends`"

    def test_extend_unknown_merge_strategy(self):
        p = GitlabracadabraParser.from_yaml(
            "-",
            """
            key1:
              hello: world
            key2:
              extends:
                - key1: keep-the-best
            """,
        )
        with pytest.raises(ValueError) as cm:
            dict(p._items())

        assert str(cm.value) == "- (`key1` from `key2`): Unknown merge strategy `keep-the-best`"

    def test_include_and_extend(self):
        contents_map = {
            "gitlabracadabra.yml": """
                group1/project1:
                  name: foo
                  extends: .e1
                  members:
                   baz: owner

                .e1:
                  branches:
                    - main
                    - develop
                  members:
                    bar: maintainer

                include:
                  - local: included.yml
                """,
            "included.yml": """
                .e1:
                  branches:
                    - main
                    - prod
                    - staging
                  members:
                    foo: developer
                """,
        }
        with patch_open(contents_map):
            p = GitlabracadabraParser.from_yaml_file("gitlabracadabra.yml")
            d = dict(p._items())

        assert ".e1" not in d
        assert "include" not in d
        assert d["group1/project1"] == {
            "name": "foo",
            "members": {"foo": "developer", "bar": "maintainer", "baz": "owner"},
            "branches": ["main", "develop"],
        }

    def test_extend_list(self):
        contents_map = {
            "gitlabracadabra.yml": """
                group1/project1:
                  name: foo
                  extends:
                    - .e1
                    - .e2: replace
                    - .e3
                  members:
                   foo: owner

                .e1:
                  branches:
                    - main1
                    - develop1
                  members:
                    bar: maintainer

                .e2:
                  branches:
                    - main2
                    - develop2
                  members:
                    baz: developer

                .e3:
                  branches:
                    - main3
                    - develop3
                  members:
                    bar: developer # override .e1 maintainer
                """,
        }
        with patch_open(contents_map):
            p = GitlabracadabraParser.from_yaml_file("gitlabracadabra.yml")
            d = dict(p._items())

        assert ".e1" not in d
        assert "include" not in d
        assert d["group1/project1"] == {
            "name": "foo",
            "members": {"foo": "owner", "bar": "developer"},
            "branches": ["main3", "develop3"],
        }

    def test_extend_list_aggregate(self):
        contents_map = {
            "gitlabracadabra.yml": """
                group1/project1:
                  name: foo
                  extends:
                    - .e1
                    - .e2: aggregate
                    - .e3
                  members:
                   foo: owner

                .e1:
                  branches:
                    - main1
                    - develop1
                  members:
                    bar: maintainer

                .e2:
                  branches:
                    - main2
                    - develop2
                  members:
                    baz: developer

                .e3:
                  branches:
                    - main3
                    - develop3
                  members:
                    bar: developer # override .e1 maintainer
                """,
        }
        with patch_open(contents_map):
            p = GitlabracadabraParser.from_yaml_file("gitlabracadabra.yml")
            d = dict(p._items())

        assert ".e1" not in d
        assert "include" not in d
        assert d["group1/project1"] == {
            "name": "foo",
            "members": {"foo": "owner", "bar": "developer", "baz": "developer"},
            "branches": ["main2", "develop2", "main3", "develop3"],
        }

    def test_objects(self):
        p = GitlabracadabraParser.from_yaml(
            "-",
            """
            .project-template:
              wiki_enabled: true
              issues_enabled: true
            group1/:
              description: My group
            group1/project1:
              extends: .project-template
              wiki_enabled: false
              description: My project
              buggy_param: Oh no!
            """,
        )
        d = p.objects()

        assert ".project-template" not in d

        assert "group1" in d
        group1 = d["group1"]
        assert isinstance(group1, GitLabracadabraGroup)
        assert group1._errors == []
        assert group1._content == {"description": "My group"}

        assert "group1/project1" in d
        project1 = d["group1/project1"]
        assert isinstance(project1, GitLabracadabraProject)
        assert len(project1._errors) == 1
        assert project1._errors[0].message == "Additional properties are not allowed " "('buggy_param' was unexpected)"
        assert project1._content == {
            "buggy_param": "Oh no!",
            "description": "My project",
            "issues_enabled": True,
            "wiki_enabled": False,
        }