From c6976901f49f4d85bcced518928683c1d3c4cfed Mon Sep 17 00:00:00 2001 From: Alexander Grund Date: Tue, 1 Dec 2020 13:45:35 +0100 Subject: [PATCH] Remove duplicated branch and use read_file and contextmanager for logToFile --- easybuild/tools/filetools.py | 10 ++-------- test/framework/filetools.py | 17 ++++++----------- test/framework/utilities.py | 11 +++++++++++ 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/easybuild/tools/filetools.py b/easybuild/tools/filetools.py index 88185b5350..4c95b7a9ce 100644 --- a/easybuild/tools/filetools.py +++ b/easybuild/tools/filetools.py @@ -1475,7 +1475,7 @@ def apply_regex_substitutions(paths, regex_subs, backup='.orig.eb', on_missing_m :param backup: create backup of original file with specified suffix (no backup if value evaluates to False) :param on_missing_match: Define what to do when no match was found in the file. Can be 'error' to raise an error, 'warn' to print a warning or 'ignore' to do nothing - Defaults to value of --strict + Defaults to the value of --strict """ if on_missing_match is None: on_missing_match = build_option('strict') @@ -1540,13 +1540,7 @@ def apply_regex_substitutions(paths, regex_subs, backup='.orig.eb', on_missing_m elif on_missing_match == run.WARN: _log.warning(msg) else: - msg = 'Nothing found to replace in %s' % path - if on_missing_match == run.ERROR: - raise EasyBuildError(msg) - elif on_missing_match == run.WARN: - print_warning(msg) - else: - _log.info(msg) + _log.info(msg) except (IOError, OSError) as err: raise EasyBuildError("Failed to patch %s: %s", path, err) diff --git a/test/framework/filetools.py b/test/framework/filetools.py index a6f532016f..a45a286e6e 100644 --- a/test/framework/filetools.py +++ b/test/framework/filetools.py @@ -48,7 +48,6 @@ from easybuild.tools.config import IGNORE, ERROR from easybuild.tools.multidiff import multidiff from easybuild.tools.py2vs3 import std_urllib -from easybuild.base import fancylogger class FileToolsTest(EnhancedTestCase): @@ -1266,22 +1265,18 @@ def test_apply_regex_substitutions(self): self.assertErrorRegex(EasyBuildError, error_pat, ft.apply_regex_substitutions, testfile, regex_subs_no_match, on_missing_match=run.ERROR) - fancylogger.logToFile(self.logfile) - # Warn - ft.apply_regex_substitutions(testfile, regex_subs_no_match, on_missing_match=run.WARN) - with open(self.logfile, 'r') as f: - logtxt = f.read() + with self.log_to_testlogfile(): + ft.apply_regex_substitutions(testfile, regex_subs_no_match, on_missing_match=run.WARN) + logtxt = ft.read_file(self.logfile) self.assertTrue('WARNING ' + error_pat in logtxt) # Ignore - ft.apply_regex_substitutions(testfile, regex_subs_no_match, on_missing_match=run.IGNORE) - with open(self.logfile, 'r') as f: - logtxt = f.read() + with self.log_to_testlogfile(): + ft.apply_regex_substitutions(testfile, regex_subs_no_match, on_missing_match=run.IGNORE) + logtxt = ft.read_file(self.logfile) self.assertTrue('INFO ' + error_pat in logtxt) - fancylogger.logToFile(self.logfile, enable=False) - # clean error on non-existing file error_pat = "Failed to patch .*/nosuchfile.txt: .*No such file or directory" path = os.path.join(self.test_prefix, 'nosuchfile.txt') diff --git a/test/framework/utilities.py b/test/framework/utilities.py index 0088de959b..d36c9da53a 100644 --- a/test/framework/utilities.py +++ b/test/framework/utilities.py @@ -36,6 +36,7 @@ import sys import tempfile import unittest +from contextlib import contextmanager from easybuild.base import fancylogger from easybuild.base.testing import TestCase @@ -205,6 +206,16 @@ def allow_deprecated_behaviour(self): del os.environ['EASYBUILD_DEPRECATED'] eb_build_log.CURRENT_VERSION = self.orig_current_version + @contextmanager + def log_to_testlogfile(self): + """Context manager class to capture log output in self.logfile for the scope used. Clears the file first""" + open(self.logfile, 'w').close() # Remove all contents + fancylogger.logToFile(self.logfile) + try: + yield self.logfile + finally: + fancylogger.logToFile(self.logfile, enable=False) + def tearDown(self): """Clean up after running testcase.""" super(EnhancedTestCase, self).tearDown()