Objects that implicitely load configuration are a nightmare to test
A lot of the configuration in different objects is loaded using swh.core.config
either implicitely (using CONFIG_BASE_FILENAME
and the SWHConfig
mixin) or explicitely (config.load_named_config
) are very difficult to test.
Since they usually load their configuration and initialize their objects given this configuration in __init__()
, you have to either:
-
mock.patch
theconfig.load_named_config
function - subclass and to override the
__init__
method of the class - move the initialization behavior in another method
- hope that the bogus default config (or any locally installed config) won't crash the initialization, and then mock the attributes to replace them with proper values
All of this is tied to the fact that classes shouldn't be allowed to load their config explicitely. The config should be loaded in the entry points of the program, and then passed around as state to the objects that need to be initialized.
Migrated from T826 (view on Phabricator)