Skip to content

Commit c697690

Browse files
committed
Remove duplicated branch and use read_file and contextmanager for logToFile
1 parent b48032d commit c697690

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
@@ -1475,7 +1475,7 @@ def apply_regex_substitutions(paths, regex_subs, backup='.orig.eb', on_missing_m
14751475
:param backup: create backup of original file with specified suffix (no backup if value evaluates to False)
14761476
:param on_missing_match: Define what to do when no match was found in the file.
14771477
Can be 'error' to raise an error, 'warn' to print a warning or 'ignore' to do nothing
1478-
Defaults to value of --strict
1478+
Defaults to the value of --strict
14791479
"""
14801480
if on_missing_match is None:
14811481
on_missing_match = build_option('strict')
@@ -1540,13 +1540,7 @@ def apply_regex_substitutions(paths, regex_subs, backup='.orig.eb', on_missing_m
15401540
elif on_missing_match == run.WARN:
15411541
_log.warning(msg)
15421542
else:
1543-
msg = 'Nothing found to replace in %s' % path
1544-
if on_missing_match == run.ERROR:
1545-
raise EasyBuildError(msg)
1546-
elif on_missing_match == run.WARN:
1547-
print_warning(msg)
1548-
else:
1549-
_log.info(msg)
1543+
_log.info(msg)
15501544

15511545
except (IOError, OSError) as err:
15521546
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
@@ -48,7 +48,6 @@
4848
from easybuild.tools.config import IGNORE, ERROR
4949
from easybuild.tools.multidiff import multidiff
5050
from easybuild.tools.py2vs3 import std_urllib
51-
from easybuild.base import fancylogger
5251

5352

5453
class FileToolsTest(EnhancedTestCase):
@@ -1266,22 +1265,18 @@ def test_apply_regex_substitutions(self):
12661265
self.assertErrorRegex(EasyBuildError, error_pat, ft.apply_regex_substitutions, testfile, regex_subs_no_match,
12671266
on_missing_match=run.ERROR)
12681267

1269-
fancylogger.logToFile(self.logfile)
1270-
12711268
# Warn
1272-
ft.apply_regex_substitutions(testfile, regex_subs_no_match, on_missing_match=run.WARN)
1273-
with open(self.logfile, 'r') as f:
1274-
logtxt = f.read()
1269+
with self.log_to_testlogfile():
1270+
ft.apply_regex_substitutions(testfile, regex_subs_no_match, on_missing_match=run.WARN)
1271+
logtxt = ft.read_file(self.logfile)
12751272
self.assertTrue('WARNING ' + error_pat in logtxt)
12761273

12771274
# Ignore
1278-
ft.apply_regex_substitutions(testfile, regex_subs_no_match, on_missing_match=run.IGNORE)
1279-
with open(self.logfile, 'r') as f:
1280-
logtxt = f.read()
1275+
with self.log_to_testlogfile():
1276+
ft.apply_regex_substitutions(testfile, regex_subs_no_match, on_missing_match=run.IGNORE)
1277+
logtxt = ft.read_file(self.logfile)
12811278
self.assertTrue('INFO ' + error_pat in logtxt)
12821279

1283-
fancylogger.logToFile(self.logfile, enable=False)
1284-
12851280
# clean error on non-existing file
12861281
error_pat = "Failed to patch .*/nosuchfile.txt: .*No such file or directory"
12871282
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
@@ -205,6 +206,16 @@ def allow_deprecated_behaviour(self):
205206
del os.environ['EASYBUILD_DEPRECATED']
206207
eb_build_log.CURRENT_VERSION = self.orig_current_version
207208

209+
@contextmanager
210+
def log_to_testlogfile(self):
211+
"""Context manager class to capture log output in self.logfile for the scope used. Clears the file first"""
212+
open(self.logfile, 'w').close() # Remove all contents
213+
fancylogger.logToFile(self.logfile)
214+
try:
215+
yield self.logfile
216+
finally:
217+
fancylogger.logToFile(self.logfile, enable=False)
218+
208219
def tearDown(self):
209220
"""Clean up after running testcase."""
210221
super(EnhancedTestCase, self).tearDown()

0 commit comments

Comments
 (0)