Commit b1d459ea authored by Gregory Mierzwinski's avatar Gregory Mierzwinski
Browse files

Bug 1713815 - Add a utility function for converting strings to lists. r=jmaher

This patch adds a new utility function that can be used for converting strings to lists. The format the strings must follow are shown in the function comment. In future patches, it'll be used in Raptor and MozPerftest.

Differential Revision: https://phabricator.services.mozilla.com/D126693
parent a75bd5a4
Loading
Loading
Loading
Loading
+21 −0
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@

from __future__ import absolute_import, print_function, unicode_literals

import ast
import os


@@ -25,3 +26,23 @@ def normsep(path):
        else:
            path = path.replace(os.altsep, "/")
    return path


def evaluate_list_from_string(list_string):
    """
    This is a utility function for converting a string obtained from a manifest
    into a list. If the string is not a valid list when converted, an error will be
    raised from `ast.eval_literal`. For example, you can convert entries like this
    into a list:
    ```
        test_settings=
            ["hello", "world"],
            [1, 10, 100],
        values=
            5,
            6,
            7,
            8,
    ```
    """
    return ast.literal_eval("[" + "".join(list_string.strip(",").split("\n")) + "]")
+1 −0
Original line number Diff line number Diff line
@@ -10,3 +10,4 @@ subsuite = mozbase
[test_convert_symlinks.py]
disabled = https://bugzilla.mozilla.org/show_bug.cgi?id=920938
[test_default_overrides.py]
[test_util.py]
+107 −0
Original line number Diff line number Diff line
#!/usr/bin/env python

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

from __future__ import absolute_import

from textwrap import dedent

import mozunit
import pytest
from six import StringIO

from manifestparser import read_ini
from manifestparser.util import evaluate_list_from_string


@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()