def test_write_defaults_handles_permission_error(self): handler = SettingsHandler() with named_file("") as f: handler._get_settings_filename = lambda: f with patch("orangewidget.settings.log.error") as log, \ patch('orangewidget.settings.open', create=True, side_effect=PermissionError): handler.write_defaults() log.assert_called()
def test_write_defaults_handles_writing_errors(self): handler = SettingsHandler() for error in (EOFError, IOError, pickle.PicklingError): f = NamedTemporaryFile("wt", delete=False) f.close() # so it can be opened on windows handler._get_settings_filename = lambda x=f: x.name with patch("orangewidget.settings.log.error") as log, \ patch.object(handler, "write_defaults_file", side_effect=error): handler.write_defaults() log.assert_called() # Corrupt setting files should be removed self.assertFalse(os.path.exists(f.name))
def override_default_settings(self, widget, defaults=None): if defaults is None: defaults = {} h = SettingsHandler() h.widget_class = widget h.defaults = defaults filename = h._get_settings_filename() os.makedirs(os.path.dirname(filename), exist_ok=True) with open(filename, "wb") as f: pickle.dump(defaults, f) yield if os.path.isfile(filename): os.remove(filename)
def test_write_defaults(self): fd, settings_file = mkstemp(suffix='.ini') handler = SettingsHandler() handler.widget_class = SimpleWidget handler.defaults = {'a': 5, 'b': {1: 5}} handler._get_settings_filename = lambda: settings_file handler.write_defaults() with open(settings_file, 'rb') as f: default_settings = pickle.load(f) os.close(fd) self.assertEqual(default_settings.pop(VERSION_KEY, -0xBAD), handler.widget_class.settings_version,) self.assertEqual(default_settings, handler.defaults) os.remove(settings_file)