From 9845f1aadc5c30b7387809a3baa3a2bcaaa1a597 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20HUBSCHER?= Date: Mon, 20 Feb 2017 11:01:34 +0100 Subject: [PATCH 1/5] Niceties. --- pyramid/scripts/pserve.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pyramid/scripts/pserve.py b/pyramid/scripts/pserve.py index e2d97f5ecc..b1b9cd0096 100644 --- a/pyramid/scripts/pserve.py +++ b/pyramid/scripts/pserve.py @@ -35,10 +35,12 @@ from pyramid.path import AssetResolver from pyramid.settings import aslist + def main(argv=sys.argv, quiet=False): command = PServeCommand(argv, quiet=quiet) return command.run() + class PServeCommand(object): description = """\ @@ -113,7 +115,6 @@ class PServeCommand(object): "passed here.", ) - ConfigParser = configparser.ConfigParser # testing loadapp = staticmethod(loadapp) # testing loadserver = staticmethod(loadserver) # testing @@ -126,7 +127,7 @@ def __init__(self, argv, quiet=False): self.args.verbose = 0 self.watch_files = [] - def out(self, msg): # pragma: no cover + def out(self, msg): # pragma: no cover if self.args.verbose > 0: print(msg) @@ -239,8 +240,9 @@ def open_browser(): msg = '' self.out('Exiting%s (-v to see traceback)' % msg) + # For paste.deploy server instantiation (egg:pyramid#wsgiref) -def wsgiref_server_runner(wsgi_app, global_conf, **kw): # pragma: no cover +def wsgiref_server_runner(wsgi_app, global_conf, **kw): # pragma: no cover from wsgiref.simple_server import make_server host = kw.get('host', '0.0.0.0') port = int(kw.get('port', 8080)) @@ -248,13 +250,14 @@ def wsgiref_server_runner(wsgi_app, global_conf, **kw): # pragma: no cover print('Starting HTTP server on http://%s:%s' % (host, port)) server.serve_forever() + # For paste.deploy server instantiation (egg:pyramid#cherrypy) def cherrypy_server_runner( app, global_conf=None, host='127.0.0.1', port=None, ssl_pem=None, protocol_version=None, numthreads=None, server_name=None, max=None, request_queue_size=None, timeout=None - ): # pragma: no cover + ): # pragma: no cover """ Entry point for CherryPy's WSGI server @@ -361,5 +364,6 @@ def cherrypy_server_runner( return server -if __name__ == '__main__': # pragma: no cover + +if __name__ == '__main__': # pragma: no cover sys.exit(main() or 0) From d64e8d14c9cda3b95aac54a468f4e18cbb9c8a75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20HUBSCHER?= Date: Mon, 20 Feb 2017 11:03:11 +0100 Subject: [PATCH 2/5] Make sure PServeCommand kwargs are passed to the hupper worker. --- pyramid/scripts/pserve.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pyramid/scripts/pserve.py b/pyramid/scripts/pserve.py index b1b9cd0096..b89f9b9825 100644 --- a/pyramid/scripts/pserve.py +++ b/pyramid/scripts/pserve.py @@ -125,6 +125,8 @@ def __init__(self, argv, quiet=False): self.args = self.parser.parse_args(argv[1:]) if quiet: self.args.verbose = 0 + if self.args.reload: + self.worker_kwargs = {'argv': argv, "quiet": quiet} self.watch_files = [] def out(self, msg): # pragma: no cover @@ -204,6 +206,7 @@ def open_browser(): 'pyramid.scripts.pserve.main', reload_interval=int(self.args.reload_interval), verbose=self.args.verbose, + worker_kwargs=self.worker_kwargs ) return 0 From b2a749b34c1140613b3deaa2b55ab6c29dd343cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20HUBSCHER?= Date: Mon, 20 Feb 2017 11:31:38 +0100 Subject: [PATCH 3/5] Add test. --- pyramid/tests/test_scripts/test_pserve.py | 20 +++++++++++++++++++- setup.py | 3 ++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/pyramid/tests/test_scripts/test_pserve.py b/pyramid/tests/test_scripts/test_pserve.py index 18b0c84b6c..f681eaedbc 100644 --- a/pyramid/tests/test_scripts/test_pserve.py +++ b/pyramid/tests/test_scripts/test_pserve.py @@ -1,9 +1,11 @@ +import mock import os import unittest from pyramid.tests.test_scripts import dummy here = os.path.abspath(os.path.dirname(__file__)) + class TestPServeCommand(unittest.TestCase): def setUp(self): from pyramid.compat import NativeIO @@ -48,10 +50,11 @@ def test_parse_vars_good(self): inst.loadserver = self._get_server app = dummy.DummyApp() + def get_app(*args, **kwargs): app.global_conf = kwargs.get('global_conf', None) - inst.loadapp = get_app + inst.loadapp = get_app inst.run() self.assertEqual(app.global_conf, {'a': '1', 'b': '2'}) @@ -77,6 +80,21 @@ def test_config_file_finds_watch_files(self): os.path.abspath(os.path.join(here, '*.py')), ]) + def test_reload_call_hupper_with_correct_args(self): + with mock.patch('pyramid.scripts.pserve.hupper') as hupper_mock: + hupper_mock.is_active.side_effect = [False, True] + inst = self._makeOne('--reload', 'development.ini') + inst.loadserver = mock.MagicMock() + inst.loadapp = mock.MagicMock() + inst.run() + hupper_mock.start_reloader.assert_called_with( + 'pyramid.scripts.pserve.main', + reload_interval=1, + verbose=1, + worker_kwargs={'argv': ['pserve', '--reload', 'development.ini'], + 'quiet': False}) + + class Test_main(unittest.TestCase): def _callFUT(self, argv): from pyramid.scripts.pserve import main diff --git a/setup.py b/setup.py index d9fcec4c81..bd0e7f4d7e 100644 --- a/setup.py +++ b/setup.py @@ -65,9 +65,10 @@ ] testing_extras = tests_require + [ + 'mock', 'nose', 'coverage', - 'virtualenv', # for scaffolding tests + 'virtualenv', # for scaffolding tests ] setup(name='pyramid', From 1702daa1d312381a37b86ebf869cf4e1abe2c185 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20HUBSCHER?= Date: Mon, 27 Feb 2017 18:32:58 +0100 Subject: [PATCH 4/5] Rewrite test without mock. --- pyramid/tests/test_scripts/test_pserve.py | 37 +++++++++++++++-------- setup.py | 1 - 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/pyramid/tests/test_scripts/test_pserve.py b/pyramid/tests/test_scripts/test_pserve.py index f681eaedbc..18451df64e 100644 --- a/pyramid/tests/test_scripts/test_pserve.py +++ b/pyramid/tests/test_scripts/test_pserve.py @@ -1,8 +1,8 @@ -import mock import os import unittest from pyramid.tests.test_scripts import dummy + here = os.path.abspath(os.path.dirname(__file__)) @@ -81,18 +81,29 @@ def test_config_file_finds_watch_files(self): ]) def test_reload_call_hupper_with_correct_args(self): - with mock.patch('pyramid.scripts.pserve.hupper') as hupper_mock: - hupper_mock.is_active.side_effect = [False, True] - inst = self._makeOne('--reload', 'development.ini') - inst.loadserver = mock.MagicMock() - inst.loadapp = mock.MagicMock() - inst.run() - hupper_mock.start_reloader.assert_called_with( - 'pyramid.scripts.pserve.main', - reload_interval=1, - verbose=1, - worker_kwargs={'argv': ['pserve', '--reload', 'development.ini'], - 'quiet': False}) + from pyramid.scripts import pserve + + class AttrDict(dict): + def __init__(self, *args, **kwargs): + super(AttrDict, self).__init__(*args, **kwargs) + self.__dict__ = self + + def dummy_start_reloader(*args, **kwargs): + dummy_start_reloader.args = args + dummy_start_reloader.kwargs = kwargs + + pserve.hupper = AttrDict(is_active=lambda: False, + start_reloader=dummy_start_reloader) + + inst = self._makeOne('--reload', 'development.ini') + inst.run() + + self.assertEquals(dummy_start_reloader.args, ('pyramid.scripts.pserve.main',)) + self.assertEquals(dummy_start_reloader.kwargs, { + 'reload_interval': 1, + 'verbose': 1, + 'worker_kwargs': {'argv': ['pserve', '--reload', 'development.ini'], + 'quiet': False}}) class Test_main(unittest.TestCase): diff --git a/setup.py b/setup.py index bd0e7f4d7e..ab2170fecb 100644 --- a/setup.py +++ b/setup.py @@ -65,7 +65,6 @@ ] testing_extras = tests_require + [ - 'mock', 'nose', 'coverage', 'virtualenv', # for scaffolding tests From 2798f253a907310643b1d2f8a7935c09d9582d49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20HUBSCHER?= Date: Fri, 3 Mar 2017 19:19:08 +0100 Subject: [PATCH 5/5] @mmerickel review. --- pyramid/tests/test_scripts/test_pserve.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pyramid/tests/test_scripts/test_pserve.py b/pyramid/tests/test_scripts/test_pserve.py index 18451df64e..8eb63b8d6b 100644 --- a/pyramid/tests/test_scripts/test_pserve.py +++ b/pyramid/tests/test_scripts/test_pserve.py @@ -92,11 +92,15 @@ def dummy_start_reloader(*args, **kwargs): dummy_start_reloader.args = args dummy_start_reloader.kwargs = kwargs - pserve.hupper = AttrDict(is_active=lambda: False, - start_reloader=dummy_start_reloader) - - inst = self._makeOne('--reload', 'development.ini') - inst.run() + orig_hupper = pserve.hupper + try: + pserve.hupper = AttrDict(is_active=lambda: False, + start_reloader=dummy_start_reloader) + + inst = self._makeOne('--reload', 'development.ini') + inst.run() + finally: + pserve.hupper = orig_hupper self.assertEquals(dummy_start_reloader.args, ('pyramid.scripts.pserve.main',)) self.assertEquals(dummy_start_reloader.kwargs, {