def test_error(): with pytest.raises(RuntimeError): Options.set("no key", 42) with configure_scope() as scope: scope._should_capture = False Options.set("no key", 42, fail_on_error=False) with pytest.raises(TypeError) as err: Options.set("delay", "foo") msg = err.value.args[0] assert "delay" in msg assert "test.ini" not in msg assert "debugging" not in msg # Test the 'section' arg with pytest.raises(TypeError) as err: Options.set("delay", "foo", file="test.ini") msg = err.value.args[0] assert "delay" in msg assert "test.ini" in msg assert "debugging" not in msg # Test `file` and `section` args with pytest.raises(TypeError) as err: Options.set("delay", "foo", file="test.ini", section="debugging") msg = err.value.args[0] assert "delay" in msg assert "test.ini" in msg assert "debugging" in msg
def load_config(self, parser): import ConfigParser config_name = 'config.ini' config = ConfigParser.ConfigParser() configs = [] path = os.path.join(os.path.dirname(sys.executable), config_name) if os.path.exists(path): configs.append(path) if os.path.exists(config_name): configs.append(config_name) user_ini = os.path.expanduser(os.path.join(Options.nxdrive_home, config_name)) if os.path.exists(user_ini): configs.append(user_ini) if configs: config.read(configs) args = AbstractOSIntegration.get(None).get_system_configuration() if config.has_option(ConfigParser.DEFAULTSECT, 'env'): env = config.get(ConfigParser.DEFAULTSECT, 'env') for item in config.items(env): if item[0] == 'env': continue value = item[1] if value == '': continue if '\n' in item[1]: # Treat multiline option as a set value = tuple(sorted(item[1].split())) args[item[0].replace('-', '_')] = value if args: Options.update(args, setter='local') parser.set_defaults(**args)
def test_batch_update_from_dict(): """ Simulate local and server conf files. """ options = {"debug": True, "locale": "fr"} Options.update(options, setter="local") assert Options.debug assert Options.locale == "fr"
def test_cli_args(tmp): url = "http://*****:*****@localhost:8899" Options.set("proxy_server", url, setter="cli") with Manager(tmp()) as manager: proxy = manager.proxy assert isinstance(proxy, ManualProxy) assert proxy.url == url settings = proxy.settings() assert settings["http"] == settings["https"] == url
def test_batch_update_from_argparse(): """ Simulate CLI args. """ parser = argparse.ArgumentParser(add_help=False) parser.add_argument('--debug', default=True, action='store_true') parser.add_argument('--delay', default=0, type=int) options = parser.parse_args([]) Options.update(options, setter='cli') assert Options.debug assert not Options.delay
def test_batch_update_from_dict(): """ Simulate local and server conf files. """ options = { 'debug': True, 'locale': 'fr', } Options.update(options, setter='local') assert Options.debug assert Options.locale == 'fr'
def test_batch_update_from_dict_with_unknown_option(): options = { 'foo': 42, 'debug': True, } with pytest.raises(KeyError): Options.update(options, setter='local', fail_on_error=True) assert Options.debug assert not Options.foo
def handle(self, argv): """ Parse options, setup logs and manager and dispatch execution. """ options = self.parse_cli(argv) if hasattr(options, 'local_folder'): options.local_folder = normalized_path(options.local_folder) # 'launch' is the default command if None is provided command = getattr(options, 'command', 'launch') if command != 'uninstall': # Configure the logging framework, except for the tests as they # configure their own. # Don't need uninstall logs either for now. self._configure_logger(command, options) self.log = get_logger(__name__) self.log.debug("Command line: argv=%r, options=%r", ' '.join(argv), options) # Update default options Options.update(options, setter='cli') if command != 'uninstall': # Install utility to help debugging segmentation faults self._install_faulthandler() # Initialize a manager for this process self.manager = self.get_manager() # Find the command to execute based on the handler = getattr(self, command, None) if not handler: raise NotImplementedError( 'No handler implemented for command ' + command) try: return handler(options) except Exception as e: if Options.debug: # Make it possible to use the postmortem debugger raise msg = e.msg if hasattr(e, 'msg') else e self.log.error("Error executing '%s': %s", command, msg, exc_info=True)
def test_batch_update_from_dict_with_unknown_option(): options = {"debug": True, "foo": 42} with pytest.raises(RuntimeError) as err: Options.update(options, setter="local") msg = err.value.args[0] assert "foo" in msg assert "test.ini" not in msg assert "debugging" not in msg # Test the 'section' arg with pytest.raises(RuntimeError) as err: Options.update(options, setter="local", file="test.ini") msg = err.value.args[0] assert "foo" in msg assert "test.ini" in msg assert "debugging" not in msg # Test `file` and `section` args with pytest.raises(RuntimeError) as err: Options.update(options, setter="local", file="test.ini", section="debugging") msg = err.value.args[0] assert "foo" in msg assert "test.ini" in msg assert "debugging" in msg assert Options.debug assert not Options.foo
def test_list_conversion_and_original_values_updated(): assert isinstance(Options.ignored_suffixes, tuple) assert "azerty" not in Options.ignored_suffixes current_len = len(Options.ignored_suffixes) Options.set("ignored_suffixes", ["azerty"], setter="manual") assert isinstance(Options.ignored_suffixes, tuple) assert "azerty" in Options.ignored_suffixes assert len(Options.ignored_suffixes) == current_len + 1 new_values = { "ignored_files": ["bim", "bam", "boom", "zzzzzzzzzz"], "force_locale": "zh", } Options.update(new_values, setter="manual") assert isinstance(Options.ignored_files, tuple) assert "bim" in Options.ignored_files assert "bam" in Options.ignored_files assert "boom" in Options.ignored_files assert len(Options.ignored_files) > 4 # Check it is sorted assert Options.ignored_files[-1] == "zzzzzzzzzz"
def test_error(): with pytest.raises(KeyError): Options.set('no key', 42) Options.set('no key', 42, fail_on_error=False) with pytest.raises(TypeError): Options.set('delay', 'foo')
def test_error(): with pytest.raises(RuntimeError): Options.set("no key", 42) Options.set("no key", 42, fail_on_error=False) with pytest.raises(TypeError): Options.set("delay", "foo")
def test_error(): with pytest.raises(KeyError): Options.set('no key', 42) Options.set('no key', 42, fail_on_error=False) with pytest.raises(TypeError): Options.set('delay', '30')
def parse_cli(self, argv): """Parse the command line argument using argparse and protocol URL""" # Filter psn argument provided by OSX .app service launcher # https://developer.apple.com/library/mac/documentation/Carbon/Reference/LaunchServicesReference/LaunchServicesReference.pdf # When run from the .app bundle generated with py2app with # argv_emulation=True this is already filtered out but we keep it # for running CLI from the source folder in development. argv = [a for a in argv if not a.startswith("-psn_")] # Preprocess the args to detect protocol handler calls and be more # tolerant to missing subcommand has_command = False filtered_args = [] for arg in argv[1:]: if arg.startswith('nxdrive://'): Options.set('protocol_url', arg, setter='cli') continue if not arg.startswith('-'): has_command = True filtered_args.append(arg) parser = self.make_cli_parser(add_subparsers=has_command) # Change default value according to config.ini self.load_config(parser) options = parser.parse_args(filtered_args) if options.debug: # Install Post-Mortem debugger hook def info(etype, value, tb): traceback.print_exception(etype, value, tb) pdb.pm() sys.excepthook = info return options
def _poll(self): # type: () -> bool """ Check for the configuration file and apply updates. """ for _, engine in self.manager._engines.items(): client = engine.get_remote_doc_client() if not client: continue try: raw, _ = client.do_get(client.server_url + SERVER_CONF_URL) conf = json.loads(raw, encoding='utf-8') except HTTPError as exc: if exc.code == 404: self._enable = False log.info('Disabling server configuration updater thread') break except (URLError, ValueError): continue else: Options.update(conf, setter='server', fail_on_error=True) break return True
def test_setters(): """ Check setters level. """ Options.set("delay", 1) assert Options.delay == 1 Options.set("delay", 2, setter="server") assert Options.delay == 2 Options.set("delay", 1) assert Options.delay == 2 Options.set("delay", 3, setter="local") assert Options.delay == 3 Options.set("delay", 2, setter="server") assert Options.delay == 3 Options.set("delay", 42, setter="manual") assert Options.delay == 42 Options.set("delay", 0, setter="local") assert Options.delay == 42 Options.delay = 222 assert Options.delay == 222
def test_setters(): """ Check setters level. """ Options.set('delay', 1) assert Options.delay == 1 Options.set('delay', 2, setter='server') assert Options.delay == 2 Options.set('delay', 1) assert Options.delay == 2 Options.set('delay', 3, setter='local') assert Options.delay == 3 Options.set('delay', 2, setter='server') assert Options.delay == 3 Options.set('delay', 42, setter='manual') assert Options.delay == 42 Options.set('delay', 0, setter='local') assert Options.delay == 42 Options.delay = 222 assert Options.delay == 222