Example #1
0
    def test_warningToFile(self):
        """
        L{twisted.python.log.showwarning} passes warnings with an explicit file
        target on to the underlying Python warning system.
        """
        message = "another unique message"
        category = FakeWarning
        filename = "warning-filename.py"
        lineno = 31

        output = StringIO()
        log.showwarning(message, category, filename, lineno, file=output)

        self.assertEqual(
            output.getvalue(),
            warnings.formatwarning(message, category, filename, lineno))

        # In Python 2.6 and higher, warnings.showwarning accepts
        # a "line" argument which gives the source line the warning
        # message is to include.
        line = "hello world"
        output = StringIO()
        log.showwarning(message, category, filename, lineno, file=output,
                        line=line)

        self.assertEqual(
            output.getvalue(),
            warnings.formatwarning(message, category, filename, lineno,
                                   line))
    def test_reporter(self):
        """
        Test for LimitedReporter.
        Use test file of indentation to test
        whether limited messages are returned when using LimitedReporter.

        Complete run on the test file will return two warnings:
        W0311 and W0312, but with limited report it returns only one.
        """
        moduleTestIndentation = "twistedchecker.functionaltests.indentation"
        pathTestIndentation = os.path.join(twistedchecker.abspath,
                                      "functionaltests", "indentation.py")
        # assert the test file exists
        self.assertTrue(os.path.exists(pathTestIndentation))
        streamTestResult = NativeStringIO()
        runner = Runner()
        runner.setOutput(streamTestResult)
        # defaultly, runner will use LimitedReporter as its output reporter
        # set allowed messages for it
        runner.linter.reporter.messagesAllowed = set(["W0311"])

        exitResult = self.assertRaises(
            SystemExit, runner.run, [moduleTestIndentation])

        # check the results to see only W0311 is reported
        resultTest = streamTestResult.getvalue()
        self.assertTrue("W0311" in resultTest)
        self.assertTrue("W0312" not in resultTest)
        self.assertEqual(4, exitResult.code)
Example #3
0
    def test_warningToFile(self):
        """
        L{twisted.python.log.showwarning} passes warnings with an explicit file
        target on to the underlying Python warning system.
        """
        # log.showwarning depends on _oldshowwarning being set, which only
        # happens in startLogging(), which doesn't happen if you're not
        # running under trial. So this test only passes by accident of runner
        # environment.
        if log._oldshowwarning is None:
            raise unittest.SkipTest("Currently this test only runs under trial.")
        message = "another unique message"
        category = FakeWarning
        filename = "warning-filename.py"
        lineno = 31

        output = StringIO()
        log.showwarning(message, category, filename, lineno, file=output)

        self.assertEqual(
            output.getvalue(),
            warnings.formatwarning(message, category, filename, lineno))

        # In Python 2.6, warnings.showwarning accepts a "line" argument which
        # gives the source line the warning message is to include.
        if sys.version_info >= (2, 6):
            line = "hello world"
            output = StringIO()
            log.showwarning(message, category, filename, lineno, file=output,
                            line=line)

            self.assertEqual(
                output.getvalue(),
                warnings.formatwarning(message, category, filename, lineno,
                                       line))
Example #4
0
    def test_emitPrefix(self):
        """
        FileLogObserver.emit() will add a timestamp and system prefix to its
        file output.
        """
        output = StringIO()
        flo = log.FileLogObserver(output)
        events = []

        def observer(event):
            # Capture the event for reference and pass it along to flo
            events.append(event)
            flo.emit(event)

        publisher = log.LogPublisher()
        publisher.addObserver(observer)

        publisher.msg("Hello!")
        self.assertEqual(len(events), 1)
        event = events[0]

        result = output.getvalue()
        prefix = "{time} [{system}] ".format(
            time=flo.formatTime(event["time"]), system=event["system"],
        )

        self.assertTrue(
            result.startswith(prefix),
            "{0!r} does not start with {1!r}".format(result, prefix)
        )
Example #5
0
 def test_startLogging(self):
     """
     startLogging() installs FileLogObserver and overrides sys.stdout and
     sys.stderr.
     """
     origStdout, origStderr = sys.stdout, sys.stderr
     self._startLoggingCleanup()
     # When done with test, reset stdout and stderr to current values:
     fakeFile = StringIO()
     observer = log.startLogging(fakeFile)
     self.addCleanup(observer.stop)
     log.msg("Hello!")
     self.assertIn("Hello!", fakeFile.getvalue())
     self.assertIsInstance(sys.stdout, LoggingFile)
     self.assertEqual(sys.stdout.level, NewLogLevel.info)
     encoding = getattr(origStdout, "encoding", None)
     if not encoding:
         encoding = sys.getdefaultencoding()
     self.assertEqual(sys.stdout.encoding.upper(), encoding.upper())
     self.assertIsInstance(sys.stderr, LoggingFile)
     self.assertEqual(sys.stderr.level, NewLogLevel.error)
     encoding = getattr(origStderr, "encoding", None)
     if not encoding:
         encoding = sys.getdefaultencoding()
     self.assertEqual(sys.stderr.encoding.upper(), encoding.upper())
Example #6
0
    def test_removeSafelyRemoveFailsMoveSucceeds(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.
        """
        def dummyRemove():
            """
            Raise an C{OSError} to emulate the branch of L{util._removeSafely}
            in which path removal fails.
            """
            raise OSError()

        # 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() raises an OSError
        dirPath.remove = dummyRemove

        util._removeSafely(dirPath)
        self.assertIn("could not remove FilePath", out.getvalue())
Example #7
0
    def render(self, request):
        """
        Render me to a web client.

        Load my file, execute it in a special namespace (with 'request' and
        '__file__' global vars) and finish the request.  Output to the web-page
        will NOT be handled with print - standard output goes to the log - but
        with request.write.
        """
        request.setHeader(b"x-powered-by", networkString("Twisted/%s" % copyright.version))
        namespace = {'request': request,
                     '__file__': _coerceToFilesystemEncoding("", self.filename),
                     'registry': self.registry}
        try:
            execfile(self.filename, namespace, namespace)
        except IOError as e:
            if e.errno == 2: #file not found
                request.setResponseCode(http.NOT_FOUND)
                request.write(resource.NoResource("File not found.").render(request))
        except:
            io = NativeStringIO()
            traceback.print_exc(file=io)
            output = util._PRE(io.getvalue())
            if _PY3:
                output = output.encode("utf8")
            request.write(output)
        request.finish()
        return server.NOT_DONE_YET
    def setUp(self):
        """
        Add our example directory to the path and record which modules are
        currently loaded.
        """
        self.originalPath = sys.path[:]
        self.originalModules = sys.modules.copy()

        # Python usually expects native strs to be written to sys.stdout/stderr
        self.fakeErr = NativeStringIO()
        self.patch(sys, 'stderr', self.fakeErr)
        self.fakeOut = NativeStringIO()
        self.patch(sys, 'stdout', self.fakeOut)

        # Get documentation root
        here = (
            FilePath(__file__)
            .parent().parent().parent().parent()
            .child('docs')
        )

        # Find the example script within this branch
        for childName in self.exampleRelativePath.split('/'):
            here = here.child(childName)
            if not here.exists():
                raise SkipTest(
                    "Examples (%s) not found - cannot test" % (here.path,))
        self.examplePath = here

        # Add the example parent folder to the Python path
        sys.path.append(self.examplePath.parent().path)

        # Import the example as a module
        moduleName = self.examplePath.basename().split('.')[0]
        self.example = __import__(moduleName)
Example #9
0
    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.0rc1"
""")
Example #10
0
 def test_startLogging(self):
     """
     startLogging() installs FileLogObserver and overrides sys.stdout and
     sys.stderr.
     """
     origStdout, origStderr = sys.stdout, sys.stderr
     self._startLoggingCleanup()
     # When done with test, reset stdout and stderr to current values:
     fakeFile = StringIO()
     observer = log.startLogging(fakeFile)
     self.addCleanup(observer.stop)
     log.msg("Hello!")
     self.assertIn("Hello!", fakeFile.getvalue())
     self.assertIsInstance(sys.stdout, log.StdioOnnaStick)
     self.assertEqual(sys.stdout.isError, False)
     encoding = getattr(origStdout, "encoding", None)
     if not encoding:
         encoding = sys.getdefaultencoding()
     self.assertEqual(sys.stdout.encoding, encoding)
     self.assertIsInstance(sys.stderr, log.StdioOnnaStick)
     self.assertEqual(sys.stderr.isError, True)
     encoding = getattr(origStderr, "encoding", None)
     if not encoding:
         encoding = sys.getdefaultencoding()
     self.assertEqual(sys.stderr.encoding, encoding)
Example #11
0
 def setUp(self):
     """
     Redirect stdout to a temp C{StringIO} stream.
     """
     self.outputStream = NativeStringIO()
     self.patch(sys, "stdout", self.outputStream)
     self.errorStream = NativeStringIO()
     self.patch(sys, "stderr", self.errorStream)
 def test_writeResults(self):
     """
     L{DistTrialRunner.writeResults} writes to the stream specified in the
     init.
     """
     stringIO = StringIO()
     result = DistReporter(Reporter(stringIO))
     self.runner.writeResults(result)
     self.assertTrue(stringIO.tell() > 0)
Example #13
0
        def dump():
            if not self._passed:
                dump = NativeStringIO()
                print("FAILED! dumping build db for debug", file=dump)
                builds = yield self.master.data.get(("builds",))
                for build in builds:
                    yield self.printBuild(build, dump, withLogs=True)

                raise self.failureException(dump.getvalue())
 def test_suppresses(self):
     """
     Any warnings emitted by a call to a function passed to
     L{_collectWarnings} are not actually emitted to the warning system.
     """
     output = StringIO()
     self.patch(sys, 'stdout', output)
     _collectWarnings(lambda x: None, warnings.warn, "text")
     self.assertEqual(output.getvalue(), "")
 def mocked_open(*args, **kwargs):
     """
     Mock for the open call to prevent actually opening a log file.
     """
     open_calls.append((args, kwargs))
     io = NativeStringIO()
     io.name = args[0]
     open_rvalues.append(io)
     return io
Example #16
0
    def test_twisted_import(self):
        """Importing twisted.__main__ does not execute twist."""
        output = StringIO()
        monkey = self.patch(sys, 'stdout', output)

        import twisted.__main__
        self.assertTrue(twisted.__main__)  # Appease pyflakes

        monkey.restore()
        self.assertEqual(output.getvalue(), "")
Example #17
0
 def test_run_bad(self):
     self.patch(sys, 'argv', ['buildbot', 'my', '-l'])
     stdout = NativeStringIO()
     self.patch(sys, 'stdout', stdout)
     try:
         runner.run()
     except SystemExit as e:
         self.assertEqual(e.args[0], 1)
     else:
         self.fail("didn't exit")
     self.assertIn('THIS IS ME', stdout.getvalue())
Example #18
0
    def test_exitMessageZero(self):
        """
        L{exit} given a status code of zero (C{0}) writes the given message to
        standard output.
        """
        out = NativeStringIO()
        self.patch(_exit, "stdout", out)

        message = "Hello, world."
        exit(0, message)

        self.assertEqual(out.getvalue(), message + "\n")
Example #19
0
 def test_printingCapturedVarsCleanedSmokeTest(self):
     """
     C{printDetailedTraceback} includes information about local variables on
     the stack after C{cleanFailure} has been called.
     """
     exampleLocalVar = 'xyzzy'
     f = getDivisionFailure(captureVars=True)
     f.cleanFailure()
     out = NativeStringIO()
     f.printDetailedTraceback(out)
     self.assertNotEqual(None, re.search('exampleLocalVar.*xyzzy',
                                         out.getvalue()))
Example #20
0
    def test_exitMessageNonZero(self):
        """
        L{exit} given a non-zero status code writes the given message to
        standard error.
        """
        out = NativeStringIO()
        self.patch(_exit, "stderr", out)

        message = "Hello, world."
        exit(64, message)

        self.assertEqual(out.getvalue(), message + "\n")
Example #21
0
    def test_run(self):
        """
        Calling run() with no args will cause it to print help.
        """
        stringio = NativeStringIO()
        self.patch(sys, 'stdout', stringio)

        with self.assertRaises(SystemExit) as e:
            run(["--help"])

        self.assertEqual(e.exception.args[0], 0)
        self.assertIn("Show this message and exit", stringio.getvalue())
 def test_flushed(self):
     """
     Any warnings emitted by a test which are flushed are not emitted to the
     Python warning system.
     """
     result = TestResult()
     case = Mask.MockTests('test_flushed')
     output = StringIO()
     monkey = self.patch(sys, 'stdout', output)
     case.run(result)
     monkey.restore()
     self.assertEqual(output.getvalue(), "")
Example #23
0
def runTrial(*args):
    from twisted.trial import reporter
    config = trial.Options()
    config.parseOptions(args)
    output = NativeStringIO()
    myRunner = runner.TrialRunner(
        reporter.VerboseTextReporter,
        stream=output,
        workingDirectory=config['temp-directory'])
    suite = trial._getSuite(config)
    myRunner.run(suite)
    return output.getvalue()
Example #24
0
 def test_printingCaptureVars(self):
     """
     Calling C{Failure(captureVars=True)} captures the locals and globals
     for its stack frames, so L{printDetailedTraceback} will show them in
     its output.
     """
     out = NativeStringIO()
     f = getDivisionFailure(captureVars=True)
     f.printDetailedTraceback(out)
     # Variables are printed on lines with 2 leading spaces.
     linesWithVars = [line for line in out.getvalue().splitlines()
                      if line.startswith('  ')]
     self.assertNotEqual([], linesWithVars)
Example #25
0
def _safeFormat(formatter, o):
    """
    Helper function for L{safe_repr} and L{safe_str}.
    """
    try:
        return formatter(o)
    except:
        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)
Example #26
0
 def test_startLoggingOverridesWarning(self):
     """
     startLogging() overrides global C{warnings.showwarning} such that
     warnings go to Twisted log observers.
     """
     self._startLoggingCleanup()
     # Ugggh, pretend we're starting from newly imported module:
     log._oldshowwarning = None
     fakeFile = StringIO()
     observer = log.startLogging(fakeFile)
     self.addCleanup(observer.stop)
     warnings.warn("hello!")
     output = fakeFile.getvalue()
     self.assertIn("UserWarning: hello!", output)
Example #27
0
def UL(lst):
    io = StringIO()
    io.write("<ul>\n")
    for el in lst:
        io.write("<li> %s</li>\n" % el)
    io.write("</ul>")
    return io.getvalue()
Example #28
0
def linkList(lst):
    io = StringIO()
    io.write("<ul>\n")
    for hr, el in lst:
        io.write('<li> <a href="%s">%s</a></li>\n' % (hr, el))
    io.write("</ul>")
    return io.getvalue()
Example #29
0
    def test_startLoggingOverridesWarning(self):
        """
        startLogging() overrides global C{warnings.showwarning} such that
        warnings go to Twisted log observers.
        """
        self._startLoggingCleanup()
        newPublisher = NewLogPublisher()

        class SysModule(object):
            stdout = object()
            stderr = object()

        tempLogPublisher = LogPublisher(
            newPublisher, newPublisher,
            logBeginner=LogBeginner(newPublisher, StringIO(), SysModule,
                                    warnings)
        )
        # Trial reports warnings in two ways.  First, it intercepts the global
        # 'showwarning' function *itself*, after starting logging (by way of
        # the '_collectWarnings' function which collects all warnings as a
        # around the test's 'run' method).  Second, it has a log observer which
        # immediately reports warnings when they're propagated into the log
        # system (which, in normal operation, happens only at the end of the
        # test case).  In order to avoid printing a spurious warning in this
        # test, we first replace the global log publisher's 'showwarning' in
        # the module with our own.
        self.patch(log, "theLogPublisher", tempLogPublisher)
        # And, one last thing, pretend we're starting from a fresh import, or
        # warnings.warn won't be patched at all.
        log._oldshowwarning = None
        # Global mutable state is bad, kids.  Stay in school.
        fakeFile = StringIO()
        # We didn't previously save log messages, so let's make sure we don't
        # save them any more.
        evt = {"pre-start": "event"}
        received = []

        def preStartObserver(x):
            if 'pre-start' in x.keys():
                received.append(x)

        newPublisher(evt)
        newPublisher.addObserver(preStartObserver)
        log.startLogging(fakeFile, setStdout=False)
        self.addCleanup(tempLogPublisher._stopLogging)
        self.assertEqual(received, [])
        warnings.warn("hello!")
        output = fakeFile.getvalue()
        self.assertIn("UserWarning: hello!", output)
Example #30
0
 def test_testmoduleOnModuleName(self):
     """
     Check that --testmodule does *not* support module names as arguments
     and that it displays a meaningful error message.
     """
     buffy = NativeStringIO()
     stderr, sys.stderr = sys.stderr, buffy
     moduleName = 'twisted.trial.test.test_script'
     try:
         self.config.opt_testmodule(moduleName)
         self.assertEqual(0, len(self.config['tests']))
         self.assertEqual("File %r doesn't exist\n" % (moduleName,),
                              buffy.getvalue())
     finally:
         sys.stderr = stderr
Example #31
0
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 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')
Example #33
0
    def _fork(self, path, uid, gid, executable, args, environment, **kwargs):
        """
        Fork and then exec sub-process.

        @param path: the path where to run the new process.
        @type path: C{str}
        @param uid: if defined, the uid used to run the new process.
        @type uid: C{int}
        @param gid: if defined, the gid used to run the new process.
        @type gid: C{int}
        @param executable: the executable to run in a new process.
        @type executable: C{str}
        @param args: arguments used to create the new process.
        @type args: C{list}.
        @param environment: environment used for the new process.
        @type environment: C{dict}.
        @param kwargs: keyword arguments to L{_setupChild} method.
        """
        collectorEnabled = gc.isenabled()
        gc.disable()
        try:
            self.pid = os.fork()
        except:
            # Still in the parent process
            if collectorEnabled:
                gc.enable()
            raise
        else:
            if self.pid == 0: # pid is 0 in the child process
                # do not put *ANY* code outside the try block. The child process
                # must either exec or _exit. If it gets outside this block (due
                # to an exception that is not handled here, but which might be
                # handled higher up), there will be two copies of the parent
                # running in parallel, doing all kinds of damage.

                # After each change to this code, review it to make sure there
                # are no exit paths.
                try:
                    # Stop debugging. If I am, I don't care anymore.
                    sys.settrace(None)
                    self._setupChild(**kwargs)
                    self._execChild(
                        path, uid, gid, executable, args, environment)
                except:
                    # If there are errors, bail and try to write something
                    # descriptive to stderr.
                    # XXX: The parent's stderr isn't necessarily fd 2 anymore, or
                    #      even still available
                    # XXXX: however even libc assumes write(2, err) is a useful
                    #       thing to attempt
                    try:
                        stderr = os.fdopen(2, 'wb')
                        msg = ("Upon execvpe {0} {1} in environment id {2}"
                               "\n:").format(executable, str(args),
                                             id(environment))
                        tb = NativeStringIO()
                        traceback.print_exc(file=tb)
                        tb = tb.getvalue()

                        if _PY3:
                            msg = msg.encode(sys.getfilesystemencoding())
                            tb = tb.encode(sys.getfilesystemencoding())

                        stderr.write(msg)
                        stderr.write(tb)
                        stderr.flush()

                        for fd in xrange(3):
                            os.close(fd)
                    except:
                        pass # make *sure* the child terminates
                # Did you read the comment about not adding code here?
                os._exit(1)

        # we are now in parent process
        if collectorEnabled:
            gc.enable()
        self.status = -1 # this records the exit status of the child
Example #34
0
 def setUp(self):
     """
     Setup our test case
     """
     self.result = reporter.Reporter(NativeStringIO())
     self.loader = runner.TestLoader()
Example #35
0
def getHTMLOf(ob):
    wr = templatewriter.TemplateWriter('')
    wr.system = ob.system
    f = NativeStringIO()
    wr.writeDocsForOne(ob, f)
    return f.getvalue()
Example #36
0
class ExampleTestBase(object):
    """
    This is a mixin which adds an example to the path, tests it, and then
    removes it from the path and unimports the modules which the test loaded.
    Test cases which test example code and documentation listings should use
    this.

    This is done this way so that examples can live in isolated path entries,
    next to the documentation, replete with their own plugin packages and
    whatever other metadata they need.  Also, example code is a rare instance
    of it being valid to have multiple versions of the same code in the
    repository at once, rather than relying on version control, because
    documentation will often show the progression of a single piece of code as
    features are added to it, and we want to test each one.
    """
    def setUp(self):
        """
        Add our example directory to the path and record which modules are
        currently loaded.
        """
        self.originalPath = sys.path[:]
        self.originalModules = sys.modules.copy()

        # Python usually expects native strs to be written to sys.stdout/stderr
        self.fakeErr = NativeStringIO()
        self.patch(sys, 'stderr', self.fakeErr)
        self.fakeOut = NativeStringIO()
        self.patch(sys, 'stdout', self.fakeOut)

        # Get documentation root
        here = (FilePath(__file__).parent().parent().parent().parent().child(
            'docs'))

        # Find the example script within this branch
        for childName in self.exampleRelativePath.split('/'):
            here = here.child(childName)
            if not here.exists():
                raise SkipTest("Examples (%s) not found - cannot test" %
                               (here.path, ))
        self.examplePath = here

        # Add the example parent folder to the Python path
        sys.path.append(self.examplePath.parent().path)

        # Import the example as a module
        moduleName = self.examplePath.basename().split('.')[0]
        self.example = __import__(moduleName)

    def tearDown(self):
        """
        Remove the example directory from the path and remove all
        modules loaded by the test from sys.modules.
        """
        sys.modules.clear()
        sys.modules.update(self.originalModules)
        sys.path[:] = self.originalPath

    def test_shebang(self):
        """
        The example scripts start with the standard shebang line.
        """
        self.assertEqual(self.examplePath.open().readline().rstrip(),
                         b'#!/usr/bin/env python')

    def test_usageConsistency(self):
        """
        The example script prints a usage message to stdout if it is
        passed a --help option and then exits.

        The first line should contain a USAGE summary, explaining the
        accepted command arguments.
        """
        # Pass None as first parameter - the reactor - it shouldn't
        # get as far as calling it.
        self.assertRaises(SystemExit, self.example.main, None, '--help')

        out = self.fakeOut.getvalue().splitlines()
        self.assertTrue(
            out[0].startswith('Usage:'),
            'Usage message first line should start with "Usage:". '
            'Actual: %r' % (out[0], ))

    def test_usageConsistencyOnError(self):
        """
        The example script prints a usage message to stderr if it is
        passed unrecognized command line arguments.

        The first line should contain a USAGE summary, explaining the
        accepted command arguments.

        The last line should contain an ERROR summary, explaining that
        incorrect arguments were supplied.
        """
        # Pass None as first parameter - the reactor - it shouldn't
        # get as far as calling it.
        self.assertRaises(SystemExit, self.example.main, None,
                          '--unexpected_argument')

        err = self.fakeErr.getvalue().splitlines()
        self.assertTrue(
            err[0].startswith('Usage:'),
            'Usage message first line should start with "Usage:". '
            'Actual: %r' % (err[0], ))
        self.assertTrue(
            err[-1].startswith('ERROR:'),
            'Usage message last line should start with "ERROR:" '
            'Actual: %r' % (err[-1], ))
Example #37
0
 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)
Example #38
0
 def setUpStdoutAssertions(self):
     self.stdout = NativeStringIO()
     self.patch(sys, 'stdout', self.stdout)
 def test_parseJob_empty(self):
     sched = trysched.Try_Jobdir(name='tsched',
                                 builderNames=['a'],
                                 jobdir='foo')
     with self.assertRaises(trysched.BadJobfile):
         sched.parseJob(NativeStringIO(''))
Example #40
0
 def setUp(self):
     self.output = NativeStringIO()
     self.reporter = reporter.TestResult()
     self.loader = runner.TestLoader()
Example #41
0
 def readlines(self):
     alltext = "".join(self.getChunks([self.STDOUT], onlyText=True))
     io = NativeStringIO(alltext)
     return io.readlines()
Example #42
0
 def setUp(self):
     self.stream = NativeStringIO()
     self.runner = runner.TrialRunner(CapturingReporter,
                                      stream=self.stream,
                                      uncleanWarnings=True)
     self.test = TrialRunnerTests('test_empty')
Example #43
0
 def setUp(self):
     UntilFailureTests.FailAfter.count = []
     self.test = UntilFailureTests.FailAfter('test_foo')
     self.stream = NativeStringIO()
     self.runner = runner.TrialRunner(reporter.Reporter, stream=self.stream)
Example #44
0
 def setUp(self):
     self.result = reporter.Reporter(NativeStringIO())
     self.loader = runner.TestLoader()
Example #45
0
def flatten(t):
    io = NativeStringIO()
    writer.flattenToFile(io, t)
    return io.getvalue()
Example #46
0
class OptionsTests(TestCase):
    """
    Tests for L{parseOptions} which parses command line arguments and reads
    message text from stdin to produce an L{Options} instance which can be
    used to send a message.
    """
    memoryReactor = MemoryReactor()

    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_unspecifiedRecipients(self):
        """
        If no recipients are given in the argument list and there is no
        recipient header in the message text, L{parseOptions} raises
        L{SystemExit} with a string describing the problem.
        """
        self.patch(sys, 'stdin',
                   NativeStringIO('Subject: foo\n'
                                  '\n'
                                  'Hello, goodbye.\n'))
        exc = self.assertRaises(SystemExit, parseOptions, [])
        self.assertEqual(exc.args, ('No recipients specified.', ))

    def test_listQueueInformation(self):
        """
        The I{-bp} option for listing queue information is unsupported and
        if it is passed to L{parseOptions}, L{SystemExit} is raised.
        """
        exc = self.assertRaises(SystemExit, parseOptions, ['-bp'])
        self.assertEqual(exc.args, ("Unsupported option.", ))

    def test_stdioTransport(self):
        """
        The I{-bs} option for using stdin and stdout as the SMTP transport
        is unsupported and if it is passed to L{parseOptions}, L{SystemExit}
        is raised.
        """
        exc = self.assertRaises(SystemExit, parseOptions, ['-bs'])
        self.assertEqual(exc.args, ("Unsupported option.", ))

    def test_ignoreFullStop(self):
        """
        The I{-i} and I{-oi} options for ignoring C{"."} by itself on a line
        are unsupported and if either is passed to L{parseOptions},
        L{SystemExit} is raised.
        """
        exc = self.assertRaises(SystemExit, parseOptions, ['-i'])
        self.assertEqual(exc.args, ("Unsupported option.", ))
        exc = self.assertRaises(SystemExit, parseOptions, ['-oi'])
        self.assertEqual(exc.args, ("Unsupported option.", ))

    def test_copyAliasedSender(self):
        """
        The I{-om} option for copying the sender if they appear in an alias
        expansion is unsupported and if it is passed to L{parseOptions},
        L{SystemExit} is raised.
        """
        exc = self.assertRaises(SystemExit, parseOptions, ['-om'])
        self.assertEqual(exc.args, ("Unsupported option.", ))

    def test_version(self):
        """
        The I{--version} option displays the version and raises
        L{SystemExit} with L{None} as the exit code.
        """
        out = NativeStringIO()
        self.patch(sys, 'stdout', out)
        systemExitCode = self.assertRaises(SystemExit, parseOptions,
                                           '--version')
        # SystemExit.code is None on success
        self.assertEqual(systemExitCode.code, None)
        data = out.getvalue()
        self.assertEqual(data, "mailmail version: {}\n".format(version))

    def test_backgroundDelivery(self):
        """
        The I{-odb} flag specifies background delivery.
        """
        stdin = NativeStringIO('\n')
        self.patch(sys, 'stdin', stdin)
        o = parseOptions("-odb")
        self.assertTrue(o.background)

    def test_foregroundDelivery(self):
        """
        The I{-odf} flags specifies foreground delivery.
        """
        stdin = NativeStringIO('\n')
        self.patch(sys, 'stdin', stdin)
        o = parseOptions("-odf")
        self.assertFalse(o.background)

    def test_recipientsFromHeaders(self):
        """
        The I{-t} flags specifies that recipients should be obtained
        from headers.
        """
        stdin = NativeStringIO('To: Curly <*****@*****.**>\n'
                               'Cc: Larry <*****@*****.**>\n'
                               'Bcc: Moe <*****@*****.**>\n'
                               '\n'
                               'Oh, a wise guy?\n')
        self.patch(sys, 'stdin', stdin)
        o = parseOptions("-t")
        self.assertEqual(len(o.to), 3)

    def test_setFrom(self):
        """
        When a message has no I{From:} header, a I{From:} value can be
        specified with the I{-F} flag.
        """
        stdin = NativeStringIO('To: [email protected]\n'
                               'Subject: A wise guy?\n\n')
        self.patch(sys, 'stdin', stdin)
        o = parseOptions(["-F", "Larry <*****@*****.**>", "-t"])
        self.assertEqual(o.sender, "Larry <*****@*****.**>")

    def test_overrideFromFlagByFromHeader(self):
        """
        The I{-F} flag specifies the From: value.  However, I{-F} flag is
        overriden by the value of From: in the e-mail header.
        """
        stdin = NativeStringIO('To: Curly <*****@*****.**>\n'
                               'From: Shemp <*****@*****.**>\n')
        self.patch(sys, 'stdin', stdin)
        o = parseOptions(["-F", "Groucho <*****@*****.**>", "-t"])
        self.assertEqual(o.sender, "*****@*****.**")

    @skipIf(platformType == "win32",
            "mailmail.run() does not work on win32 due to lack of support for"
            " getuid()")
    def test_runErrorsToStderr(self):
        """
        Call L{mailmail.run}, and specify I{-oep} to print errors
        to stderr.  The sender, to, and printErrors options should be
        set and there should be no failure.
        """
        argv = ("test_mailmail.py", "*****@*****.**", "-oep")
        stdin = NativeStringIO('\n')
        self.patch(sys, 'argv', argv)
        self.patch(sys, 'stdin', stdin)
        mailmail.run()
        self.assertEqual(self.options.sender, mailmail.getlogin())
        self.assertEqual(self.options.to, ["*****@*****.**"])
        # We should have printErrors set because we specified "-oep"
        self.assertTrue(self.options.printErrors)
        # We should not have any failures.
        self.assertIsNone(mailmail.failed)

    @skipIf(platformType == "win32",
            "mailmail.run() does not work on win32 due to lack of support for"
            " getuid()")
    def test_readInvalidConfig(self):
        """
        Error messages for illegal UID value, illegal GID value, and illegal
        identity entry will be sent to stderr.
        """
        stdin = NativeStringIO('\n')
        self.patch(sys, 'stdin', stdin)

        filename = self.mktemp()
        myUid = os.getuid()
        myGid = os.getgid()

        with open(filename, "w") as f:
            # Create a config file with some invalid values
            f.write("[useraccess]\n"
                    "allow=invaliduser2,invaliduser1\n"
                    "deny=invaliduser3,invaliduser4,{}\n"
                    "order=allow,deny\n"
                    "[groupaccess]\n"
                    "allow=invalidgid1,invalidgid2\n"
                    "deny=invalidgid1,invalidgid2,{}\n"
                    "order=deny,allow\n"
                    "[identity]\n"
                    "localhost=funny\n"
                    "[addresses]\n"
                    "smarthost=localhost\n"
                    "default_domain=example.com\n".format(myUid, myGid))

        # The mailmail script looks in
        # the twisted.mail.scripts.GLOBAL_CFG variable
        # and then the twisted.mail.scripts.LOCAL_CFG
        # variable for the path to it's  config file.
        #
        # Override twisted.mail.scripts.LOCAL_CFG with the file we just
        # created.
        self.patch(mailmail, "LOCAL_CFG", filename)

        argv = ("test_mailmail.py", "*****@*****.**", "-oep")
        self.patch(sys, 'argv', argv)
        mailmail.run()
        self.assertRegex(
            self.out.getvalue(), "Illegal UID in \\[useraccess\\] section: "
            "invaliduser1")
        self.assertRegex(
            self.out.getvalue(), "Illegal GID in \\[groupaccess\\] section: "
            "invalidgid1")
        self.assertRegex(self.out.getvalue(),
                         'Illegal entry in \\[identity\\] section: funny')

    def getConfigFromFile(self, config):
        """
        Read a mailmail configuration file.

        The mailmail script checks the twisted.mail.scripts.mailmail.GLOBAL_CFG
        variable and then the twisted.mail.scripts.mailmail.LOCAL_CFG
        variable for the path to its  config file.

        @param config: path to config file
        @type config: L{str}

        @return: A parsed config.
        @rtype: L{twisted.mail.scripts.mailmail.Configuration}
        """

        from twisted.mail.scripts.mailmail import loadConfig

        filename = self.mktemp()

        with open(filename, "w") as f:
            f.write(config)

        return loadConfig(filename)

    def test_loadConfig(self):
        """
        L{twisted.mail.scripts.mailmail.loadConfig}
        parses the config file for mailmail.
        """
        config = self.getConfigFromFile("""
[addresses]
smarthost=localhost""")
        self.assertEqual(config.smarthost, "localhost")

        config = self.getConfigFromFile("""
[addresses]
default_domain=example.com""")
        self.assertEqual(config.domain, "example.com")

        config = self.getConfigFromFile("""
[addresses]
smarthost=localhost
default_domain=example.com""")
        self.assertEqual(config.smarthost, "localhost")
        self.assertEqual(config.domain, "example.com")

        config = self.getConfigFromFile("""
[identity]
host1=invalid
host2=username:password""")
        self.assertNotIn("host1", config.identities)
        self.assertEqual(config.identities["host2"], ["username", "password"])

        config = self.getConfigFromFile("""
[useraccess]
allow=invalid1,35
order=allow""")
        self.assertEqual(config.allowUIDs, [35])

        config = self.getConfigFromFile("""
[useraccess]
deny=35,36
order=deny""")
        self.assertEqual(config.denyUIDs, [35, 36])

        config = self.getConfigFromFile("""
[useraccess]
allow=35,36
deny=37,38
order=deny""")
        self.assertEqual(config.allowUIDs, [35, 36])
        self.assertEqual(config.denyUIDs, [37, 38])

        config = self.getConfigFromFile("""
[groupaccess]
allow=gid1,41
order=allow""")
        self.assertEqual(config.allowGIDs, [41])

        config = self.getConfigFromFile("""
[groupaccess]
deny=41
order=deny""")
        self.assertEqual(config.denyGIDs, [41])

        config = self.getConfigFromFile("""
[groupaccess]
allow=41,42
deny=43,44
order=allow,deny""")
        self.assertEqual(config.allowGIDs, [41, 42])
        self.assertEqual(config.denyGIDs, [43, 44])

    def test_senderror(self):
        """
        L{twisted.mail.scripts.mailmail.senderror} sends mail back to the
        sender if an error occurs while sending mail to the recipient.
        """
        def sendmail(host, sender, recipient, body):
            self.assertRegex(sender, "postmaster@")
            self.assertEqual(recipient, ["testsender"])
            self.assertRegex(body.getvalue(), "ValueError")
            return Deferred()

        self.patch(smtp, "sendmail", sendmail)
        opts = mailmail.Options()
        opts.sender = "testsender"
        fail = Failure(ValueError())
        mailmail.senderror(fail, opts)
Example #47
0
 def __repr__(self):
     from pprint import pprint
     sio = NativeStringIO()
     pprint(self._adapterCache, sio)
     return sio.getvalue()
Example #48
0
 def setUp(self):
     self.stream = StringIO()
     self.distReporter = DistReporter(TreeReporter(self.stream))
     self.test = TestCase()
Example #49
0
 def __init__(self):
     self.s = 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))
Example #51
0
class SourceWriter(object):
    _i = 0

    def __init__(self):
        self.s = NativeStringIO()

    def w(self, s):
        self.s.write(s)

    def nl(self):
        self.s.write('\n')
        self.s.write(' ' * 4 * self._i)

    def indent(self):
        self._i += 1
        self.nl()

    def dedent(self):
        self._i -= 1
        self.nl()

    def visitModule(self, node):
        if node.doc is not None:
            self.wl(repr(node.doc))
        walk(node.node, self)

    def visitStmt(self, node):
        for n in node.getChildren():
            walk(n, self)

    def _functionSignature(self, node, fmt):
        if node.defaults:
            nargs = len(node.argnames)
            ndefs = len(node.defaults)
            noDefaults = node.argnames[:nargs - ndefs]
            s = ', '.join(node.argnames[:noDefaults])
            if ndefs < nargs:
                argdefs = zip(node.argnames[noDefaults:], node.defaults)
                s = s + ', ' + ', '.join(['='.join(x) for x in argdefs])
        else:
            s = ', '.join(node.argnames)
        self.w(fmt % (s, ))

    def visitLambda(self, node):
        self._functionSignature(node, 'lambda %s: ')
        walk(node.code, self)

    def visitFunction(self, node):
        self._functionSignature(node, 'def %s(%%s):' % node.name)
        self.indent()
        try:
            walk(node.code, self)
        finally:
            self.dedent()

    def visitAssign(self, node):
        walk(node.nodes[0], self)
        self.w(' = ')
        walk(node.expr, self)
        self.nl()

    def visitAssName(self, node):
        self.w(node.name)

    def visitCallFunc(self, node):
        walk(node.node, self)
        self.w('(')
        for a in node.args[:-1]:
            walk(a, self)
            self.w(', ')
        for a in node.args[-1:]:
            walk(a, self)
        self.w(')')

    def visitListComp(self, node):
        self.w('[')
        walk(node.expr, self)
        for q in node.quals:
            walk(q, self)
        self.w(']')

    def visitList(self, node):
        self.w('[')
        for a in node.nodes[:-1]:
            walk(a, self)
            self.w(', ')
        for a in node.nodes[-1:]:
            walk(a, self)
        self.w(']')

    def visitSet(self, node):
        self.w('{')
        for a in node.nodes[:-1]:
            walk(a, self)
            self.w(', ')
        for a in node.nodes[-1:]:
            walk(a, self)
        self.w('}')

    def visitListCompFor(self, node):
        self.w(' for ')
        walk(node.assign, self)
        self.w(' in ')
        walk(node.list, self)
        for expr in node.ifs:
            self.w(' if ')
            walk(expr, self)

    def visitName(self, node):
        self.w(node.name)

    def visitDiscard(self, node):
        walk(node.expr, self)
        self.nl()

    def visitPrintnl(self, node):
        self.w('print ')
        if node.dest:
            self.w('>>')
            walk(node.dest, self)
            self.w(', ')
        for e in node.nodes:
            walk(e, self)
        self.nl()

    def visitGetattr(self, node):
        walk(node.expr, self)
        self.w('.')
        self.w(node.attrname)

    def visitImport(self, node):
        self.w('import ')
        for (mod, as_) in node.names:
            self.w(mod)
            if as_ is not None:
                self.w(' as ')
                self.w(as_)
            self.w(', ')
        self.nl()

    def visitFrom(self, node):
        self.w('from ')
        self.w(node.modname)
        self.w(' import ')
        for (mod, as_) in node.names:
            self.w(mod)
            if as_ is not None:
                self.w(' as ')
                self.w(as_)
            self.w(', ')
        self.nl()

    def visitConst(self, node):
        self.w(repr(node.value))

    def visitReturn(self, node):
        self.w('return ')
        walk(node.value, self)
        self.nl()

    def visitClass(self, node):
        self.w('class ')
        self.w(node.name)
        if node.bases:
            self.w('(')
            for b in node.bases:
                walk(b, self)
                self.w(', ')
            self.w('):')
        self.indent()
        try:
            if node.doc is not None:
                self.w(repr(node.doc))
            walk(node.code, self)
        finally:
            self.dedent()

    def visitAssAttr(self, node):
        walk(node.expr, self)
        self.w('.')
        self.w(node.attrname)

    def visitMul(self, node):
        walk(node.left, self)
        self.w(' * ')
        walk(node.right, self)

    def visitSub(self, node):
        walk(node.left, self)
        self.w(' - ')
        walk(node.right, self)

    def visitAdd(self, node):
        walk(node.left, self)
        self.w(' + ')
        walk(node.right, self)

    def visitPower(self, node):
        walk(node.left, self)
        self.w('**')
        walk(node.right, self)

    def visitMod(self, node):
        walk(node.left, self)
        self.w(' % ')
        walk(node.right, self)

    def visitAugAssign(self, node):
        walk(node.node, self)
        self.w(' ')
        self.w(node.op)
        self.w(' ')
        walk(node.expr, self)
        self.nl()

    def visitIf(self, node):
        keyword = 'if'
        for (cond, body) in node.tests:
            self.w(keyword)
            self.w(' ')
            walk(cond, self)
            self.w(':')
            self.indent()
            try:
                walk(body, self)
            finally:
                self.dedent()
            keyword = 'elif'
        if node.else_:
            self.w('else:')
            self.indent()
            try:
                walk(node.else_, self)
            finally:
                self.dedent()

    def visitCompare(self, node):
        walk(node.expr, self)
        for (op, arg) in node.ops:
            self.w(' ')
            self.w(op)
            self.w(' ')
            walk(arg, self)

    def visitFor(self, node):
        self.w('for ')
        walk(node.assign, self)
        self.w(' in ')
        walk(node.list, self)
        self.w(':')
        self.indent()
        try:
            walk(node.body, self)
        finally:
            self.dedent()
        if node.else_:
            self.w('else:')
            self.indent()
            try:
                walk(node.else_, self)
            finally:
                self.dedent()

    def visitSlice(self, node):
        walk(node.expr, self)
        self.w('[')
        if node.lower:
            walk(node.lower, self)
        self.w(':')
        if node.upper:
            walk(node.upper, self)
        self.w(']')

    def visitTuple(self, node):
        self.w('(')
        if len(node.nodes) == 0:
            pass
        elif len(node.nodes) == 1:
            walk(node.nodes[0], self)
            self.w(',')
        else:
            for expr in node.nodes[:-1]:
                walk(expr, self)
                self.w(', ')
            walk(node.nodes[-1], self)
        self.w(')')

    def visitTryFinally(self, node):
        self.w('try:')
        self.indent()
        try:
            walk(node.body, self)
        finally:
            self.dedent()
        self.w('finally:')
        self.indent()
        try:
            walk(node.final, self)
        finally:
            self.dedent()

    def visitSubscript(self, node):
        walk(node.expr, self)
        self.w('[')
        walk(node.subs[0], self)
        self.w(']')

    def visitUnarySub(self, node):
        self.w('-')
        walk(node.expr, self)

    def visitAssTuple(self, node):
        self.w('(')
        for expr in node.nodes:
            walk(expr, self)
            self.w(', ')
        self.w(')')

    def visitRaise(self, node):
        self.w('raise ')
        walk(node.expr1, self)
        if node.expr2:
            self.w(', ')
            walk(node.expr2, self)
            if node.expr3:
                self.w(', ')
                walk(node.expr3, self)
        self.nl()

    def visitDict(self, node):
        self.w('{')
        for (k, v) in node.items[:-1]:
            walk(k, self)
            self.w(':')
            walk(v, self)
            self.w(', ')
        for (k, v) in node.items[-1:]:
            walk(k, self)
            self.w(':')
            walk(v, self)
        self.w('}')

    def __str__(self):
        return self.s.getvalue()
Example #52
0
 def getTraceback(self, elideFrameworkCode=0, detail='default'):
     io = StringIO()
     self.printTraceback(file=io,
                         elideFrameworkCode=elideFrameworkCode,
                         detail=detail)
     return io.getvalue()
Example #53
0
    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)
Example #54
0
 def open(self, name):
     assert self.openFile is None, "open() called too many times"
     self.openFile = NativeStringIO()
     return self.openFile
Example #55
0
class PythonLoggingObserverTestCase(unittest.SynchronousTestCase):
    """
    Test the bridge with python logging module.
    """
    def setUp(self):
        self.out = StringIO()

        rootLogger = logging.getLogger("")
        self.originalLevel = rootLogger.getEffectiveLevel()
        rootLogger.setLevel(logging.DEBUG)
        self.hdlr = logging.StreamHandler(self.out)
        fmt = logging.Formatter(logging.BASIC_FORMAT)
        self.hdlr.setFormatter(fmt)
        rootLogger.addHandler(self.hdlr)

        self.lp = log.LogPublisher()
        self.obs = log.PythonLoggingObserver()
        self.lp.addObserver(self.obs.emit)

    def tearDown(self):
        rootLogger = logging.getLogger("")
        rootLogger.removeHandler(self.hdlr)
        rootLogger.setLevel(self.originalLevel)
        logging.shutdown()

    def test_singleString(self):
        """
        Test simple output, and default log level.
        """
        self.lp.msg("Hello, world.")
        self.assertIn("Hello, world.", self.out.getvalue())
        self.assertIn("INFO", self.out.getvalue())

    def test_errorString(self):
        """
        Test error output.
        """
        self.lp.msg(failure=failure.Failure(ValueError("That is bad.")), isError=True)
        self.assertIn("ERROR", self.out.getvalue())

    def test_formatString(self):
        """
        Test logging with a format.
        """
        self.lp.msg(format="%(bar)s oo %(foo)s", bar="Hello", foo="world")
        self.assertIn("Hello oo world", self.out.getvalue())

    def test_customLevel(self):
        """
        Test the logLevel keyword for customizing level used.
        """
        self.lp.msg("Spam egg.", logLevel=logging.DEBUG)
        self.assertIn("Spam egg.", self.out.getvalue())
        self.assertIn("DEBUG", self.out.getvalue())
        self.out.seek(0, 0)
        self.out.truncate()
        self.lp.msg("Foo bar.", logLevel=logging.WARNING)
        self.assertIn("Foo bar.", self.out.getvalue())
        self.assertIn("WARNING", self.out.getvalue())

    def test_strangeEventDict(self):
        """
        Verify that an event dictionary which is not an error and has an empty
        message isn't recorded.
        """
        self.lp.msg(message='', isError=False)
        self.assertEqual(self.out.getvalue(), '')
 def test_parseJob_invalid(self):
     sched = trysched.Try_Jobdir(name='tsched',
                                 builderNames=['a'],
                                 jobdir='foo')
     self.assertRaises(trysched.BadJobfile, sched.parseJob,
                       NativeStringIO('this is not a netstring'))
Example #57
0
class MainTests(TestCase):
    """
    Tests for L{main}.
    """
    def setUp(self):
        self.readStream = StringIO()
        self.writeStream = StringIO()
        self.patch(workertrial, 'startLoggingWithObserver',
                   self.startLoggingWithObserver)
        self.addCleanup(setattr, sys, "argv", sys.argv)
        sys.argv = ["trial"]

    def fdopen(self, fd, mode=None):
        """
        Fake C{os.fdopen} implementation which returns C{self.readStream} for
        the stdin fd and C{self.writeStream} for the stdout fd.
        """
        if fd == _WORKER_AMP_STDIN:
            self.assertIdentical(None, mode)
            return self.readStream
        elif fd == _WORKER_AMP_STDOUT:
            self.assertEqual('w', mode)
            return self.writeStream
        else:
            raise AssertionError("Unexpected fd %r" % (fd, ))

    def startLoggingWithObserver(self, emit, setStdout):
        """
        Override C{startLoggingWithObserver} for not starting logging.
        """
        self.assertFalse(setStdout)

    def test_empty(self):
        """
        If no data is ever written, L{main} exits without writing data out.
        """
        main(self.fdopen)
        self.assertEqual('', self.writeStream.getvalue())

    def test_forwardCommand(self):
        """
        L{main} forwards data from its input stream to a L{WorkerProtocol}
        instance which writes data to the output stream.
        """
        client = FakeAMP()
        clientTransport = StringTransport()
        client.makeConnection(clientTransport)
        client.callRemote(workercommands.Run, testCase=b"doesntexist")
        self.readStream = clientTransport.io
        self.readStream.seek(0, 0)
        main(self.fdopen)
        self.assertIn("No module named 'doesntexist'",
                      self.writeStream.getvalue())

    if _PY3:
        test_forwardCommand.skip = "Does not work on Python 3 (https://tm.tl/8944)"

    def test_readInterrupted(self):
        """
        If reading the input stream fails with a C{IOError} with errno
        C{EINTR}, L{main} ignores it and continues reading.
        """
        excInfos = []

        class FakeStream(object):
            count = 0

            def read(oself, size):
                oself.count += 1
                if oself.count == 1:
                    raise IOError(errno.EINTR)
                else:
                    excInfos.append(sys.exc_info())
                return ''

        self.readStream = FakeStream()
        main(self.fdopen)
        self.assertEqual('', self.writeStream.getvalue())
        self.assertEqual([(None, None, None)], excInfos)

    def test_otherReadError(self):
        """
        L{main} only ignores C{IOError} with C{EINTR} errno: otherwise, the
        error pops out.
        """
        class FakeStream(object):
            count = 0

            def read(oself, size):
                oself.count += 1
                if oself.count == 1:
                    raise IOError("Something else")
                return ''

        self.readStream = FakeStream()
        self.assertRaises(IOError, main, self.fdopen)
 def test_parseJob_invalid_version(self):
     sched = trysched.Try_Jobdir(name='tsched',
                                 builderNames=['a'],
                                 jobdir='foo')
     self.assertRaises(trysched.BadJobfile, sched.parseJob,
                       NativeStringIO('1:9,'))
Example #59
0
 def setUp(self):
     self.setUpDirs('test')
     self.stdout = NativeStringIO()
     self.setUpStdoutAssertions()
Example #60
0
 def getBriefTraceback(self):
     io = StringIO()
     self.printBriefTraceback(file=io)
     return io.getvalue()