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