def test_from_raw(self): serverlocal = Address.as_server('localhost', 9876) serverremote = Address.as_server('1.2.3.4', 9876) clientlocal = Address.as_client('localhost', 9876) clientremote = Address.as_client('1.2.3.4', 9876) default = Address(None, 1111) external = Address('', 1111) values = [ (serverlocal, serverlocal), (serverremote, serverremote), (clientlocal, clientlocal), (clientremote, clientremote), (None, default), ('', external), ([], default), ({}, default), (9876, serverlocal), ('localhost:9876', clientlocal), ('1.2.3.4:9876', clientremote), ('*:9876', Address.as_server('', 9876)), ('*', external), (':9876', Address.as_server('', 9876)), ('localhost', Address('localhost', 1111)), (':', external), (dict(host='localhost'), Address('localhost', 1111)), (dict(port=9876), serverlocal), (dict(host=None, port=9876), serverlocal), (dict(host='localhost', port=9876), clientlocal), (dict(host='localhost', port='9876'), clientlocal), ] for value, expected in values: addr = Address.from_raw(value, defaultport=1111) assert addr == expected
def test_as_client_bad_port(self): port = None for host in [None, '', 'localhost', '1.2.3.4']: with self.subTest((host, port)): with self.assertRaises(TypeError): Address.as_client(host, port) for port in ['', -1, 65536]: for host in [None, '', 'localhost', '1.2.3.4']: with self.subTest((host, port)): with self.assertRaises(ValueError): Address.as_client(host, port)
def _parse_args(prog, argv): parser = argparse.ArgumentParser( prog=prog, usage=USAGE.format(prog), ) parser.add_argument('--nodebug', action='store_true') host = parser.add_mutually_exclusive_group() host.add_argument('--host') host.add_argument('--server-host') parser.add_argument('--port', type=int, required=True) target = parser.add_mutually_exclusive_group(required=True) target.add_argument('-m', dest='module') target.add_argument('--pid', type=int) target.add_argument('filename', nargs='?') parser.add_argument('--single-session', action='store_true') parser.add_argument('--wait', action='store_true') parser.add_argument('-V', '--version', action='version') parser.version = __version__ args = parser.parse_args(argv) ns = vars(args) serverhost = ns.pop('server_host', None) clienthost = ns.pop('host', None) if serverhost: args.address = Address.as_server(serverhost, ns.pop('port')) elif not clienthost: if args.nodebug: args.address = Address.as_client(clienthost, ns.pop('port')) else: args.address = Address.as_server(clienthost, ns.pop('port')) else: args.address = Address.as_client(clienthost, ns.pop('port')) pid = ns.pop('pid') module = ns.pop('module') filename = ns.pop('filename') if pid is not None: args.name = pid args.kind = 'pid' elif module is not None: args.name = module args.kind = 'module' else: args.name = filename args.kind = 'script' return args
def test_remote_localhost(self): addr = Address.as_client(None, 8888) run_module(addr, 'spam', _run=self._run, _prog='eggs') self.assertEqual(self.argv, _get_args( '--module', '--file', 'spam:', ptvsd_extras=['--client', 'localhost'])) self.assertEqual(self.addr, Address.as_client(*addr)) self.assertEqual(self.kwargs, { 'singlesession': True, })
def test_as_client_public_host(self): addr = Address.as_client('', 9786) self.assertEqual( addr, Address('', 9786, isserver=False), )
def test_extra_sys_argv(self): filename = 'spam.py' port = 8888 debug_id = 1 debug_options = {'x': 'y'} sys.argv = [filename, '--abc', 'xyz', '42'] debug(filename, port, debug_id, debug_options, 'script', _run=self._run, _prog='eggs') self.assertEqual( self.argv, _get_args('--file', 'spam.py', '--abc', 'xyz', '42', ptvsd_extras=['--client', LOCALHOST])) self.assertEqual(self.addr, Address.as_client(None, port)) self.assertEqual(self.kwargs, { 'singlesession': True, })
def test_pseudo_backward_compatibility_nodebug(self): args, extra = parse_args([ 'eggs', '--nodebug', '--client', '--host', 'localhost', '--port', '8888', '--module', '--file', 'spam', ]) self.assertEqual( vars(args), { 'kind': 'script', 'name': 'spam', 'address': Address.as_client('localhost', 8888), 'nodebug': True, 'single_session': False, 'wait': False, 'multiprocess': False, }) self.assertEqual(extra, ['--module'] + self.EXPECTED_EXTRA)
def test_as_client_default_host(self): addr = Address.as_client(None, 9786) self.assertEqual( addr, Address('localhost', 9786, isserver=False), )
def test_extra_nodebug(self): args, extra = parse_args([ 'eggs', '--DEBUG', '--nodebug', '--port', '8888', '--vm_type', '???', 'spam.py', '--xyz', '123', 'abc', '--cmd-line', '--', 'foo', '--server', '--bar' ]) self.assertEqual( vars(args), { 'kind': 'script', 'name': 'spam.py', 'address': Address.as_client(None, 8888), 'nodebug': True, 'single_session': False, 'wait': False, }) self.assertEqual( extra, [ '--DEBUG', '--vm_type', '???', '--', # Expected pydevd defaults separator '--xyz', '123', 'abc', '--cmd-line', 'foo', '--server', '--bar', ])
def test_as_client_valid_address(self): for host in ['localhost', '127.0.0.1', '::', '1.2.3.4']: with self.subTest(host): addr = Address.as_client(host, 9786) self.assertEqual( addr, Address(host, 9786, isserver=False), )
def _parse_args(prog, argv): parser = argparse.ArgumentParser( prog=prog, usage=USAGE.format(prog), ) parser.add_argument('--nodebug', action='store_true') host = parser.add_mutually_exclusive_group() host.add_argument('--host') host.add_argument('--server-host') parser.add_argument('--port', type=int, required=True) target = parser.add_mutually_exclusive_group(required=True) target.add_argument('-m', dest='module') target.add_argument('filename', nargs='?') args = parser.parse_args(argv) ns = vars(args) serverhost = ns.pop('server_host', None) clienthost = ns.pop('host', None) if serverhost: args.address = Address.as_server(serverhost, ns.pop('port')) elif not clienthost: if args.nodebug: args.address = Address.as_client(clienthost, ns.pop('port')) else: args.address = Address.as_server(clienthost, ns.pop('port')) else: args.address = Address.as_client(clienthost, ns.pop('port')) module = ns.pop('module') filename = ns.pop('filename') if module is None: args.name = filename args.kind = 'script' else: args.name = module args.kind = 'module' #if argv[-1] != args.name or (module and argv[-1] != '-m'): # parser.error('script/module must be last arg') return args
def test_unsupported(self, setUp): filename = 'spam' port = 8888 debug_id = 1 debug_options = {'x': 'y'} sys.argv = [filename] debug(filename, port, debug_id, debug_options, '???', _run=self._run, _prog='eggs') assert self.argv == _get_args('--file', 'spam', ptvsd_extras=['--client', LOCALHOST]) assert self.addr == Address.as_client(None, port) assert self.kwargs == {'singlesession': True}
def test_remote(self): addr = ('1.2.3.4', 8888) run_file(addr, 'spam.py', _run=self._run, _prog='eggs') self.assertEqual(self.argv, _get_args( '--file', 'spam.py', ptvsd_extras=['--client', '1.2.3.4'])) self.assertEqual(self.addr, Address.as_client(*addr)) self.assertEqual(self.kwargs, { 'singlesession': True, })
def test_script_nodebug(self): args, extra = parse_args([ 'eggs', '--nodebug', '--port', '8888', 'spam.py', ]) self.assertEqual( vars(args), { 'kind': 'script', 'name': 'spam.py', 'address': Address.as_client(None, 8888), 'nodebug': True, 'single_session': False, }) self.assertEqual(extra, self.EXPECTED_EXTRA)
def test_backward_compatibility_host(self): args, extra = parse_args([ 'eggs', '--client', '1.2.3.4', '--port', '8888', '-m', 'spam', ]) self.assertEqual(vars(args), { 'kind': 'module', 'name': 'spam', 'address': Address.as_client('1.2.3.4', 8888), 'nodebug': False, 'single_session': False, 'wait': False, 'multiprocess': False, }) self.assertEqual(extra, self.EXPECTED_EXTRA)
def test_remote_localhost(self): args, extra = parse_args([ 'eggs', '--host', 'localhost', '--port', '8888', 'spam.py', ]) self.assertEqual(vars(args), { 'kind': 'script', 'name': 'spam.py', 'address': Address.as_client('localhost', 8888), 'nodebug': False, 'single_session': False, 'wait': False, 'multiprocess': False, }) self.assertEqual(extra, self.EXPECTED_EXTRA)
def test_remote_localhost(): args, extra = parse_args([ 'eggs', '--client', '--host', 'localhost', '--port', '8888', 'spam.py', ]) assert vars(args) == { 'kind': 'script', 'name': 'spam.py', 'address': Address.as_client('localhost', 8888), 'nodebug': False, 'single_session': False, 'wait': False, 'multiprocess': False, } assert extra == EXPECTED_EXTRA
def test_backward_compatibility_module_nodebug(self): args, extra = parse_args([ 'eggs', '--nodebug', '--port', '8888', '--module', '--file', 'spam:', ]) self.assertEqual( vars(args), { 'kind': 'module', 'name': 'spam', 'address': Address.as_client(None, 8888), 'nodebug': True, 'single_session': False, }) self.assertEqual(extra, self.EXPECTED_EXTRA)
def test_code(self): filename = "print('spam')" port = 8888 debug_id = 1 debug_options = {'x': 'y'} sys.argv = [filename] debug(filename, port, debug_id, debug_options, 'code', _run=self._run, _prog='eggs') self.assertEqual( self.argv, _get_args('--file', filename, ptvsd_extras=['--client', LOCALHOST])) self.assertEqual(self.addr, Address.as_client(None, port)) self.assertEqual(self.kwargs, { 'singlesession': True, })
def test_extra_nodebug(): args, extra = parse_args([ 'eggs', '--DEBUG', '--nodebug', '--client', '--host', 'localhost', '--port', '8888', '--vm_type', '???', 'spam.py', '--xyz', '123', 'abc', '--cmd-line', '--', 'foo', '--server', '--bar' ]) assert vars(args) == { 'kind': 'script', 'name': 'spam.py', 'address': Address.as_client('localhost', 8888), 'nodebug': True, 'single_session': False, 'wait': False, 'multiprocess': False, } assert extra == [ '--DEBUG', '--vm_type', '???', '--', # Expected pydevd defaults separator '--xyz', '123', 'abc', '--cmd-line', 'foo', '--server', '--bar', ]
def start_as_client(cls, addr, *args, **kwargs): addr = Address.as_client(*addr) return cls._start_as(addr, *args, server=False, **kwargs)
def _parse_args(prog, argv): parser = argparse.ArgumentParser(prog=prog) parser.add_argument('--nodebug', action='store_true') host = parser.add_mutually_exclusive_group() host.add_argument('--host') host.add_argument('--server-host') parser.add_argument('--port', type=int, required=True) def port_range(arg): arg = tuple(int(s) for s in arg.split('-')) if len(arg) != 2: raise ValueError return arg parser.add_argument('--multiprocess', action='store_true') parser.add_argument('--multiprocess-port-range', type=port_range) target = parser.add_mutually_exclusive_group(required=True) target.add_argument('-m', dest='module') target.add_argument('-c', dest='code') target.add_argument('--pid', type=int) target.add_argument('filename', nargs='?') parser.add_argument('--single-session', action='store_true') parser.add_argument('--wait', action='store_true') parser.add_argument('-V', '--version', action='version') parser.version = __version__ args = parser.parse_args(argv) ns = vars(args) serverhost = ns.pop('server_host', None) clienthost = ns.pop('host', None) if serverhost: args.address = Address.as_server(serverhost, ns.pop('port')) elif not clienthost: if args.nodebug: args.address = Address.as_client(clienthost, ns.pop('port')) else: args.address = Address.as_server(clienthost, ns.pop('port')) else: args.address = Address.as_client(clienthost, ns.pop('port')) multiprocess_port_range = ns.pop('multiprocess_port_range') if multiprocess_port_range is not None: if not ns['multiprocess']: parser.error('--multiprocess-port-range requires --multiprocess') multiproc.subprocess_port_range = multiprocess_port_range if ns['multiprocess']: multiproc.enable() pid = ns.pop('pid') module = ns.pop('module') filename = ns.pop('filename') code = ns.pop('code') if pid is not None: args.name = pid args.kind = 'pid' elif module is not None: args.name = module args.kind = 'module' elif code is not None: options.code = code args.name = 'ptvsd.run_code' args.kind = 'module' else: args.name = filename args.kind = 'script' return args
def test_as_client_bad_port2(self, host, port): with pytest.raises(ValueError): Address.as_client(host, port)
def test_as_client_bad_port(self, host): port = None with pytest.raises(TypeError): Address.as_client(host, port)
def test_as_client_default_host(self): addr = Address.as_client(None, 9786) assert addr == Address('localhost', 9786, isserver=False)
def test_as_client_public_host(self): addr = Address.as_client('', 9786) assert addr == Address('', 9786, isserver=False)
def test_as_client_valid_address(self, host): addr = Address.as_client(host, 9786) assert addr == Address(host, 9786, isserver=False)