Mercurial > python-cmd2
comparison cmd2.py @ 257:d62bb3dd58a0
multiple test files still not quite working, yet works when run through debugger, aaargh
author | Catherine Devlin <catherine.devlin@gmail.com> |
---|---|
date | Thu, 02 Apr 2009 15:55:33 -0400 |
parents | 573bf9c2a043 |
children | 7a98d7f2da30 |
comparison
equal
deleted
inserted
replaced
256:9ec55d2b3e99 | 257:d62bb3dd58a0 |
---|---|
25 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest | 25 import cmd, re, os, sys, optparse, subprocess, tempfile, pyparsing, doctest |
26 import unittest, string, datetime, urllib, glob | 26 import unittest, string, datetime, urllib, glob |
27 from code import InteractiveConsole, InteractiveInterpreter, softspace | 27 from code import InteractiveConsole, InteractiveInterpreter, softspace |
28 from optparse import make_option | 28 from optparse import make_option |
29 __version__ = '0.5.1' | 29 __version__ = '0.5.1' |
30 import logging | |
31 | |
32 logging.basicConfig(filename='log.txt') | |
30 | 33 |
31 class OptionParser(optparse.OptionParser): | 34 class OptionParser(optparse.OptionParser): |
32 def exit(self, status=0, msg=None): | 35 def exit(self, status=0, msg=None): |
33 self.values._exit = True | 36 self.values._exit = True |
34 if msg: | 37 if msg: |
287 'terminators': str(self.terminators), | 290 'terminators': str(self.terminators), |
288 'settable': ' '.join(self.settable) | 291 'settable': ' '.join(self.settable) |
289 }) | 292 }) |
290 | 293 |
291 def do_help(self, arg): | 294 def do_help(self, arg): |
292 funcname = self.func_named(arg) | 295 if arg: |
293 if funcname: | 296 funcname = self.func_named(arg) |
294 fn = getattr(self, funcname) | 297 if funcname: |
295 try: | 298 fn = getattr(self, funcname) |
296 fn.optionParser.print_help(file=self.stdout) | 299 try: |
297 except AttributeError: | 300 fn.optionParser.print_help(file=self.stdout) |
298 cmd.Cmd.do_help(self, funcname[3:]) | 301 except AttributeError: |
302 cmd.Cmd.do_help(self, funcname[3:]) | |
303 else: | |
304 cmd.Cmd.do_help(self, arg) | |
299 | 305 |
300 def __init__(self, *args, **kwargs): | 306 def __init__(self, *args, **kwargs): |
301 cmd.Cmd.__init__(self, *args, **kwargs) | 307 cmd.Cmd.__init__(self, *args, **kwargs) |
302 self.history = History() | 308 self.history = History() |
303 self._init_parser() | 309 self._init_parser() |
1103 return result.strip('\x00') | 1109 return result.strip('\x00') |
1104 def tearDown(self): | 1110 def tearDown(self): |
1105 sys.stdout = self.old_stdout | 1111 sys.stdout = self.old_stdout |
1106 | 1112 |
1107 class Cmd2TestCase(unittest.TestCase): | 1113 class Cmd2TestCase(unittest.TestCase): |
1108 '''Subclass this, setting CmdApp and transcriptFileName, to make a unittest.TestCase class | 1114 '''Subclass this, setting CmdApp, to make a unittest.TestCase class |
1109 that will execute the commands in transcriptFileName and expect the results shown. | 1115 that will execute the commands in a transcript file and expect the results shown. |
1110 See example.py''' | 1116 See example.py''' |
1111 CmdApp = None | 1117 CmdApp = None |
1112 transcriptFileName = '' | |
1113 transcriptExtension = '' | |
1114 def fetchTranscripts(self): | 1118 def fetchTranscripts(self): |
1115 import pdb; pdb.set_trace() | 1119 self.transcripts = {} |
1116 self.transcripts = [] | 1120 for fname in glob.glob(self.CmdApp.testfile): |
1117 #if self.CmdApp.testfile: | |
1118 # transcriptfilenames = [self.CmdApp.testfile] | |
1119 if self.transcriptExtension: | |
1120 transcriptfilenames = glob.glob('*.' + self.transcriptExtension) | |
1121 elif self.transcriptFileName: | |
1122 transcriptfilenames = [self.transcriptFileName] | |
1123 for fname in transcriptfilenames: | |
1124 tfile = open(fname) | 1121 tfile = open(fname) |
1125 self.transcripts.append(iter(tfile.readlines())) | 1122 self.transcripts[fname] = iter(tfile.readlines()) |
1126 tfile.close() | 1123 tfile.close() |
1127 | 1124 |
1128 def setUp(self): | 1125 def setUp(self): |
1129 if self.CmdApp: | 1126 if self.CmdApp: |
1130 self.outputTrap = OutputTrap() | 1127 self.outputTrap = OutputTrap() |
1131 self.cmdapp = self.CmdApp() | 1128 self.cmdapp = self.CmdApp() |
1132 import pdb; pdb.set_trace() | |
1133 self.fetchTranscripts() | 1129 self.fetchTranscripts() |
1134 def assertEqualEnough(self, got, expected, message): | 1130 def assertEqualEnough(self, got, expected, message): |
1135 got = got.strip().splitlines() | 1131 got = got.strip().splitlines() |
1136 expected = expected.strip().splitlines() | 1132 expected = expected.strip().splitlines() |
1137 self.assertEqual(len(got), len(expected), message) | 1133 self.assertEqual(len(got), len(expected), message) |
1139 matchme = re.escape(lineexpected.strip()).replace('\\*', '.*'). \ | 1135 matchme = re.escape(lineexpected.strip()).replace('\\*', '.*'). \ |
1140 replace('\\ ', ' ') | 1136 replace('\\ ', ' ') |
1141 self.assert_(re.match(matchme, linegot.strip()), message) | 1137 self.assert_(re.match(matchme, linegot.strip()), message) |
1142 def testall(self): | 1138 def testall(self): |
1143 if self.CmdApp: | 1139 if self.CmdApp: |
1144 for transcript in self.transcripts: | 1140 its = sorted(self.transcripts.items()) |
1145 self._test_transcript(transcript) | 1141 logging.error(str(its)) |
1146 def _test_transcript(self, transcript): | 1142 for (fname, transcript) in its: |
1143 logging.error(fname) | |
1144 self._test_transcript(fname, transcript) | |
1145 logging.error('finished') | |
1146 def _test_transcript(self, fname, transcript): | |
1147 lineNum = 0 | 1147 lineNum = 0 |
1148 try: | 1148 try: |
1149 line = transcript.next() | 1149 line = transcript.next() |
1150 while True: | 1150 while True: |
1151 while not line.startswith(self.cmdapp.prompt): | 1151 while not line.startswith(self.cmdapp.prompt): |
1159 self.cmdapp.onecmd(command) | 1159 self.cmdapp.onecmd(command) |
1160 result = self.outputTrap.read() | 1160 result = self.outputTrap.read() |
1161 if line.startswith(self.cmdapp.prompt): | 1161 if line.startswith(self.cmdapp.prompt): |
1162 self.assertEqualEnough(result.strip(), '', | 1162 self.assertEqualEnough(result.strip(), '', |
1163 '\nFile %s, line %d\nCommand was:\n%s\nExpected: (nothing) \nGot:\n%s\n' % | 1163 '\nFile %s, line %d\nCommand was:\n%s\nExpected: (nothing) \nGot:\n%s\n' % |
1164 (self.transcriptFileName, lineNum, command, result)) | 1164 (fname, lineNum, command, result)) |
1165 continue | 1165 continue |
1166 expected = [] | 1166 expected = [] |
1167 while not line.startswith(self.cmdapp.prompt): | 1167 while not line.startswith(self.cmdapp.prompt): |
1168 expected.append(line) | 1168 expected.append(line) |
1169 line = transcript.next() | 1169 line = transcript.next() |
1170 expected = ''.join(expected) | 1170 expected = ''.join(expected) |
1171 self.assertEqualEnough(expected.strip(), result.strip(), | 1171 self.assertEqualEnough(expected.strip(), result.strip(), |
1172 '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n' % | 1172 '\nFile %s, line %d\nCommand was:\n%s\nExpected:\n%s\nGot:\n%s\n' % |
1173 (self.transcriptFileName, lineNum, command, expected, result)) | 1173 (fname, lineNum, command, expected, result)) |
1174 # this needs to account for a line-by-line strip()ping | 1174 # this needs to account for a line-by-line strip()ping |
1175 except StopIteration: | 1175 except StopIteration: |
1176 pass | 1176 pass |
1177 def tearDown(self): | 1177 def tearDown(self): |
1178 if self.CmdApp: | 1178 if self.CmdApp: |