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

"""
test .ini parsing

ensure our .ini parser is doing what we want; to be deprecated for
python's standard ConfigParser when 2.7 is reality so OrderedDict
is the default:

http://docs.python.org/2/library/configparser.html
"""

from textwrap import dedent

import mozunit
import pytest
from manifestparser import read_ini
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


def test_inline_comments(parse_manifest):
    result = parse_manifest(
        """
    [test_felinicity.py]
    kittens = true # This test requires kittens
    cats = false#but not cats
    """
    )[0][1]

    # make sure inline comments get stripped out, but comments without a space in front don't
    assert result["kittens"] == "true"
    assert result["cats"] == "false#but not cats"


def test_line_continuation(parse_manifest):
    result = parse_manifest(
        """
    [test_caninicity.py]
    breeds =
      sheppard
      retriever
      terrier

    [test_cats_and_dogs.py]
      cats=yep
      dogs=
        yep
          yep
    birds=nope
      fish=nope
    """
    )
    assert result[0][1]["breeds"].split() == ["sheppard", "retriever", "terrier"]
    assert result[1][1]["cats"] == "yep"
    assert result[1][1]["dogs"].split() == ["yep", "yep"]
    assert result[1][1]["birds"].split() == ["nope", "fish=nope"]


def test_dupes_error(parse_manifest):
    dupes = """
    [test_dupes.py]
    foo = bar
    foo = baz
    """
    with pytest.raises(AssertionError):
        parse_manifest(dupes, strict=True)

    with pytest.raises(AssertionError):
        parse_manifest(dupes, strict=False)


def test_defaults_handling(parse_manifest):
    manifest = """
    [DEFAULT]
    flower = rose
    skip-if = true

    [test_defaults]
    """

    result = parse_manifest(manifest)[0][1]
    assert result["flower"] == "rose"
    assert result["skip-if"] == "true"

    result = parse_manifest(
        manifest,
        defaults={
            "flower": "tulip",
            "colour": "pink",
            "skip-if": "false",
        },
    )[0][1]
    assert result["flower"] == "rose"
    assert result["colour"] == "pink"
    assert result["skip-if"] == "false\ntrue"

    result = parse_manifest(manifest.replace("DEFAULT", "default"))[0][1]
    assert result["flower"] == "rose"
    assert result["skip-if"] == "true"


def test_multiline_skip(parse_manifest):
    manifest = """
    [test_multiline_skip]
    skip-if =
        os == "mac"  # bug 123
        os == "linux" && debug  # bug 456
    """

    result = parse_manifest(manifest)[0][1]
    assert (
        result["skip-if"].replace("\r\n", "\n")
        == dedent(
            """
        os == "mac"
        os == "linux" && debug
    """
        ).rstrip()
    )


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