def _safeFormat(formatter, o): """ Helper function for L{safe_repr} and L{safe_str}. Called when C{repr} or C{str} fail. Returns a string containing info about C{o} and the latest exception. @param formatter: C{str} or C{repr}. @type formatter: C{type} @param o: Any object. @rtype: C{str} @return: A string containing information about C{o} and the raised exception. """ io = NativeStringIO() traceback.print_exc(file=io) className = _determineClassName(o) tbValue = io.getvalue() return "<%s instance at 0x%x with %s error:\n %s>" % ( className, id(o), formatter.__name__, tbValue)
def test_help_ordersPrintsSynopsisAndQuits(self): """ --help-orders prints each of the available orders and then exits. """ self.patch(sys, "stdout", NativeStringIO()) exc = self.assertRaises(SystemExit, trial.Options().parseOptions, ["--help-orders"]) self.assertEqual(exc.code, 0) output = sys.stdout.getvalue() msg = "%r with its description not properly described in %r" for orderName, (orderDesc, _) in trial._runOrders.items(): match = re.search( "%s.*%s" % (re.escape(orderName), re.escape(orderDesc)), output, ) self.assertTrue(match, msg=msg % (orderName, output))
def test_parseJob_v5_no_properties(self): sched = trysched.Try_Jobdir(name='tsched', builderNames=['buildera', 'builderb'], jobdir='foo') jobstr = self.makeNetstring( '5', json.dumps({ 'jobid': 'extid', 'branch': 'trunk', 'baserev': '1234', 'patch_level': '1', 'diff': 'this is my diff, -- ++, etc.', 'repository': 'repo', 'project': 'proj', 'who': 'who', 'comment': 'comment', 'builderNames': ['buildera', 'builderb'], 'properties': {}, })) parsedjob = sched.parseJob(NativeStringIO(jobstr)) self.assertEqual(parsedjob['properties'], {})
def test_parseJob_v5_empty_branch_rev(self): sched = trysched.Try_Jobdir(name='tsched', builderNames=['buildera', 'builderb'], jobdir='foo') jobstr = self.makeNetstring( # blank branch, rev are turned to None '4', 'extid', '', '', '1', 'this is my diff, -- ++, etc.', 'repo', 'proj', 'who', 'comment', 'buildera', 'builderc') parsedjob = sched.parseJob(NativeStringIO(jobstr)) self.assertEqual(parsedjob['branch'], None) self.assertEqual(parsedjob['baserev'], None)
def test_parseJob_v1(self): sched = trysched.Try_Jobdir( name='tsched', builderNames=['buildera', 'builderb'], jobdir='foo') jobstr = self.makeNetstring( '1', 'extid', 'trunk', '1234', '1', 'this is my diff, -- ++, etc.', 'buildera', 'builderc' ) parsedjob = sched.parseJob(NativeStringIO(jobstr)) self.assertEqual(parsedjob, { 'baserev': '1234', 'branch': 'trunk', 'builderNames': ['buildera', 'builderc'], 'jobid': 'extid', 'patch_body': 'this is my diff, -- ++, etc.', 'patch_level': 1, 'project': '', 'who': '', 'comment': '', 'repository': '', 'properties': {}, })
def render_control_resource(self, rsrc, path=b'/', params=None, requestJson=None, action="notfound", id=None, content_type=b'application/json'): # pass *either* a request or postpath if params is None: params = {} id = id or self.UUID request = self.make_request(path) request.method = b"POST" request.content = NativeStringIO(requestJson or json.dumps({ "jsonrpc": "2.0", "method": action, "params": params, "id": id })) request.input_headers = {b'content-type': content_type} rv = rsrc.render(request) if rv != server.NOT_DONE_YET: d = defer.succeed(rv) else: d = request.deferred @d.addCallback def check(_json): res = json.loads(bytes2NativeString(_json)) self.assertIn("jsonrpc", res) self.assertEqual(res["jsonrpc"], "2.0") if not requestJson: # requestJson is used for invalid requests, so don't expect ID self.assertIn("id", res) self.assertEqual(res["id"], id) return d
def test_insufficient_args(self): """ Calling run() with no args will cause it to print help. """ stringio = NativeStringIO() self.patch(sys, "stdout", stringio) self.patch(os, "getcwd", self.getcwd) self.patch(datetime, "date", self.date) with self.assertRaises(SystemExit) as e: run(["inctestpkg", "--rc"]) self.assertEqual(e.exception.args[0], 0) self.assertIn("Updating codebase", stringio.getvalue()) self.assertEqual( self.packagedir.child("_version.py").getContent(), b'''""" Provides inctestpkg version information. """ # This file is auto-generated! Do not edit! # Use `python -m incremental.update inctestpkg` to change this file. from incremental import Version __version__ = Version("inctestpkg", 16, 8, 0, release_candidate=1) __all__ = ["__version__"] ''', ) self.assertEqual( self.packagedir.child("__init__.py").getContent(), b""" from incremental import Version introduced_in = Version("inctestpkg", 16, 8, 0, release_candidate=1).short() next_released_version = "inctestpkg 16.8.0.rc1" """, )
def test_reactorSelectionMixinNotAvailable(self): """ Test that the usage mixin exits when trying to use a reactor not available (the reactor raises an error at installation), giving an error message. """ class ReactorSelectionOptions(usage.Options, app.ReactorSelectionMixin): pass message = "Missing foo bar" def install(): raise ImportError(message) name = 'fakereactortest' package = __name__ description = 'description' self.pluginResults = [FakeReactor(install, name, package, description)] options = ReactorSelectionOptions() options.messageOutput = NativeStringIO() e = self.assertRaises(usage.UsageError, options.parseOptions, ['--reactor', 'fakereactortest', 'subcommand']) self.assertIn(message, e.args[0]) self.assertIn("help-reactors", e.args[0])
def setUp(self): """ Override some things in mailmail, so that we capture C{stdout}, and do not call L{reactor.stop}. """ self.out = NativeStringIO() # Override the mailmail logger, so we capture stderr output from twisted.logger import textFileLogObserver, Logger logObserver = textFileLogObserver(self.out) self.patch(mailmail, '_log', Logger(observer=logObserver)) self.host = None self.options = None self.ident = None # Override mailmail.sendmail, so we don't call reactor.stop() def sendmail(host, options, ident): self.host = host self.options = options self.ident = ident return smtp.sendmail(host, options.sender, options.to, options.body, reactor=self.memoryReactor) self.patch(mailmail, 'sendmail', sendmail)
def test_removeSafelyRemoveFailsMoveFails(self): """ If an L{OSError} is raised while removing a path in L{util._removeSafely}, an attempt is made to move the path to a new name. If that attempt fails, the L{OSError} is re-raised. """ def dummyRemove(): """ Raise an C{OSError} to emulate the branch of L{util._removeSafely} in which path removal fails. """ raise OSError("path removal failed") def dummyMoveTo(path): """ Raise an C{OSError} to emulate the branch of L{util._removeSafely} in which path movement fails. """ raise OSError("path movement failed") # Patch stdout so we can check the print statements in _removeSafely out = NativeStringIO() self.patch(sys, 'stdout', out) # Set up a trial directory with a _trial_marker directory = self.mktemp().encode("utf-8") os.mkdir(directory) dirPath = filepath.FilePath(directory) dirPath.child(b'_trial_marker').touch() # Ensure that path.remove() and path.moveTo() both raise OSErrors dirPath.remove = dummyRemove dirPath.moveTo = dummyMoveTo error = self.assertRaises(OSError, util._removeSafely, dirPath) self.assertEqual(str(error), "path movement failed") self.assertIn("could not remove FilePath", out.getvalue())
def setUpStdoutAssertions(self): self.stdout = NativeStringIO() self.patch(sys, 'stdout', self.stdout)
def open(self, name): assert self.openFile is None, "open() called too many times" self.openFile = NativeStringIO() return self.openFile
def parseOptions(argv): o = Options() o.to = [e for e in argv if not e.startswith('-')] o.sender = getlogin() # Just be very stupid # Skip -bm -- it is the default # Add a non-standard option for querying the version of this tool. if '--version' in argv: print('mailmail version:', version) raise SystemExit() # -bp lists queue information. Screw that. if '-bp' in argv: raise _unsupportedOption # -bs makes sendmail use stdin/stdout as its transport. Screw that. if '-bs' in argv: raise _unsupportedOption # -F sets who the mail is from, but is overridable by the From header if '-F' in argv: o.sender = argv[argv.index('-F') + 1] o.to.remove(o.sender) # -i and -oi makes us ignore lone "." if ('-i' in argv) or ('-oi' in argv): raise _unsupportedOption # -odb is background delivery if '-odb' in argv: o.background = True else: o.background = False # -odf is foreground delivery if '-odf' in argv: o.background = False else: o.background = True # -oem and -em cause errors to be mailed back to the sender. # It is also the default. # -oep and -ep cause errors to be printed to stderr if ('-oep' in argv) or ('-ep' in argv): o.printErrors = True else: o.printErrors = False # -om causes a copy of the message to be sent to the sender if the sender # appears in an alias expansion. We do not support aliases. if '-om' in argv: raise _unsupportedOption # -t causes us to pick the recipients of the message from # the To, Cc, and Bcc headers, and to remove the Bcc header # if present. if '-t' in argv: o.recipientsFromHeaders = True o.excludeAddresses = o.to o.to = [] else: o.recipientsFromHeaders = False o.exludeAddresses = [] requiredHeaders = { 'from': [], 'to': [], 'cc': [], 'bcc': [], 'date': [], } buffer = NativeStringIO() while 1: write = 1 line = sys.stdin.readline() if not line.strip(): break hdrs = line.split(': ', 1) hdr = hdrs[0].lower() if o.recipientsFromHeaders and hdr in ('to', 'cc', 'bcc'): o.to.extend([email.utils.parseaddr(hdrs[1])[1]]) if hdr == 'bcc': write = 0 elif hdr == 'from': o.sender = email.utils.parseaddr(hdrs[1])[1] if hdr in requiredHeaders: requiredHeaders[hdr].append(hdrs[1]) if write: buffer.write(line) if not requiredHeaders['from']: buffer.write('From: {}\r\n'.format(o.sender)) if not requiredHeaders['to']: if not o.to: raise SystemExit("No recipients specified.") buffer.write('To: {}\r\n'.format(', '.join(o.to))) if not requiredHeaders['date']: buffer.write('Date: {}\r\n'.format(smtp.rfc822date())) buffer.write(line) if o.recipientsFromHeaders: for a in o.excludeAddresses: try: o.to.remove(a) except: pass buffer.seek(0, 0) o.body = NativeStringIO(buffer.getvalue() + sys.stdin.read()) return o
def setUp(self): self.setUpDirs('test') self.stdout = NativeStringIO() self.setUpStdoutAssertions()
def createSummary(self, log): for line in NativeStringIO(log.getText()): # what we do with the line isn't important to the test assert line in ('some\n', 'output\n')
def setUp(self): self.output = NativeStringIO() self.reporter = reporter.TestResult() self.loader = runner.TestLoader()
def test_parseJob_invalid_version(self): sched = trysched.Try_Jobdir(name='tsched', builderNames=['a'], jobdir='foo') with self.assertRaises(trysched.BadJobfile): sched.parseJob(NativeStringIO('1:9,'))
def __repr__(self): from pprint import pprint sio = NativeStringIO() pprint(self._adapterCache, sio) return sio.getvalue()
def readlines(self): alltext = "".join(self.getChunks([self.STDOUT], onlyText=True)) io = NativeStringIO(alltext) return io.readlines()
def __init__(self): self.s = NativeStringIO()
def test_parseJob_empty(self): sched = trysched.Try_Jobdir(name='tsched', builderNames=['a'], jobdir='foo') self.assertRaises(trysched.BadJobfile, sched.parseJob, NativeStringIO(''))
def test_parseJob_v5_invalid_json(self): sched = trysched.Try_Jobdir( name='tsched', builderNames=['buildera', 'builderb'], jobdir='foo') jobstr = self.makeNetstring('5', '{"comment": "com}') with self.assertRaises(trysched.BadJobfile): sched.parseJob(NativeStringIO(jobstr))
def setUp(self): self.result = reporter.Reporter(NativeStringIO()) self.loader = runner.TestLoader()
def mocked_open(*args, **kwargs): """ Mock for the open call to prevent actually opening /proc/net/tcp. """ open_calls.append((args, kwargs)) return NativeStringIO(self.sampleFile)
def assertDetailedTraceback(self, captureVars=False, cleanFailure=False): """ Assert that L{printDetailedTraceback} produces and prints a detailed traceback. The detailed traceback consists of a header:: *--- Failure #20 --- The body contains the stacktrace:: /twisted/trial/_synctest.py:1180: _run(...) /twisted/python/util.py:1076: runWithWarningsSuppressed(...) --- <exception caught here> --- /twisted/test/test_failure.py:39: getDivisionFailure(...) If C{captureVars} is enabled the body also includes a list of globals and locals:: [ Locals ] exampleLocalVar : 'xyz' ... ( Globals ) ... Or when C{captureVars} is disabled:: [Capture of Locals and Globals disabled (use captureVars=True)] When C{cleanFailure} is enabled references to other objects are removed and replaced with strings. And finally the footer with the L{Failure}'s value:: exceptions.ZeroDivisionError: float division *--- End of Failure #20 --- @param captureVars: Enables L{Failure.captureVars}. @type captureVars: C{bool} @param cleanFailure: Enables L{Failure.cleanFailure}. @type cleanFailure: C{bool} """ if captureVars: exampleLocalVar = 'xyz' f = getDivisionFailure(captureVars=captureVars) out = NativeStringIO() if cleanFailure: f.cleanFailure() f.printDetailedTraceback(out) tb = out.getvalue() start = "*--- Failure #%d%s---\n" % (f.count, (f.pickled and ' (pickled) ') or ' ') end = "%s: %s\n*--- End of Failure #%s ---\n" % (reflect.qual( f.type), reflect.safe_str(f.value), f.count) self.assertTracebackFormat(tb, start, end) # Variables are printed on lines with 2 leading spaces. linesWithVars = [ line for line in tb.splitlines() if line.startswith(' ') ] if captureVars: self.assertNotEqual([], linesWithVars) if cleanFailure: line = ' exampleLocalVar : "\'xyz\'"' else: line = " exampleLocalVar : 'xyz'" self.assertIn(line, linesWithVars) else: self.assertEqual([], linesWithVars) self.assertIn( ' [Capture of Locals and Globals disabled (use ' 'captureVars=True)]\n', tb)
def setUp(self): self.stream = NativeStringIO() self.runner = runner.TrialRunner(CapturingReporter, stream=self.stream, uncleanWarnings=True) self.test = TrialRunnerTests('test_empty')
def prepareDiff(self): """ Prepare to run the checker and get diff results. """ self.streamForDiff = NativeStringIO() self.linter.reporter.set_output(self.streamForDiff)
def setUp(self): UntilFailureTests.FailAfter.count = [] self.test = UntilFailureTests.FailAfter('test_foo') self.stream = NativeStringIO() self.runner = runner.TrialRunner(reporter.Reporter, stream=self.stream)
def setUp(self): """ Setup our test case """ self.result = reporter.Reporter(NativeStringIO()) self.loader = runner.TestLoader()
def test_parseJob_invalid(self): sched = trysched.Try_Jobdir(name='tsched', builderNames=['a'], jobdir='foo') with self.assertRaises(trysched.BadJobfile): sched.parseJob(NativeStringIO('this is not a netstring'))