Fix .testr.conf detection: test path follows discover

Apparently if -t is not specified, the path to the unit test directory
follows immediately the "discover" keyword.
This fixes the discovery for the .testr.conf in the sahara repository
(even if is being replaced by .stestr.conf, the fix may still be
useful).

Also, split the code which discover the legacy values in its own function
to simplify the testing.

Finally, disable hacking rule H405 which kept flagging incorrectly a long
multiline string as docstring.

Change-Id: Ide155a8e6b2b746c81388bacc0822c68d853b5a1
This commit is contained in:
Luigi Toscano 2017-09-27 17:17:45 +02:00
parent 7dd678e372
commit b6ac6ac48e
3 changed files with 90 additions and 21 deletions

View File

@ -106,6 +106,35 @@ def get_parser(args):
return parser.parse_known_args(args)
def _parse_testrconf():
# Parse the legacy .testr.conf file.
test_dir = None
top_dir = None
group_regex = None
with open('.testr.conf', 'r') as testr_conf_file:
config = six.moves.configparser.ConfigParser()
config.readfp(testr_conf_file)
test_command = config.get('DEFAULT', 'test_command')
group_regex = None
if config.has_option('DEFAULT', 'group_regex'):
group_regex = config.get('DEFAULT', 'group_regex')
for line in test_command.split('\n'):
if 'subunit.run discover' in line:
command_parts = line.split(' ')
top_dir_present = '-t' in line
for idx, val in enumerate(command_parts):
if top_dir_present:
if val == '-t':
top_dir = command_parts[idx + 1]
test_dir = command_parts[idx + 2]
else:
if val == 'discover':
test_dir = command_parts[idx + 1]
return (test_dir, top_dir, group_regex)
def call_testr(regex, subunit, pretty, list_tests, slowest, parallel, concur,
until_failure, color, others=None, blacklist_file=None,
whitelist_file=None, black_regex=None, load_list=None):
@ -119,26 +148,7 @@ def call_testr(regex, subunit, pretty, list_tests, slowest, parallel, concur,
'in the stestr git repository.')
warnings.warn(msg)
with open('.testr.conf', 'r') as testr_conf_file:
config = six.moves.configparser.ConfigParser()
config.readfp(testr_conf_file)
test_command = config.get('DEFAULT', 'test_command')
group_regex = None
if config.has_option('DEFAULT', 'group_regex'):
group_regex = config.get('DEFAULT', 'group_regex')
for line in test_command.split('\n'):
if 'subunit.run discover' in line:
command_parts = line.split(' ')
top_dir_present = '-t' in line
for idx, val in enumerate(command_parts):
if top_dir_present:
if val == '-t':
top_dir = command_parts[idx + 1]
test_dir = command_parts[idx + 2]
else:
if val == 'discover':
test_dir = command_parts[idx + 2]
test_dir, top_dir, group_regex = _parse_testrconf()
elif not os.path.isfile(
'.testr.conf') and not os.path.isfile('.stestr.conf'):
msg = ('No .stestr.conf found, please create one.')

View File

@ -185,3 +185,61 @@ class TestCallers(base.TestCase):
self.assertTrue(mock_run.called, 'testtools.run was never called')
count = mock_run.call_count
self.assertEqual(1, count, 'testtools.run called more than once')
def test_parse_legacy_testrconf_discover(self):
'''Test _parse_testrconf
Test ostestr _parse_testrconf function when:
-t is not specified and discover is specified
'''
testrconf_data = u"""
[DEFAULT]
test_command=OS_STDOUT_CAPTURE=${OS_STDOUT_CAPTURE:-1} \
OS_STDERR_CAPTURE=${OS_STDERR_CAPTURE:-1} \
OS_TEST_TIMEOUT=${OS_TEST_TIMEOUT:-60} \
${PYTHON:-python} -m subunit.run discover mytestdir \
$LISTOPT $IDOPTION
test_id_option=--load-list $IDFILE
test_list_option=--list
group_regex=([^\.]+\.)+
"""
with io.StringIO() as testrconf_data_file:
testrconf_data_file.write(testrconf_data)
testrconf_data_file.seek(0)
with mock.patch('six.moves.builtins.open',
return_value=testrconf_data_file, autospec=True):
parsed_values = os_testr._parse_testrconf()
# validate the discovery of the options from the legacy
# .testr.conf
self.assertEqual(parsed_values, ('mytestdir', None,
'([^\.]+\.)+'))
def test_parse_legacy_testrconf_topdir(self):
'''Test parse_testrconf
Test ostestr _parse_testrconf function when:
-t is specified
'''
testrconf_data = u"""
[DEFAULT]
test_command=OS_STDOUT_CAPTURE=${OS_STDOUT_CAPTURE:-1} \
OS_STDERR_CAPTURE=${OS_STDERR_CAPTURE:-1} \
OS_TEST_TIMEOUT=${OS_TEST_TIMEOUT:-60} \
${PYTHON:-python} -m subunit.run discover -t .. mytestdir \
$LISTOPT $IDOPTION
test_id_option=--load-list $IDFILE
test_list_option=--list
group_regex=([^\.]+\.)+
"""
with io.StringIO() as testrconf_data_file:
testrconf_data_file.write(testrconf_data)
testrconf_data_file.seek(0)
with mock.patch('six.moves.builtins.open',
return_value=testrconf_data_file, autospec=True):
parsed_values = os_testr._parse_testrconf()
# validate the discovery of the options from the legacy
# .testr.conf
self.assertEqual(parsed_values, ('mytestdir', '..',
'([^\.]+\.)+'))

View File

@ -44,8 +44,9 @@ commands = oslo_debug_helper {posargs}
[flake8]
# E123, E125 skipped as they are invalid PEP-8.
# H405 skipped as it wrongly recognizes a multiline string as docstring
show-source = True
ignore = E123,E125
ignore = E123,E125,H405
builtins = _
exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,build