Skip to content

Commit 45d40dc

Browse files
committed
Add testing interactive mode to roundup_admin. remove redundant imports
1 parent 3254b45 commit 45d40dc

File tree

2 files changed

+53
-34
lines changed

2 files changed

+53
-34
lines changed

roundup/admin.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ class AdminTool:
8383
8484
Additional help may be supplied by help_*() methods.
8585
"""
86+
87+
# Make my_input a property to allow overriding in testing.
88+
# my_input is imported in other places, so just set it from
89+
# the imported value rather than moving def here.
90+
my_input = my_input
91+
8692
def __init__(self):
8793
self.commands = CommandDict()
8894
for k in AdminTool.__dict__:
@@ -442,7 +448,7 @@ def do_install(self, tracker_home, args):
442448
if list(filter(os.path.exists, [config_ini_file,
443449
os.path.join(tracker_home, 'config.py')])):
444450
if not self.force:
445-
ok = my_input(_(
451+
ok = self.my_input(_(
446452
"""WARNING: There appears to be a tracker in "%(tracker_home)s"!
447453
If you re-install it, you will lose all the data!
448454
Erase it? Y/N: """) % locals()) # noqa: E122
@@ -548,7 +554,7 @@ def _get_choice(self, list_name, prompt, options, argument, default=None):
548554
return default
549555
sys.stdout.write('%s: %s\n' % (list_name, ', '.join(options)))
550556
while argument not in options:
551-
argument = my_input('%s [%s]: ' % (prompt, default))
557+
argument = self.my_input('%s [%s]: ' % (prompt, default))
552558
if not argument:
553559
return default
554560
return argument
@@ -605,7 +611,7 @@ def do_initialise(self, tracker_home, args):
605611
# is there already a database?
606612
if tracker.exists():
607613
if not self.force:
608-
ok = my_input(_(
614+
ok = self.my_input(_(
609615
"""WARNING: The database is already initialised!
610616
If you re-initialise it, you will lose all the data!
611617
Erase it? Y/N: """)) # noqa: E122
@@ -1017,7 +1023,7 @@ def do_create(self, args):
10171023
if value:
10181024
props[key] = value
10191025
else:
1020-
value = my_input(_('%(propname)s (%(proptype)s): ') % {
1026+
value = self.my_input(_('%(propname)s (%(proptype)s): ') % {
10211027
'propname': key.capitalize(), 'proptype': name})
10221028
if value:
10231029
props[key] = value
@@ -1751,7 +1757,7 @@ def run_command(self, args):
17511757
# make sure we have a tracker_home
17521758
while not self.tracker_home:
17531759
if not self.force:
1754-
self.tracker_home = my_input(_('Enter tracker home: ')).strip()
1760+
self.tracker_home = self.my_input(_('Enter tracker home: ')).strip()
17551761
else:
17561762
self.tracker_home = os.curdir
17571763

@@ -1825,7 +1831,7 @@ def interactive(self):
18251831

18261832
while 1:
18271833
try:
1828-
command = my_input('roundup> ')
1834+
command = self.my_input('roundup> ')
18291835
except EOFError:
18301836
print(_('exit...'))
18311837
break
@@ -1840,7 +1846,7 @@ def interactive(self):
18401846

18411847
# exit.. check for transactions
18421848
if self.db and self.db_uncommitted:
1843-
commit = my_input(_('There are unsaved changes. Commit them (y/N)? '))
1849+
commit = self.my_input(_('There are unsaved changes. Commit them (y/N)? '))
18441850
if commit and commit[0].lower() == 'y':
18451851
self.db.commit()
18461852
return 0

test/test_admin.py

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,50 @@ def install_init(self, type="classic",
9494
self.assertEqual(ret, 0)
9595

9696

97+
def testBasicInteractive(self):
98+
# first command is an error that should be handled
99+
inputs = iter(["'quit", "quit"])
100+
101+
orig_input = AdminTool.my_input
102+
103+
AdminTool.my_input = lambda _self, _prompt: next(inputs)
104+
105+
self.install_init()
106+
self.admin=AdminTool()
107+
sys.argv=['main', '-i', self.dirname]
108+
109+
with captured_output() as (out, err):
110+
ret = self.admin.main()
111+
112+
out = out.getvalue().strip()
113+
114+
print(ret)
115+
self.assertTrue(ret == 0)
116+
expected = 'ready for input.\nType "help" for help.'
117+
self.assertEqual(expected, out[-1*len(expected):])
118+
119+
inputs = iter(["list user", "quit"])
120+
121+
AdminTool.my_input = lambda _self, _prompt: next(inputs)
122+
123+
with captured_output() as (out, err):
124+
ret = self.admin.main()
125+
126+
out = out.getvalue().strip()
127+
128+
print(ret)
129+
self.assertTrue(ret == 0)
130+
expected = 'help.\n 1: admin\n 2: anonymous'
131+
self.assertEqual(expected, out[-1*len(expected):])
132+
133+
134+
AdminTool.my_input = orig_input
135+
97136
def testGet(self):
98137
''' Note the tests will fail if you run this under pdb.
99138
the context managers capture the pdb prompts and this screws
100139
up the stdout strings with (pdb) prefixed to the line.
101140
'''
102-
import sys
103-
104141
self.install_init()
105142
self.admin=AdminTool()
106143

@@ -187,7 +224,6 @@ def testGet(self):
187224
self.assertEqual(len(err), 0)
188225

189226
def testInit(self):
190-
import sys
191227
self.admin=AdminTool()
192228
sys.argv=['main', '-i', self.dirname, 'install', 'classic', self.backend]
193229
ret = self.admin.main()
@@ -197,7 +233,6 @@ def testInit(self):
197233
self.assertTrue(os.path.isfile(self.dirname + "/schema.py"))
198234

199235
def testInitWithConfig_ini(self):
200-
import sys
201236
from roundup.configuration import CoreConfig
202237
self.admin=AdminTool()
203238
sys.argv=['main', '-i', self.dirname, 'install', 'classic', self.backend]
@@ -233,8 +268,6 @@ def testFind(self):
233268
the context managers capture the pdb prompts and this screws
234269
up the stdout strings with (pdb) prefixed to the line.
235270
'''
236-
import sys
237-
238271
self.admin=AdminTool()
239272
self.install_init()
240273

@@ -304,7 +337,7 @@ def testGenconfigUpdate(self):
304337
the context managers capture the pdb prompts and this screws
305338
up the stdout strings with (pdb) prefixed to the line.
306339
'''
307-
import sys, filecmp
340+
import filecmp
308341

309342
self.admin=AdminTool()
310343
self.install_init()
@@ -369,8 +402,6 @@ def testCliParse(self):
369402
the context managers capture the pdb prompts and this screws
370403
up the stdout strings with (pdb) prefixed to the line.
371404
'''
372-
import sys
373-
374405
self.admin=AdminTool()
375406
self.install_init()
376407

@@ -443,8 +474,6 @@ def testFilter(self):
443474
the context managers capture the pdb prompts and this screws
444475
up the stdout strings with (pdb) prefixed to the line.
445476
'''
446-
import sys
447-
448477
self.admin=AdminTool()
449478
self.install_init()
450479

@@ -664,8 +693,6 @@ def disabletestHelpInitopts(self):
664693
the context managers capture the pdb prompts and this screws
665694
up the stdout strings with (pdb) prefixed to the line.
666695
'''
667-
import sys
668-
669696
self.install_init()
670697
self.admin=AdminTool()
671698

@@ -687,8 +714,6 @@ def testSecurity(self):
687714
the context managers capture the pdb prompts and this screws
688715
up the stdout strings with (pdb) prefixed to the line.
689716
'''
690-
import sys
691-
692717
self.install_init()
693718
self.admin=AdminTool()
694719

@@ -759,8 +784,6 @@ def testSecurityInvalidAttribute(self):
759784
the context managers capture the pdb prompts and this screws
760785
up the stdout strings with (pdb) prefixed to the line.
761786
'''
762-
import sys
763-
764787
self.maxDiff = None # we want full diff
765788

766789
self.install_init()
@@ -838,8 +861,6 @@ def testSet(self):
838861
the context managers capture the pdb prompts and this screws
839862
up the stdout strings with (pdb) prefixed to the line.
840863
'''
841-
import sys
842-
843864
self.install_init()
844865
self.admin=AdminTool()
845866

@@ -945,8 +966,6 @@ def testSetOnClass(self):
945966
the context managers capture the pdb prompts and this screws
946967
up the stdout strings with (pdb) prefixed to the line.
947968
'''
948-
import sys
949-
950969
self.install_init()
951970

952971
self.admin=AdminTool()
@@ -1018,8 +1037,6 @@ def testSpecification(self):
10181037
the context managers capture the pdb prompts and this screws
10191038
up the stdout strings with (pdb) prefixed to the line.
10201039
'''
1021-
import sys
1022-
10231040
self.install_init()
10241041
self.admin=AdminTool()
10251042

@@ -1048,8 +1065,6 @@ def testRetireRestore(self):
10481065
the context managers capture the pdb prompts and this screws
10491066
up the stdout strings with (pdb) prefixed to the line.
10501067
'''
1051-
import sys
1052-
10531068
# create user1 at id 3
10541069
self.install_init()
10551070
self.admin=AdminTool()
@@ -1136,8 +1151,6 @@ def testTable(self):
11361151
the context managers capture the pdb prompts and this screws
11371152
up the stdout strings with (pdb) prefixed to the line.
11381153
'''
1139-
import sys
1140-
11411154
self.install_init()
11421155
self.admin=AdminTool()
11431156

0 commit comments

Comments
 (0)