/usr/lib/python2.7/dist-packages/twisted/names/test/test_examples.py is in python-twisted-names 13.2.0-1ubuntu1.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | # Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.
"""
Tests for L{twisted.names} example scripts.
"""
import sys
from StringIO import StringIO
from twisted.python.filepath import FilePath
from twisted.trial.unittest import SkipTest, TestCase
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()
self.fakeErr = StringIO()
self.patch(sys, 'stderr', self.fakeErr)
self.fakeOut = StringIO()
self.patch(sys, 'stdout', self.fakeOut)
# Get branch root
here = FilePath(__file__).parent().parent().parent().parent()
# 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.assertEquals(
self.examplePath.open().readline().rstrip(),
'#!/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],))
class TestDnsTests(ExampleTestBase, TestCase):
"""
Test the testdns.py example script.
"""
exampleRelativePath = 'doc/names/examples/testdns.py'
class GetHostByNameTests(ExampleTestBase, TestCase):
"""
Test the gethostbyname.py example script.
"""
exampleRelativePath = 'doc/names/examples/gethostbyname.py'
class DnsServiceTests(ExampleTestBase, TestCase):
"""
Test the dns-service.py example script.
"""
exampleRelativePath = 'doc/names/examples/dns-service.py'
|