|  | 
| 1 |  | -# Copyright (c) 2022 - 2023, Oracle and/or its affiliates. All rights reserved. | 
|  | 1 | +# Copyright (c) 2022 - 2024, Oracle and/or its affiliates. All rights reserved. | 
| 2 | 2 | # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/. | 
| 3 | 3 | 
 | 
| 4 | 4 | """This module tests the defaults module.""" | 
| 5 | 5 | 
 | 
| 6 | 6 | import os | 
|  | 7 | +from pathlib import Path | 
| 7 | 8 | 
 | 
| 8 | 9 | import pytest | 
| 9 | 10 | 
 | 
| 10 |  | -from macaron.config.defaults import ConfigParser, create_defaults, defaults, load_defaults | 
|  | 11 | +from macaron.config.defaults import create_defaults, defaults, load_defaults | 
| 11 | 12 | from macaron.config.global_config import global_config | 
| 12 | 13 | 
 | 
| 13 | 14 | 
 | 
| @@ -41,93 +42,194 @@ def test_create_defaults_without_permission() -> None: | 
| 41 | 42 | 
 | 
| 42 | 43 | 
 | 
| 43 | 44 | @pytest.mark.parametrize( | 
| 44 |  | -    ("section", "item", "delimiter", "strip", "duplicated_ok", "expect"), | 
|  | 45 | +    ("user_config_input", "delimiter", "strip", "expect"), | 
| 45 | 46 |     [ | 
| 46 | 47 |         ( | 
| 47 |  | -            "test.list", | 
| 48 |  | -            "commas_string", | 
|  | 48 | +            """ | 
|  | 49 | +            [test.list] | 
|  | 50 | +            list = ,github.com, gitlab.com, space string, space string | 
|  | 51 | +            """, | 
| 49 | 52 |             ",", | 
| 50 | 53 |             False, | 
|  | 54 | +            ["", "github.com", " gitlab.com", " space string", " space string"], | 
|  | 55 | +        ), | 
|  | 56 | +        ( | 
|  | 57 | +            """ | 
|  | 58 | +            [test.list] | 
|  | 59 | +            list = ,github.com, gitlab.com, space string, space string | 
|  | 60 | +            """, | 
|  | 61 | +            ",", | 
| 51 | 62 |             True, | 
| 52 |  | -            ["", " gitlab.com", " space string", " space string", "github.com"], | 
|  | 63 | +            ["github.com", "gitlab.com", "space string", "space string"], | 
| 53 | 64 |         ), | 
| 54 |  | -        ("test.list", "commas_string", ",", False, False, ["", " gitlab.com", " space string", "github.com"]), | 
| 55 |  | -        ("test.list", "commas_string", ",", True, True, ["github.com", "gitlab.com", "space string", "space string"]), | 
| 56 |  | -        ("test.list", "commas_string", ",", True, False, ["github.com", "gitlab.com", "space string"]), | 
| 57 |  | -        # Using None as the delimiter parameter will ignore cleanup | 
| 58 |  | -        ("test.list", "default", None, True, False, ["comma_ended,", "github.com", "space", "string"]), | 
| 59 |  | -        ("test.list", "default", None, False, False, ["comma_ended,", "github.com", "space", "string"]), | 
|  | 65 | +        # Using None as the `delimiter` will also remove leading and trailing spaces of elements. Therefore, | 
|  | 66 | +        # the results must be the same whether `strip` is set to True or False. | 
| 60 | 67 |         ( | 
| 61 |  | -            "test.list", | 
| 62 |  | -            "default", | 
|  | 68 | +            """ | 
|  | 69 | +            [test.list] | 
|  | 70 | +            list = | 
|  | 71 | +                github.com | 
|  | 72 | +                comma_ended, | 
|  | 73 | +                space string | 
|  | 74 | +                space string | 
|  | 75 | +            """, | 
| 63 | 76 |             None, | 
| 64 |  | -            False, | 
| 65 | 77 |             True, | 
| 66 |  | -            ["comma_ended,", "github.com", "space", "space", "string", "string"], | 
|  | 78 | +            ["github.com", "comma_ended,", "space", "string", "space", "string"], | 
| 67 | 79 |         ), | 
| 68 |  | -        ("test.list", "one_line", None, True, False, ["comma_ended,", "github.com", "space", "string"]), | 
| 69 | 80 |         ( | 
| 70 |  | -            "test.list", | 
| 71 |  | -            "one_line", | 
|  | 81 | +            """ | 
|  | 82 | +            [test.list] | 
|  | 83 | +            list = | 
|  | 84 | +                github.com | 
|  | 85 | +                comma_ended, | 
|  | 86 | +                space string | 
|  | 87 | +                space string | 
|  | 88 | +            """, | 
|  | 89 | +            None, | 
|  | 90 | +            False, | 
|  | 91 | +            ["github.com", "comma_ended,", "space", "string", "space", "string"], | 
|  | 92 | +        ), | 
|  | 93 | +        ( | 
|  | 94 | +            """ | 
|  | 95 | +            [test.list] | 
|  | 96 | +            list = github.com comma_ended, space string space string | 
|  | 97 | +            """, | 
| 72 | 98 |             None, | 
| 73 | 99 |             True, | 
| 74 |  | -            True, | 
| 75 |  | -            ["comma_ended,", "github.com", "space", "space", "string", "string"], | 
|  | 100 | +            ["github.com", "comma_ended,", "space", "string", "space", "string"], | 
| 76 | 101 |         ), | 
| 77 | 102 |     ], | 
| 78 | 103 | ) | 
| 79 | 104 | def test_get_str_list_with_custom_delimiter( | 
| 80 |  | -    section: str, item: str, delimiter: str, strip: bool, duplicated_ok: bool, expect: list[str] | 
|  | 105 | +    user_config_input: str, | 
|  | 106 | +    delimiter: str, | 
|  | 107 | +    strip: bool, | 
|  | 108 | +    expect: list[str], | 
|  | 109 | +    tmp_path: Path, | 
| 81 | 110 | ) -> None: | 
| 82 | 111 |     """Test getting a list of strings from defaults.ini using a custom delimiter.""" | 
| 83 |  | -    content = """ | 
| 84 |  | -    [test.list] | 
| 85 |  | -    default = | 
| 86 |  | -        github.com | 
| 87 |  | -        comma_ended, | 
| 88 |  | -        space string | 
| 89 |  | -        space string | 
| 90 |  | -    empty = | 
| 91 |  | -    one_line = github.com comma_ended, space string space string | 
| 92 |  | -    commas_string = ,github.com, gitlab.com, space string, space string | 
| 93 |  | -    """ | 
| 94 |  | -    custom_defaults = ConfigParser() | 
| 95 |  | -    custom_defaults.read_string(content) | 
|  | 112 | +    user_config_path = os.path.join(tmp_path, "config.ini") | 
|  | 113 | +    with open(user_config_path, "w", encoding="utf-8") as user_config_file: | 
|  | 114 | +        user_config_file.write(user_config_input) | 
|  | 115 | +    load_defaults(user_config_path) | 
| 96 | 116 | 
 | 
| 97 |  | -    results = custom_defaults.get_list(section, item, delimiter=delimiter, strip=strip, duplicated_ok=duplicated_ok) | 
| 98 |  | -    results.sort() | 
|  | 117 | +    results = defaults.get_list(section="test.list", option="list", delimiter=delimiter, strip=strip) | 
| 99 | 118 |     assert results == expect | 
| 100 | 119 | 
 | 
| 101 | 120 | 
 | 
| 102 | 121 | @pytest.mark.parametrize( | 
| 103 |  | -    ("section", "item", "strip", "duplicated_ok", "fallback", "expect"), | 
|  | 122 | +    ("user_config_input", "expect"), | 
| 104 | 123 |     [ | 
| 105 |  | -        ("test.list", "default", True, False, [], ["comma_ended,", "github.com", "space string"]), | 
| 106 |  | -        ("test.list", "default", True, True, [], ["comma_ended,", "github.com", "space string", "space string"]), | 
| 107 |  | -        ("test.list", "empty", False, True, [], [""]), | 
| 108 |  | -        ("test.list", "empty", True, True, [], []), | 
| 109 |  | -        # Test for an item that does not exist in defaults.ini | 
| 110 |  | -        ("test.list", "item_not_exist", True, True, [], []), | 
| 111 |  | -        # Test value for fallback. The fallback value must be returned as is and shouldn't be modified by the method. | 
| 112 |  | -        ("test.list", "item_not_exist", True, True, ["", "fallback_val"], ["", "fallback_val"]), | 
|  | 124 | +        ( | 
|  | 125 | +            """ | 
|  | 126 | +            [test.list] | 
|  | 127 | +            list = ,github.com, gitlab.com, space string, space string | 
|  | 128 | +            """, | 
|  | 129 | +            [",github.com, gitlab.com, space string, space string"], | 
|  | 130 | +        ), | 
|  | 131 | +        ( | 
|  | 132 | +            """ | 
|  | 133 | +            [test.list] | 
|  | 134 | +            list = | 
|  | 135 | +                github.com | 
|  | 136 | +                comma_ended, | 
|  | 137 | +                space string | 
|  | 138 | +                space string | 
|  | 139 | +            """, | 
|  | 140 | +            ["github.com", "comma_ended,", "space string", "space string"], | 
|  | 141 | +        ), | 
|  | 142 | +        ( | 
|  | 143 | +            """ | 
|  | 144 | +            [test.list] | 
|  | 145 | +            list = | 
|  | 146 | +            """, | 
|  | 147 | +            [], | 
|  | 148 | +        ), | 
| 113 | 149 |     ], | 
| 114 | 150 | ) | 
| 115 |  | -def test_get_str_list_with_default_delimiter( | 
| 116 |  | -    section: str, item: str, strip: bool, duplicated_ok: bool, fallback: list[str], expect: list[str] | 
|  | 151 | +def test_get_str_list_default( | 
|  | 152 | +    user_config_input: str, | 
|  | 153 | +    expect: list[str], | 
|  | 154 | +    tmp_path: Path, | 
| 117 | 155 | ) -> None: | 
| 118 |  | -    """Test getting a list of strings from defaults.ini using the default delimiter.""" | 
|  | 156 | +    """Test default behavior of getting a list of strings from an option in defaults.ini.""" | 
|  | 157 | +    user_config_path = os.path.join(tmp_path, "config.ini") | 
|  | 158 | +    with open(user_config_path, "w", encoding="utf-8") as user_config_file: | 
|  | 159 | +        user_config_file.write(user_config_input) | 
|  | 160 | +    load_defaults(user_config_path) | 
|  | 161 | + | 
|  | 162 | +    results = defaults.get_list(section="test.list", option="list") | 
|  | 163 | +    assert results == expect | 
|  | 164 | + | 
|  | 165 | + | 
|  | 166 | +@pytest.mark.parametrize( | 
|  | 167 | +    ("section", "option", "fallback", "expect"), | 
|  | 168 | +    [ | 
|  | 169 | +        ( | 
|  | 170 | +            "section", | 
|  | 171 | +            "non-existing", | 
|  | 172 | +            None, | 
|  | 173 | +            [], | 
|  | 174 | +        ), | 
|  | 175 | +        ( | 
|  | 176 | +            "non-existing", | 
|  | 177 | +            "option", | 
|  | 178 | +            None, | 
|  | 179 | +            [], | 
|  | 180 | +        ), | 
|  | 181 | +        ( | 
|  | 182 | +            "non-existing", | 
|  | 183 | +            "non-existing", | 
|  | 184 | +            None, | 
|  | 185 | +            [], | 
|  | 186 | +        ), | 
|  | 187 | +        ( | 
|  | 188 | +            "section", | 
|  | 189 | +            "non-existing", | 
|  | 190 | +            ["some", "fallback", "value"], | 
|  | 191 | +            ["some", "fallback", "value"], | 
|  | 192 | +        ), | 
|  | 193 | +        ( | 
|  | 194 | +            "non-existing", | 
|  | 195 | +            "option", | 
|  | 196 | +            ["some", "fallback", "value"], | 
|  | 197 | +            ["some", "fallback", "value"], | 
|  | 198 | +        ), | 
|  | 199 | +        ( | 
|  | 200 | +            "non-existing", | 
|  | 201 | +            "non-existing", | 
|  | 202 | +            ["some", "fallback", "value"], | 
|  | 203 | +            ["some", "fallback", "value"], | 
|  | 204 | +        ), | 
|  | 205 | +    ], | 
|  | 206 | +) | 
|  | 207 | +def test_get_str_list_default_with_errors( | 
|  | 208 | +    section: str, | 
|  | 209 | +    option: str, | 
|  | 210 | +    fallback: list[str] | None, | 
|  | 211 | +    expect: list[str], | 
|  | 212 | +    tmp_path: Path, | 
|  | 213 | +) -> None: | 
|  | 214 | +    """Test default behavior of getting a list of strings with errors from defaults.ini.""" | 
| 119 | 215 |     content = """ | 
| 120 |  | -    [test.list] | 
| 121 |  | -    default = | 
|  | 216 | +    [section] | 
|  | 217 | +    option = | 
| 122 | 218 |         github.com | 
| 123 | 219 |         comma_ended, | 
| 124 | 220 |         space string | 
| 125 | 221 |         space string | 
| 126 |  | -    empty = | 
| 127 | 222 |     """ | 
| 128 |  | -    custom_defaults = ConfigParser() | 
| 129 |  | -    custom_defaults.read_string(content) | 
| 130 |  | - | 
| 131 |  | -    results = custom_defaults.get_list(section, item, strip=strip, fallback=fallback, duplicated_ok=duplicated_ok) | 
| 132 |  | -    results.sort() | 
| 133 |  | -    assert results == expect | 
|  | 223 | +    user_config_path = os.path.join(tmp_path, "config.ini") | 
|  | 224 | +    with open(user_config_path, "w", encoding="utf-8") as user_config_file: | 
|  | 225 | +        user_config_file.write(content) | 
|  | 226 | +    load_defaults(user_config_path) | 
|  | 227 | + | 
|  | 228 | +    assert ( | 
|  | 229 | +        defaults.get_list( | 
|  | 230 | +            section=section, | 
|  | 231 | +            option=option, | 
|  | 232 | +            fallback=fallback, | 
|  | 233 | +        ) | 
|  | 234 | +        == expect | 
|  | 235 | +    ) | 
0 commit comments