def test_run_in_runclassmethod(): class C(object): @classmethod def fn(cls, *args, **kwargs): return cls, args, kwargs C.__name__ = 'C_' + str(uuid4()).replace('-', '_') C.__module__ = 'pytest_shutil.run' with patch('pytest_shutil.run.execnet') as execnet: gw = execnet.makegateway.return_value chan = gw.remote_exec.return_value chan.receive.return_value = cPickle.dumps(sentinel.ret) c = C() with patch.object(run, C.__name__, C, create=True): run.run_in_subprocess(c.fn, python='sentinel.python')(ARG, kw=KW) ((s,), _) = chan.send.call_args if sys.version_info < (3, 0, 0): # Class methods are not pickleable in Python 2. assert cPickle.loads(s) == (run._invoke_method, (C, 'fn', ARG), {'kw': KW}) else: # Class methods are pickleable in Python 3. assert cPickle.loads(s) == (c.fn, (ARG,), {'kw': KW}) ((remote_fn,), _) = gw.remote_exec.call_args ((chan.receive.return_value,), _) = chan.send.call_args remote_fn(chan) chan.send.assert_called_with(cPickle.dumps((C, (ARG,), {'kw': KW}), protocol=0))
def test_run_in_runtimeout(): with patch.multiple('pytest_shutil.run', cPickle=DEFAULT, execnet=DEFAULT) as mocks: run.run_in_subprocess(Mock(__name__='fn'), python='sentinel.python', timeout=sentinel.timeout)(sentinel.arg, kw=sentinel.kw) gw = mocks['execnet'].makegateway.return_value chan = gw.remote_exec.return_value chan.receive.assert_called_with(sentinel.timeout)
def test_run_in_rununbound_method_on_unpickleable_class(): class C(object): def fn(self, *args, **kwargs): return self, args, kwargs with patch('pytest_shutil.run.execnet'): with pytest.raises(cPickle.PicklingError): run.run_in_subprocess(C.fn, python='sentinel.python')(C(), ARG, kw=KW)
def test_run_in_runnested_function(): def fn(*args, **kwargs): return args, kwargs source = """def fn(*args, **kwargs): return args, kwargs """ with patch('pytest_shutil.run.execnet') as execnet: gw = execnet.makegateway.return_value chan = gw.remote_exec.return_value chan.receive.return_value = cPickle.dumps(sentinel.ret) run.run_in_subprocess(fn, python='sentinel.python')(ARG, kw=KW) ((s, ), _) = chan.send.call_args assert cPickle.loads(s) == (run._evaluate_fn_source, ( source, ARG, ), { 'kw': KW }) ((remote_fn, ), _) = gw.remote_exec.call_args ((chan.receive.return_value, ), _) = chan.send.call_args remote_fn(chan) chan.send.assert_called_with( cPickle.dumps(((ARG, ), { 'kw': KW }), protocol=0))
def test_run_in_runstaticmethod_on_unpickleable_class(): class C(object): @staticmethod def fn(*args, **kwargs): return args, kwargs source = """@staticmethod def fn(*args, **kwargs): return args, kwargs """ C.__name__ = 'C_' + str(uuid4()).replace('-', '_') C.fn.__module__ = 'pytest_shutil.run' with patch('pytest_shutil.run.execnet') as execnet: gw = execnet.makegateway.return_value chan = gw.remote_exec.return_value chan.receive.return_value = cPickle.dumps(sentinel.ret) with patch.object(run, C.__name__, C, create=True): run.run_in_subprocess(C.fn, python='sentinel.python')(ARG, kw=KW) ((s, ), _) = chan.send.call_args assert cPickle.loads(s) == (run._evaluate_fn_source, ( source, ARG, ), { 'kw': KW }) ((remote_fn, ), _) = gw.remote_exec.call_args ((chan.receive.return_value, ), _) = chan.send.call_args remote_fn(chan) chan.send.assert_called_with( cPickle.dumps(((ARG, ), { 'kw': KW }), protocol=0))
def test_run_in_runclassmethod(): class C(object): @classmethod def fn(cls, *args, **kwargs): return cls, args, kwargs C.__name__ = 'C_' + str(uuid4()).replace('-', '_') C.__module__ = 'pytest_shutil.run' with patch('pytest_shutil.run.execnet') as execnet: gw = execnet.makegateway.return_value chan = gw.remote_exec.return_value chan.receive.return_value = cPickle.dumps(sentinel.ret) c = C() with patch.object(run, C.__name__, C, create=True): run.run_in_subprocess(c.fn, python='sentinel.python')(ARG, kw=KW) ((s, ), _) = chan.send.call_args assert cPickle.loads(s) == (run._invoke_method, (C, 'fn', ARG), { 'kw': KW }) ((remote_fn, ), _) = gw.remote_exec.call_args ((chan.receive.return_value, ), _) = chan.send.call_args remote_fn(chan) chan.send.assert_called_with( cPickle.dumps((C, (ARG, ), { 'kw': KW }), protocol=0))
def test_run_in_subprocess_exception(): def fn(v): raise v v = ValueError(uuid4()) with pytest.raises(execnet.RemoteError) as exc: # @UndefinedVariable with no_cov(): run.run_in_subprocess(fn)(v) assert str(v) in str(exc.value)
def test_run_in_runbound_method_on_unpickleable_class(): class C(object): def fn(self, *args, **kwargs): return self, args, kwargs with patch('pytest_shutil.run.execnet'): with pytest.raises(cPickle.PicklingError): run.run_in_subprocess(C().fn, python='sentinel.python')(ARG, kw=KW)
def test_run_in_runcd(): with patch.multiple('pytest_shutil.run', cPickle=DEFAULT, execnet=DEFAULT) as mocks: run.run_in_subprocess(Mock(__name__='fn'), python='sentinel.python', cd='sentinel.cd')(sentinel.arg, kw=sentinel.kw) mocks['execnet'].makegateway.assert_called_once_with( 'popen//python=sentinel.python//chdir=sentinel.cd')
def test_run_in_runstr(): source = """def fn(*args, **kwargs): return args, kwargs """ with patch('pytest_shutil.run.execnet') as execnet: gw = execnet.makegateway.return_value chan = gw.remote_exec.return_value chan.receive.return_value = cPickle.dumps(sentinel.ret) run.run_in_subprocess(source, python='sentinel.python')(ARG, kw=KW) ((s,), _) = chan.send.call_args assert cPickle.loads(s) == (run._evaluate_fn_source, (source, ARG,), {'kw': KW}) ((remote_fn,), _) = gw.remote_exec.call_args ((chan.receive.return_value,), _) = chan.send.call_args remote_fn(chan) chan.send.assert_called_with(cPickle.dumps(((ARG,), {'kw': KW}), protocol=0))
def test_run_in_subprocess(): with patch.multiple('pytest_shutil.run', cPickle=DEFAULT, execnet=DEFAULT) as mocks: fn = Mock(__name__='fn') res = run.run_in_subprocess(fn, python='sentinel.python')(sentinel.arg, kw=sentinel.kw) mocks['execnet'].makegateway.assert_called_once_with('popen//python=sentinel.python') gw = mocks['execnet'].makegateway.return_value ((remote_fn,), _) = gw.remote_exec.call_args chan = gw.remote_exec.return_value mocks['cPickle'].dumps.assert_called_with((fn, (sentinel.arg,), {'kw': sentinel.kw}), protocol=0) chan.send.assert_called_with(mocks['cPickle'].dumps.return_value) chan.receive.assert_has_calls([call(None) for _i in range(gw.remote_exec.call_count)]) mocks['cPickle'].loads.assert_called_once_with(chan.receive.return_value) assert res is mocks['cPickle'].loads.return_value chan.close.assert_has_calls([call() for _i in range(gw.remote_exec.call_count)]) gw.exit.assert_called_once_with() with patch('six.moves.cPickle') as cPickle: channel, fn = Mock(), Mock() cPickle.loads.return_value = (fn, (sentinel.arg,), {'kw': sentinel.kw}) remote_fn(channel) channel.receive.assert_called_once_with(None) cPickle.loads.assert_called_once_with(channel.receive.return_value) fn.assert_called_once_with(sentinel.arg, kw=sentinel.kw) cPickle.dumps.assert_called_once_with(fn.return_value, protocol=0) channel.send.assert_called_once_with(cPickle.dumps.return_value)
def test_run_in_subprocess_uses_passed_python(): def fn(): import sys # @Reimport return sys.executable with no_cov(): python = run.run_in_subprocess(fn, python=sys.executable)() assert python == sys.executable
def test_run_in_subprocess(): def fn(): return None with no_cov(): res = run.run_in_subprocess(fn)() assert res is None
def test_run_in_runpickleable_function(): def fn(*args, **kwargs): return args, kwargs fn.__name__ = 'fn_' + str(uuid4()).replace('-', '_') fn.__module__ = 'pytest_shutil.run' with patch('pytest_shutil.run.execnet') as execnet: gw = execnet.makegateway.return_value chan = gw.remote_exec.return_value chan.receive.return_value = cPickle.dumps(sentinel.ret) with patch.object(run, fn.__name__, fn, create=True): run.run_in_subprocess(fn, python='sentinel.python')(ARG, kw=KW) ((s,), _) = chan.send.call_args assert cPickle.loads(s) == (fn, (ARG,), {'kw': KW}) ((remote_fn,), _) = gw.remote_exec.call_args ((chan.receive.return_value,), _) = chan.send.call_args remote_fn(chan) chan.send.assert_called_with(cPickle.dumps(((ARG,), {'kw': KW}), protocol=0))
def test_run_in_runstaticmethod(): class C(object): @staticmethod def fn(*args, **kwargs): return args, kwargs C.__name__ = 'C_' + str(uuid4()).replace('-', '_') C.__module__ = C.fn.__module__ = 'pytest_shutil.run' with patch('pytest_shutil.run.execnet') as execnet: gw = execnet.makegateway.return_value chan = gw.remote_exec.return_value chan.receive.return_value = cPickle.dumps(sentinel.ret) with patch.object(run, C.__name__, C, create=True): run.run_in_subprocess(C.fn, python='sentinel.python')(ARG, kw=KW) ((s,), _) = chan.send.call_args assert cPickle.loads(s) == (run._invoke_method, (C, 'fn', ARG,), {'kw': KW}) ((remote_fn,), _) = gw.remote_exec.call_args ((chan.receive.return_value,), _) = chan.send.call_args remote_fn(chan) chan.send.assert_called_with(cPickle.dumps(((ARG,), {'kw': KW}), protocol=0))
def test_run_in_runpickleable_function(): def fn(*args, **kwargs): return args, kwargs fn.__name__ = 'fn_' + str(uuid4()).replace('-', '_') fn.__module__ = 'pytest_shutil.run' with patch('pytest_shutil.run.execnet') as execnet: gw = execnet.makegateway.return_value chan = gw.remote_exec.return_value chan.receive.return_value = cPickle.dumps(sentinel.ret) with patch.object(run, fn.__name__, fn, create=True): run.run_in_subprocess(fn, python='sentinel.python')(ARG, kw=KW) ((s, ), _) = chan.send.call_args assert cPickle.loads(s) == (fn, (ARG, ), {'kw': KW}) ((remote_fn, ), _) = gw.remote_exec.call_args ((chan.receive.return_value, ), _) = chan.send.call_args remote_fn(chan) chan.send.assert_called_with( cPickle.dumps(((ARG, ), { 'kw': KW }), protocol=0))
def test_run_in_runbound_method(): class C(tuple): # for equality of instances def fn(self, *args, **kwargs): return self, args, kwargs C.__name__ = 'C_' + str(uuid4()).replace('-', '_') C.__module__ = 'pytest_shutil.run' with patch('pytest_shutil.run.execnet') as execnet: gw = execnet.makegateway.return_value chan = gw.remote_exec.return_value chan.receive.return_value = cPickle.dumps(sentinel.ret) c = C() with patch.object(run, C.__name__, C, create=True): run.run_in_subprocess(c.fn, python='sentinel.python')(ARG, kw=KW) ((s, ), _) = chan.send.call_args if sys.version_info < (3, 0, 0): # Bound methods are not pickleable in Python 2. assert cPickle.loads(s) == (run._invoke_method, ( c, 'fn', ARG, ), { 'kw': KW }) else: # Bound methods are pickleable in Python 3. assert cPickle.loads(s) == (c.fn, (ARG, ), {'kw': KW}) ((remote_fn, ), _) = gw.remote_exec.call_args ((chan.receive.return_value, ), _) = chan.send.call_args remote_fn(chan) chan.send.assert_called_with( cPickle.dumps((c, (ARG, ), { 'kw': KW }), protocol=0))
def test_run_in_subprocess(): with patch.multiple('pytest_shutil.run', cPickle=DEFAULT, execnet=DEFAULT) as mocks: fn = Mock(__name__='fn') res = run.run_in_subprocess(fn, python='sentinel.python')(sentinel.arg, kw=sentinel.kw) mocks['execnet'].makegateway.assert_called_once_with( 'popen//python=sentinel.python') gw = mocks['execnet'].makegateway.return_value ((remote_fn, ), _) = gw.remote_exec.call_args chan = gw.remote_exec.return_value mocks['cPickle'].dumps.assert_called_with((fn, (sentinel.arg, ), { 'kw': sentinel.kw }), protocol=0) chan.send.assert_called_with(mocks['cPickle'].dumps.return_value) chan.receive.assert_has_calls( [call(None) for _i in range(gw.remote_exec.call_count)]) mocks['cPickle'].loads.assert_called_once_with( chan.receive.return_value) assert res is mocks['cPickle'].loads.return_value chan.close.assert_has_calls( [call() for _i in range(gw.remote_exec.call_count)]) gw.exit.assert_called_once_with() with patch('six.moves.cPickle') as cPickle: channel, fn = Mock(), Mock() cPickle.loads.return_value = (fn, (sentinel.arg, ), { 'kw': sentinel.kw }) remote_fn(channel) channel.receive.assert_called_once_with(None) cPickle.loads.assert_called_once_with(channel.receive.return_value) fn.assert_called_once_with(sentinel.arg, kw=sentinel.kw) cPickle.dumps.assert_called_once_with(fn.return_value, protocol=0) channel.send.assert_called_once_with(cPickle.dumps.return_value)
def test_run_in_subprocess_cd(): with workspace.Workspace() as ws: with no_cov(): cwd = run.run_in_subprocess(os.getcwd, cd=ws.workspace)() assert cwd == ws.workspace
def test_run_in_subprocess_is_a_subprocess(): pid = run.run_in_subprocess(os.getpid)() assert pid != os.getpid()
def test_run_in_subprocess_timeout(): with pytest.raises(execnet.TimeoutError) as exc: # @UndefinedVariable with no_cov(): run.run_in_subprocess(time.sleep, timeout=0)(1) assert 'no item after 0 seconds' in str(exc.value)
def test_run_in_runcd(): with patch.multiple('pytest_shutil.run', cPickle=DEFAULT, execnet=DEFAULT) as mocks: run.run_in_subprocess(Mock(__name__='fn'), python='sentinel.python', cd='sentinel.cd')(sentinel.arg, kw=sentinel.kw) mocks['execnet'].makegateway.assert_called_once_with('popen//python=sentinel.python//chdir=sentinel.cd')
def test_run_in_subprocess_cd(): with workspace.Workspace() as ws: with no_cov(): cwd = run.run_in_subprocess(os.getcwd, cd=ws.workspace)() assert os.path.realpath(cwd) == os.path.realpath(ws.workspace)