Skip to content

Commit 40f921d

Browse files
committed
Some patches from wking to make admin.py bypass checks like nuking a
db. This is or use for testing only. Also some internal restructuring to use use new utility function _get_choice. Add a new test for admin install interface.
1 parent ed7bbde commit 40f921d

File tree

3 files changed

+69
-21
lines changed

3 files changed

+69
-21
lines changed

CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ Features:
140140
submitting a new keyword. Usually after submission, you will see the
141141
the page for the new keyword to allow you to change the name of the
142142
keyword. (John Rouillard)
143+
- issue2550757 - internal restructuring to allow admin.py to be tested
144+
more easily. W. Trevor King (wking)/ John Rouillard.
143145

144146
Fixed:
145147

roundup/admin.py

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def __init__(self):
7272
self.tracker_home = ''
7373
self.db = None
7474
self.db_uncommitted = False
75+
self.force = None
7576

7677
def get_class(self, classname):
7778
"""Get the class - raise an exception if it doesn't exist.
@@ -378,36 +379,35 @@ def do_install(self, tracker_home, args):
378379
# check for both old- and new-style configs
379380
if list(filter(os.path.exists, [config_ini_file,
380381
os.path.join(tracker_home, 'config.py')])):
381-
ok = raw_input(_(
382+
if not self.force:
383+
ok = raw_input(_(
382384
"""WARNING: There appears to be a tracker in "%(tracker_home)s"!
383385
If you re-install it, you will lose all the data!
384386
Erase it? Y/N: """) % locals())
385-
if ok.strip().lower() != 'y':
386-
return 0
387+
if ok.strip().lower() != 'y':
388+
return 0
387389

388390
# clear it out so the install isn't confused
389391
shutil.rmtree(tracker_home)
390392

391393
# select template
392394
templates = self.listTemplates()
393-
template = len(args) > 1 and args[1] or ''
394-
if template not in templates:
395-
print _('Templates:'), ', '.join(templates)
396-
while template not in templates:
397-
template = raw_input(_('Select template [classic]: ')).strip()
398-
if not template:
399-
template = 'classic'
395+
template = self._get_choice(
396+
list_name=_('Templates:'),
397+
prompt=_('Select template'),
398+
options=templates,
399+
argument=len(args) > 1 and args[1] or '',
400+
default='classic')
400401

401402
# select hyperdb backend
402403
import roundup.backends
403404
backends = roundup.backends.list_backends()
404-
backend = len(args) > 2 and args[2] or ''
405-
if backend not in backends:
406-
print _('Back ends:'), ', '.join(backends)
407-
while backend not in backends:
408-
backend = raw_input(_('Select backend [anydbm]: ')).strip()
409-
if not backend:
410-
backend = 'anydbm'
405+
backend = self._get_choice(
406+
list_name=_('Back ends:'),
407+
prompt=_('Select backend'),
408+
options=backends,
409+
argument=len(args) > 2 and args[2] or '',
410+
default='anydbm')
411411
# XXX perform a unit test based on the user's selections
412412

413413
# Process configuration file definitions
@@ -456,6 +456,20 @@ def do_install(self, tracker_home, args):
456456
}
457457
return 0
458458

459+
def _get_choice(self, list_name, prompt, options, argument, default=None):
460+
if default is None:
461+
default = options[0] # just pick the first one
462+
if argument in options:
463+
return argument
464+
if self.force:
465+
return default
466+
sys.stdout.write('%s: %s\n' % (list_name, ', '.join(options)))
467+
while argument not in options:
468+
argument = raw_input('%s [%s]: ' % (prompt, default))
469+
if not argument:
470+
return default
471+
return argument
472+
459473
def do_genconfig(self, args):
460474
''"""Usage: genconfig <filename>
461475
Generate a new tracker config file (ini style) with default values
@@ -494,12 +508,13 @@ def do_initialise(self, tracker_home, args):
494508

495509
# is there already a database?
496510
if tracker.exists():
497-
ok = raw_input(_(
511+
if not self.force:
512+
ok = raw_input(_(
498513
"""WARNING: The database is already initialised!
499514
If you re-initialise it, you will lose all the data!
500515
Erase it? Y/N: """))
501-
if ok.strip().lower() != 'y':
502-
return 0
516+
if ok.strip().lower() != 'y':
517+
return 0
503518

504519
# nuke it
505520
tracker.nuke()
@@ -1449,7 +1464,10 @@ def run_command(self, args):
14491464

14501465
# make sure we have a tracker_home
14511466
while not self.tracker_home:
1452-
self.tracker_home = raw_input(_('Enter tracker home: ')).strip()
1467+
if not self.force:
1468+
self.tracker_home = raw_input(_('Enter tracker home: ')).strip()
1469+
else:
1470+
self.tracker_home = os.curdir
14531471

14541472
# before we open the db, we may be doing an install or init
14551473
if command == 'initialise':

test/db_test_base.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2174,6 +2174,34 @@ def stdoutwrite(s):
21742174
finally:
21752175
roundup.admin.sys = sys
21762176

2177+
2178+
# test duplicate relative tracker home initialisation (issue2550757)
2179+
def testAdminDuplicateInitialisation(self):
2180+
import roundup.admin
2181+
output = []
2182+
def stderrwrite(s):
2183+
output.append(s)
2184+
roundup.admin.sys = MockNull ()
2185+
t = '_test_initialise'
2186+
try:
2187+
roundup.admin.sys.stderr.write = stderrwrite
2188+
tool = roundup.admin.AdminTool()
2189+
tool.force = True
2190+
args = (None, 'classic', 'anydbm',
2191+
'MAIL_DOMAIN=%s' % config.MAIL_DOMAIN)
2192+
tool.do_install(t, args=args)
2193+
args = (None, 'mypasswd')
2194+
tool.do_initialise(t, args=args)
2195+
tool.do_initialise(t, args=args)
2196+
try: # python >=2.7
2197+
self.assertNotIn(t, os.listdir(t))
2198+
except AttributeError:
2199+
self.assertFalse('db' in os.listdir(t))
2200+
finally:
2201+
roundup.admin.sys = sys
2202+
if os.path.exists(t):
2203+
shutil.rmtree(t)
2204+
21772205
def testAddProperty(self):
21782206
self.db.issue.create(title="spam", status='1')
21792207
self.db.commit()

0 commit comments

Comments
 (0)