summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/manifestparser/tests/test_util.py
blob: e32ecbab79bf9d53b9064ad9dc8bd5c169adcebb (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
#!/usr/bin/env python

"""
Test how our utility functions are working.
"""

from textwrap import dedent

import mozunit
import pytest
from manifestparser import read_ini
from manifestparser.util import evaluate_list_from_string
from six import StringIO


@pytest.fixture(scope="module")
def parse_manifest():
    def inner(string, **kwargs):
        buf = StringIO()
        buf.write(dedent(string))
        buf.seek(0)
        return read_ini(buf, **kwargs)[0]

    return inner


@pytest.mark.parametrize(
    "test_manifest, expected_list",
    [
        [
            """
            [test_felinicity.py]
            kittens = true
            cats =
                "I",
                "Am",
                "A",
                "Cat",
            """,
            ["I", "Am", "A", "Cat"],
        ],
        [
            """
            [test_felinicity.py]
            kittens = true
            cats =
                ["I", 1],
                ["Am", 2],
                ["A", 3],
                ["Cat", 4],
            """,
            [
                ["I", 1],
                ["Am", 2],
                ["A", 3],
                ["Cat", 4],
            ],
        ],
    ],
)
def test_string_to_list_conversion(test_manifest, expected_list, parse_manifest):
    parsed_tests = parse_manifest(test_manifest)
    assert evaluate_list_from_string(parsed_tests[0][1]["cats"]) == expected_list


@pytest.mark.parametrize(
    "test_manifest, failure",
    [
        [
            """
            # This will fail since the elements are not enlosed in quotes
            [test_felinicity.py]
            kittens = true
            cats =
                I,
                Am,
                A,
                Cat,
            """,
            ValueError,
        ],
        [
            """
            # This will fail since the syntax is incorrect
            [test_felinicity.py]
            kittens = true
            cats =
                ["I", 1,
                ["Am", 2,
                ["A", 3],
                ["Cat", 4],
            """,
            SyntaxError,
        ],
    ],
)
def test_string_to_list_conversion_failures(test_manifest, failure, parse_manifest):
    parsed_tests = parse_manifest(test_manifest)
    with pytest.raises(failure):
        evaluate_list_from_string(parsed_tests[0][1]["cats"])


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