Skip to content

Commit

Permalink
Capture warnings in tests
Browse files Browse the repository at this point in the history
This makes it clearer to see which tests rely on deprecated features.
  • Loading branch information
rbarrois committed Aug 22, 2023
1 parent fb1301a commit d2dbf5f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ testall:

# DOC: Run tests for the currently installed version
test:
python -Wdefault -m nose2
python -Werr -m nose2

# DOC: Perform code quality tasks
lint: check-manifest flake8
Expand Down
2 changes: 1 addition & 1 deletion semantic_version/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ def compare(v1, v2):


def match(spec, version):
return Spec(spec).match(Version(version))
return SimpleSpec(spec).match(Version(version))


def validate(version_string):
Expand Down
28 changes: 28 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ def test_compare_to_self(self):
(1, 1, 3, (), ('build', '2012-04-13', 'HUY', 'alpha-12', '1')),
}

@testing.expect_warning(testing.WARN_SPEC_PARTIAL)
def test_parsing_partials(self):
for text, expected_fields in self.partial_versions.items():
with self.subTest(text=text):
Expand All @@ -170,13 +171,15 @@ def test_parsing_partials(self):
self.assertEqual(expected_fields, actual_fields)
self.assertTrue(version.partial, "%r should have partial=True" % version)

@testing.expect_warning(testing.WARN_SPEC_PARTIAL)
def test_str_partials(self):
for text in self.partial_versions:
with self.subTest(text=text):
version = base.Version(text, partial=True)
self.assertEqual(text, str(version))
self.assertEqual("Version('%s', partial=True)" % text, repr(version))

@testing.expect_warning(testing.WARN_SPEC_PARTIAL)
def test_compare_partial_to_self(self):
for text in self.partial_versions:
with self.subTest(text=text):
Expand All @@ -185,6 +188,7 @@ def test_compare_partial_to_self(self):
base.Version(text, partial=True))
self.assertNotEqual(text, base.Version(text, partial=True))

@testing.expect_warning(testing.WARN_SPEC_PARTIAL)
def test_hash(self):
self.assertEqual(
1,
Expand Down Expand Up @@ -411,6 +415,7 @@ class SpecItemTestCase(testing.TestCase):
'>0.2.3-rc2+',
]

@testing.expect_warning(testing.WARN_SPEC_PARTIAL, testing.WARN_SPECITEM)
def test_invalids(self):
for invalid in self.invalids:
with self.subTest(invalid=invalid):
Expand All @@ -435,6 +440,7 @@ def test_invalids(self):
'^0.1.3': (base.SpecItem.KIND_CARET, 0, 1, 3, None, None),
}

@testing.expect_warning(testing.WARN_SPEC_CLASS, testing.WARN_SPEC_PARTIAL, testing.WARN_SPECITEM)
def test_components(self):
for spec_text, components in self.components.items():
with self.subTest(spec_text=spec_text):
Expand Down Expand Up @@ -543,6 +549,7 @@ def test_components(self):
),
}

@testing.expect_warning(testing.WARN_SPEC_CLASS, testing.WARN_SPEC_PARTIAL, testing.WARN_SPECITEM)
def test_matches(self):
for spec_text, versions in self.matches.items():
spec = base.SpecItem(spec_text)
Expand All @@ -560,17 +567,20 @@ def test_matches(self):
spec.match(version),
"%r should not match %r" % (version, spec))

@testing.expect_warning(testing.WARN_SPEC_CLASS, testing.WARN_SPEC_PARTIAL, testing.WARN_SPECITEM)
def test_equality(self):
spec1 = base.SpecItem('==0.1.0')
spec2 = base.SpecItem('==0.1.0')
self.assertEqual(spec1, spec2)
self.assertFalse(spec1 == '==0.1.0')

@testing.expect_warning(testing.WARN_SPEC_CLASS, testing.WARN_SPEC_PARTIAL, testing.WARN_SPECITEM)
def test_to_string(self):
spec = base.SpecItem('==0.1.0')
self.assertEqual('==0.1.0', str(spec))
self.assertEqual(base.SpecItem.KIND_EQUAL, spec.kind)

@testing.expect_warning(testing.WARN_SPEC_CLASS, testing.WARN_SPEC_PARTIAL, testing.WARN_SPECITEM)
def test_hash(self):
self.assertEqual(
1,
Expand Down Expand Up @@ -613,6 +623,7 @@ class SpecTestCase(testing.TestCase):
'~=1.2.3': ['>=1.2.3', '<1.3.0'],
}

@testing.expect_warning(testing.WARN_SPEC_CLASS, testing.WARN_SPEC_ITER, testing.WARN_SPEC_PARTIAL)
def test_parsing(self):
for spec_list_text, specs in self.examples.items():
with self.subTest(spec=spec_list_text):
Expand All @@ -627,6 +638,13 @@ def test_parsing(self):
('>=0.1.0', '!=0.1.3-rc1,<0.1.3'): ['>=0.1.0', '!=0.1.3-rc1', '<0.1.3'],
}

@testing.expect_warning(
testing.WARN_SIMPLESPEC_MANY,
testing.WARN_SPECITEM,
testing.WARN_SPEC_CLASS,
testing.WARN_SPEC_ITER,
testing.WARN_SPEC_PARTIAL,
)
def test_parsing_split(self):
for spec_list_texts, specs in self.split_examples.items():
with self.subTest(spec=spec_list_texts):
Expand Down Expand Up @@ -665,6 +683,7 @@ def test_parsing_split(self):
),
}

@testing.expect_warning(testing.WARN_SPEC_CLASS)
def test_matches(self):
for spec_list_text, versions in self.matches.items():
spec_list = base.Spec(spec_list_text)
Expand All @@ -690,6 +709,7 @@ def test_matches(self):
spec_list.match(version),
"%r should not match %r" % (version, spec_list))

@testing.expect_warning(testing.WARN_SPEC_CLASS)
def test_equality(self):
for spec_list_text in self.examples:
with self.subTest(spec=spec_list_text):
Expand All @@ -698,11 +718,13 @@ def test_equality(self):
self.assertEqual(slist1, slist2)
self.assertFalse(slist1 == spec_list_text)

@testing.expect_warning(testing.WARN_SPEC_CLASS)
def test_filter_empty(self):
s = base.Spec('>=0.1.1')
res = tuple(s.filter(()))
self.assertEqual((), res)

@testing.expect_warning(testing.WARN_SPEC_CLASS)
def test_filter_incompatible(self):
s = base.Spec('>=0.1.1,!=0.1.4')
res = tuple(s.filter([
Expand All @@ -712,6 +734,7 @@ def test_filter_incompatible(self):
]))
self.assertEqual((), res)

@testing.expect_warning(testing.WARN_SPEC_CLASS)
def test_filter_compatible(self):
s = base.Spec('>=0.1.1,!=0.1.4,<0.2.0')
res = tuple(s.filter([
Expand All @@ -732,10 +755,12 @@ def test_filter_compatible(self):

self.assertEqual(expected, res)

@testing.expect_warning(testing.WARN_SPEC_CLASS)
def test_select_empty(self):
s = base.Spec('>=0.1.1')
self.assertIsNone(s.select(()))

@testing.expect_warning(testing.WARN_SPEC_CLASS)
def test_select_incompatible(self):
s = base.Spec('>=0.1.1,!=0.1.4')
res = s.select([
Expand All @@ -745,6 +770,7 @@ def test_select_incompatible(self):
])
self.assertIsNone(res)

@testing.expect_warning(testing.WARN_SPEC_CLASS)
def test_select_compatible(self):
s = base.Spec('>=0.1.1,!=0.1.4,<0.2.0')
res = s.select([
Expand All @@ -759,9 +785,11 @@ def test_select_compatible(self):

self.assertEqual(base.Version('0.1.5'), res)

@testing.expect_warning(testing.WARN_SPEC_CLASS)
def test_contains(self):
self.assertFalse('ii' in base.Spec('>=0.1.1'))

@testing.expect_warning(testing.WARN_SPEC_CLASS)
def test_hash(self):
self.assertEqual(
1,
Expand Down
9 changes: 8 additions & 1 deletion tests/test_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@
from .setup_django import configure_django
from . import testing

configure_django()
with testing.expect_warning(testing.WARN_VERSION_PARTIAL):
configure_django()

from .django_test_app import models # noqa: E402


test_state = {}


@testing.expect_warning(testing.WARN_VERSION_PARTIAL)
def setUpModule():
django_test_utils.setup_test_environment()
runner = django_test_runner.DiscoverRunner()
Expand Down Expand Up @@ -119,6 +121,7 @@ def test_partial_spec_clean(self):
self.assertEqual(Version('0.1.1'), obj.version)
self.assertEqual(SimpleSpec('==0,!=0.2'), obj.spec)

@testing.expect_warning(testing.WARN_SPEC_PARTIAL)
def test_coerce_clean(self):
obj = models.CoerceVersionModel(version='0.1.1a+2', partial='23')
obj.full_clean()
Expand All @@ -137,6 +140,7 @@ def test_invalid_input(self):
v2 = models.VersionModel(version='0.1', spec='==0.1.1,!=0.1.1-alpha')
self.assertRaises(ValueError, v2.full_clean)

@testing.expect_warning(testing.WARN_SPEC_PARTIAL)
def test_partial(self):
obj = models.PartialVersionModel(partial=Version('0.1.0'))

Expand Down Expand Up @@ -181,6 +185,7 @@ def test_serialization(self):
self.assertEqual(o2.spec, obj2.object.spec)
self.assertEqual(o2.npm_spec, obj2.object.npm_spec)

@testing.expect_warning(testing.WARN_SPEC_PARTIAL)
def test_serialization_partial(self):
o1 = models.PartialVersionModel(
partial=Version('0.1.1', partial=True),
Expand All @@ -203,6 +208,7 @@ def test_serialization_partial(self):


class FieldMigrationTests(DjangoTestCase):
@testing.expect_warning(testing.WARN_VERSION_PARTIAL)
def test_version_field(self):
field = django_fields.VersionField(
partial=True,
Expand All @@ -227,6 +233,7 @@ def test_nondefault_spec_field(self):


class FullMigrateTests(TransactionTestCase):
@testing.expect_warning(testing.WARN_VERSION_PARTIAL)
def test_migrate(self):
# Let's check that this does not crash
call_command('makemigrations', verbosity=0)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,22 @@ class MatchTestCase(testing.TestCase):
],
}

@testing.expect_warning(testing.WARN_SPEC_CLASS)
def test_invalid(self):
for invalid in self.invalid_specs:
with self.subTest(spec=invalid):
with self.assertRaises(ValueError, msg="Spec(%r) should be invalid" % invalid):
semantic_version.Spec(invalid)

@testing.expect_warning(testing.WARN_SPECITEM, testing.WARN_SPEC_CLASS, testing.WARN_SPEC_PARTIAL)
def test_simple(self):
for valid in self.valid_specs:
with self.subTest(spec=valid):
spec = semantic_version.SpecItem(valid)
normalized = str(spec)
self.assertEqual(spec, semantic_version.SpecItem(normalized))

@testing.expect_warning(testing.WARN_SPEC_CLASS)
def test_match(self):
for spec_text, versions in self.matches.items():
for version_text in versions:
Expand All @@ -141,6 +144,7 @@ def test_match(self):
self.assertTrue(spec.match(version), "%r does not match %r" % (version, spec))
self.assertTrue(semantic_version.match(spec_text, version_text))

@testing.expect_warning(testing.WARN_SPEC_CLASS)
def test_contains(self):
spec = semantic_version.Spec('<=0.1.1')
self.assertFalse('0.1.0' in spec, "0.1.0 should not be in %r" % spec)
Expand All @@ -151,13 +155,15 @@ def test_contains(self):
version = semantic_version.Version('0.1.1-rc1+4.2')
self.assertTrue(version in spec, "%r should be in %r" % (version, spec))

@testing.expect_warning(testing.WARN_SPEC_CLASS)
def test_prerelease_check(self):
strict_spec = semantic_version.Spec('>=0.1.1-')
lax_spec = semantic_version.Spec('>=0.1.1')
version = semantic_version.Version('0.1.1-rc1+4.2')
self.assertFalse(version in lax_spec, "%r should not be in %r" % (version, lax_spec))
self.assertFalse(version in strict_spec, "%r should not be in %r" % (version, strict_spec))

@testing.expect_warning(testing.WARN_SPEC_CLASS)
def test_build_check(self):
spec = semantic_version.Spec('<=0.1.1-rc1')
version = semantic_version.Version('0.1.1-rc1+4.2')
Expand Down
22 changes: 21 additions & 1 deletion tests/testing.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import contextlib
import unittest
import sys
import warnings


class TestCase(unittest.TestCase):
if sys.version_info[0] <= 2:
import contextlib

@contextlib.contextmanager
def subTest(self, **kwargs):
Expand All @@ -18,3 +18,23 @@ def assertCountEqual(self, a, b):
collections.Counter(a),
collections.Counter(b),
)


@contextlib.contextmanager
def expect_warning(*messages):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
yield
actual = {str(warning.message) for warning in w}
assert actual == set(messages), "{actual!r} should match {expected!r}".format(
actual=actual,
expected=set(messages),
)


WARN_SIMPLESPEC_MANY = "Passing 2+ arguments to SimpleSpec will be removed in 3.0; concatenate them with ',' instead."
WARN_SPECITEM = "The `SpecItem` class will be removed in 3.0."
WARN_SPEC_CLASS = "The Spec() class will be removed in 3.1; use SimpleSpec() instead."
WARN_SPEC_ITER = "Iterating over the components of a SimpleSpec object will be removed in 3.0."
WARN_SPEC_PARTIAL = "Partial versions will be removed in 3.0; use SimpleSpec('1.x.x') instead."
WARN_VERSION_PARTIAL = "Use of `partial=True` will be removed in 3.0."

0 comments on commit d2dbf5f

Please sign in to comment.