|
| 1 | +# SPDX-FileCopyrightText: 2025 Justin Myers |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +import os |
| 5 | +from unittest import mock |
| 6 | +import pytest |
| 7 | +from adafruit_blinka import load_settings_toml |
| 8 | + |
| 9 | +try: |
| 10 | + import tomllib |
| 11 | +except ImportError: |
| 12 | + import toml as tomllib |
| 13 | + |
| 14 | +# pylint: disable=no-self-use,unused-argument |
| 15 | + |
| 16 | +CONVERTED_TOML = { |
| 17 | + "123": 123, |
| 18 | + "test": "test", |
| 19 | + "test-hyphen": "test-hyphen", |
| 20 | + "test_bool": True, |
| 21 | + "test_number": 123, |
| 22 | + "test_space": "test space", |
| 23 | + "test_underscore": "test_underscore", |
| 24 | + "true": False, |
| 25 | +} |
| 26 | + |
| 27 | + |
| 28 | +INVALID_TOML = b""" |
| 29 | +# strings |
| 30 | +test=test |
| 31 | +""" |
| 32 | + |
| 33 | + |
| 34 | +VALID_TOML = b""" |
| 35 | +# strings |
| 36 | +test="test" |
| 37 | +test_space="test space" |
| 38 | +test_underscore="test_underscore" |
| 39 | +test-hyphen="test-hyphen" |
| 40 | +# number |
| 41 | +test_number=123 |
| 42 | +# bool |
| 43 | +test_bool=true |
| 44 | +# other |
| 45 | +123=123 |
| 46 | +true=false |
| 47 | +""" |
| 48 | + |
| 49 | +VALID_TOML_WITH_UNSUPPORTED_DATA_DICT = b""" |
| 50 | +# dict |
| 51 | +data = { key_1 = "value", key_2 = "value" } |
| 52 | +""" |
| 53 | + |
| 54 | +VALID_TOML_WITH_UNSUPPORTED_DATA_LIST = b""" |
| 55 | +# list |
| 56 | +numbers = [ 1, 2, 3 ] |
| 57 | +""" |
| 58 | + |
| 59 | +VALID_TOML_WITH_UNSUPPORTED_DATA_MANY = b""" |
| 60 | +# dict |
| 61 | +data = { key_1 = "value", key_2 = "value" } |
| 62 | +
|
| 63 | +# list |
| 64 | +numbers = [ 1, 2, 3 ] |
| 65 | +
|
| 66 | +[nested] |
| 67 | +test="test" |
| 68 | +""" |
| 69 | + |
| 70 | +VALID_TOML_WITH_UNSUPPORTED_DATA_NESTED = b""" |
| 71 | +[nested] |
| 72 | +test="test" |
| 73 | +""" |
| 74 | + |
| 75 | + |
| 76 | +class TestLoadSettingsToml: |
| 77 | + @mock.patch("adafruit_blinka.os.path.isfile", mock.Mock(return_value=False)) |
| 78 | + def test_raises_with_no_file(self): |
| 79 | + with pytest.raises( |
| 80 | + FileNotFoundError, match="settings.toml not cound in current directory." |
| 81 | + ): |
| 82 | + load_settings_toml(return_toml=True) |
| 83 | + |
| 84 | + @mock.patch("adafruit_blinka.os.path.isfile", mock.Mock(return_value=True)) |
| 85 | + @mock.patch("builtins.open", mock.mock_open(read_data=INVALID_TOML)) |
| 86 | + def test_raises_with_invalid_file(self): |
| 87 | + with pytest.raises( |
| 88 | + tomllib.TOMLDecodeError, match="Error with settings.toml file." |
| 89 | + ): |
| 90 | + load_settings_toml(return_toml=True) |
| 91 | + |
| 92 | + @mock.patch("adafruit_blinka.os.path.isfile", mock.Mock(return_value=True)) |
| 93 | + @mock.patch( |
| 94 | + "builtins.open", mock.mock_open(read_data=VALID_TOML_WITH_UNSUPPORTED_DATA_DICT) |
| 95 | + ) |
| 96 | + def test_raises_with_invalid_file_dict(self): |
| 97 | + with pytest.raises( |
| 98 | + ValueError, match="The types: 'dict' are not supported in settings.toml." |
| 99 | + ): |
| 100 | + load_settings_toml(return_toml=True) |
| 101 | + |
| 102 | + @mock.patch("adafruit_blinka.os.path.isfile", mock.Mock(return_value=True)) |
| 103 | + @mock.patch( |
| 104 | + "builtins.open", mock.mock_open(read_data=VALID_TOML_WITH_UNSUPPORTED_DATA_LIST) |
| 105 | + ) |
| 106 | + def test_raises_with_invalid_file_list(self): |
| 107 | + with pytest.raises( |
| 108 | + ValueError, match="The types: 'list' are not supported in settings.toml." |
| 109 | + ): |
| 110 | + load_settings_toml(return_toml=True) |
| 111 | + |
| 112 | + @mock.patch("adafruit_blinka.os.path.isfile", mock.Mock(return_value=True)) |
| 113 | + @mock.patch( |
| 114 | + "builtins.open", mock.mock_open(read_data=VALID_TOML_WITH_UNSUPPORTED_DATA_MANY) |
| 115 | + ) |
| 116 | + def test_raises_with_invalid_file_many(self): |
| 117 | + with pytest.raises( |
| 118 | + ValueError, |
| 119 | + match="The types: 'dict, list' are not supported in settings.toml.", |
| 120 | + ): |
| 121 | + load_settings_toml(return_toml=True) |
| 122 | + |
| 123 | + @mock.patch("adafruit_blinka.os.path.isfile", mock.Mock(return_value=True)) |
| 124 | + @mock.patch( |
| 125 | + "builtins.open", |
| 126 | + mock.mock_open(read_data=VALID_TOML_WITH_UNSUPPORTED_DATA_NESTED), |
| 127 | + ) |
| 128 | + def test_raises_with_invalid_file_nested(self): |
| 129 | + with pytest.raises( |
| 130 | + ValueError, match="The types: 'dict' are not supported in settings.toml." |
| 131 | + ): |
| 132 | + load_settings_toml(return_toml=True) |
| 133 | + |
| 134 | + @mock.patch("adafruit_blinka.os.path.isfile", mock.Mock(return_value=True)) |
| 135 | + @mock.patch("builtins.open", mock.mock_open(read_data=VALID_TOML)) |
| 136 | + @mock.patch.dict(os.environ, {}, clear=True) |
| 137 | + def test_returns_data(self): |
| 138 | + for key in CONVERTED_TOML: |
| 139 | + assert os.getenv(key) is None |
| 140 | + |
| 141 | + assert load_settings_toml() is None |
| 142 | + |
| 143 | + for key, value in CONVERTED_TOML.items(): |
| 144 | + assert os.getenv(key) == str(value) |
| 145 | + |
| 146 | + @mock.patch("adafruit_blinka.os.path.isfile", mock.Mock(return_value=True)) |
| 147 | + @mock.patch("builtins.open", mock.mock_open(read_data=VALID_TOML)) |
| 148 | + @mock.patch.dict(os.environ, {}, clear=True) |
| 149 | + def test_returns_data_when_asked(self): |
| 150 | + for key in CONVERTED_TOML: |
| 151 | + assert os.getenv(key) is None |
| 152 | + |
| 153 | + assert load_settings_toml(return_toml=True) == CONVERTED_TOML |
| 154 | + |
| 155 | + for key, value in CONVERTED_TOML.items(): |
| 156 | + assert os.getenv(key) == str(value) |
0 commit comments