class TestPServeCommand(unittest.TestCase): def setUp(self): from pyramid.compat import NativeIO self.out_ = NativeIO() def out(self, msg): self.out_.write(msg) def _getTargetClass(self): from pyramid.scripts.pserve import PServeCommand return PServeCommand def _makeOne(self, *args): effargs = ['pserve'] effargs.extend(args) cmd = self._getTargetClass()(effargs) cmd.out = self.out return cmd def test_run_no_args(self): inst = self._makeOne() result = inst.run() self.assertEqual(result, 2) self.assertEqual(self.out_.getvalue(), 'You must give a config file') def test_run_stop_daemon_no_such_pid_file(self): path = os.path.join(os.path.dirname(__file__), 'wontexist.pid') inst = self._makeOne('--stop-daemon', '--pid-file=%s' % path) inst.run() self.assertEqual(self.out_.getvalue(),'No PID file exists in %s' % path) def test_run_stop_daemon_bad_pid_file(self): path = __file__ inst = self._makeOne('--stop-daemon', '--pid-file=%s' % path) inst.run() self.assertEqual( self.out_.getvalue(),'Not a valid PID file in %s' % path) def test_run_stop_daemon_invalid_pid_in_file(self): fn = tempfile.mktemp() with open(fn, 'wb') as tmp: tmp.write(b'9999999') tmp.close() inst = self._makeOne('--stop-daemon', '--pid-file=%s' % fn) inst.run() self.assertEqual(self.out_.getvalue(), 'PID in %s is not valid (deleting)' % fn) def test_parse_vars_good(self): vars = ['a=1', 'b=2'] inst = self._makeOne('development.ini') result = inst.parse_vars(vars) self.assertEqual(result, {'a': '1', 'b': '2'}) def test_parse_vars_bad(self): vars = ['a'] inst = self._makeOne('development.ini') self.assertRaises(ValueError, inst.parse_vars, vars)
class TestPServeCommand(unittest.TestCase): def setUp(self): from pyramid.compat import NativeIO self.out_ = NativeIO() def out(self, msg): self.out_.write(msg) def _get_server(*args, **kwargs): def server(app): return '' return server def _getTargetClass(self): from pyramid.scripts.pserve import PServeCommand return PServeCommand def _makeOne(self, *args): effargs = ['pserve'] effargs.extend(args) cmd = self._getTargetClass()(effargs) cmd.out = self.out return cmd def test_run_no_args(self): inst = self._makeOne() result = inst.run() self.assertEqual(result, 2) self.assertEqual(self.out_.getvalue(), 'You must give a config file') def test_get_options_no_command(self): inst = self._makeOne() inst.args = ['foo', 'a=1', 'b=2'] result = inst.get_options() self.assertEqual(result, {'a': '1', 'b': '2'}) def test_parse_vars_good(self): from pyramid.tests.test_scripts.dummy import DummyApp inst = self._makeOne('development.ini', 'a=1', 'b=2') inst.loadserver = self._get_server app = DummyApp() def get_app(*args, **kwargs): app.global_conf = kwargs.get('global_conf', None) inst.loadapp = get_app inst.run() self.assertEqual(app.global_conf, {'a': '1', 'b': '2'}) def test_parse_vars_bad(self): inst = self._makeOne('development.ini', 'a') inst.loadserver = self._get_server self.assertRaises(ValueError, inst.run)
class TestPServeCommand(unittest.TestCase): def setUp(self): from pyramid.compat import NativeIO self.out_ = NativeIO() def out(self, msg): self.out_.write(msg) def _get_server(*args, **kwargs): def server(app): return '' return server def _getTargetClass(self): from pyramid.scripts.pserve import PServeCommand return PServeCommand def _makeOne(self, *args): effargs = ['pserve'] effargs.extend(args) cmd = self._getTargetClass()(effargs) cmd.out = self.out return cmd def test_run_no_args(self): inst = self._makeOne() result = inst.run() self.assertEqual(result, 2) self.assertEqual(self.out_.getvalue(), 'You must give a config file') def test_get_options_no_command(self): inst = self._makeOne() inst.args = ['foo', 'a=1', 'b=2'] result = inst.get_options() self.assertEqual(result, {'a': '1', 'b': '2'}) def test_parse_vars_good(self): from pyramid.tests.test_scripts.dummy import DummyApp inst = self._makeOne('development.ini', 'a=1', 'b=2') inst.loadserver = self._get_server app = DummyApp() def get_app(*args, **kwargs): app.global_conf = kwargs.get('global_conf', None) inst.loadapp = get_app inst.run() self.assertEqual(app.global_conf, {'a': '1', 'b': '2'}) def test_parse_vars_bad(self): inst = self._makeOne('development.ini', 'a') inst.loadserver = self._get_server self.assertRaises(ValueError, inst.run)
class TestPServeCommand(unittest.TestCase): def setUp(self): from pyramid.compat import NativeIO self.out_ = NativeIO() self.config_factory = dummy.DummyConfigParserFactory() def out(self, msg): self.out_.write(msg) def _get_server(*args, **kwargs): def server(app): return '' return server def _getTargetClass(self): from pyramid.scripts.pserve import PServeCommand return PServeCommand def _makeOne(self, *args): effargs = ['pserve'] effargs.extend(args) cmd = self._getTargetClass()(effargs) cmd.out = self.out cmd.ConfigParser = self.config_factory return cmd def test_run_no_args(self): inst = self._makeOne() result = inst.run() self.assertEqual(result, 2) self.assertEqual(self.out_.getvalue(), 'You must give a config file') def test_config_vars_no_command(self): inst = self._makeOne() inst.args.config_uri = 'foo' inst.args.config_vars = ['a=1', 'b=2'] result = inst.get_config_vars() self.assertEqual(result, {'a': '1', 'b': '2'}) def test_parse_vars_good(self): inst = self._makeOne('development.ini', 'a=1', 'b=2') 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.run() self.assertEqual(app.global_conf, {'a': '1', 'b': '2'}) def test_parse_vars_bad(self): inst = self._makeOne('development.ini', 'a') inst.loadserver = self._get_server self.assertRaises(ValueError, inst.run) def test_config_file_finds_watch_files(self): inst = self._makeOne('development.ini') self.config_factory.items = [( 'watch_files', 'foo\n/baz\npyramid.tests.test_scripts:*.py', )] inst.pserve_file_config('/base/path.ini', global_conf={'a': '1'}) self.assertEqual(self.config_factory.defaults, { 'a': '1', 'here': os.path.abspath('/base'), }) self.assertEqual(inst.watch_files, [ os.path.abspath('/base/foo'), os.path.abspath('/baz'), os.path.abspath(os.path.join(here, '*.py')), ])
class TestPServeCommand(unittest.TestCase): def setUp(self): from pyramid.compat import NativeIO self.out_ = NativeIO() self.pid_file = None def tearDown(self): if self.pid_file and os.path.exists(self.pid_file): os.remove(self.pid_file) def out(self, msg): self.out_.write(msg) def _get_server(*args, **kwargs): def server(app): return '' return server def _getTargetClass(self): from pyramid.scripts.pserve import PServeCommand return PServeCommand def _makeOne(self, *args): effargs = ['pserve'] effargs.extend(args) cmd = self._getTargetClass()(effargs) cmd.out = self.out return cmd def _makeOneWithPidFile(self, pid): self.pid_file = tempfile.mktemp() inst = self._makeOne() with open(self.pid_file, 'w') as f: f.write(str(pid)) return inst def test_remove_pid_file_verbose(self): inst = self._makeOneWithPidFile(os.getpid()) inst._remove_pid_file(os.getpid(), self.pid_file, verbosity=1) self._assert_pid_file_removed(verbose=True) def test_remove_pid_file_not_verbose(self): inst = self._makeOneWithPidFile(os.getpid()) inst._remove_pid_file(os.getpid(), self.pid_file, verbosity=0) self._assert_pid_file_removed(verbose=False) def test_remove_pid_not_a_number(self): inst = self._makeOneWithPidFile('not a number') inst._remove_pid_file(os.getpid(), self.pid_file, verbosity=1) self._assert_pid_file_removed(verbose=True) def test_remove_pid_current_pid_is_not_written_pid(self): inst = self._makeOneWithPidFile(os.getpid()) inst._remove_pid_file('99999', self.pid_file, verbosity=1) self._assert_pid_file_not_removed('') def test_remove_pid_current_pid_is_not_pid_in_file(self): inst = self._makeOneWithPidFile('99999') inst._remove_pid_file(os.getpid(), self.pid_file, verbosity=1) msg = 'PID file %s contains 99999, not expected PID %s' self._assert_pid_file_not_removed(msg % (self.pid_file, os.getpid())) def test_remove_pid_no_pid_file(self): inst = self._makeOne() self.pid_file = 'some unknown path' inst._remove_pid_file(os.getpid(), self.pid_file, verbosity=1) self._assert_pid_file_removed(verbose=False) def test_remove_pid_file_unlink_exception(self): inst = self._makeOneWithPidFile(os.getpid()) self._remove_pid_unlink_exception(inst) msg = [ 'Removing PID file %s' % (self.pid_file), 'Cannot remove PID file: (Some OSError - unlink)', 'Stale PID removed'] self._assert_pid_file_not_removed(msg=''.join(msg)) with open(self.pid_file) as f: self.assertEqual(f.read(), '') def test_remove_pid_file_stale_pid_write_exception(self): inst = self._makeOneWithPidFile(os.getpid()) self._remove_pid_unlink_and_write_exceptions(inst) msg = [ 'Removing PID file %s' % (self.pid_file), 'Cannot remove PID file: (Some OSError - unlink)', 'Stale PID left in file: %s ' % (self.pid_file), '(Some OSError - open)'] self._assert_pid_file_not_removed(msg=''.join(msg)) with open(self.pid_file) as f: self.assertEqual(int(f.read()), os.getpid()) def test_record_pid_verbose(self): self._assert_record_pid(verbosity=2, msg='Writing PID %d to %s') def test_record_pid_not_verbose(self): self._assert_record_pid(verbosity=1, msg='') def _remove_pid_unlink_exception(self, inst): old_unlink = os.unlink def fake_unlink(filename): raise OSError('Some OSError - unlink') try: os.unlink = fake_unlink inst._remove_pid_file(os.getpid(), self.pid_file, verbosity=1) finally: os.unlink = old_unlink def _remove_pid_unlink_and_write_exceptions(self, inst): old_unlink = os.unlink def fake_unlink(filename): raise OSError('Some OSError - unlink') run_already = [] old_open = __builtin__.open def fake_open(*args): if not run_already: run_already.append(True) return old_open(*args) raise OSError('Some OSError - open') try: os.unlink = fake_unlink __builtin__.open = fake_open inst._remove_pid_file(os.getpid(), self.pid_file, verbosity=1) finally: os.unlink = old_unlink __builtin__.open = old_open def _assert_pid_file_removed(self, verbose=False): self.assertFalse(os.path.exists(self.pid_file)) msg = 'Removing PID file %s' % (self.pid_file) if verbose else '' self.assertEqual(self.out_.getvalue(), msg) def _assert_pid_file_not_removed(self, msg): self.assertTrue(os.path.exists(self.pid_file)) self.assertEqual(self.out_.getvalue(), msg) def _assert_record_pid(self, verbosity, msg): old_atexit = atexit.register def fake_atexit(*args): pass self.pid_file = tempfile.mktemp() pid = os.getpid() inst = self._makeOne() inst.options.verbose = verbosity try: atexit.register = fake_atexit inst.record_pid(self.pid_file) finally: atexit.register = old_atexit msg = msg % (pid, self.pid_file) if msg else '' self.assertEqual(self.out_.getvalue(), msg) with open(self.pid_file) as f: self.assertEqual(int(f.read()), pid) def test_run_no_args(self): inst = self._makeOne() result = inst.run() self.assertEqual(result, 2) self.assertEqual(self.out_.getvalue(), 'You must give a config file') def test_run_stop_daemon_no_such_pid_file(self): path = os.path.join(os.path.dirname(__file__), 'wontexist.pid') inst = self._makeOne('--stop-daemon', '--pid-file=%s' % path) inst.run() msg = 'No PID file exists in %s' % path self.assertEqual(self.out_.getvalue(), msg) def test_run_stop_daemon_bad_pid_file(self): path = __file__ inst = self._makeOne('--stop-daemon', '--pid-file=%s' % path) inst.run() msg = 'Not a valid PID file in %s' % path self.assertEqual(self.out_.getvalue(), msg) def test_run_stop_daemon_invalid_pid_in_file(self): fn = tempfile.mktemp() with open(fn, 'wb') as tmp: tmp.write(b'9999999') tmp.close() inst = self._makeOne('--stop-daemon', '--pid-file=%s' % fn) inst.run() msg = 'PID in %s is not valid (deleting)' % fn self.assertEqual(self.out_.getvalue(), msg) def test_get_options_with_command(self): inst = self._makeOne() inst.args = ['foo', 'stop', 'a=1', 'b=2'] result = inst.get_options() self.assertEqual(result, {'a': '1', 'b': '2'}) def test_get_options_no_command(self): inst = self._makeOne() inst.args = ['foo', 'a=1', 'b=2'] result = inst.get_options() self.assertEqual(result, {'a': '1', 'b': '2'}) def test_parse_vars_good(self): from pyramid.tests.test_scripts.dummy import DummyApp inst = self._makeOne('development.ini', 'a=1', 'b=2') inst.loadserver = self._get_server app = DummyApp() def get_app(*args, **kwargs): app.global_conf = kwargs.get('global_conf', None) inst.loadapp = get_app inst.run() self.assertEqual(app.global_conf, {'a': '1', 'b': '2'}) def test_parse_vars_bad(self): inst = self._makeOne('development.ini', 'a') inst.loadserver = self._get_server self.assertRaises(ValueError, inst.run)
class TestPServeCommand(unittest.TestCase): def setUp(self): from pyramid.compat import NativeIO self.out_ = NativeIO() self.pid_file = None def tearDown(self): if self.pid_file and os.path.exists(self.pid_file): os.remove(self.pid_file) def out(self, msg): self.out_.write(msg) def _getTargetClass(self): from pyramid.scripts.pserve import PServeCommand return PServeCommand def _makeOne(self, *args): effargs = ['pserve'] effargs.extend(args) cmd = self._getTargetClass()(effargs) cmd.out = self.out return cmd def _makeOneWithPidFile(self, pid): self.pid_file = tempfile.mktemp() inst = self._makeOne() with open(self.pid_file, 'w') as f: f.write(str(pid)) return inst def test_remove_pid_file_verbose(self): inst = self._makeOneWithPidFile(os.getpid()) inst._remove_pid_file(os.getpid(), self.pid_file, verbosity=1) self._assert_pid_file_removed(verbose=True) def test_remove_pid_file_not_verbose(self): inst = self._makeOneWithPidFile(os.getpid()) inst._remove_pid_file(os.getpid(), self.pid_file, verbosity=0) self._assert_pid_file_removed(verbose=False) def test_remove_pid_not_a_number(self): inst = self._makeOneWithPidFile('not a number') inst._remove_pid_file(os.getpid(), self.pid_file, verbosity=1) self._assert_pid_file_removed(verbose=True) def test_remove_pid_current_pid_is_not_written_pid(self): inst = self._makeOneWithPidFile(os.getpid()) inst._remove_pid_file('99999', self.pid_file, verbosity=1) self._assert_pid_file_not_removed('') def test_remove_pid_current_pid_is_not_pid_in_file(self): inst = self._makeOneWithPidFile('99999') inst._remove_pid_file(os.getpid(), self.pid_file, verbosity=1) msg = 'PID file %s contains 99999, not expected PID %s' self._assert_pid_file_not_removed(msg % (self.pid_file, os.getpid())) def test_remove_pid_no_pid_file(self): inst = self._makeOne() self.pid_file = 'some unknown path' inst._remove_pid_file(os.getpid(), self.pid_file, verbosity=1) self._assert_pid_file_removed(verbose=False) def test_remove_pid_file_unlink_exception(self): inst = self._makeOneWithPidFile(os.getpid()) self._remove_pid_unlink_exception(inst) msg = [ 'Removing PID file %s' % (self.pid_file), 'Cannot remove PID file: (Some OSError - unlink)', 'Stale PID removed' ] self._assert_pid_file_not_removed(msg=''.join(msg)) with open(self.pid_file) as f: self.assertEqual(f.read(), '') def test_remove_pid_file_stale_pid_write_exception(self): inst = self._makeOneWithPidFile(os.getpid()) self._remove_pid_unlink_and_write_exceptions(inst) msg = [ 'Removing PID file %s' % (self.pid_file), 'Cannot remove PID file: (Some OSError - unlink)', 'Stale PID left in file: %s ' % (self.pid_file), '(Some OSError - open)' ] self._assert_pid_file_not_removed(msg=''.join(msg)) with open(self.pid_file) as f: self.assertEqual(int(f.read()), os.getpid()) def test_record_pid_verbose(self): self._assert_record_pid(verbosity=2, msg='Writing PID %d to %s') def test_record_pid_not_verbose(self): self._assert_record_pid(verbosity=1, msg='') def _remove_pid_unlink_exception(self, inst): old_unlink = os.unlink def fake_unlink(filename): raise OSError('Some OSError - unlink') try: os.unlink = fake_unlink inst._remove_pid_file(os.getpid(), self.pid_file, verbosity=1) finally: os.unlink = old_unlink def _remove_pid_unlink_and_write_exceptions(self, inst): old_unlink = os.unlink def fake_unlink(filename): raise OSError('Some OSError - unlink') run_already = [] old_open = __builtin__.open def fake_open(*args): if not run_already: run_already.append(True) return old_open(*args) raise OSError('Some OSError - open') try: os.unlink = fake_unlink __builtin__.open = fake_open inst._remove_pid_file(os.getpid(), self.pid_file, verbosity=1) finally: os.unlink = old_unlink __builtin__.open = old_open def _assert_pid_file_removed(self, verbose=False): self.assertFalse(os.path.exists(self.pid_file)) msg = 'Removing PID file %s' % (self.pid_file) if verbose else '' self.assertEqual(self.out_.getvalue(), msg) def _assert_pid_file_not_removed(self, msg): self.assertTrue(os.path.exists(self.pid_file)) self.assertEqual(self.out_.getvalue(), msg) def _assert_record_pid(self, verbosity, msg): old_atexit = atexit.register def fake_atexit(*args): pass self.pid_file = tempfile.mktemp() pid = os.getpid() inst = self._makeOne() inst.verbose = verbosity try: atexit.register = fake_atexit inst.record_pid(self.pid_file) finally: atexit.register = old_atexit msg = msg % (pid, self.pid_file) if msg else '' self.assertEqual(self.out_.getvalue(), msg) with open(self.pid_file) as f: self.assertEqual(int(f.read()), pid) def test_run_no_args(self): inst = self._makeOne() result = inst.run() self.assertEqual(result, 2) self.assertEqual(self.out_.getvalue(), 'You must give a config file') def test_run_stop_daemon_no_such_pid_file(self): path = os.path.join(os.path.dirname(__file__), 'wontexist.pid') inst = self._makeOne('--stop-daemon', '--pid-file=%s' % path) inst.run() msg = 'No PID file exists in %s' % path self.assertEqual(self.out_.getvalue(), msg) def test_run_stop_daemon_bad_pid_file(self): path = __file__ inst = self._makeOne('--stop-daemon', '--pid-file=%s' % path) inst.run() msg = 'Not a valid PID file in %s' % path self.assertEqual(self.out_.getvalue(), msg) def test_run_stop_daemon_invalid_pid_in_file(self): fn = tempfile.mktemp() with open(fn, 'wb') as tmp: tmp.write(b'9999999') tmp.close() inst = self._makeOne('--stop-daemon', '--pid-file=%s' % fn) inst.run() msg = 'PID in %s is not valid (deleting)' % fn self.assertEqual(self.out_.getvalue(), msg) def test_parse_vars_good(self): vars = ['a=1', 'b=2'] inst = self._makeOne('development.ini') result = inst.parse_vars(vars) self.assertEqual(result, {'a': '1', 'b': '2'}) def test_parse_vars_bad(self): vars = ['a'] inst = self._makeOne('development.ini') self.assertRaises(ValueError, inst.parse_vars, vars)
class TestPCreateCommand(unittest.TestCase): def setUp(self): from pyramid.compat import NativeIO self.out_ = NativeIO() def out(self, msg): self.out_.write(msg) def _getTargetClass(self): from pyramid.scripts.pcreate import PCreateCommand return PCreateCommand def _makeOne(self, *args): effargs = ['pcreate'] effargs.extend(args) cmd = self._getTargetClass()(effargs) cmd.out = self.out return cmd def test_run_show_scaffolds_exist(self): cmd = self._makeOne('-l') result = cmd.run() self.assertEqual(result, 0) out = self.out_.getvalue() self.assertTrue(out.startswith('Available scaffolds')) def test_run_show_scaffolds_none_exist(self): cmd = self._makeOne('-l') cmd.scaffolds = [] result = cmd.run() self.assertEqual(result, 0) out = self.out_.getvalue() self.assertTrue(out.startswith('No scaffolds available')) def test_run_no_scaffold_name(self): cmd = self._makeOne() result = cmd.run() self.assertEqual(result, 2) out = self.out_.getvalue() self.assertTrue( out.startswith('You must provide at least one scaffold name')) def test_no_project_name(self): cmd = self._makeOne('-s', 'dummy') result = cmd.run() self.assertEqual(result, 2) out = self.out_.getvalue() self.assertTrue(out.startswith('You must provide a project name')) def test_unknown_scaffold_name(self): cmd = self._makeOne('-s', 'dummyXX', 'distro') result = cmd.run() self.assertEqual(result, 2) out = self.out_.getvalue() self.assertTrue(out.startswith('Unavailable scaffolds')) def test_known_scaffold_single_rendered(self): import os cmd = self._makeOne('-s', 'dummy', 'Distro') scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] result = cmd.run() self.assertEqual(result, 0) self.assertEqual(scaffold.output_dir, os.path.normpath(os.path.join(os.getcwd(), 'Distro'))) self.assertEqual(scaffold.vars, { 'project': 'Distro', 'egg': 'Distro', 'package': 'distro' }) def test_known_scaffold_absolute_path(self): import os path = os.path.abspath('Distro') cmd = self._makeOne('-s', 'dummy', path) scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] result = cmd.run() self.assertEqual(result, 0) self.assertEqual(scaffold.output_dir, os.path.normpath(os.path.join(os.getcwd(), 'Distro'))) self.assertEqual(scaffold.vars, { 'project': 'Distro', 'egg': 'Distro', 'package': 'distro' }) def test_known_scaffold_multiple_rendered(self): import os cmd = self._makeOne('-s', 'dummy1', '-s', 'dummy2', 'Distro') scaffold1 = DummyScaffold('dummy1') scaffold2 = DummyScaffold('dummy2') cmd.scaffolds = [scaffold1, scaffold2] result = cmd.run() self.assertEqual(result, 0) self.assertEqual(scaffold1.output_dir, os.path.normpath(os.path.join(os.getcwd(), 'Distro'))) self.assertEqual(scaffold1.vars, { 'project': 'Distro', 'egg': 'Distro', 'package': 'distro' }) self.assertEqual(scaffold2.output_dir, os.path.normpath(os.path.join(os.getcwd(), 'Distro'))) self.assertEqual(scaffold2.vars, { 'project': 'Distro', 'egg': 'Distro', 'package': 'distro' })
class TestPCreateCommand(unittest.TestCase): def setUp(self): from pyramid.compat import NativeIO self.out_ = NativeIO() def out(self, msg): self.out_.write(msg) def _getTargetClass(self): from pyramid.scripts.pcreate import PCreateCommand return PCreateCommand def _makeOne(self, *args, **kw): effargs = ['pcreate'] effargs.extend(args) cmd = self._getTargetClass()(effargs, **kw) cmd.out = self.out return cmd def test_run_show_scaffolds_exist(self): cmd = self._makeOne('-l') result = cmd.run() self.assertEqual(result, 0) out = self.out_.getvalue() self.assertTrue(out.startswith('Available scaffolds')) def test_run_show_scaffolds_none_exist(self): cmd = self._makeOne('-l') cmd.scaffolds = [] result = cmd.run() self.assertEqual(result, 0) out = self.out_.getvalue() self.assertTrue(out.startswith('No scaffolds available')) def test_run_no_scaffold_no_args(self): cmd = self._makeOne(quiet=True) result = cmd.run() self.assertEqual(result, 2) def test_run_no_scaffold_name(self): cmd = self._makeOne('dummy') result = cmd.run() self.assertEqual(result, 2) out = self.out_.getvalue() self.assertTrue( out.startswith('You must provide at least one scaffold name')) def test_no_project_name(self): cmd = self._makeOne('-s', 'dummy') result = cmd.run() self.assertEqual(result, 2) out = self.out_.getvalue() self.assertTrue(out.startswith('You must provide a project name')) def test_unknown_scaffold_name(self): cmd = self._makeOne('-s', 'dummyXX', 'distro') result = cmd.run() self.assertEqual(result, 2) out = self.out_.getvalue() self.assertTrue(out.startswith('Unavailable scaffolds')) def test_known_scaffold_single_rendered(self): import os cmd = self._makeOne('-s', 'dummy', 'Distro') scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] cmd.pyramid_dist = DummyDist("0.1") result = cmd.run() self.assertEqual(result, 0) self.assertEqual(scaffold.output_dir, os.path.normpath(os.path.join(os.getcwd(), 'Distro'))) self.assertEqual( scaffold.vars, { 'project': 'Distro', 'egg': 'Distro', 'package': 'distro', 'pyramid_version': '0.1', 'pyramid_docs_branch': '0.1-branch' }) def test_scaffold_with_hyphen_in_project_name(self): import os cmd = self._makeOne('-s', 'dummy', 'Distro-') scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] cmd.pyramid_dist = DummyDist("0.1") result = cmd.run() self.assertEqual(result, 0) self.assertEqual( scaffold.output_dir, os.path.normpath(os.path.join(os.getcwd(), 'Distro-'))) self.assertEqual( scaffold.vars, { 'project': 'Distro-', 'egg': 'Distro_', 'package': 'distro_', 'pyramid_version': '0.1', 'pyramid_docs_branch': '0.1-branch' }) def test_known_scaffold_absolute_path(self): import os path = os.path.abspath('Distro') cmd = self._makeOne('-s', 'dummy', path) cmd.pyramid_dist = DummyDist("0.1") scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] cmd.pyramid_dist = DummyDist("0.1") result = cmd.run() self.assertEqual(result, 0) self.assertEqual(scaffold.output_dir, os.path.normpath(os.path.join(os.getcwd(), 'Distro'))) self.assertEqual( scaffold.vars, { 'project': 'Distro', 'egg': 'Distro', 'package': 'distro', 'pyramid_version': '0.1', 'pyramid_docs_branch': '0.1-branch' }) def test_known_scaffold_multiple_rendered(self): import os cmd = self._makeOne('-s', 'dummy1', '-s', 'dummy2', 'Distro') scaffold1 = DummyScaffold('dummy1') scaffold2 = DummyScaffold('dummy2') cmd.scaffolds = [scaffold1, scaffold2] cmd.pyramid_dist = DummyDist("0.1") result = cmd.run() self.assertEqual(result, 0) self.assertEqual(scaffold1.output_dir, os.path.normpath(os.path.join(os.getcwd(), 'Distro'))) self.assertEqual( scaffold1.vars, { 'project': 'Distro', 'egg': 'Distro', 'package': 'distro', 'pyramid_version': '0.1', 'pyramid_docs_branch': '0.1-branch' }) self.assertEqual(scaffold2.output_dir, os.path.normpath(os.path.join(os.getcwd(), 'Distro'))) self.assertEqual( scaffold2.vars, { 'project': 'Distro', 'egg': 'Distro', 'package': 'distro', 'pyramid_version': '0.1', 'pyramid_docs_branch': '0.1-branch' }) def test_known_scaffold_with_path_as_project_target_rendered(self): import os cmd = self._makeOne('-s', 'dummy', '/tmp/foo/Distro/') scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] cmd.pyramid_dist = DummyDist("0.1") result = cmd.run() self.assertEqual(result, 0) self.assertEqual( scaffold.output_dir, os.path.normpath(os.path.join(os.getcwd(), '/tmp/foo/Distro'))) self.assertEqual( scaffold.vars, { 'project': 'Distro', 'egg': 'Distro', 'package': 'distro', 'pyramid_version': '0.1', 'pyramid_docs_branch': '0.1-branch' }) def test_scaffold_with_prod_pyramid_version(self): cmd = self._makeOne('-s', 'dummy', 'Distro') scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] cmd.pyramid_dist = DummyDist("0.2") result = cmd.run() self.assertEqual(result, 0) self.assertEqual( scaffold.vars, { 'project': 'Distro', 'egg': 'Distro', 'package': 'distro', 'pyramid_version': '0.2', 'pyramid_docs_branch': '0.2-branch' }) def test_scaffold_with_prod_pyramid_long_version(self): cmd = self._makeOne('-s', 'dummy', 'Distro') scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] cmd.pyramid_dist = DummyDist("0.2.1") result = cmd.run() self.assertEqual(result, 0) self.assertEqual( scaffold.vars, { 'project': 'Distro', 'egg': 'Distro', 'package': 'distro', 'pyramid_version': '0.2.1', 'pyramid_docs_branch': '0.2-branch' }) def test_scaffold_with_prod_pyramid_unparsable_version(self): cmd = self._makeOne('-s', 'dummy', 'Distro') scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] cmd.pyramid_dist = DummyDist("abc") result = cmd.run() self.assertEqual(result, 0) self.assertEqual( scaffold.vars, { 'project': 'Distro', 'egg': 'Distro', 'package': 'distro', 'pyramid_version': 'abc', 'pyramid_docs_branch': 'latest' }) def test_scaffold_with_dev_pyramid_version(self): cmd = self._makeOne('-s', 'dummy', 'Distro') scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] cmd.pyramid_dist = DummyDist("0.12dev") result = cmd.run() self.assertEqual(result, 0) self.assertEqual( scaffold.vars, { 'project': 'Distro', 'egg': 'Distro', 'package': 'distro', 'pyramid_version': '0.12dev', 'pyramid_docs_branch': 'master' }) def test_scaffold_with_dev_pyramid_long_version(self): cmd = self._makeOne('-s', 'dummy', 'Distro') scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] cmd.pyramid_dist = DummyDist("0.10.1dev") result = cmd.run() self.assertEqual(result, 0) self.assertEqual( scaffold.vars, { 'project': 'Distro', 'egg': 'Distro', 'package': 'distro', 'pyramid_version': '0.10.1dev', 'pyramid_docs_branch': 'master' })
class TestPCreateCommand(unittest.TestCase): def setUp(self): from pyramid.compat import NativeIO self.out_ = NativeIO() def out(self, msg): self.out_.write(msg) def _getTargetClass(self): from pyramid.scripts.pcreate import PCreateCommand return PCreateCommand def _makeOne(self, *args, **kw): effargs = ['pcreate'] effargs.extend(args) cmd = self._getTargetClass()(effargs, **kw) cmd.out = self.out return cmd def test_run_show_scaffolds_exist(self): cmd = self._makeOne('-l') result = cmd.run() self.assertEqual(result, 0) out = self.out_.getvalue() self.assertTrue(out.startswith('Available scaffolds')) def test_run_show_scaffolds_none_exist(self): cmd = self._makeOne('-l') cmd.scaffolds = [] result = cmd.run() self.assertEqual(result, 0) out = self.out_.getvalue() self.assertTrue(out.startswith('No scaffolds available')) def test_run_no_scaffold_no_args(self): cmd = self._makeOne(quiet=True) result = cmd.run() self.assertEqual(result, 2) def test_run_no_scaffold_name(self): cmd = self._makeOne('dummy') result = cmd.run() self.assertEqual(result, 2) out = self.out_.getvalue() self.assertTrue(out.startswith( 'You must provide at least one scaffold name')) def test_no_project_name(self): cmd = self._makeOne('-s', 'dummy') result = cmd.run() self.assertEqual(result, 2) out = self.out_.getvalue() self.assertTrue(out.startswith('You must provide a project name')) def test_unknown_scaffold_name(self): cmd = self._makeOne('-s', 'dummyXX', 'distro') result = cmd.run() self.assertEqual(result, 2) out = self.out_.getvalue() self.assertTrue(out.startswith('Unavailable scaffolds')) def test_known_scaffold_single_rendered(self): import os cmd = self._makeOne('-s', 'dummy', 'Distro') scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] cmd.pyramid_dist = DummyDist("0.1") result = cmd.run() self.assertEqual(result, 0) self.assertEqual( scaffold.output_dir, os.path.normpath(os.path.join(os.getcwd(), 'Distro')) ) self.assertEqual( scaffold.vars, {'project': 'Distro', 'egg': 'Distro', 'package': 'distro', 'pyramid_version': '0.1', 'pyramid_docs_branch':'0.1-branch'}) def test_scaffold_with_hyphen_in_project_name(self): import os cmd = self._makeOne('-s', 'dummy', 'Distro-') scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] cmd.pyramid_dist = DummyDist("0.1") result = cmd.run() self.assertEqual(result, 0) self.assertEqual( scaffold.output_dir, os.path.normpath(os.path.join(os.getcwd(), 'Distro-')) ) self.assertEqual( scaffold.vars, {'project': 'Distro-', 'egg': 'Distro_', 'package': 'distro_', 'pyramid_version': '0.1', 'pyramid_docs_branch':'0.1-branch'}) def test_known_scaffold_absolute_path(self): import os path = os.path.abspath('Distro') cmd = self._makeOne('-s', 'dummy', path) cmd.pyramid_dist = DummyDist("0.1") scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] cmd.pyramid_dist = DummyDist("0.1") result = cmd.run() self.assertEqual(result, 0) self.assertEqual( scaffold.output_dir, os.path.normpath(os.path.join(os.getcwd(), 'Distro')) ) self.assertEqual( scaffold.vars, {'project': 'Distro', 'egg': 'Distro', 'package': 'distro', 'pyramid_version': '0.1', 'pyramid_docs_branch':'0.1-branch'}) def test_known_scaffold_multiple_rendered(self): import os cmd = self._makeOne('-s', 'dummy1', '-s', 'dummy2', 'Distro') scaffold1 = DummyScaffold('dummy1') scaffold2 = DummyScaffold('dummy2') cmd.scaffolds = [scaffold1, scaffold2] cmd.pyramid_dist = DummyDist("0.1") result = cmd.run() self.assertEqual(result, 0) self.assertEqual( scaffold1.output_dir, os.path.normpath(os.path.join(os.getcwd(), 'Distro')) ) self.assertEqual( scaffold1.vars, {'project': 'Distro', 'egg': 'Distro', 'package': 'distro', 'pyramid_version': '0.1', 'pyramid_docs_branch':'0.1-branch'}) self.assertEqual( scaffold2.output_dir, os.path.normpath(os.path.join(os.getcwd(), 'Distro')) ) self.assertEqual( scaffold2.vars, {'project': 'Distro', 'egg': 'Distro', 'package': 'distro', 'pyramid_version': '0.1', 'pyramid_docs_branch':'0.1-branch'}) def test_known_scaffold_with_path_as_project_target_rendered(self): import os cmd = self._makeOne('-s', 'dummy', '/tmp/foo/Distro/') scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] cmd.pyramid_dist = DummyDist("0.1") result = cmd.run() self.assertEqual(result, 0) self.assertEqual( scaffold.output_dir, os.path.normpath(os.path.join(os.getcwd(), '/tmp/foo/Distro')) ) self.assertEqual( scaffold.vars, {'project': 'Distro', 'egg': 'Distro', 'package': 'distro', 'pyramid_version': '0.1', 'pyramid_docs_branch':'0.1-branch'}) def test_scaffold_with_prod_pyramid_version(self): cmd = self._makeOne('-s', 'dummy', 'Distro') scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] cmd.pyramid_dist = DummyDist("0.2") result = cmd.run() self.assertEqual(result, 0) self.assertEqual( scaffold.vars, {'project': 'Distro', 'egg': 'Distro', 'package': 'distro', 'pyramid_version': '0.2', 'pyramid_docs_branch':'0.2-branch'}) def test_scaffold_with_prod_pyramid_long_version(self): cmd = self._makeOne('-s', 'dummy', 'Distro') scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] cmd.pyramid_dist = DummyDist("0.2.1") result = cmd.run() self.assertEqual(result, 0) self.assertEqual( scaffold.vars, {'project': 'Distro', 'egg': 'Distro', 'package': 'distro', 'pyramid_version': '0.2.1', 'pyramid_docs_branch':'0.2-branch'}) def test_scaffold_with_prod_pyramid_unparsable_version(self): cmd = self._makeOne('-s', 'dummy', 'Distro') scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] cmd.pyramid_dist = DummyDist("abc") result = cmd.run() self.assertEqual(result, 0) self.assertEqual( scaffold.vars, {'project': 'Distro', 'egg': 'Distro', 'package': 'distro', 'pyramid_version': 'abc', 'pyramid_docs_branch':'latest'}) def test_scaffold_with_dev_pyramid_version(self): cmd = self._makeOne('-s', 'dummy', 'Distro') scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] cmd.pyramid_dist = DummyDist("0.12dev") result = cmd.run() self.assertEqual(result, 0) self.assertEqual( scaffold.vars, {'project': 'Distro', 'egg': 'Distro', 'package': 'distro', 'pyramid_version': '0.12dev', 'pyramid_docs_branch': 'master'}) def test_scaffold_with_dev_pyramid_long_version(self): cmd = self._makeOne('-s', 'dummy', 'Distro') scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] cmd.pyramid_dist = DummyDist("0.10.1dev") result = cmd.run() self.assertEqual(result, 0) self.assertEqual( scaffold.vars, {'project': 'Distro', 'egg': 'Distro', 'package': 'distro', 'pyramid_version': '0.10.1dev', 'pyramid_docs_branch': 'master'})
class TestPCreateCommand(unittest.TestCase): def setUp(self): from pyramid.compat import NativeIO self.out_ = NativeIO() def out(self, msg): self.out_.write(msg) def _getTargetClass(self): from pyramid.scripts.pcreate import PCreateCommand return PCreateCommand def _makeOne(self, *args): effargs = ['pcreate'] effargs.extend(args) cmd = self._getTargetClass()(effargs) cmd.out = self.out return cmd def test_run_show_scaffolds_exist(self): cmd = self._makeOne('-l') result = cmd.run() self.assertEqual(result, 0) out = self.out_.getvalue() self.assertTrue(out.startswith('Available scaffolds')) def test_run_show_scaffolds_none_exist(self): cmd = self._makeOne('-l') cmd.scaffolds = [] result = cmd.run() self.assertEqual(result, 0) out = self.out_.getvalue() self.assertTrue(out.startswith('No scaffolds available')) def test_run_no_scaffold_name(self): cmd = self._makeOne() result = cmd.run() self.assertEqual(result, 2) out = self.out_.getvalue() self.assertTrue(out.startswith( 'You must provide at least one scaffold name')) def test_no_project_name(self): cmd = self._makeOne('-s', 'dummy') result = cmd.run() self.assertEqual(result, 2) out = self.out_.getvalue() self.assertTrue(out.startswith('You must provide a project name')) def test_unknown_scaffold_name(self): cmd = self._makeOne('-s', 'dummyXX', 'distro') result = cmd.run() self.assertEqual(result, 2) out = self.out_.getvalue() self.assertTrue(out.startswith('Unavailable scaffolds')) def test_known_scaffold_single_rendered(self): import os cmd = self._makeOne('-s', 'dummy', 'Distro') scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] result = cmd.run() self.assertEqual(result, 0) self.assertEqual( scaffold.output_dir, os.path.normpath(os.path.join(os.getcwd(), 'Distro')) ) self.assertEqual( scaffold.vars, {'project': 'Distro', 'egg': 'Distro', 'package': 'distro'}) def test_known_scaffold_absolute_path(self): import os path = os.path.abspath('Distro') cmd = self._makeOne('-s', 'dummy', path) scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] result = cmd.run() self.assertEqual(result, 0) self.assertEqual( scaffold.output_dir, os.path.normpath(os.path.join(os.getcwd(), 'Distro')) ) self.assertEqual( scaffold.vars, {'project': 'Distro', 'egg': 'Distro', 'package': 'distro'}) def test_known_scaffold_multiple_rendered(self): import os cmd = self._makeOne('-s', 'dummy1', '-s', 'dummy2', 'Distro') scaffold1 = DummyScaffold('dummy1') scaffold2 = DummyScaffold('dummy2') cmd.scaffolds = [scaffold1, scaffold2] result = cmd.run() self.assertEqual(result, 0) self.assertEqual( scaffold1.output_dir, os.path.normpath(os.path.join(os.getcwd(), 'Distro')) ) self.assertEqual( scaffold1.vars, {'project': 'Distro', 'egg': 'Distro', 'package': 'distro'}) self.assertEqual( scaffold2.output_dir, os.path.normpath(os.path.join(os.getcwd(), 'Distro')) ) self.assertEqual( scaffold2.vars, {'project': 'Distro', 'egg': 'Distro', 'package': 'distro'}) def test_known_scaffold_with_path_as_project_target_rendered(self): import os cmd = self._makeOne('-s', 'dummy', '/tmp/foo/Distro/') scaffold = DummyScaffold('dummy') cmd.scaffolds = [scaffold] result = cmd.run() self.assertEqual(result, 0) self.assertEqual( scaffold.output_dir, os.path.normpath(os.path.join(os.getcwd(), '/tmp/foo/Distro')) ) self.assertEqual( scaffold.vars, {'project': 'Distro', 'egg': 'Distro', 'package': 'distro'})
class TestPServeCommand(unittest.TestCase): def setUp(self): from pyramid.compat import NativeIO self.out_ = NativeIO() self.config_factory = dummy.DummyConfigParserFactory() def out(self, msg): self.out_.write(msg) def _get_server(*args, **kwargs): def server(app): return '' return server def _getTargetClass(self): from pyramid.scripts.pserve import PServeCommand return PServeCommand def _makeOne(self, *args): effargs = ['pserve'] effargs.extend(args) cmd = self._getTargetClass()(effargs) cmd.out = self.out cmd.ConfigParser = self.config_factory return cmd def test_run_no_args(self): inst = self._makeOne() result = inst.run() self.assertEqual(result, 2) self.assertEqual(self.out_.getvalue(), 'You must give a config file') def test_config_vars_no_command(self): inst = self._makeOne() inst.args.config_uri = 'foo' inst.args.config_vars = ['a=1', 'b=2'] result = inst.get_config_vars() self.assertEqual(result, {'a': '1', 'b': '2'}) def test_parse_vars_good(self): inst = self._makeOne('development.ini', 'a=1', 'b=2') 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.run() self.assertEqual(app.global_conf, {'a': '1', 'b': '2'}) def test_parse_vars_bad(self): inst = self._makeOne('development.ini', 'a') inst.loadserver = self._get_server self.assertRaises(ValueError, inst.run) def test_config_file_finds_watch_files(self): inst = self._makeOne('development.ini') self.config_factory.items = [( 'watch_files', 'foo\n/baz\npyramid.tests.test_scripts:*.py', )] inst.pserve_file_config('/base/path.ini', global_conf={'a': '1'}) self.assertEqual(self.config_factory.defaults, { 'a': '1', 'here': os.path.abspath('/base'), }) self.assertEqual( inst.watch_files, set([ os.path.abspath('/base/foo'), os.path.abspath('/baz'), os.path.abspath(os.path.join(here, '*.py')), ])) def test_config_file_finds_open_url(self): inst = self._makeOne('development.ini') self.config_factory.items = [( 'open_url', 'http://127.0.0.1:8080/', )] inst.pserve_file_config('/base/path.ini', global_conf={'a': '1'}) self.assertEqual(self.config_factory.defaults, { 'a': '1', 'here': os.path.abspath('/base'), }) self.assertEqual(inst.open_url, 'http://127.0.0.1:8080/') def test__guess_server_url(self): inst = self._makeOne('development.ini') self.config_factory.items = [( 'port', '8080', )] url = inst._guess_server_url('/base/path.ini', 'main', global_conf={'a': '1'}) self.assertEqual(self.config_factory.defaults, { 'a': '1', 'here': os.path.abspath('/base'), }) self.assertEqual(self.config_factory.parser.section, 'server:main') self.assertEqual(url, 'http://127.0.0.1:8080') def test_reload_call_hupper_with_correct_args(self): 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 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, { 'reload_interval': 1, 'verbose': 1, 'worker_kwargs': { 'argv': ['pserve', '--reload', 'development.ini'], 'quiet': False } })
class TestPServeCommand(unittest.TestCase): def setUp(self): from pyramid.compat import NativeIO self.out_ = NativeIO() def out(self, msg): self.out_.write(msg) def _getTargetClass(self): from pyramid.scripts.pserve import PServeCommand return PServeCommand def _makeOne(self, *args): effargs = ['pserve'] effargs.extend(args) cmd = self._getTargetClass()(effargs) cmd.out = self.out return cmd def test_run_no_args(self): inst = self._makeOne() result = inst.run() self.assertEqual(result, 2) self.assertEqual(self.out_.getvalue(), 'You must give a config file') def test_run_stop_daemon_no_such_pid_file(self): path = os.path.join(os.path.dirname(__file__), 'wontexist.pid') inst = self._makeOne('--stop-daemon', '--pid-file=%s' % path) inst.run() self.assertEqual(self.out_.getvalue(), 'No PID file exists in %s' % path) def test_run_stop_daemon_bad_pid_file(self): path = __file__ inst = self._makeOne('--stop-daemon', '--pid-file=%s' % path) inst.run() self.assertEqual(self.out_.getvalue(), 'Not a valid PID file in %s' % path) def test_run_stop_daemon_invalid_pid_in_file(self): import tempfile tmp = tempfile.NamedTemporaryFile() tmp.write(b'9999999') tmp.flush() tmpname = tmp.name inst = self._makeOne('--stop-daemon', '--pid-file=%s' % tmpname) inst.run() try: tmp.close() except: pass self.assertEqual(self.out_.getvalue(), 'PID in %s is not valid (deleting)' % tmpname) def test_parse_vars_good(self): vars = ['a=1', 'b=2'] inst = self._makeOne('development.ini') result = inst.parse_vars(vars) self.assertEqual(result, {'a': '1', 'b': '2'}) def test_parse_vars_bad(self): vars = ['a'] inst = self._makeOne('development.ini') self.assertRaises(ValueError, inst.parse_vars, vars)
class TestPServeCommand(unittest.TestCase): def setUp(self): from pyramid.compat import NativeIO self.out_ = NativeIO() def out(self, msg): self.out_.write(msg) def _getTargetClass(self): from pyramid.scripts.pserve import PServeCommand return PServeCommand def _makeOne(self, *args): effargs = ['pserve'] effargs.extend(args) cmd = self._getTargetClass()(effargs) cmd.out = self.out self.loader = dummy.DummyLoader() cmd._get_config_loader = self.loader return cmd def test_run_no_args(self): inst = self._makeOne() result = inst.run() self.assertEqual(result, 2) self.assertEqual(self.out_.getvalue(), 'You must give a config file') def test_parse_vars_good(self): inst = self._makeOne('development.ini', 'a=1', 'b=2') app = dummy.DummyApp() def get_app(name, global_conf): app.name = name app.global_conf = global_conf return app self.loader.get_wsgi_app = get_app self.loader.server = lambda x: x inst.run() self.assertEqual(app.global_conf, {'a': '1', 'b': '2'}) def test_parse_vars_bad(self): inst = self._makeOne('development.ini', 'a') self.assertRaises(ValueError, inst.run) def test_config_file_finds_watch_files(self): inst = self._makeOne('development.ini') loader = self.loader('/base/path.ini') loader.settings = {'pserve': { 'watch_files': 'foo\n/baz\npyramid.tests.test_scripts:*.py', }} inst.pserve_file_config(loader, global_conf={'a': '1'}) self.assertEqual(loader.calls[0]['defaults'], { 'a': '1', }) self.assertEqual(inst.watch_files, set([ os.path.abspath('/base/foo'), os.path.abspath('/baz'), os.path.abspath(os.path.join(here, '*.py')), ])) def test_config_file_finds_open_url(self): inst = self._makeOne('development.ini') loader = self.loader('/base/path.ini') loader.settings = {'pserve': { 'open_url': 'http://127.0.0.1:8080/', }} inst.pserve_file_config(loader, global_conf={'a': '1'}) self.assertEqual(loader.calls[0]['defaults'], { 'a': '1', }) self.assertEqual(inst.open_url, 'http://127.0.0.1:8080/') def test_guess_server_url(self): inst = self._makeOne('development.ini') loader = self.loader('/base/path.ini') loader.settings = {'server:foo': { 'port': '8080', }} url = inst.guess_server_url(loader, 'foo', global_conf={'a': '1'}) self.assertEqual(loader.calls[0]['defaults'], { 'a': '1', }) self.assertEqual(url, 'http://127.0.0.1:8080') def test_reload_call_hupper_with_correct_args(self): 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 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, { 'reload_interval': 1, 'verbose': 1, 'worker_kwargs': {'argv': ['pserve', '--reload', 'development.ini'], 'quiet': False}})
class TestPServeCommand(unittest.TestCase): def setUp(self): from pyramid.compat import NativeIO self.out_ = NativeIO() self.config_factory = dummy.DummyConfigParserFactory() def out(self, msg): self.out_.write(msg) def _get_server(*args, **kwargs): def server(app): return '' return server def _getTargetClass(self): from pyramid.scripts.pserve import PServeCommand return PServeCommand def _makeOne(self, *args): effargs = ['pserve'] effargs.extend(args) cmd = self._getTargetClass()(effargs) cmd.out = self.out cmd.ConfigParser = self.config_factory return cmd def test_run_no_args(self): inst = self._makeOne() result = inst.run() self.assertEqual(result, 2) self.assertEqual(self.out_.getvalue(), 'You must give a config file') def test_config_vars_no_command(self): inst = self._makeOne() inst.args.config_uri = 'foo' inst.args.config_vars = ['a=1', 'b=2'] result = inst.get_config_vars() self.assertEqual(result, {'a': '1', 'b': '2'}) def test_parse_vars_good(self): inst = self._makeOne('development.ini', 'a=1', 'b=2') 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.run() self.assertEqual(app.global_conf, {'a': '1', 'b': '2'}) def test_parse_vars_bad(self): inst = self._makeOne('development.ini', 'a') inst.loadserver = self._get_server self.assertRaises(ValueError, inst.run) def test_config_file_finds_watch_files(self): inst = self._makeOne('development.ini') self.config_factory.items = [( 'watch_files', 'foo\n/baz\npyramid.tests.test_scripts:*.py', )] inst.pserve_file_config('/base/path.ini', global_conf={'a': '1'}) self.assertEqual(self.config_factory.defaults, { 'a': '1', 'here': os.path.abspath('/base'), }) self.assertEqual(inst.watch_files, [ os.path.abspath('/base/foo'), os.path.abspath('/baz'), os.path.abspath(os.path.join(here, '*.py')), ])
class TestPServeCommand(unittest.TestCase): def setUp(self): from pyramid.compat import NativeIO self.out_ = NativeIO() def out(self, msg): self.out_.write(msg) def _getTargetClass(self): from pyramid.scripts.pserve import PServeCommand return PServeCommand def _makeOne(self, *args): effargs = ['pserve'] effargs.extend(args) cmd = self._getTargetClass()(effargs) cmd.out = self.out self.loader = dummy.DummyLoader() cmd._get_config_loader = self.loader return cmd def test_run_no_args(self): inst = self._makeOne() result = inst.run() self.assertEqual(result, 2) self.assertEqual(self.out_.getvalue(), 'You must give a config file') def test_parse_vars_good(self): inst = self._makeOne('development.ini', 'a=1', 'b=2') app = dummy.DummyApp() def get_app(name, global_conf): app.name = name app.global_conf = global_conf return app self.loader.get_wsgi_app = get_app self.loader.server = lambda x: x inst.run() self.assertEqual(app.global_conf, {'a': '1', 'b': '2'}) def test_parse_vars_bad(self): inst = self._makeOne('development.ini', 'a') self.assertRaises(ValueError, inst.run) def test_config_file_finds_watch_files(self): inst = self._makeOne('development.ini') loader = self.loader('/base/path.ini') loader.settings = { 'pserve': { 'watch_files': 'foo\n/baz\ntests.test_scripts:*.py' } } inst.pserve_file_config(loader, global_conf={'a': '1'}) self.assertEqual(loader.calls[0]['defaults'], {'a': '1'}) self.assertEqual( inst.watch_files, set([ os.path.abspath('/base/foo'), os.path.abspath('/baz'), os.path.abspath(os.path.join(here, '*.py')), ]), ) def test_config_file_finds_open_url(self): inst = self._makeOne('development.ini') loader = self.loader('/base/path.ini') loader.settings = {'pserve': {'open_url': 'http://127.0.0.1:8080/'}} inst.pserve_file_config(loader, global_conf={'a': '1'}) self.assertEqual(loader.calls[0]['defaults'], {'a': '1'}) self.assertEqual(inst.open_url, 'http://127.0.0.1:8080/') def test_guess_server_url(self): inst = self._makeOne('development.ini') loader = self.loader('/base/path.ini') loader.settings = {'server:foo': {'port': '8080'}} url = inst.guess_server_url(loader, 'foo', global_conf={'a': '1'}) self.assertEqual(loader.calls[0]['defaults'], {'a': '1'}) self.assertEqual(url, 'http://127.0.0.1:8080') def test_reload_call_hupper_with_correct_args(self): 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 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, { 'reload_interval': 1, 'verbose': 1, 'worker_kwargs': { 'argv': ['pserve', '--reload', 'development.ini'], 'quiet': False, }, }, )