Skip to content

Commit c3f47fd

Browse files
author
Vasileios Karakasis
committed
Add unit tests + fix
1 parent 30dfc63 commit c3f47fd

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

reframe/core/warnings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def _format_warning(message, category, filename, lineno, line=None):
1717
import reframe.utility.color as color
1818

1919
if category != ReframeDeprecationWarning:
20-
return _format_warning_orig
20+
return _format_warning_orig(message, category, filename, lineno, line)
2121

2222
if line is None:
2323
# Read in the line from the file

unittests/test_warnings.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import pytest
2+
import warnings
3+
4+
import reframe.core.runtime as rt
5+
import reframe.core.warnings as warn
6+
import reframe.utility.color as color
7+
import unittests.fixtures as fixtures
8+
9+
10+
@pytest.fixture(params=['colors', 'nocolors'])
11+
def with_colors(request):
12+
with rt.temp_runtime(fixtures.BUILTIN_CONFIG_FILE, 'generic',
13+
{'general/colorize': request.param == 'colors'}):
14+
yield request.param == 'colors'
15+
16+
17+
def test_deprecation_warning():
18+
with pytest.warns(warn.ReframeDeprecationWarning):
19+
warn.user_deprecation_warning('deprecated')
20+
21+
22+
def test_deprecation_warning_formatting(with_colors):
23+
message = warnings.formatwarning(
24+
'deprecated', warn.ReframeDeprecationWarning, 'file', 10, 'a = 1'
25+
)
26+
expected = 'file:10: WARNING: deprecated\na = 1\n'
27+
if with_colors:
28+
expected = color.colorize(expected, color.YELLOW)
29+
30+
assert message == expected
31+
32+
33+
def test_deprecation_warning_formatting_noline(tmp_path, with_colors):
34+
srcfile = tmp_path / 'file'
35+
srcfile.touch()
36+
37+
message = warnings.formatwarning(
38+
'deprecated', warn.ReframeDeprecationWarning, srcfile, 10
39+
)
40+
expected = f'{srcfile}:10: WARNING: deprecated\n<no line information>\n'
41+
if with_colors:
42+
expected = color.colorize(expected, color.YELLOW)
43+
44+
assert message == expected
45+
46+
47+
def test_random_warning_formatting():
48+
message = warnings.formatwarning(
49+
'deprecated', UserWarning, 'file', 10, 'a = 1'
50+
)
51+
assert message == f'file:10: UserWarning: deprecated\n a = 1\n'

0 commit comments

Comments
 (0)