1 # SPDX-FileCopyrightText: 2025 Justin Myers
3 # SPDX-License-Identifier: MIT
5 from unittest import mock
7 from adafruit_blinka import load_settings_toml
12 import toml as tomllib
14 # pylint: disable=no-self-use,unused-argument
19 "test-hyphen": "test-hyphen",
22 "test_space": "test space",
23 "test_underscore": "test_underscore",
37 test_space="test space"
38 test_underscore="test_underscore"
39 test-hyphen="test-hyphen"
49 VALID_TOML_WITH_UNSUPPORTED_DATA_DICT = b"""
51 data = { key_1 = "value", key_2 = "value" }
54 VALID_TOML_WITH_UNSUPPORTED_DATA_LIST = b"""
59 VALID_TOML_WITH_UNSUPPORTED_DATA_MANY = b"""
61 data = { key_1 = "value", key_2 = "value" }
70 VALID_TOML_WITH_UNSUPPORTED_DATA_NESTED = b"""
76 class TestLoadSettingsToml:
77 @mock.patch("adafruit_blinka.os.path.isfile", mock.Mock(return_value=False))
78 def test_raises_with_no_file(self):
80 FileNotFoundError, match="settings.toml not cound in current directory."
82 load_settings_toml(return_toml=True)
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):
88 tomllib.TOMLDecodeError, match="Error with settings.toml file."
90 load_settings_toml(return_toml=True)
92 @mock.patch("adafruit_blinka.os.path.isfile", mock.Mock(return_value=True))
94 "builtins.open", mock.mock_open(read_data=VALID_TOML_WITH_UNSUPPORTED_DATA_DICT)
96 def test_raises_with_invalid_file_dict(self):
98 ValueError, match="The types: 'dict' are not supported in settings.toml."
100 load_settings_toml(return_toml=True)
102 @mock.patch("adafruit_blinka.os.path.isfile", mock.Mock(return_value=True))
104 "builtins.open", mock.mock_open(read_data=VALID_TOML_WITH_UNSUPPORTED_DATA_LIST)
106 def test_raises_with_invalid_file_list(self):
108 ValueError, match="The types: 'list' are not supported in settings.toml."
110 load_settings_toml(return_toml=True)
112 @mock.patch("adafruit_blinka.os.path.isfile", mock.Mock(return_value=True))
114 "builtins.open", mock.mock_open(read_data=VALID_TOML_WITH_UNSUPPORTED_DATA_MANY)
116 def test_raises_with_invalid_file_many(self):
119 match="The types: 'dict, list' are not supported in settings.toml.",
121 load_settings_toml(return_toml=True)
123 @mock.patch("adafruit_blinka.os.path.isfile", mock.Mock(return_value=True))
126 mock.mock_open(read_data=VALID_TOML_WITH_UNSUPPORTED_DATA_NESTED),
128 def test_raises_with_invalid_file_nested(self):
130 ValueError, match="The types: 'dict' are not supported in settings.toml."
132 load_settings_toml(return_toml=True)
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
141 assert load_settings_toml() is None
143 for key, value in CONVERTED_TOML.items():
144 assert os.getenv(key) == str(value)
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
153 assert load_settings_toml(return_toml=True) == CONVERTED_TOML
155 for key, value in CONVERTED_TOML.items():
156 assert os.getenv(key) == str(value)