def test_check_compatibility__manifest_version_12__exception_raised( self, ext_dir): manifest = ExtensionManifest('test_extension', {'required_api_version': '0'}, ext_dir) with pytest.raises(ExtensionManifestError) as e: manifest.check_compatibility() assert e.value.error_name == ErrorName.ExtensionCompatibilityError.value
def test_validate__prefs_incorrect_type__exception_raised( self, valid_manifest): valid_manifest['preferences'] = [{'id': 'id', 'type': 'incorrect'}] manifest = ExtensionManifest(valid_manifest) with pytest.raises(ExtensionManifestError) as e: manifest.validate() assert e.value.error_name == ExtensionError.InvalidManifest.value
def test_get_preference(self, ext_dir): manifest_dict = { 'preferences': [{ 'id': 'myid', 'type': 'keyword', 'default_value': 'mi', 'name': "Mimimi", 'icon': 'images/icon.png' }, { 'id': 'newid', 'type': 'input', 'default_value': 'ni' }] } manifest = ExtensionManifest('test_extension', manifest_dict, ext_dir) assert manifest.get_preference('newid') == { 'id': 'newid', 'type': 'input', 'default_value': 'ni', } assert manifest.get_preference('myid') == { 'id': 'myid', 'type': 'keyword', 'default_value': 'mi', 'name': 'Mimimi', 'icon': 'images/icon.png' }
def test_validate__prefs_empty_id__exception_raised( self, ext_dir, valid_manifest): valid_manifest['preferences'] = [{}] manifest = ExtensionManifest('test_extension', valid_manifest, ext_dir) with pytest.raises(ExtensionManifestError) as e: manifest.validate() assert e.value.error_name == ErrorName.InvalidManifestJson.value
def test_check_compatibility__manifest_version_12__exception_raised(self): manifest = ExtensionManifest({ "name": "Test", "required_api_version": "0" }) with pytest.raises(ExtensionManifestError) as e: manifest.check_compatibility() assert e.value.error_name == ExtensionError.Incompatible.value
def test_validate__type_kw_empty_name__exception_raised( self, ext_dir, valid_manifest): valid_manifest['preferences'] = [{ 'id': 'id', 'type': 'incorrect', 'keyword': 'kw' }] manifest = ExtensionManifest('test_extension', valid_manifest, ext_dir) with pytest.raises(ExtensionManifestError) as e: manifest.validate() assert e.value.error_name == ErrorName.InvalidManifestJson.value
def test_validate__raises_error_if_empty_default_value_for_keyword( self, valid_manifest): valid_manifest['preferences'] = [{ 'id': 'id', 'type': 'keyword', 'name': 'My Keyword' }] manifest = ExtensionManifest(valid_manifest) with pytest.raises(ExtensionManifestError) as e: manifest.validate() assert e.value.error_name == ExtensionError.InvalidManifest.value
def configure(self, extension_id): """ * Adds itself to the controllers dict * Validates manifest file. * Sends :class:`PreferencesEvent` to extension """ self.extension_id = extension_id if not self.extension_id: raise RuntimeError("No extension_id provided") logger.info('Extension "%s" connected', self.extension_id) self.manifest = ExtensionManifest.open(self.extension_id) try: self.manifest.validate() except ExtensionManifestError as e: logger.warning("Couldn't connect '%s'. %s: %s", self.extension_id, type(e).__name__, e) self.framer.close() return self.preferences = ExtensionPreferences.create_instance( self.extension_id) self.controllers[self.extension_id] = self self._debounced_send_event = debounce( self.manifest.get_option('query_debounce', 0.05))(self._send_event) self._send_event(PreferencesEvent(self.preferences.get_dict()))
def test_validate__doesnt_raise_if_empty_default_value_for_non_keyword( self, valid_manifest): valid_manifest['preferences'] = [ { 'id': 'id', 'type': 'keyword', 'name': 'My Keyword', 'default_value': 'kw' }, { 'id': 'city', 'type': 'input', 'name': 'City' }, ] manifest = ExtensionManifest(valid_manifest) manifest.validate()
def run(self, extension_id): """ * Validates manifest * Runs extension in a new process """ if self.is_running(extension_id): raise ExtensionIsRunningError(f"Extension ID: {extension_id}") manifest = ExtensionManifest.open(extension_id) manifest.validate() manifest.check_compatibility() cmd = [ sys.executable, os.path.join(self.extensions_dir, extension_id, 'main.py') ] env = {} env['PYTHONPATH'] = ':'.join( filter( bool, [ULAUNCHER_APP_DIR, os.getenv('PYTHONPATH')])) if self.verbose: env['VERBOSE'] = '1' if self.dont_run_extensions: args = [env.get('VERBOSE', ''), env['PYTHONPATH']] args.extend(cmd) run_cmd = 'VERBOSE={} PYTHONPATH={} {} {}'.format(*args) logger.warning('Copy and run the following command to start %s', extension_id) logger.warning(run_cmd) self.set_extension_error(extension_id, ExtRunErrorName.NoExtensionsFlag, run_cmd) return launcher = Gio.SubprocessLauncher.new(Gio.SubprocessFlags.STDERR_PIPE) for env_name, env_value in env.items(): launcher.setenv(env_name, env_value, True) t_start = time() subproc = launcher.spawnv(cmd) error_line_str = Gio.DataInputStream.new(subproc.get_stderr_pipe()) self.extension_procs[extension_id] = ExtensionProc( extension_id=extension_id, subprocess=subproc, start_time=t_start, error_stream=error_line_str, recent_errors=deque(maxlen=1)) logger.debug("Launched %s using Gio.Subprocess", extension_id) subproc.wait_async(None, self.handle_wait, extension_id) self.read_stderr_line(self.extension_procs[extension_id])
def test_check_compatibility__required_api_version_1__no_exceptions( self, ext_dir): manifest = ExtensionManifest('test_extension', {'required_api_version': '2'}, ext_dir) manifest.check_compatibility()
def create_instance(cls, ext_id): return cls(ext_id, ExtensionManifest.open(ext_id))
def test_validate__prefs_empty_id__exception_raised(self, valid_manifest): valid_manifest['preferences'] = [{}] manifest = ExtensionManifest(valid_manifest) with pytest.raises(ExtensionManifestError) as e: manifest.validate() assert e.value.error_name == ExtensionError.InvalidManifest.value
def test_get_option__option_exists__value_returned(self, ext_dir): manifest_dict = {'options': {'query_debounce': 0.5}} manifest = ExtensionManifest('test_extension', manifest_dict, ext_dir) assert manifest.get_option('query_debounce') == 0.5
def test_validate__name_empty__exception_raised(self): manifest = ExtensionManifest({"required_api_version": "1"}) with pytest.raises(ExtensionManifestError) as e: manifest.validate() assert e.value.error_name == ExtensionError.InvalidManifest.value
def test_validate__valid_manifest__no_exceptions_raised( self, valid_manifest): manifest = ExtensionManifest(valid_manifest) manifest.validate()
def test_get_option__option_doesnt_exist__default_returned(self): manifest = ExtensionManifest({"options": {}}) assert manifest.get_option('query_debounce', 0.4) == 0.4
def test_open__manifest_file__is_read(self): ext_dir = os.path.dirname(os.path.abspath(__file__)) manifest = ExtensionManifest.open('test_extension', ext_dir) assert manifest.get_name() == "Test Extension"
def test_check_compatibility__required_api_version_1__no_exceptions(self): manifest = ExtensionManifest({ "name": "Test", "required_api_version": "2" }) manifest.check_compatibility()
def test_get_option__option_exists__value_returned(self): manifest = ExtensionManifest({"options": {"query_debounce": 0.5}}) assert manifest.get_option("query_debounce") == 0.5
def test_get_option__option_doesnt_exist__default_returned(self, ext_dir): manifest_dict = {'options': {}} manifest = ExtensionManifest('test_extension', manifest_dict, ext_dir) assert manifest.get_option('query_debounce', 0.4) == 0.4
def test_open__manifest_file__is_read(self, ext_dir): manifest = ExtensionManifest.open('test_extension', ext_dir) assert manifest.get_name() == "Test Extension"
def test_refresh__name__is_reloaded(self, ext_dir): manifest = ExtensionManifest('test_extension', {'name': 'Old'}, ext_dir) assert manifest.get_name() == 'Old' manifest.refresh() assert manifest.get_name() == "Test Extension"
def test_validate__name_empty__exception_raised(self, ext_dir): manifest = ExtensionManifest('test_extension', {'required_api_version': '1'}, ext_dir) with pytest.raises(ExtensionManifestError) as e: manifest.validate() assert e.value.error_name == ErrorName.InvalidManifestJson.value
def test_validate__valid_manifest__no_exceptions_raised( self, ext_dir, valid_manifest): manifest = ExtensionManifest('test_extension', valid_manifest, ext_dir) manifest.validate()