Skip to content

Commit

Permalink
Remove duplicated branch and use read_file and contextmanager for log…
Browse files Browse the repository at this point in the history
…ToFile
  • Loading branch information
Flamefire committed May 25, 2021
1 parent b48032d commit c697690
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
10 changes: 2 additions & 8 deletions easybuild/tools/filetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)
Expand Down
17 changes: 6 additions & 11 deletions test/framework/filetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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')
Expand Down
11 changes: 11 additions & 0 deletions test/framework/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit c697690

Please sign in to comment.