1#!/usr/bin/env python
2
3"""
4test .ini parsing
5
6ensure our .ini parser is doing what we want; to be deprecated for
7python's standard ConfigParser when 2.7 is reality so OrderedDict
8is the default:
9
10http://docs.python.org/2/library/configparser.html
11"""
12
13from __future__ import absolute_import
14
15from textwrap import dedent
16
17import mozunit
18import pytest
19from six import StringIO
20
21from manifestparser import read_ini
22
23
24@pytest.fixture(scope="module")
25def parse_manifest():
26    def inner(string, **kwargs):
27        buf = StringIO()
28        buf.write(dedent(string))
29        buf.seek(0)
30        return read_ini(buf, **kwargs)[0]
31
32    return inner
33
34
35def test_inline_comments(parse_manifest):
36    result = parse_manifest(
37        """
38    [test_felinicity.py]
39    kittens = true # This test requires kittens
40    cats = false#but not cats
41    """
42    )[0][1]
43
44    # make sure inline comments get stripped out, but comments without a space in front don't
45    assert result["kittens"] == "true"
46    assert result["cats"] == "false#but not cats"
47
48
49def test_line_continuation(parse_manifest):
50    result = parse_manifest(
51        """
52    [test_caninicity.py]
53    breeds =
54      sheppard
55      retriever
56      terrier
57
58    [test_cats_and_dogs.py]
59      cats=yep
60      dogs=
61        yep
62          yep
63    birds=nope
64      fish=nope
65    """
66    )
67    assert result[0][1]["breeds"].split() == ["sheppard", "retriever", "terrier"]
68    assert result[1][1]["cats"] == "yep"
69    assert result[1][1]["dogs"].split() == ["yep", "yep"]
70    assert result[1][1]["birds"].split() == ["nope", "fish=nope"]
71
72
73def test_dupes_error(parse_manifest):
74    dupes = """
75    [test_dupes.py]
76    foo = bar
77    foo = baz
78    """
79    with pytest.raises(AssertionError):
80        parse_manifest(dupes, strict=True)
81
82    with pytest.raises(AssertionError):
83        parse_manifest(dupes, strict=False)
84
85
86def test_defaults_handling(parse_manifest):
87    manifest = """
88    [DEFAULT]
89    flower = rose
90    skip-if = true
91
92    [test_defaults]
93    """
94
95    result = parse_manifest(manifest)[0][1]
96    assert result["flower"] == "rose"
97    assert result["skip-if"] == "true"
98
99    result = parse_manifest(
100        manifest,
101        defaults={
102            "flower": "tulip",
103            "colour": "pink",
104            "skip-if": "false",
105        },
106    )[0][1]
107    assert result["flower"] == "rose"
108    assert result["colour"] == "pink"
109    assert result["skip-if"] == "false\ntrue"
110
111    result = parse_manifest(manifest.replace("DEFAULT", "default"))[0][1]
112    assert result["flower"] == "rose"
113    assert result["skip-if"] == "true"
114
115
116def test_multiline_skip(parse_manifest):
117    manifest = """
118    [test_multiline_skip]
119    skip-if =
120        os == "mac"  # bug 123
121        os == "linux" && debug  # bug 456
122    """
123
124    result = parse_manifest(manifest)[0][1]
125    assert (
126        result["skip-if"].replace("\r\n", "\n")
127        == dedent(
128            """
129        os == "mac"
130        os == "linux" && debug
131    """
132        ).rstrip()
133    )
134
135
136if __name__ == "__main__":
137    mozunit.main()
138