def test_cleanup(configdir, defaults): """ Test cleaning up the configuration files. """ conf = UserConfig(NAME, defaults=defaults, load=True, path=configdir, backup=True, version='0.1.0', raw_mode=True) assert osp.exists(conf.get_filename()) conf.cleanup() assert not osp.exists(conf.get_filename())
def test_bump_version(configdir, defaults): """ Test bumping the configuration version and assert that the defaults gets updated as expected. """ conf = UserConfig(NAME, defaults=defaults, load=True, path=configdir, backup=True, version='0.1.0', raw_mode=True) assert osp.exists(conf.get_filename()) assert osp.exists(osp.join(conf.path, 'defaults', 'defaults-0.1.0.ini')) # Delete option#4 in main section. del defaults[0][1]['option#4'] # Delete section#1 in defaults. del defaults[1] # Change the default value of option#5 in main. defaults[0][1]['option#5'] = 'main_opt5_new_default' # Add and option to main section. defaults[0][1]['option#999'] = 'main_opt999_default' # Add a new section to defaults. defaults.append(('section#999', {'option#999': 'sec999_opt999_default'})) del conf conf = UserConfig(NAME, defaults=defaults, load=True, path=configdir, backup=True, version='0.2.0', raw_mode=True) assert osp.exists(osp.join(conf.path, 'defaults', 'defaults-0.2.0.ini')) assert osp.exists(conf.get_filename() + '-0.1.0.bak') assert not conf.has_option('main', 'option#4') assert not conf.has_section('section#1') assert conf.get('main', 'option#5') == 'main_opt5_new_default' assert conf.get_default('main', 'option#5') == 'main_opt5_new_default' assert conf.get('main', 'option#999') == 'main_opt999_default' assert conf.get_default('main', 'option#999') == 'main_opt999_default' assert conf.get('section#999', 'option#999') == 'sec999_opt999_default' assert conf.get_default('section#999', 'option#999') == ( 'sec999_opt999_default')
def test_reset_to_defaults(configdir, defaults, mocker, save_value): """ Test resetting config values to defaults. """ conf = UserConfig(NAME, defaults=defaults, load=True, path=configdir, backup=True, version='0.1.0', raw_mode=True) conf._create_backup() # Change the value of option#3 in main section. conf.set('main', 'option#3', 65.23) assert conf.get('main', 'option#3') == 65.23 assert conf.get_default('main', 'option#3') == 24.567 assert filecmp.cmp( conf.get_filename(), conf.get_filename() + '.bak', shallow=False ) is False # Reset the config values to defaults. conf.reset_to_defaults(save=save_value) assert conf.get('main', 'option#3') == 24.567 assert conf.get_default('main', 'option#3') == 24.567 assert filecmp.cmp( conf.get_filename(), conf.get_filename() + '.bak', shallow=False ) is save_value
def test_files_creation(configdir, backup_value, defaults): """ Test that the .ini, .bak, and default files are created as expected in the specified app config directory. """ conf = UserConfig(NAME, defaults=defaults, load=True, path=configdir, backup=backup_value, version=CONF_VERSION, raw_mode=True) ini_name = conf.get_filename() bak_name = conf.get_filename() + '.bak' defaults_name = osp.join(conf.path, 'defaults', 'defaults-0.1.0.ini') assert ini_name == osp.join(configdir, NAME + '.ini') assert osp.exists(ini_name) assert osp.exists(defaults_name) assert osp.exists(bak_name) is False # Init UserConfig a second time to trigger the creation of a backup file. conf = UserConfig(NAME, defaults=defaults, load=True, path=configdir, backup=backup_value, version=CONF_VERSION, raw_mode=True) assert osp.exists(bak_name) is backup_value if backup_value is True: assert filecmp.cmp(ini_name, bak_name, shallow=False)