Skip to content

Commit

Permalink
add package filter tests
Browse files Browse the repository at this point in the history
fixes #1237
  • Loading branch information
nerdvegas committed Mar 1, 2022
1 parent a4d2706 commit 3c857fc
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/rez/package_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,21 +499,27 @@ class TimestampRule(Rule):
"""
name = "timestamp"

def __init__(self, timestamp, family=None, reverse=False):
def __init__(self, timestamp, family=None, reverse=False,
match_untimestamped=False):
"""Create a timestamp rule.
Args:
timestamp (int): Epoch time.
family (str): Package family to apply the rule to.
reverse (bool): If True, reverse the logic so that packages released
*after* the timestamp are matched.
match_untimestamped (bool): Defines behaviour on non-timestamped
packages.
"""
self.timestamp = timestamp
self.reverse = reverse
self.match_untimestamped = match_untimestamped
self._family = family

def match(self, package):
if self.reverse:
if not package.timestamp:
return self.match_untimestamped
elif self.reverse:
return (package.timestamp > self.timestamp)
else:
return (package.timestamp <= self.timestamp)
Expand Down
161 changes: 161 additions & 0 deletions src/rez/tests/test_package_filter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright Contributors to the Rez Project


"""
Test cases for package_filter.py (package filtering)
"""
from rez.tests.util import TestBase
from rez.packages import iter_packages
from rez.package_filter import PackageFilter, PackageFilterList, GlobRule


class TestPackageFilter(TestBase):
"""Tests package filtering.
"""
@classmethod
def setUpClass(cls):
cls.py_packages_path = cls.data_path("packages", "py_packages")
cls.solver_packages_path = cls.data_path("solver", "packages")

cls.settings = dict(
packages_path=[
cls.solver_packages_path,
cls.py_packages_path
],
package_filter=None)

def _test(self, fltr, pkg_family, expected_result):

# convert from json if required
if isinstance(fltr, dict):
fltr = PackageFilter.from_pod(fltr)
elif isinstance(fltr, list):
fltr = PackageFilterList.from_pod(fltr)

def filter_versions(fltr_):
matching_versions = set()

for pkg in iter_packages(pkg_family):
if not fltr_.excludes(pkg):
matching_versions.add(str(pkg.version))

self.assertEqual(matching_versions, set(expected_result))

# apply filter to all pkg versions
filter_versions(fltr)

# serialise to/from json and do it again
data = fltr.to_pod()
fltr2 = fltr.from_pod(data)
filter_versions(fltr2)

def test_empty_filter(self):
"""Test that empty filter has no effect
"""
fltr = PackageFilter()
self._test(
fltr,
"pydad",
["1", "2", "3"]
)

def test_empty_filter_list(self):
"""Test that empty filter list has no effect
"""
fltr = PackageFilterList()
self._test(
fltr,
"pydad",
["1", "2", "3"]
)

def test_glob_filter(self):
"""Very simply filter test
"""
fltr = PackageFilter()
fltr.add_exclusion(GlobRule("timestamped-*.5"))

self._test(
fltr,
"timestamped",
[
"1.0.6",
"1.1.0",
"1.1.1",
"1.2.0",
"2.0.0",
"2.1.0"
]
)

def test_otherfam_filter(self):
"""Test that a filter on a different fam has no effect
"""
fltr = PackageFilter()
fltr.add_exclusion(GlobRule("timestamped-*"))

self._test(
fltr,
"pydad",
["1", "2", "3"]
)

def test_excl_and_incl(self):
"""Test that combo of exclusion and inclusion works as expected
"""
self._test(
{
"excludes": ["glob(*.5)"],
"includes": ["range(timestamped-2)"]
},
"timestamped",
[
# "1.0.5", due to excludes
"1.0.6",
"1.1.0",
"1.1.1",
"1.2.0",
"2.0.0",
"2.1.0",
"2.1.5" # due to includes
]
)

def test_filter_list(self):
"""Test that logic wrt list of filters works as expected
"""

# exclude all *.0 packages, and all 2.* packages except for pymum
fltrs = [
# fltr-1
{
"excludes": ["*.0"]
},
# fltr-2
{
"excludes": ["*-2.*"],
"includes": ["pymum"]
}
]

self._test(
fltrs,
"timestamped",
[
"1.0.5",
"1.0.6",
# "1.1.0", due to fltr-1
"1.1.1",
# "1.2.0", due to fltr-1
# "2.0.0", due to fltr-1 and fltr-2
# "2.1.0", due to fltr-1 and fltr-2
# "2.1.5" due to fltr-2
]
)

self._test(
fltrs,
"pymum",
["1", "2", "3"]
)
2 changes: 1 addition & 1 deletion src/rez/utils/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@


# Update this value to version up Rez. Do not place anything else in this file.
_rez_version = "2.104.9"
_rez_version = "2.105.0"

0 comments on commit 3c857fc

Please sign in to comment.