Skip to content

Commit

Permalink
Merge pull request #159 from SpiNNakerManchester/unittest_setup
Browse files Browse the repository at this point in the history
Unittest setup
  • Loading branch information
Christian-B committed Jun 21, 2021
2 parents 45aae68 + 15622a2 commit 803a200
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
34 changes: 26 additions & 8 deletions spinn_utilities/config_holder.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
logger = FormatAdapter(logging.getLogger(__file__))

__config = None

__default_config_files = []
__config_file = None
__unittest_mode = False


def add_default_cfg(default):
Expand All @@ -37,11 +37,19 @@ def add_default_cfg(default):
__default_config_files.append(default)


def clear_cfg_files():
global __config, __config_file
def clear_cfg_files(unittest_mode):
"""
Clears any previous set configs and config files.
After this method add_default_cfg and set_cfg_files need to be called
:param bool unittest_mode: Flag to put the holder into unittests mode
"""
global __config, __config_file, __unittest_mode
__config = None
__default_config_files.clear()
__config_file = None
__unittest_mode = unittest_mode


def set_cfg_files(configfile, default):
Expand All @@ -65,17 +73,16 @@ def _pre_load_config():
Loads configs due to early access to a config value
"""
# Only expected to happen in unittests but just in case
logger.warning(
"Accessing config before setup is not recommended as setup could"
" change some config values. ")
# If you getthis error during a unittest then unittest_step was not called
if not __unittest_mode:
raise Exception(
"Accessing config values before setup is not supported")
load_config()


def load_config():
"""
Reads in all the config files, resetting all values.
"""
global __config
if not __default_config_files:
Expand Down Expand Up @@ -173,10 +180,21 @@ def get_config_bool(section, option):
def set_config(section, option, value):
""" Sets the value of a config option.
This method should only be called by the simulator or by unittests
:param str section: What section to set the option in.
:param str option: What option to set.
:param object value: Value to set option to
"""
if __config is None:
if __unittest_mode:
load_config()
else:
# The actual error is that load_config should be called before
# set_config but this discourages the use outside of unittests
raise Exception(
"set_config should only be called by unittests "
"which should have called unittest_setup")
__config.set(section, option, value)
# Intentionally no try here to force tests that set to
# load_default_configs before AND after
Expand Down
5 changes: 3 additions & 2 deletions spinn_utilities/config_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@
BASE_CONFIG_FILE = "spinn_utilities.cfg"


def reset_configs():
def unittest_setup(unittest_mode):
"""
Resets the configs so only the local default config is included.
.. note::
This file should only be called from SpiNNUtils/unittests
:param unittest_mode: Flag to indicate in unittests
"""
clear_cfg_files()
clear_cfg_files(True)
add_spinn_utilities_cfg()


Expand Down

0 comments on commit 803a200

Please sign in to comment.