Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | # SPDX-License-Identifier: GPL-2.0+ # Copyright (c) 2024 Google, Inc """Tests for bsettings.py""" import os import shutil import tempfile import unittest from unittest import mock from buildman import bsettings from u_boot_pylib import tools class TestBsettings(unittest.TestCase): """Test bsettings module""" def setUp(self): self._tmpdir = tempfile.mkdtemp() def tearDown(self): shutil.rmtree(self._tmpdir) def test_setup_no_file(self): """Test setup() with fname=None (no config file)""" bsettings.setup(None) # Should not raise, settings should be empty self.assertEqual([], bsettings.get_items('nonexistent')) def test_setup_default_missing(self): """Test setup() creates config when default file missing""" # Use a non-existent path for HOME fake_home = os.path.join(self._tmpdir, 'fakehome') os.makedirs(fake_home) config_file = os.path.join(fake_home, '.buildman') with mock.patch.dict(os.environ, {'HOME': fake_home}): with mock.patch('builtins.print'): bsettings.setup('') # Config file should have been created self.assertTrue(os.path.exists(config_file)) def test_setup_existing_file(self): """Test setup() reads existing config file""" config_file = os.path.join(self._tmpdir, 'test.buildman') tools.write_file(config_file, '[toolchain]\narm = /opt/arm\n', binary=False) bsettings.setup(config_file) items = bsettings.get_items('toolchain') self.assertEqual([('arm', '/opt/arm')], items) def test_add_file(self): """Test add_file() adds config data""" bsettings.setup(None) bsettings.add_file('[test]\nkey = value\n') items = bsettings.get_items('test') self.assertEqual([('key', 'value')], items) def test_add_section(self): """Test add_section() creates new section""" bsettings.setup(None) bsettings.add_section('newsection') # Section should exist but be empty self.assertEqual([], bsettings.get_items('newsection')) def test_get_items_missing_section(self): """Test get_items() returns empty list for missing section""" bsettings.setup(None) self.assertEqual([], bsettings.get_items('nonexistent')) def test_get_items_other_error(self): """Test get_items() re-raises non-NoSectionError exceptions""" bsettings.setup(None) with mock.patch.object(bsettings.settings, 'items', side_effect=ValueError('test error')): with self.assertRaises(ValueError): bsettings.get_items('test') def test_get_global_item_value(self): """Test get_global_item_value() retrieves global items""" bsettings.setup(None) bsettings.add_file('[global]\nmykey = myvalue\n') self.assertEqual('myvalue', bsettings.get_global_item_value('mykey')) self.assertIsNone(bsettings.get_global_item_value('missing')) def test_set_item(self): """Test set_item() sets value and writes to file""" config_file = os.path.join(self._tmpdir, 'test_set.buildman') tools.write_file(config_file, '[toolchain]\n', binary=False) bsettings.setup(config_file) bsettings.set_item('toolchain', 'newkey', 'newvalue') # Value should be set in memory items = dict(bsettings.get_items('toolchain')) self.assertEqual('newvalue', items['newkey']) # Value should be written to file content = tools.read_file(config_file, binary=False) self.assertIn('newkey', content) self.assertIn('newvalue', content) def test_set_item_no_file(self): """Test set_item() when config_fname is None""" # Explicitly reset config_fname to None bsettings.config_fname = None bsettings.setup(None) bsettings.add_section('test') # Should not raise even though there's no file to write bsettings.set_item('test', 'key', 'value') items = dict(bsettings.get_items('test')) self.assertEqual('value', items['key']) def test_create_buildman_config_file(self): """Test create_buildman_config_file() creates valid config""" config_file = os.path.join(self._tmpdir, 'new.buildman') bsettings.create_buildman_config_file(config_file) self.assertTrue(os.path.exists(config_file)) content = tools.read_file(config_file, binary=False) self.assertIn('[toolchain]', content) self.assertIn('[toolchain-prefix]', content) self.assertIn('[toolchain-alias]', content) self.assertIn('[make-flags]', content) def test_create_buildman_config_file_error(self): """Test create_buildman_config_file() handles IOError""" # Try to create file in non-existent directory bad_path = '/nonexistent/path/config' with mock.patch('builtins.print'): with self.assertRaises(IOError): bsettings.create_buildman_config_file(bad_path) if __name__ == '__main__': unittest.main() |