Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_save_config(self):
"""
Explicitly save the config to file and look did that actually happen and whether all the information was
preserved
"""
# 'board' is non-default, 'project'-section parameter
project = stm32pio.lib.Stm32pio(FIXTURE_PATH, parameters={'project': {'board': TEST_PROJECT_BOARD}})
project.save_config()
self.assertTrue(FIXTURE_PATH.joinpath(stm32pio.settings.config_file_name).is_file(),
msg=f"{stm32pio.settings.config_file_name} file hasn't been created")
config = configparser.ConfigParser(interpolation=None)
self.assertGreater(len(config.read(str(FIXTURE_PATH.joinpath(stm32pio.settings.config_file_name)))), 0,
msg="Config is empty")
for section, parameters in stm32pio.settings.config_default.items():
for option, value in parameters.items():
with self.subTest(section=section, option=option,
msg="Section/key is not found in the saved config file"):
self.assertNotEqual(config.get(section, option, fallback="Not found"), "Not found")
self.assertEqual(config.get('project', 'board', fallback="Not found"), TEST_PROJECT_BOARD,
msg="'board' has not been set")
Returns:
new configparser.ConfigParser instance
"""
if runtime_parameters is None:
runtime_parameters = {}
config = configparser.ConfigParser(interpolation=None)
# Fill with default values ...
config.read_dict(copy.deepcopy(stm32pio.settings.config_default))
# ... then merge with user's config file values (if exist) ...
self.logger.debug(f"searching for {stm32pio.settings.config_file_name}...")
config.read(self.path.joinpath(stm32pio.settings.config_file_name))
ini_config = configparser.ConfigParser(interpolation=None)
ini_config.read(self.path.joinpath(stm32pio.settings.config_file_name))
runtime_config = configparser.ConfigParser(interpolation=None)
runtime_config.read_dict(runtime_parameters)
if len(ini_config.sections()):
if len(runtime_config.sections()):
for ini_sect in ini_config.sections():
if runtime_config.has_section(ini_sect):
for ini_key, ini_value in ini_config.items(ini_sect):
if runtime_config.get(ini_sect, ini_key, fallback=None) not in [None, ini_value]:
self.logger.info(f"given '{ini_key}' has taken a precedence over the .ini one")
else:
self.logger.debug(f"no or empty {stm32pio.settings.config_file_name} config file, will use the default one")