def test_open_permissions_new_database(self): # ConfigurationFile.open() applies restrictive file permissions to # newly created configuration databases. config_file = os.path.join(self.make_dir(), "config") with ConfigurationFile.open(config_file): perms = FilePath(config_file).getPermissions() self.assertEqual("rw-r-----", perms.shorthand())
def test_open_yields_immutable_backend(self): config_file = os.path.join(self.make_dir(), "config") config_key = factory.make_name("key") with ConfigurationFile.open(config_file) as config: with ExpectedException(ConfigurationImmutable): config[config_key] = factory.make_name("value") with ExpectedException(ConfigurationImmutable): del config[config_key]
def test_open_and_close(self): # ConfigurationFile.open() returns a context manager. config_file = os.path.join(self.make_dir(), "config") config_ctx = ConfigurationFile.open(config_file) self.assertIsInstance(config_ctx, contextlib._GeneratorContextManager) with config_ctx as config: self.assertIsInstance(config, ConfigurationFile) self.assertThat(config_file, FileExists()) self.assertEqual({}, config.config) self.assertFalse(config.dirty) self.assertThat(config_file, FileContains(""))
def test_opened_configuration_file_saves_on_exit(self): # ConfigurationFile.open() returns a context manager that will save an # updated configuration on a clean exit. config_file = os.path.join(self.make_dir(), "config") config_key = factory.make_name("key") config_value = factory.make_name("value") with ConfigurationFile.open_for_update(config_file) as config: config[config_key] = config_value self.assertEqual({config_key: config_value}, config.config) self.assertTrue(config.dirty) with ConfigurationFile.open(config_file) as config: self.assertEqual(config_value, config[config_key])
def test_opened_configuration_file_does_not_save_on_unclean_exit(self): config_file = os.path.join(self.make_dir(), "config") config_key = factory.make_name("key") config_value = factory.make_name("value") exception_type = factory.make_exception_type() # Set a configuration option, then crash. with ExpectedException(exception_type): with ConfigurationFile.open_for_update(config_file) as config: config[config_key] = config_value raise exception_type() # No value has been saved for `config_key`. with ConfigurationFile.open(config_file) as config: self.assertRaises(KeyError, lambda: config[config_key])