1
# Copyright (c) 2006 Twisted Matrix Laboratories.
2
# See LICENSE for details.
5
Test cases for twisted.python.zshcomp
9
from cStringIO import StringIO
11
from twisted.trial import unittest
12
from twisted.python import zshcomp, usage
14
class ZshcompTestCase(unittest.TestCase):
16
Tests for the zsh completion function builder in twisted/python/zshcomp.py
18
def test_buildAll(self):
20
Build all the completion functions for twisted commands - no errors
23
dirname = self.mktemp()
25
skippedCmds = [x[0] for x in zshcomp.makeCompFunctionFiles(dirname)]
27
# verify a zsh function was created for each twisted command
28
for info in zshcomp.generateFor:
29
if info[0] in skippedCmds:
31
funcPath = os.path.join(dirname, '_' + info[0])
32
self.failUnless(os.path.exists(funcPath))
34
def test_accumulateMetadata(self):
36
Test that the zsh_* variables you can place on Option classes gets
40
ag = zshcomp.ArgumentsGenerator('dummy_cmd', opts, 'dummy_value')
42
altArgDescr = TestOptions.zsh_altArgDescr.copy()
43
altArgDescr.update(TestOptions2.zsh_altArgDescr)
45
actionDescr = TestOptions.zsh_actionDescr.copy()
46
actionDescr.update(TestOptions2.zsh_actionDescr)
48
self.failUnlessEquals(ag.altArgDescr, altArgDescr)
49
self.failUnlessEquals(ag.actionDescr, actionDescr)
50
self.failUnlessEquals(ag.multiUse, TestOptions.zsh_multiUse)
51
self.failUnlessEquals(ag.mutuallyExclusive,
52
TestOptions.zsh_mutuallyExclusive)
53
self.failUnlessEquals(ag.actions, TestOptions.zsh_actions)
54
self.failUnlessEquals(ag.extras, TestOptions.zsh_extras)
56
def test_accumulateAdditionalOptions(self):
58
Test that we pick up options that are only defined by having an
59
appropriately named method on your Options class,
60
e.g. def opt_foo(self, foo)
63
ag = zshcomp.ArgumentsGenerator('dummy_cmd', opts, 'dummy_value')
65
self.failUnless('nocrash' in ag.optFlags_d and \
66
'nocrash' in ag.optAll_d)
67
self.failUnless('difficulty' in ag.optParams_d and \
68
'difficulty' in ag.optAll_d)
70
def test_verifyZshNames(self):
72
Test that using a parameter/flag name that doesn't exist
75
class TmpOptions(TestOptions2):
76
zsh_actions = {'detaill' : 'foo'} # Note typo of detail
79
self.failUnlessRaises(ValueError, zshcomp.ArgumentsGenerator,
80
'dummy_cmd', opts, 'dummy_value')
82
def test_zshCode(self):
84
Generate a completion function, and test the textual output
85
against a known correct output
88
opts = CodeTestOptions()
90
b = zshcomp.Builder(cmd_name, opts, f)
93
self.failUnlessEquals(f.read(), testOutput1)
95
def test_skipBuild(self):
97
Test that makeCompFunctionFiles skips building for commands whos
98
script module cannot be imported
100
generateFor = [('test_cmd', 'no.way.your.gonna.import.this', 'Foo')]
101
skips = zshcomp.makeCompFunctionFiles('out_dir', generateFor, {})
102
# no exceptions should be raised. hooray.
103
self.failUnlessEqual(len(skips), 1)
104
self.failUnlessEqual(len(skips[0]), 2)
105
self.failUnlessEqual(skips[0][0], 'test_cmd')
106
self.failUnless(isinstance(skips[0][1], ImportError))
107
self.flushLoggedErrors(self, ImportError)
109
class TestOptions(usage.Options):
111
Command-line options for an imaginary game
113
optFlags = [['fokker', 'f',
114
'Select the Fokker Dr.I as your dogfighter aircraft'],
116
'Select the Albatros D-III as your dogfighter aircraft'],
118
'Select the SPAD S.VII as your dogfighter aircraft'],
120
'Select the Bristol Scout as your dogfighter aircraft'],
122
'Enable secret Twisted physics engine'],
124
'Enable a small chance that your machine guns will jam!'],
126
'Verbose logging (may be specified more than once)'],
129
optParameters = [['pilot-name', None, "What's your name, Ace?",
130
'Manfred von Richthofen'],
132
'Select the level of rendering detail (1-5)', '3'],
136
zsh_altArgDescr = {'physics' : 'Twisted-Physics',
137
'detail' : 'Rendering detail level'}
138
zsh_actionDescr = {'detail' : 'Pick your detail'}
139
zsh_multiUse = ['verbose']
140
zsh_mutuallyExclusive = [['fokker', 'albatros', 'spad', 'bristol']]
141
zsh_actions = {'detail' : '(1 2 3 4 5)'}
142
zsh_extras = [':saved game file to load:_files']
144
class TestOptions2(TestOptions):
146
Extend the options and zsh metadata provided by TestOptions. zshcomp must
147
accumulate options and metadata from all classes in the hiearchy so this
148
is important for testing
150
optFlags = [['no-stalls', None,
151
'Turn off the ability to stall your aircraft']]
152
optParameters = [['reality-level', None,
153
'Select the level of physics reality (1-5)', '5']]
155
zsh_altArgDescr = {'no-stalls' : 'Can\'t stall your plane'}
156
zsh_actionDescr = {'reality-level' : 'Physics reality level'}
158
def opt_nocrash(self):
159
"""Select that you can't crash your plane"""
161
def opt_difficulty(self, difficulty):
162
"""How tough are you? (1-10)"""
164
def _accuracyAction():
167
class CodeTestOptions(usage.Options):
169
Command-line options for an imaginary program
171
optFlags = [['color', 'c', 'Turn on color output'],
172
['gray', 'g', 'Turn on gray-scale output'],
174
'Verbose logging (may be specified more than once)'],
177
optParameters = [['optimization', None,
178
'Select the level of optimization (1-5)', '5'],
180
'Select the level of accuracy (1-3)', '3'],
184
zsh_altArgDescr = {'color' : 'Color on',
185
'optimization' : 'Optimization level'}
186
zsh_actionDescr = {'optimization' : 'Optimization?',
187
'accuracy' : 'Accuracy?'}
188
zsh_multiUse = ['verbose']
189
zsh_mutuallyExclusive = [['color', 'gray']]
190
zsh_actions = {'optimization' : '(1 2 3 4 5)',
191
'accuracy' : _accuracyAction}
192
zsh_extras = [':output file:_files']
194
testOutput1 = """#compdef testprog
195
_arguments -s -A "-*" \\
196
':output file:_files' \\
197
'(--accuracy)-a[3]:Accuracy?:(1 2 3)' \\
198
'(-a)--accuracy=[3]:Accuracy?:(1 2 3)' \\
199
'(--gray -g --color)-c[Color on]' \\
200
'(--gray -g -c)--color[Color on]' \\
201
'(--color -c --gray)-g[Turn on gray-scale output]' \\
202
'(--color -c -g)--gray[Turn on gray-scale output]' \\
203
'--help[Display this help and exit.]' \\
204
'--optimization=[Optimization level]:Optimization?:(1 2 3 4 5)' \\
205
'*-v[Verbose logging (may be specified more than once)]' \\
206
'*--verbose[Verbose logging (may be specified more than once)]' \\
207
'--version[version]' \\