From 5aa38493b096d1902500e16a30179decaeadde75 Mon Sep 17 00:00:00 2001 From: Jackson Burns Date: Fri, 20 Jan 2023 16:06:28 -0500 Subject: [PATCH 1/4] add helpful warning message for failed julia dependency imports --- rmgpy/rmg/reactors.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/rmgpy/rmg/reactors.py b/rmgpy/rmg/reactors.py index bf68e3dd67..c2af903204 100644 --- a/rmgpy/rmg/reactors.py +++ b/rmgpy/rmg/reactors.py @@ -39,8 +39,13 @@ from pyrms import rms from diffeqpy import de from julia import Main -except: - pass +except ImportError as e: + if 'nose' not in sys.modules.keys(): + warnings.warn(""" + Import of Julia dependencies failed. Ensure the environment is correctly built and Julia dependencies have been linked properly. + + Original Exception: + """ + str(e),RuntimeWarning) from rmgpy.species import Species from rmgpy.reaction import Reaction From 11279b48a2f81e812bc6ffa97d4022050bd2a260 Mon Sep 17 00:00:00 2001 From: Jackson Burns <33505528+JacksonBurns@users.noreply.github.com> Date: Sat, 21 Jan 2023 19:33:52 -0500 Subject: [PATCH 2/4] leave try/except bare for nosetests --- rmgpy/rmg/reactors.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/rmgpy/rmg/reactors.py b/rmgpy/rmg/reactors.py index c2af903204..21ea94f9aa 100644 --- a/rmgpy/rmg/reactors.py +++ b/rmgpy/rmg/reactors.py @@ -39,13 +39,11 @@ from pyrms import rms from diffeqpy import de from julia import Main -except ImportError as e: +except: if 'nose' not in sys.modules.keys(): warnings.warn(""" Import of Julia dependencies failed. Ensure the environment is correctly built and Julia dependencies have been linked properly. - - Original Exception: - """ + str(e),RuntimeWarning) + """,RuntimeWarning) from rmgpy.species import Species from rmgpy.reaction import Reaction From 1fcca426613490c0c55c68c0fddd46c41f31a24f Mon Sep 17 00:00:00 2001 From: Jackson Burns Date: Mon, 30 Jan 2023 13:39:30 -0500 Subject: [PATCH 3/4] change the warning to an error since this issue will only cause an exception to be raised later on during execution, it makes more sense to raise a more helpful exception here --- rmgpy/rmg/reactors.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/rmgpy/rmg/reactors.py b/rmgpy/rmg/reactors.py index 21ea94f9aa..a0c65f732d 100644 --- a/rmgpy/rmg/reactors.py +++ b/rmgpy/rmg/reactors.py @@ -41,9 +41,7 @@ from julia import Main except: if 'nose' not in sys.modules.keys(): - warnings.warn(""" - Import of Julia dependencies failed. Ensure the environment is correctly built and Julia dependencies have been linked properly. - """,RuntimeWarning) + raise RuntimeError("Import of Julia dependencies failed. Ensure the environment is correctly built and Julia dependencies have been linked properly.") from rmgpy.species import Species from rmgpy.reaction import Reaction From 3283cb49306cfe6b408d50feac03207cfc9f258f Mon Sep 17 00:00:00 2001 From: Jackson Burns Date: Mon, 30 Jan 2023 13:40:30 -0500 Subject: [PATCH 4/4] add reactorsTest.py this is a barebones file that currently only tests the new import logic. more importantly, it demonstrates that we may be able to add more test coverage with clever use of patching and magicmock --- rmgpy/rmg/reactorsTest.py | 56 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 rmgpy/rmg/reactorsTest.py diff --git a/rmgpy/rmg/reactorsTest.py b/rmgpy/rmg/reactorsTest.py new file mode 100644 index 0000000000..43cc91960f --- /dev/null +++ b/rmgpy/rmg/reactorsTest.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 + +############################################################################### +# # +# RMG - Reaction Mechanism Generator # +# # +# Copyright (c) 2002-2021 Prof. William H. Green (whgreen@mit.edu), # +# Prof. Richard H. West (r.west@neu.edu) and the RMG Team (rmg_dev@mit.edu) # +# # +# Permission is hereby granted, free of charge, to any person obtaining a # +# copy of this software and associated documentation files (the 'Software'), # +# to deal in the Software without restriction, including without limitation # +# the rights to use, copy, modify, merge, publish, distribute, sublicense, # +# and/or sell copies of the Software, and to permit persons to whom the # +# Software is furnished to do so, subject to the following conditions: # +# # +# The above copyright notice and this permission notice shall be included in # +# all copies or substantial portions of the Software. # +# # +# THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING # +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # +# DEALINGS IN THE SOFTWARE. # +# # +############################################################################### + +import unittest +import sys + +from unittest.mock import MagicMock, patch + +################################################### + + +class TestReactors(unittest.TestCase): + + def test_missing_julia_warning(self): + """ + Ensure that a missing Julia install will raise an error to the user. + """ + # make a MagicMock that replaces pyrms and makes it look like it is missing + rmsModuleMock = MagicMock() + rmsModuleMock.side_effect = ModuleNotFoundError + with patch.dict('sys.modules', pyrms=rmsModuleMock): + # RuntimeError is not raised when running test - trick it into thinking we are not in a test + del sys.modules['nose'] + # use a second context to check for the error on import + with self.assertRaises(RuntimeError): + import rmgpy.rmg.reactors + + +if __name__ == '__main__': + unittest.main()