Initial import to Tizen
[profile/ivi/python-twisted.git] / twisted / scripts / test / test_scripts.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4 """
5 Tests for the command-line scripts in the top-level I{bin/} directory.
6
7 Tests for actual functionality belong elsewhere, written in a way that doesn't
8 involve launching child processes.
9 """
10
11 from os import devnull, getcwd, chdir
12 from sys import executable
13 from subprocess import PIPE, Popen
14
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
19
20
21 class ScriptTestsMixin:
22     """
23     Mixin for L{TestCase} subclasses which defines a helper function for testing
24     a Twisted-using script.
25     """
26     bin = getModule("twisted").pathEntry.filePath.child("bin")
27
28     def scriptTest(self, name):
29         """
30         Verify that the given script runs and uses the version of Twisted
31         currently being tested.
32
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.
37
38         @param name: A path fragment, relative to the I{bin} directory of a
39             Twisted source checkout, identifying a script to test.
40         @type name: C{str}
41
42         @raise SkipTest: if the script is not where it is expected to be.
43         """
44         script = self.bin.preauthChild(name)
45         if not script.exists():
46             raise SkipTest(
47                 "Script tests do not apply to installed configuration.")
48
49         from twisted.copyright import version
50         scriptVersion = Popen(
51             [executable, script.path, '--version'],
52             stdout=PIPE, stderr=file(devnull)).stdout.read()
53
54         self.assertIn(str(version), scriptVersion)
55
56
57
58 class ScriptTests(TestCase, ScriptTestsMixin):
59     """
60     Tests for the core scripts.
61     """
62     def test_twistd(self):
63         self.scriptTest("twistd")
64
65
66     def test_twistdPathInsert(self):
67         """
68         The twistd script adds the current working directory to sys.path so
69         that it's able to import modules from it.
70         """
71         script = self.bin.child("twistd")
72         if not script.exists():
73             raise SkipTest(
74                 "Script tests do not apply to installed configuration.")
75         cwd = getcwd()
76         self.addCleanup(chdir, cwd)
77         testDir = FilePath(self.mktemp())
78         testDir.makedirs()
79         chdir(testDir.path)
80         testDir.child("bar.tac").setContent(
81             "import sys\n"
82             "print sys.path\n")
83         output = Popen(
84             [executable, script.path, '-ny', 'bar.tac'],
85             stdout=PIPE, stderr=file(devnull)).stdout.read()
86         self.assertIn(repr(testDir.path), output)
87
88
89     def test_manhole(self):
90         self.scriptTest("manhole")
91
92
93     def test_trial(self):
94         self.scriptTest("trial")
95
96
97     def test_trialPathInsert(self):
98         """
99         The trial script adds the current working directory to sys.path so that
100         it's able to import modules from it.
101         """
102         script = self.bin.child("trial")
103         if not script.exists():
104             raise SkipTest(
105                 "Script tests do not apply to installed configuration.")
106         cwd = getcwd()
107         self.addCleanup(chdir, cwd)
108         testDir = FilePath(self.mktemp())
109         testDir.makedirs()
110         chdir(testDir.path)
111         testDir.child("foo.py").setContent("")
112         output = Popen(
113             [executable, script.path, 'foo'],
114             stdout=PIPE, stderr=file(devnull)).stdout.read()
115         self.assertIn("PASSED", output)
116
117
118     def test_pyhtmlizer(self):
119         self.scriptTest("pyhtmlizer")
120
121
122     def test_tap2rpm(self):
123         self.scriptTest("tap2rpm")
124
125
126     def test_tap2deb(self):
127         self.scriptTest("tap2deb")
128
129
130     def test_tapconvert(self):
131         self.scriptTest("tapconvert")
132
133
134     def test_deprecatedTkunzip(self):
135         """
136         The entire L{twisted.scripts.tkunzip} module, part of the old Windows
137         installer tool chain, is deprecated.
138         """
139         from twisted.scripts import tkunzip
140         warnings = self.flushWarnings(
141             offendingFunctions=[self.test_deprecatedTkunzip])
142         self.assertEqual(DeprecationWarning, warnings[0]['category'])
143         self.assertEqual(
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))
148
149
150     def test_deprecatedTapconvert(self):
151         """
152         The entire L{twisted.scripts.tapconvert} module is deprecated.
153         """
154         from twisted.scripts import tapconvert
155         warnings = self.flushWarnings(
156             offendingFunctions=[self.test_deprecatedTapconvert])
157         self.assertEqual(DeprecationWarning, warnings[0]['category'])
158         self.assertEqual(
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))
163
164
165
166 class ZshIntegrationTestCase(TestCase, ZshScriptTestMixin):
167     """
168     Test that zsh completion functions are generated without error
169     """
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')
177                    ]
178