def test_help(self): """ Help should spit out the first line of all doc strings of all commands """ class Fake: def __init__(self, doc): self.__doc__ = doc s = ShellProtocol() foo = Fake(''' this is something here. and not here ''') bar = Fake(''' bar is this guy''') s.getCommands = lambda: {'foo': foo, 'bar': bar} called = [] s.sendLine = called.append s.cmd_help() r = '\n'.join(called) self.assertTrue('this is something here.' in r) self.assertTrue('and not here' not in r, 'Only the first line should be included') self.assertTrue('bar is this guy' in r)
def test_help_nocommand(self): """ Help should tell them if the command doesn't exist """ s = ShellProtocol() called = [] s.sendLine = called.append s.cmd_help('foobar') self.assertTrue('foobar' in called[0])
def test_disconnect(self): """ should go through to hub.disconnect """ shell = ShellProtocol() stop_d = defer.Deferred() shell.hub = FakeHub(disconnect=stop_d) sendLine = [] shell.sendLine = sendLine.append shell.cmd_disconnect('tcp:8080') self.assertIn(('disconnect', 'tcp:8080'), shell.hub.called)
def test_runCmd_noCmd(self): """ if there's no such command, tell the person so """ s = ShellProtocol() s.showPrompt = lambda: None called = [] s.sendLine = called.append r = s.runCmd(*['foo', 'bar']) self.assertEqual(r, False) self.assertEqual(len(called), 1) self.assertTrue('foo' in called[0])
def test_help_one(self): """ Help should display help specific to a command. """ s = ShellProtocol() called = [] s.sendLine = called.append s.cmd_help('help') r = '\n'.join(called) self.assertTrue('usage: help [command]' in r) self.assertTrue('You can get specific help' in r)
def test_runCmd_error(self): """ If there's any other kind of error """ s = ShellProtocol() s.showPrompt = lambda: None called = [] s.sendLine = called.append def fake(arg1): raise Exception('foo') s.cmd_foo = fake r = s.runCmd(*['foo', 'arg1']) self.assertEqual(r, False) self.assertEqual(len(called), 1)
def test_build(self): s = ShellProtocol() build_response = {'uid': 'something'} s.hub = FakeHub(build=build_response) sendLine_called = [] s.sendLine = sendLine_called.append s.cmd_build('project', 'version') self.assertNotEqual(sendLine_called, [], "Should have sent something back") self.assertTrue('something' in sendLine_called[0], "Should include the build request uid in the response") self.assertEqual(s.hub.called, [ ('build', dict(project='project', version='version', test_path=None)), ])
def test_runCmd_badArgs(self): """ If the user supplies bad arguments """ s = ShellProtocol() s.showPrompt = lambda: None called = [] s.sendLine = called.append def fake(arg1, arg2): pass s.cmd_foo = fake r = s.runCmd(*['foo', 'arg1']) self.assertEqual(r, False) self.assertEqual(len(called), 1)
def test_connect(self): """ should go through to hub.connect """ shell = ShellProtocol() client = defer.Deferred() shell.hub = FakeHub(connect=client) sendLine = [] shell.sendLine = sendLine.append shell.cmd_connect('my endpoint') while sendLine: sendLine.pop() self.assertIn(('connect', 'my endpoint'), shell.hub.called) client.callback('foo') self.assertNotEqual(sendLine, [], "Once client connects, connectee " "should be notified.")
def test_stop(self): """ stop should go through to stopServer """ shell = ShellProtocol() stop_d = defer.Deferred() shell.hub = FakeHub(stopServer=stop_d) sendLine = [] shell.sendLine = sendLine.append shell.cmd_stop('tcp:8080') while sendLine: sendLine.pop() self.assertIn(('stopServer', 'tcp:8080'), shell.hub.called) stop_d.callback('foo') self.assertNotEqual(sendLine, [], "Once server stops, connectee " "should be notified.")
def test_start(self): """ start should go through to startServer """ shell = ShellProtocol() factory = object() server = defer.Deferred() shell.hub = FakeHub(getPBServerFactory=factory, startServer=server) sendLine = [] shell.sendLine = sendLine.append shell.cmd_start('tcp:8080') while sendLine: sendLine.pop() self.assertIn(('startServer', factory, 'tcp:8080'), shell.hub.called) server.callback('foo') self.assertNotEqual(sendLine, [], "Once server starts, connectee " "should be notified.")