Skip to content

Commit aa4e3e3

Browse files
committed
Remove duplicated branch and use read_file and contextmanager for logToFile
1 parent a7c9a58 commit aa4e3e3

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

easybuild/tools/filetools.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,7 @@ def apply_regex_substitutions(paths, regex_subs, backup='.orig.eb', on_missing_m
13421342
:param backup: create backup of original file with specified suffix (no backup if value evaluates to False)
13431343
:param on_missing_match: Define what to do when no match was found in the file.
13441344
Can be 'error' to raise an error, 'warn' to print a warning or 'ignore' to do nothing
1345-
Defaults to value of --strict
1345+
Defaults to the value of --strict
13461346
"""
13471347
if on_missing_match is None:
13481348
on_missing_match = build_option('strict')
@@ -1405,13 +1405,7 @@ def apply_regex_substitutions(paths, regex_subs, backup='.orig.eb', on_missing_m
14051405
elif on_missing_match == run.WARN:
14061406
_log.warning(msg)
14071407
else:
1408-
msg = 'Nothing found to replace in %s' % path
1409-
if on_missing_match == run.ERROR:
1410-
raise EasyBuildError(msg)
1411-
elif on_missing_match == run.WARN:
1412-
print_warning(msg)
1413-
else:
1414-
_log.info(msg)
1408+
_log.info(msg)
14151409

14161410
except (IOError, OSError) as err:
14171411
raise EasyBuildError("Failed to patch %s: %s", path, err)

test/framework/filetools.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
from easybuild.tools.build_log import EasyBuildError
4848
from easybuild.tools.multidiff import multidiff
4949
from easybuild.tools.py2vs3 import std_urllib
50-
from easybuild.base import fancylogger
5150

5251

5352
class FileToolsTest(EnhancedTestCase):
@@ -1217,22 +1216,18 @@ def test_apply_regex_substitutions(self):
12171216
self.assertErrorRegex(EasyBuildError, error_pat, ft.apply_regex_substitutions, testfile, regex_subs_no_match,
12181217
on_missing_match=run.ERROR)
12191218

1220-
fancylogger.logToFile(self.logfile)
1221-
12221219
# Warn
1223-
ft.apply_regex_substitutions(testfile, regex_subs_no_match, on_missing_match=run.WARN)
1224-
with open(self.logfile, 'r') as f:
1225-
logtxt = f.read()
1220+
with self.log_to_testlogfile():
1221+
ft.apply_regex_substitutions(testfile, regex_subs_no_match, on_missing_match=run.WARN)
1222+
logtxt = ft.read_file(self.logfile)
12261223
self.assertTrue('WARNING ' + error_pat in logtxt)
12271224

12281225
# Ignore
1229-
ft.apply_regex_substitutions(testfile, regex_subs_no_match, on_missing_match=run.IGNORE)
1230-
with open(self.logfile, 'r') as f:
1231-
logtxt = f.read()
1226+
with self.log_to_testlogfile():
1227+
ft.apply_regex_substitutions(testfile, regex_subs_no_match, on_missing_match=run.IGNORE)
1228+
logtxt = ft.read_file(self.logfile)
12321229
self.assertTrue('INFO ' + error_pat in logtxt)
12331230

1234-
fancylogger.logToFile(self.logfile, enable=False)
1235-
12361231
# clean error on non-existing file
12371232
error_pat = "Failed to patch .*/nosuchfile.txt: .*No such file or directory"
12381233
path = os.path.join(self.test_prefix, 'nosuchfile.txt')

test/framework/utilities.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import sys
3737
import tempfile
3838
import unittest
39+
from contextlib import contextmanager
3940

4041
from easybuild.base import fancylogger
4142
from easybuild.base.testing import TestCase
@@ -191,6 +192,16 @@ def allow_deprecated_behaviour(self):
191192
del os.environ['EASYBUILD_DEPRECATED']
192193
eb_build_log.CURRENT_VERSION = self.orig_current_version
193194

195+
@contextmanager
196+
def log_to_testlogfile(self):
197+
"""Context manager class to capture log output in self.logfile for the scope used. Clears the file first"""
198+
open(self.logfile, 'w').close() # Remove all contents
199+
fancylogger.logToFile(self.logfile)
200+
try:
201+
yield self.logfile
202+
finally:
203+
fancylogger.logToFile(self.logfile, enable=False)
204+
194205
def tearDown(self):
195206
"""Clean up after running testcase."""
196207
super(EnhancedTestCase, self).tearDown()

0 commit comments

Comments
 (0)