Commit 4906e9a2 authored by David Fairbrother's avatar David Fairbrother Committed by Peterson, Peter
Browse files

Rewrite deprecator test to not use file IO

This was racey in Windows, and adds handling where a memory mapped
buffer will work just as well.
parent d1425ee4
......@@ -6,6 +6,8 @@
# SPDX - License - Identifier: GPL - 3.0 +
# package imports
from io import StringIO
from mantid.api import PythonAlgorithm
from mantid.kernel import ConfigService, logger
from mantid.utils.deprecator import deprecated_algorithm
......@@ -18,54 +20,69 @@ import tempfile
import unittest
class deprecatorTest(unittest.TestCase):
@deprecated_algorithm('MyNewAlg', '2020-12-25')
class MyOldAlg(PythonAlgorithm):
def category(self):
return 'Inelastic\\Reduction'
def PyInit(self):
self.declareProperty('Meaning', 42, 'Assign a meaning to the Universe')
def PyExec(self):
logger.notice(f'The meaning of the Universe is {self.getPropertyValue("Meaning")}')
def test_deprecated_algorithm(self):
class RedirectStdOut:
r"""redirect logging messages to a file"""
# Adapted from https://stackoverflow.com/a/45899925
@deprecated_algorithm('MyNewAlg', '2020-12-25')
class MyOldAlg(PythonAlgorithm):
CONFIG_KEY = "logging.channels.consoleChannel.class"
def category(self):
return 'Inelastic\\Reduction'
def __init__(self):
self._stdout = None
self._string_io = None
self._original_config = None
self._config = ConfigService.Instance()
def PyInit(self):
self.declareProperty('Meaning', 42, 'Assign a meaning to the Universe')
def __enter__(self):
self._original_config = self._config[self.CONFIG_KEY]
self._stdout = sys.stdout
sys.stdout = self._string_io = StringIO()
# redirect log messages to log_file
self._config[self.CONFIG_KEY] = 'PythonStdoutChannel'
return self
def PyExec(self):
logger.notice(f'The meaning of the Universe is {self.getPropertyValue("Meaning")}')
def __exit__(self, exc_type, exc_val, exc_tb):
sys.stdout = self._stdout
self._config[self.CONFIG_KEY] = self._original_config
@contextmanager
def backup_environment():
r"""redirect logging messages to a file"""
try:
# backup the logging channel and sys.stdout
config = ConfigService.Instance()
backup = dict(channel=config['logging.channels.consoleChannel.class'],
stdout=sys.stdout)
# redirect log messages to log_file
config['logging.channels.consoleChannel.class'] = 'PythonStdoutChannel'
_, log_file = tempfile.mkstemp(suffix='.log')
sys.stdout = open(log_file, 'w', buffering=1)
yield log_file
finally:
# restore the loggin channel and sys.stdout
config['logging.channels.consoleChannel.class'] = backup['channel']
sys.stdout = backup['stdout']
os.remove(log_file) # delete the temporay log file
def __str__(self):
return self._string_io.getvalue()
with backup_environment() as log_file:
class DeprecatorTest(unittest.TestCase):
@staticmethod
def _exec_alg(log_type: str) -> str:
config = ConfigService.Instance()
original_conf = config['algorithms.deprecated']
with RedirectStdOut() as log_file:
alg = MyOldAlg()
alg.initialize()
alg.setProperty("Meaning", 42)
config = ConfigService.Instance()
config['algorithms.deprecated'] = 'Raise'
config['algorithms.deprecated'] = log_type
alg.execute()
self.assertTrue('Error in execution of algorithm MyOldAlg' in open(log_file, 'r').read())
config['algorithms.deprecated'] = 'Log'
alg.execute() # use alias, log error message
self.assertTrue(' Algorithm "MyOldAlg" is deprecated' in open(log_file, 'r').read())
config['algorithms.deprecated'] = original_conf
return str(log_file)
def test_deprecated_algorithm_raise(self):
log_file = self._exec_alg("raise")
self.assertTrue('Error in execution of algorithm MyOldAlg' in log_file)
def test_deprecated_algorithm_log(self):
log_file = self._exec_alg("log")
self.assertTrue(' Algorithm "MyOldAlg" is deprecated' in log_file)
if __name__ == "__main__":
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment