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_init(self):
"""
Check for config creation and parameters presence
"""
result = subprocess.run([PYTHON_EXEC, STM32PIO_MAIN_SCRIPT, 'init', '-d', str(FIXTURE_PATH),
'-b', TEST_PROJECT_BOARD], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
self.assertEqual(result.returncode, 0, msg="Non-zero return code")
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)
config.read(str(FIXTURE_PATH.joinpath(stm32pio.settings.config_file_name)))
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 saved config file"):
self.assertIsNotNone(config.get(section, option, fallback=None))
self.assertEqual(config.get('project', 'board', fallback="Not found"), TEST_PROJECT_BOARD,
msg="'board' has not been set")
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")
Prepare ConfigParser config for the project. Order of getting values (masking) (higher levels overwrites lower):
default dict (settings module) => config file stm32pio.ini => user-given (runtime) values
(via CLI or another way)
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]: