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_getCommands(self): """ Should return a dictionary where the key is the command name and the value is the function called to run the command. """ s = ShellProtocol() def f(): pass s.cmd_something = f c = s.getCommands() self.assertEqual(c['something'], f)
def test_getCommands_cache(self): """ getCommands should cache the results """ s = ShellProtocol() c = s.getCommands() def f(): pass s.cmd_something = f c2 = s.getCommands() self.assertEqual(c, c2)
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_cmd_quit(self): """ Should be able to quit """ called = [] class FakeTransport: def loseConnection(self): called.append(True) s = ShellProtocol() s.transport = FakeTransport() s.cmd_quit() self.assertEqual(called, [True])
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_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_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(self): """ runCmd should find the command from arg[0] and run it with args[1:]* """ s = ShellProtocol() s.showPrompt = lambda: None called = [] def fake(*args): called.append(args) return 'hey' s.cmd_foo = fake r = s.runCmd(*['foo', 'arg1', 'arg2', 'arg3']) self.assertEqual(r, 'hey') self.assertEqual(len(called), 1) self.assertEqual(called[0], ('arg1', 'arg2', 'arg3'))
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.")
def test_lineReceived(self): """ lineReceived should parse the args and call runCmd """ s = ShellProtocol() called = [] def parseCmd(s): called.append(('parse', s)) return ['parsed', 'arg'] def runCmd(cmd, *args): called.append(('run', cmd, args)) s.parseCmd = parseCmd s.runCmd = runCmd s.lineReceived('how are you') self.assertEqual(len(called), 2) self.assertEqual(called[0], ('parse', 'how are you')) self.assertEqual(called[1], ('run', 'parsed', ('arg',)))
def t(self, i, expected_output): s = ShellProtocol() r = s.parseCmd(i) self.assertEqual(r, expected_output, "From parseCmd(%r)" % i)