|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +import tempfile |
| 6 | +import unittest |
| 7 | +from tempfile import NamedTemporaryFile |
| 8 | + |
| 9 | +import can |
| 10 | + |
| 11 | + |
| 12 | +class LoadConfigTest(unittest.TestCase): |
| 13 | + configuration = { |
| 14 | + "default": {"interface": "serial", "channel": "0"}, |
| 15 | + "one": {"interface": "kvaser", "channel": "1", "bitrate": 100000}, |
| 16 | + "two": {"channel": "2"}, |
| 17 | + } |
| 18 | + |
| 19 | + def setUp(self): |
| 20 | + # Create a temporary directory |
| 21 | + self.test_dir = tempfile.mkdtemp() |
| 22 | + |
| 23 | + def tearDown(self): |
| 24 | + # Remove the directory after the test |
| 25 | + shutil.rmtree(self.test_dir) |
| 26 | + |
| 27 | + def _gen_configration_file(self, sections): |
| 28 | + with NamedTemporaryFile( |
| 29 | + mode="w", dir=self.test_dir, delete=False |
| 30 | + ) as tmp_config_file: |
| 31 | + content = [] |
| 32 | + for section in sections: |
| 33 | + content.append("[{}]".format(section)) |
| 34 | + for k, v in self.configuration[section].items(): |
| 35 | + content.append("{} = {}".format(k, v)) |
| 36 | + tmp_config_file.write("\n".join(content)) |
| 37 | + return tmp_config_file.name |
| 38 | + |
| 39 | + def _dict_to_env(self, d): |
| 40 | + return {f"CAN_{k.upper()}": str(v) for k, v in d.items()} |
| 41 | + |
| 42 | + def test_config_default(self): |
| 43 | + tmp_config = self._gen_configration_file(["default"]) |
| 44 | + config = can.util.load_config(path=tmp_config) |
| 45 | + self.assertEqual(config, self.configuration["default"]) |
| 46 | + |
| 47 | + def test_config_whole_default(self): |
| 48 | + tmp_config = self._gen_configration_file(self.configuration) |
| 49 | + config = can.util.load_config(path=tmp_config) |
| 50 | + self.assertEqual(config, self.configuration["default"]) |
| 51 | + |
| 52 | + def test_config_whole_context(self): |
| 53 | + tmp_config = self._gen_configration_file(self.configuration) |
| 54 | + config = can.util.load_config(path=tmp_config, context="one") |
| 55 | + self.assertEqual(config, self.configuration["one"]) |
| 56 | + |
| 57 | + def test_config_merge_context(self): |
| 58 | + tmp_config = self._gen_configration_file(self.configuration) |
| 59 | + config = can.util.load_config(path=tmp_config, context="two") |
| 60 | + expected = self.configuration["default"] |
| 61 | + expected.update(self.configuration["two"]) |
| 62 | + self.assertEqual(config, expected) |
| 63 | + |
| 64 | + def test_config_merge_environment_to_context(self): |
| 65 | + tmp_config = self._gen_configration_file(self.configuration) |
| 66 | + env_data = {"interface": "serial", "bitrate": 125000} |
| 67 | + env_dict = self._dict_to_env(env_data) |
| 68 | + with unittest.mock.patch.dict("os.environ", env_dict): |
| 69 | + config = can.util.load_config(path=tmp_config, context="one") |
| 70 | + expected = self.configuration["one"] |
| 71 | + expected.update(env_data) |
| 72 | + self.assertEqual(config, expected) |
| 73 | + |
| 74 | + def test_config_whole_environment(self): |
| 75 | + tmp_config = self._gen_configration_file(self.configuration) |
| 76 | + env_data = {"interface": "socketcan", "channel": "3", "bitrate": 250000} |
| 77 | + env_dict = self._dict_to_env(env_data) |
| 78 | + with unittest.mock.patch.dict("os.environ", env_dict): |
| 79 | + config = can.util.load_config(path=tmp_config, context="one") |
| 80 | + expected = self.configuration["one"] |
| 81 | + expected.update(env_data) |
| 82 | + self.assertEqual(config, expected) |
| 83 | + |
| 84 | + |
| 85 | +if __name__ == "__main__": |
| 86 | + unittest.main() |
0 commit comments