Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
[test_section]
key1 = value1
key2 = 789
''')
cli_parameter_user_value = 'nucleo_f429zi'
# Create test config
config = configparser.ConfigParser(interpolation=None)
config.read_dict({
'project': {
'platformio_ini_patch_content': config_parameter_user_value,
'board': TEST_PROJECT_BOARD
}
})
# ... save it
with FIXTURE_PATH.joinpath(stm32pio.settings.config_file_name).open(mode='w') as config_file:
config.write(config_file)
# On project creation we should interpret the CLI-provided values as superseding to the saved ones and
# saved ones, in turn, as superseding to the default ones
project = stm32pio.lib.Stm32pio(FIXTURE_PATH, parameters={'project': {'board': cli_parameter_user_value}})
project.pio_init()
project.patch()
# Actually, we can parse the platformio.ini via the configparser but this is simpler in our case
after_patch_content = FIXTURE_PATH.joinpath('platformio.ini').read_text()
self.assertIn(config_parameter_user_value, after_patch_content,
msg="User config parameter has not been prioritized over the default one")
self.assertIn(cli_parameter_user_value, after_patch_content,
msg="User CLI parameter has not been prioritized over the saved one")
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_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")
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")
def _save_config(config: configparser.ConfigParser, path: pathlib.Path, logger: logging.Logger) -> int:
"""
Writes the ConfigParser 'config' to the file 'path' and logs using the Logger 'logger'.
We declare this helper function which can be safely invoked by both internal methods and outer code. The latter
case is suitable for using in weakref' finalizer objects as one of its main requirement is to not keep
references to the destroyable object in any of the finalizer argument so the ordinary bound class method does
not fit well.
Returns:
0 on success, -1 otherwise
"""
try:
with path.joinpath(stm32pio.settings.config_file_name).open(mode='w') as config_file:
config.write(config_file)
logger.debug(f"{stm32pio.settings.config_file_name} config file has been saved")
return 0
except Exception as e:
logger.warning(f"cannot save the config: {e}", exc_info=logger.isEnabledFor(logging.DEBUG))
return -1
def _save_config(config: configparser.ConfigParser, path: pathlib.Path, logger: logging.Logger) -> int:
"""
Writes the ConfigParser 'config' to the file 'path' and logs using the Logger 'logger'.
We declare this helper function which can be safely invoked by both internal methods and outer code. The latter
case is suitable for using in weakref' finalizer objects as one of its main requirement is to not keep
references to the destroyable object in any of the finalizer argument so the ordinary bound class method does
not fit well.
Returns:
0 on success, -1 otherwise
"""
try:
with path.joinpath(stm32pio.settings.config_file_name).open(mode='w') as config_file:
config.write(config_file)
logger.debug(f"{stm32pio.settings.config_file_name} config file has been saved")
return 0
except Exception as e:
logger.warning(f"cannot save the config: {e}", exc_info=logger.isEnabledFor(logging.DEBUG))
return -1