Skip to content

Commit

Permalink
Tidy
Browse files Browse the repository at this point in the history
Including converting unittest.TestCase class to standard pytest 
functions in test_suite_files.py
  • Loading branch information
MetRonnie committed Oct 5, 2020
1 parent 901289a commit f236fc5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 75 deletions.
23 changes: 9 additions & 14 deletions cylc/flow/suite_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,7 @@ def get_contact_file(reg):
def get_flow_file(reg, suite_owner=None):
"""Return the path of a suite's flow.cylc file."""
return os.path.join(
get_suite_source_dir(reg, suite_owner),
SuiteFiles.FLOW_FILE)
get_suite_source_dir(reg, suite_owner), SuiteFiles.FLOW_FILE)


def get_suite_source_dir(reg, suite_owner=None):
Expand Down Expand Up @@ -488,13 +487,11 @@ def register(reg=None, source=None, redirect=False, rundir=None):

is_valid, message = SuiteNameValidator.validate(reg)
if not is_valid:
raise SuiteServiceFileError(
f'invalid suite name - {message}'
)
raise SuiteServiceFileError(f'invalid suite name - {message}')

if os.path.isabs(reg):
raise SuiteServiceFileError(
"suite name cannot be an absolute path: %s" % reg)
f'suite name cannot be an absolute path: {reg}')

check_nested_run_dirs(reg)

Expand Down Expand Up @@ -556,14 +553,12 @@ def register(reg=None, source=None, redirect=False, rundir=None):
if orig_source is not None and source != orig_source:
if not redirect:
raise SuiteServiceFileError(
"the name '%s' already points to %s.\nUse "
"--redirect to re-use an existing name and run "
"directory." % (reg, orig_source))
f"the name '{reg}' already points to {orig_source}.\nUse "
"--redirect to re-use an existing name and run directory.")
LOG.warning(
"the name '%(reg)s' points to %(old)s.\nIt will now"
" be redirected to %(new)s.\nFiles in the existing %(reg)s run"
" directory will be overwritten.\n",
{'reg': reg, 'old': orig_source, 'new': source})
f"the name '{reg}' points to {orig_source}.\nIt will now be "
f"redirected to {source}.\nFiles in the existing {reg} run "
"directory will be overwritten.\n")
# Remove symlink to the original suite.
os.unlink(os.path.join(srv_d, SuiteFiles.Service.SOURCE))

Expand All @@ -579,7 +574,7 @@ def register(reg=None, source=None, redirect=False, rundir=None):
source_str = source
os.symlink(source_str, target)

print('REGISTERED %s -> %s' % (reg, source))
print(f'REGISTERED {reg} -> {source}')
return reg


Expand Down
108 changes: 47 additions & 61 deletions tests/unit/test_suite_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import pytest
import unittest

from unittest import mock

import os.path
Expand Down Expand Up @@ -182,69 +180,57 @@ def get_register_test_cases():
]


class TestSuiteFiles(unittest.TestCase):

@mock.patch('os.unlink')
@mock.patch('os.makedirs')
@mock.patch('os.symlink')
@mock.patch('os.readlink')
@mock.patch('os.path.isfile')
@mock.patch('os.path.isabs')
@mock.patch('os.getcwd')
@mock.patch('os.path.abspath')
@mock.patch('cylc.flow.suite_files.get_suite_srv_dir')
def test_register(
self,
mocked_get_suite_srv_dir,
mocked_abspath,
mocked_getcwd,
mocked_isabs,
mocked_isfile,
mocked_readlink,
mocked_symlink,
mocked_makedirs,
mocked_unlink
):
"""Test the register function."""
def mkdirs_standin(_, exist_ok=False):
return True

mocked_abspath.side_effect = lambda x: x

for reg, source, redirect, cwd, isabs, isfile, \
suite_srv_dir, readlink, expected_symlink, \
expected, e_expected, e_message \
in get_register_test_cases():
mocked_getcwd.side_effect = lambda: cwd
mocked_isabs.side_effect = lambda x: isabs
@mock.patch('os.unlink')
@mock.patch('os.makedirs')
@mock.patch('os.symlink')
@mock.patch('os.readlink')
@mock.patch('os.path.isfile')
@mock.patch('os.path.isabs')
@mock.patch('os.getcwd')
@mock.patch('os.path.abspath')
@mock.patch('cylc.flow.suite_files.get_suite_srv_dir')
def test_register(mocked_get_suite_srv_dir,
mocked_abspath,
mocked_getcwd,
mocked_isabs,
mocked_isfile,
mocked_readlink,
mocked_symlink,
mocked_makedirs,
mocked_unlink):
"""Test the register function."""
def mkdirs_standin(_, exist_ok=False):
return True

mocked_isfile.side_effect = lambda x: isfile
mocked_get_suite_srv_dir.return_value = str(suite_srv_dir)
mocked_makedirs.return_value = True
mocked_unlink.return_value = True
if readlink == OSError:
mocked_readlink.side_effect = readlink
else:
mocked_readlink.side_effect = lambda x: readlink
mocked_abspath.side_effect = lambda x: x

if e_expected is None:
reg = suite_files.register(reg, source, redirect)
self.assertEqual(expected, reg)
if mocked_symlink.call_count > 0:
# first argument, of the first call
arg0 = mocked_symlink.call_args[0][0]
self.assertEqual(expected_symlink, arg0)
else:
with self.assertRaises(e_expected) as cm:
suite_files.register(reg, source, redirect)
if e_message is not None:
the_exception = cm.exception
self.assertTrue(e_message in str(the_exception),
str(the_exception))
for (reg, source, redirect, cwd, isabs, isfile, suite_srv_dir,
readlink, expected_symlink, expected, e_expected,
e_message) in get_register_test_cases():
mocked_getcwd.side_effect = lambda: cwd
mocked_isabs.side_effect = lambda x: isabs

mocked_isfile.side_effect = lambda x: isfile
mocked_get_suite_srv_dir.return_value = str(suite_srv_dir)
mocked_makedirs.return_value = True
mocked_unlink.return_value = True
if readlink == OSError:
mocked_readlink.side_effect = readlink
else:
mocked_readlink.side_effect = lambda x: readlink

if __name__ == '__main__':
unittest.main()
if e_expected is None:
reg = suite_files.register(reg, source, redirect)
assert reg == expected
if mocked_symlink.call_count > 0:
# first argument, of the first call
arg0 = mocked_symlink.call_args[0][0]
assert arg0 == expected_symlink
else:
with pytest.raises(e_expected) as exc:
suite_files.register(reg, source, redirect)
if e_message is not None:
assert e_message in str(exc.value)


@pytest.mark.parametrize('abs_path', [False, True])
Expand Down

0 comments on commit f236fc5

Please sign in to comment.