From 272b61c275f42606f4ff64672fdfe0fd460b63fd Mon Sep 17 00:00:00 2001 From: "Bruno P. Kinoshita" Date: Wed, 3 Apr 2019 15:08:20 +1300 Subject: [PATCH] Use Python 2 instead --- lib/cylc/config.py | 11 +- lib/cylc/tests/test_config.py | 205 +++++++++++++++-------------- lib/cylc/tests/test_subprocpool.py | 91 +++++++------ 3 files changed, 163 insertions(+), 144 deletions(-) diff --git a/lib/cylc/config.py b/lib/cylc/config.py index 2f54682465f..c71002a1a91 100644 --- a/lib/cylc/config.py +++ b/lib/cylc/config.py @@ -2104,13 +2104,14 @@ def load_graph(self): try: if not callable(get_func(xtrig.func_name, self.fdir)): raise SuiteConfigError( - f"ERROR, " - f"xtrigger function not callable: " - f"{xtrig.func_name}") + "ERROR, " + "xtrigger function not callable: %s" % + xtrig.func_name) except (ImportError, AttributeError): raise SuiteConfigError( - f"ERROR, " - f"xtrigger function not found: {xtrig.func_name}") + "ERROR, " + "xtrigger function not found: %s" % + xtrig.func_name) self.xtrigger_mgr.add_trig(label, xtrig) self.taskdefs[task_name].xtrig_labels.add(label) diff --git a/lib/cylc/tests/test_config.py b/lib/cylc/tests/test_config.py index 0088636199c..4dd3ac9459c 100644 --- a/lib/cylc/tests/test_config.py +++ b/lib/cylc/tests/test_config.py @@ -15,115 +15,128 @@ # along with this program. If not, see . import os -import pytest -from tempfile import TemporaryDirectory -from pathlib import Path +import shutil +import unittest +from tempfile import mkdtemp + from cylc.config import SuiteConfig, SuiteConfigError -class TestSuiteConfig(object): +class TestSuiteConfig(unittest.TestCase): """Test class for the Cylc SuiteConfig object.""" def test_xfunction_imports(self): """Test for a suite configuration with valid xtriggers""" - with TemporaryDirectory() as temp_dir: - python_dir = Path(os.path.join(temp_dir, "lib", "python")) - python_dir.mkdir(parents=True) - name_a_tree_file = python_dir / "name_a_tree.py" - with name_a_tree_file.open(mode="w") as f: - # NB: we are not returning a lambda, instead we have a scalar - f.write("""name_a_tree = lambda: 'jacaranda'""") - f.flush() - suite_rc = Path(temp_dir, "suite.rc") - with suite_rc.open(mode="w") as f: - f.write(""" - [scheduling] - initial cycle point = 2018-01-01 - [[xtriggers]] - tree = name_a_tree() - [[dependencies]] - [[[R1]]] - graph = '@tree => qux' - """) - f.flush() - suite_config = SuiteConfig(suite="name_a_tree", fpath=f.name) - config = suite_config - assert 'tree' in config.xtriggers['qux'] + temp_dir = mkdtemp() + python_dir = os.path.join(temp_dir, "lib", "python") + if not os.path.exists(python_dir): + os.makedirs(python_dir) + name_a_tree_file = os.path.join(python_dir, "name_a_tree.py") + with open(name_a_tree_file, mode="w") as f: + # NB: we are not returning a lambda, instead we have a scalar + f.write("""name_a_tree = lambda: 'jacaranda'""") + f.flush() + suite_rc = os.path.join(temp_dir, "suite.rc") + with open(suite_rc, mode="w") as f: + f.write(""" +[scheduling] + initial cycle point = 2018-01-01 + [[xtriggers]] + tree = name_a_tree() + [[dependencies]] + [[[R1]]] + graph = '@tree => qux' + """) + f.flush() + suite_config = SuiteConfig(suite="name_a_tree", fpath=f.name) + config = suite_config + self.assertTrue('tree' in config.xtriggers['qux']) + shutil.rmtree(temp_dir) def test_xfunction_import_error(self): """Test for error when a xtrigger function cannot be imported.""" - with TemporaryDirectory() as temp_dir: - python_dir = Path(os.path.join(temp_dir, "lib", "python")) - python_dir.mkdir(parents=True) - caiman_file = python_dir / "caiman.py" - with caiman_file.open(mode="w") as f: - # NB: we are not returning a lambda, instead we have a scalar - f.write("""caiman = lambda: True""") - f.flush() - suite_rc = Path(temp_dir, "suite.rc") - with suite_rc.open(mode="w") as f: - f.write(""" - [scheduling] - initial cycle point = 2018-01-01 - [[xtriggers]] - oopsie = piranha() - [[dependencies]] - [[[R1]]] - graph = '@oopsie => qux' - """) - f.flush() - with pytest.raises(SuiteConfigError) as excinfo: - SuiteConfig(suite="caiman_suite", fpath=f.name) - assert "not found" in str(excinfo.value) + temp_dir = mkdtemp() + python_dir = os.path.join(temp_dir, "lib", "python") + if not os.path.exists(python_dir): + os.makedirs(python_dir) + caiman_file = os.path.join(python_dir, "caiman.py") + with open(caiman_file, mode="w") as f: + # NB: we are not returning a lambda, instead we have a scalar + f.write("""caiman = lambda: True""") + f.flush() + suite_rc = os.path.join(temp_dir, "suite.rc") + with open(suite_rc, mode="w") as f: + f.write(""" +[scheduling] + initial cycle point = 2018-01-01 + [[xtriggers]] + oopsie = piranha() + [[dependencies]] + [[[R1]]] + graph = '@oopsie => qux' + """) + f.flush() + with self.assertRaises(SuiteConfigError) as ex: + SuiteConfig(suite="caiman_suite", fpath=f.name) + self.assertTrue("not found" in str(ex)) + shutil.rmtree(temp_dir) def test_xfunction_attribute_error(self): """Test for error when a xtrigger function cannot be imported.""" - with TemporaryDirectory() as temp_dir: - python_dir = Path(os.path.join(temp_dir, "lib", "python")) - python_dir.mkdir(parents=True) - capybara_file = python_dir / "capybara.py" - with capybara_file.open(mode="w") as f: - # NB: we are not returning a lambda, instead we have a scalar - f.write("""toucan = lambda: True""") - f.flush() - suite_rc = Path(temp_dir, "suite.rc") - with suite_rc.open(mode="w") as f: - f.write(""" - [scheduling] - initial cycle point = 2018-01-01 - [[xtriggers]] - oopsie = capybara() - [[dependencies]] - [[[R1]]] - graph = '@oopsie => qux' - """) - f.flush() - with pytest.raises(SuiteConfigError) as excinfo: - SuiteConfig(suite="capybara_suite", fpath=f.name) - assert "not found" in str(excinfo.value) + temp_dir = mkdtemp() + python_dir = os.path.join(temp_dir, "lib", "python") + if not os.path.exists(python_dir): + os.makedirs(python_dir) + capybara_file = os.path.join(python_dir, "capybara.py") + with open(capybara_file, mode="w") as f: + # NB: we are not returning a lambda, instead we have a scalar + f.write("""toucan = lambda: True""") + f.flush() + suite_rc = os.path.join(temp_dir, "suite.rc") + with open(suite_rc, mode="w") as f: + f.write(""" +[scheduling] + initial cycle point = 2018-01-01 + [[xtriggers]] + oopsie = capybara() + [[dependencies]] + [[[R1]]] + graph = '@oopsie => qux' + """) + f.flush() + with self.assertRaises(SuiteConfigError) as ex: + SuiteConfig(suite="capybara_suite", fpath=f.name) + self.assertTrue("not found" in str(ex)) + shutil.rmtree(temp_dir) def test_xfunction_not_callable(self): """Test for error when a xtrigger function is not callable.""" - with TemporaryDirectory() as temp_dir: - python_dir = Path(os.path.join(temp_dir, "lib", "python")) - python_dir.mkdir(parents=True) - not_callable_file = python_dir / "not_callable.py" - with not_callable_file.open(mode="w") as f: - # NB: we are not returning a lambda, instead we have a scalar - f.write("""not_callable = 42""") - f.flush() - suite_rc = Path(temp_dir, "suite.rc") - with suite_rc.open(mode="w") as f: - f.write(""" - [scheduling] - initial cycle point = 2018-01-01 - [[xtriggers]] - oopsie = not_callable() - [[dependencies]] - [[[R1]]] - graph = '@oopsie => qux' - """) - f.flush() - with pytest.raises(SuiteConfigError) as excinfo: - SuiteConfig(suite="suite_with_not_callable", fpath=f.name) - assert "callable" in str(excinfo.value) + temp_dir = mkdtemp() + python_dir = os.path.join(temp_dir, "lib", "python") + if not os.path.exists(python_dir): + os.makedirs(python_dir) + not_callable_file = os.path.join(python_dir, "not_callable.py") + with open(not_callable_file, mode="w") as f: + # NB: we are not returning a lambda, instead we have a scalar + f.write("""not_callable = 42""") + f.flush() + suite_rc = os.path.join(temp_dir, "suite.rc") + with open(suite_rc, mode="w") as f: + f.write(""" +[scheduling] + initial cycle point = 2018-01-01 + [[xtriggers]] + oopsie = not_callable() + [[dependencies]] + [[[R1]]] + graph = '@oopsie => qux' + """) + f.flush() + with self.assertRaises(SuiteConfigError) as ex: + SuiteConfig(suite="suite_with_not_callable", fpath=f.name) + self.assertTrue("callable" in str(ex)) + shutil.rmtree(temp_dir) + + +if __name__ == '__main__': + unittest.main() diff --git a/lib/cylc/tests/test_subprocpool.py b/lib/cylc/tests/test_subprocpool.py index e5f4d96ad21..cbfd8aa8903 100644 --- a/lib/cylc/tests/test_subprocpool.py +++ b/lib/cylc/tests/test_subprocpool.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python2 # THIS FILE IS PART OF THE CYLC SUITE ENGINE. # Copyright (C) 2008-2019 NIWA & British Crown (Met Office) & Contributors. @@ -17,10 +17,11 @@ # along with this program. If not, see . from tempfile import NamedTemporaryFile, SpooledTemporaryFile, TemporaryFile,\ - TemporaryDirectory + mkdtemp import unittest -from pathlib import Path +import os +import shutil from cylc.subprocctx import SubProcContext from cylc.subprocpool import SubProcPool, _XTRIG_FUNCS, get_func @@ -133,37 +134,39 @@ def test_run_command_with_stdin_from_paths(self): def test_xfunction(self): """Test xtrigger function import.""" - with TemporaryDirectory() as temp_dir: - python_dir = Path(temp_dir, "lib", "python") - python_dir.mkdir(parents=True) - the_answer_file = python_dir / "the_answer.py" - with the_answer_file.open(mode="w") as f: - f.write("""the_answer = lambda: 42""") - f.flush() - fn = get_func("the_answer", temp_dir) - result = fn() - self.assertEqual(42, result) + temp_dir = mkdtemp() + python_dir = os.path.join(temp_dir, "lib", "python") + os.makedirs(python_dir) + the_answer_file = os.path.join(python_dir, "the_answer.py") + with open(the_answer_file, mode="w") as f: + f.write("""the_answer = lambda: 42""") + f.flush() + fn = get_func("the_answer", temp_dir) + result = fn() + self.assertEqual(42, result) + shutil.rmtree(temp_dir) def test_xfunction_cache(self): """Test xtrigger function import cache.""" - with TemporaryDirectory() as temp_dir: - python_dir = Path(temp_dir, "lib", "python") - python_dir.mkdir(parents=True) - amandita_file = python_dir / "amandita.py" - with amandita_file.open(mode="w") as f: - f.write("""amandita = lambda: 'chocolate'""") - f.flush() - fn = get_func("amandita", temp_dir) - result = fn() - self.assertEqual('chocolate', result) - - # is in the cache - self.assertTrue('amandita' in _XTRIG_FUNCS) - # returned from cache - self.assertEqual(fn, get_func("amandita", temp_dir)) - del _XTRIG_FUNCS['amandita'] - # is not in the cache - self.assertFalse('amandita' in _XTRIG_FUNCS) + temp_dir = mkdtemp() + python_dir = os.path.join(temp_dir, "lib", "python") + os.makedirs(python_dir) + amandita_file = os.path.join(python_dir, "amandita.py") + with open(amandita_file, mode="w") as f: + f.write("""amandita = lambda: 'chocolate'""") + f.flush() + fn = get_func("amandita", temp_dir) + result = fn() + self.assertEqual('chocolate', result) + + # is in the cache + self.assertTrue('amandita' in _XTRIG_FUNCS) + # returned from cache + self.assertEqual(fn, get_func("amandita", temp_dir)) + del _XTRIG_FUNCS['amandita'] + # is not in the cache + self.assertFalse('amandita' in _XTRIG_FUNCS) + shutil.rmtree(temp_dir) def test_xfunction_import_error(self): """Test for error on importing a xtrigger function. @@ -172,21 +175,23 @@ def test_xfunction_import_error(self): and successfully imported, we use an invalid module name as per Python spec. """ - with TemporaryDirectory() as temp_dir: - with self.assertRaises(ModuleNotFoundError): - get_func("invalid-module-name", temp_dir) + temp_dir = mkdtemp() + with self.assertRaises(ImportError): + get_func("invalid-module-name", temp_dir) + shutil.rmtree(temp_dir) def test_xfunction_attribute_error(self): """Test for error on looking for an attribute in a xtrigger script.""" - with TemporaryDirectory() as temp_dir: - python_dir = Path(temp_dir, "lib", "python") - python_dir.mkdir(parents=True) - the_answer_file = python_dir / "the_sword.py" - with the_answer_file.open(mode="w") as f: - f.write("""the_droid = lambda: 'excalibur'""") - f.flush() - with self.assertRaises(AttributeError): - get_func("the_sword", temp_dir) + temp_dir = mkdtemp() + python_dir = os.path.join(temp_dir, "lib", "python") + os.makedirs(python_dir) + the_answer_file = os.path.join(python_dir, "the_sword.py") + with open(the_answer_file, mode="w") as f: + f.write("""the_droid = lambda: 'excalibur'""") + f.flush() + with self.assertRaises(AttributeError): + get_func("the_sword", temp_dir) + shutil.rmtree(temp_dir) if __name__ == '__main__':