Skip to content

Commit 4781031

Browse files
committed
Simplify warnings capture with pytest
1 parent 2dfde14 commit 4781031

File tree

1 file changed

+8
-26
lines changed

1 file changed

+8
-26
lines changed

sasstests.py

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import tempfile
1515
import traceback
1616
import unittest
17-
import warnings
1817

1918
import pytest
2019
from six import StringIO, b, string_types, text_type
@@ -424,14 +423,11 @@ def test_compile_string_deprecated_source_comments_line_numbers(self):
424423
color: red;
425424
}'''
426425
expected = sass.compile(string=source, source_comments=True)
427-
with warnings.catch_warnings(record=True) as w:
428-
warnings.simplefilter('always')
426+
with pytest.warns(FutureWarning):
429427
actual = sass.compile(
430428
string=source,
431429
source_comments='line_numbers',
432430
)
433-
assert len(w) == 1
434-
assert issubclass(w[-1].category, FutureWarning)
435431
assert expected == actual
436432

437433
def test_compile_filename(self):
@@ -465,15 +461,12 @@ def test_compile_source_map_deprecated_source_comments_map(self):
465461
filename=filename,
466462
source_map_filename='a.scss.css.map',
467463
)
468-
with warnings.catch_warnings(record=True) as w:
469-
warnings.simplefilter('always')
464+
with pytest.warns(FutureWarning):
470465
actual, actual_map = sass.compile(
471466
filename=filename,
472467
source_comments='map',
473468
source_map_filename='a.scss.css.map',
474469
)
475-
assert len(w) == 1
476-
assert issubclass(w[-1].category, FutureWarning)
477470
assert expected == actual
478471
self.assert_source_map_equal(expected_map, actual_map)
479472

@@ -595,8 +588,7 @@ def test_output_style(self):
595588
class ManifestTestCase(BaseTestCase):
596589

597590
def test_normalize_manifests(self):
598-
with warnings.catch_warnings(record=True) as w:
599-
warnings.simplefilter('always')
591+
with pytest.warns(FutureWarning) as warninfo:
600592
manifests = Manifest.normalize_manifests({
601593
'package': 'sass/path',
602594
'package.name': ('sass/path', 'css/path'),
@@ -607,8 +599,7 @@ def test_normalize_manifests(self):
607599
'strip_extension': True,
608600
},
609601
})
610-
assert len(w) == 3
611-
assert all(issubclass(x.category, FutureWarning) for x in w)
602+
assert len(warninfo) == 3
612603
assert len(manifests) == 4
613604
assert isinstance(manifests['package'], Manifest)
614605
assert manifests['package'].sass_path == 'sass/path'
@@ -635,11 +626,8 @@ def replace_source_path(s, name):
635626
return s.replace('SOURCE', test_source_path(name))
636627

637628
shutil.copytree('test', src_path)
638-
with warnings.catch_warnings(record=True) as w:
639-
warnings.simplefilter('always')
629+
with pytest.warns(FutureWarning):
640630
m = Manifest(sass_path='test', css_path='css')
641-
assert len(w) == 1
642-
assert issubclass(w[-1].category, FutureWarning)
643631

644632
m.build_one(d, 'a.scss')
645633
with open(os.path.join(d, 'css', 'a.scss.css')) as f:
@@ -720,15 +708,12 @@ def test_wsgi_sass_middleware(self):
720708
with tempdir() as css_dir:
721709
src_dir = os.path.join(css_dir, 'src')
722710
shutil.copytree('test', src_dir)
723-
with warnings.catch_warnings(record=True) as w:
724-
warnings.simplefilter('always')
711+
with pytest.warns(FutureWarning):
725712
app = SassMiddleware(
726713
self.sample_wsgi_app, {
727714
__name__: (src_dir, css_dir, '/static'),
728715
},
729716
)
730-
assert len(w) == 1
731-
assert issubclass(w[-1].category, FutureWarning)
732717
client = Client(app, Response)
733718
r = client.get('/asdf')
734719
assert r.status_code == 200
@@ -828,15 +813,12 @@ def test_pysassc_stdout(self):
828813
assert A_EXPECTED_CSS.strip() == self.out.getvalue().strip()
829814

830815
def test_sassc_stdout(self):
831-
with warnings.catch_warnings(record=True) as w:
832-
warnings.simplefilter('always')
816+
with pytest.warns(FutureWarning) as warninfo:
833817
exit_code = sassc.main(
834818
['sassc', 'test/a.scss'],
835819
self.out, self.err,
836820
)
837-
assert len(w) == 1
838-
assert issubclass(w[-1].category, FutureWarning)
839-
assert 'use `pysassc`' in str(w[-1].message)
821+
assert 'use `pysassc`' in warninfo[0].message.args[0]
840822
assert exit_code == 0
841823
assert self.err.getvalue() == ''
842824
assert A_EXPECTED_CSS.strip() == self.out.getvalue().strip()

0 commit comments

Comments
 (0)