1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
5 Tests for the command-line scripts in the top-level I{bin/} directory.
7 Tests for actual functionality belong elsewhere, written in a way that doesn't
8 involve launching child processes.
11 from os import devnull, getcwd, chdir
12 from sys import executable
13 from subprocess import PIPE, Popen
15 from twisted.trial.unittest import SkipTest, TestCase
16 from twisted.python.modules import getModule
17 from twisted.python.filepath import FilePath
18 from twisted.python.test.test_shellcomp import ZshScriptTestMixin
21 class ScriptTestsMixin:
23 Mixin for L{TestCase} subclasses which defines a helper function for testing
24 a Twisted-using script.
26 bin = getModule("twisted").pathEntry.filePath.child("bin")
28 def scriptTest(self, name):
30 Verify that the given script runs and uses the version of Twisted
31 currently being tested.
33 This only works when running tests against a vcs checkout of Twisted,
34 since it relies on the scripts being in the place they are kept in
35 version control, and exercises their logic for finding the right version
36 of Twisted to use in that situation.
38 @param name: A path fragment, relative to the I{bin} directory of a
39 Twisted source checkout, identifying a script to test.
42 @raise SkipTest: if the script is not where it is expected to be.
44 script = self.bin.preauthChild(name)
45 if not script.exists():
47 "Script tests do not apply to installed configuration.")
49 from twisted.copyright import version
50 scriptVersion = Popen(
51 [executable, script.path, '--version'],
52 stdout=PIPE, stderr=file(devnull)).stdout.read()
54 self.assertIn(str(version), scriptVersion)
58 class ScriptTests(TestCase, ScriptTestsMixin):
60 Tests for the core scripts.
62 def test_twistd(self):
63 self.scriptTest("twistd")
66 def test_twistdPathInsert(self):
68 The twistd script adds the current working directory to sys.path so
69 that it's able to import modules from it.
71 script = self.bin.child("twistd")
72 if not script.exists():
74 "Script tests do not apply to installed configuration.")
76 self.addCleanup(chdir, cwd)
77 testDir = FilePath(self.mktemp())
80 testDir.child("bar.tac").setContent(
84 [executable, script.path, '-ny', 'bar.tac'],
85 stdout=PIPE, stderr=file(devnull)).stdout.read()
86 self.assertIn(repr(testDir.path), output)
89 def test_manhole(self):
90 self.scriptTest("manhole")
94 self.scriptTest("trial")
97 def test_trialPathInsert(self):
99 The trial script adds the current working directory to sys.path so that
100 it's able to import modules from it.
102 script = self.bin.child("trial")
103 if not script.exists():
105 "Script tests do not apply to installed configuration.")
107 self.addCleanup(chdir, cwd)
108 testDir = FilePath(self.mktemp())
111 testDir.child("foo.py").setContent("")
113 [executable, script.path, 'foo'],
114 stdout=PIPE, stderr=file(devnull)).stdout.read()
115 self.assertIn("PASSED", output)
118 def test_pyhtmlizer(self):
119 self.scriptTest("pyhtmlizer")
122 def test_tap2rpm(self):
123 self.scriptTest("tap2rpm")
126 def test_tap2deb(self):
127 self.scriptTest("tap2deb")
130 def test_tapconvert(self):
131 self.scriptTest("tapconvert")
134 def test_deprecatedTkunzip(self):
136 The entire L{twisted.scripts.tkunzip} module, part of the old Windows
137 installer tool chain, is deprecated.
139 from twisted.scripts import tkunzip
140 warnings = self.flushWarnings(
141 offendingFunctions=[self.test_deprecatedTkunzip])
142 self.assertEqual(DeprecationWarning, warnings[0]['category'])
144 "twisted.scripts.tkunzip was deprecated in Twisted 11.1.0: "
145 "Seek unzipping software outside of Twisted.",
146 warnings[0]['message'])
147 self.assertEqual(1, len(warnings))
150 def test_deprecatedTapconvert(self):
152 The entire L{twisted.scripts.tapconvert} module is deprecated.
154 from twisted.scripts import tapconvert
155 warnings = self.flushWarnings(
156 offendingFunctions=[self.test_deprecatedTapconvert])
157 self.assertEqual(DeprecationWarning, warnings[0]['category'])
159 "twisted.scripts.tapconvert was deprecated in Twisted 12.1.0: "
160 "tapconvert has been deprecated.",
161 warnings[0]['message'])
162 self.assertEqual(1, len(warnings))
166 class ZshIntegrationTestCase(TestCase, ZshScriptTestMixin):
168 Test that zsh completion functions are generated without error
170 generateFor = [('twistd', 'twisted.scripts.twistd.ServerOptions'),
171 ('trial', 'twisted.scripts.trial.Options'),
172 ('pyhtmlizer', 'twisted.scripts.htmlizer.Options'),
173 ('tap2rpm', 'twisted.scripts.tap2rpm.MyOptions'),
174 ('tap2deb', 'twisted.scripts.tap2deb.MyOptions'),
175 ('tapconvert', 'twisted.scripts.tapconvert.ConvertOptions'),
176 ('manhole', 'twisted.scripts.manhole.MyOptions')