Skip to content

Commit 0a2ca2c

Browse files
committed
more proper way to filter test cases
Signed-off-by: Paul Horton <[email protected]>
1 parent ddd7847 commit 0a2ca2c

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

tests/__init__.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,24 @@ def _make_unique(self) -> str:
162162
_LIMIT_GET_BOM_BY_VERSION_REGEX = re.compile(r'^get_bom_(?P<sv>v(?P<major_version>1)_(?P<minor_version>[0-6]))?(.*)$')
163163

164164

165-
def mksname(purpose: Union[Any], sv: SchemaVersion, f: OutputFormat) -> Optional[str]:
166-
purpose = purpose if isinstance(purpose, str) else purpose.__name__
167-
restrict_to_schema = _LIMIT_GET_BOM_BY_VERSION_REGEX.match(purpose)
165+
def _get_purpose_as_str(purpose: Union[Any]) -> str:
166+
return purpose if isinstance(purpose, str) else purpose.__name__
167+
168+
169+
def is_valid_for_schema_version(purpose: Union[Any], sv: SchemaVersion) -> bool:
170+
restrict_to_schema = _LIMIT_GET_BOM_BY_VERSION_REGEX.match(_get_purpose_as_str(purpose))
171+
168172
if restrict_to_schema:
169173
mg = restrict_to_schema.groupdict()
170174
if mg.get('sv') is not None:
171-
# Restrict only to the schema version in the purpose or greater
172175
restricted_to_sv = SchemaVersion.from_version(f'{mg.get("major_version")}.{mg.get("minor_version")}')
173176
if sv >= restricted_to_sv:
174-
return f'{purpose}-{sv.to_version()}.{_SNAME_EXT[f]}'
177+
return True
175178
else:
176-
return None
179+
return False
180+
181+
return True
182+
177183

178-
return f'{purpose}-{sv.to_version()}.{_SNAME_EXT[f]}'
184+
def mksname(purpose: Union[Any], sv: SchemaVersion, f: OutputFormat) -> str:
185+
return f'{_get_purpose_as_str(purpose)}-{sv.to_version()}.{_SNAME_EXT[f]}'

tests/test_output_json.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from cyclonedx.output.json import BY_SCHEMA_VERSION, Json
3737
from cyclonedx.schema import OutputFormat, SchemaVersion
3838
from cyclonedx.validation.json import JsonStrictValidator
39-
from tests import SnapshotMixin, mksname
39+
from tests import SnapshotMixin, is_valid_for_schema_version, mksname
4040
from tests._data.models import all_get_bom_funct_invalid, all_get_bom_funct_valid, bom_all_same_bomref
4141

4242
UNSUPPORTED_SV = frozenset((SchemaVersion.V1_1, SchemaVersion.V1_0,))
@@ -56,13 +56,11 @@ def test_unsupported_schema_raises(self, sv: SchemaVersion) -> None:
5656
@named_data(*((f'{n}-{sv.to_version()}', gb, sv)
5757
for n, gb in all_get_bom_funct_valid
5858
for sv in SchemaVersion
59-
if sv not in UNSUPPORTED_SV))
59+
if sv not in UNSUPPORTED_SV and is_valid_for_schema_version(gb, sv)))
6060
@unpack
6161
@patch('cyclonedx.model.ThisTool._version', 'TESTING')
6262
def test_valid(self, get_bom: Callable[[], Bom], sv: SchemaVersion, *_: Any, **__: Any) -> None:
6363
snapshot_name = mksname(get_bom, sv, OutputFormat.JSON)
64-
if snapshot_name is None:
65-
return
6664
bom = get_bom()
6765
json = BY_SCHEMA_VERSION[sv](bom).output_as_string(indent=2)
6866
try:

tests/test_output_xml.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,16 @@
3434
from cyclonedx.output.xml import BY_SCHEMA_VERSION, Xml
3535
from cyclonedx.schema import OutputFormat, SchemaVersion
3636
from cyclonedx.validation.xml import XmlValidator
37-
from tests import SnapshotMixin, mksname
37+
from tests import SnapshotMixin, is_valid_for_schema_version, mksname
3838
from tests._data.models import all_get_bom_funct_invalid, all_get_bom_funct_valid, bom_all_same_bomref
3939

4040

4141
@ddt
4242
class TestOutputXml(TestCase, SnapshotMixin):
4343

44-
@named_data(*(
45-
(f'{n}-{sv.to_version()}', gb, sv) for n, gb in all_get_bom_funct_valid for sv in SchemaVersion
46-
))
44+
@named_data(*((f'{n}-{sv.to_version()}', gb, sv)
45+
for n, gb in all_get_bom_funct_valid
46+
for sv in SchemaVersion if is_valid_for_schema_version(gb, sv)))
4747
@unpack
4848
@patch('cyclonedx.model.ThisTool._version', 'TESTING')
4949
def test_valid(self, get_bom: Callable[[], Bom], sv: SchemaVersion, *_: Any, **__: Any) -> None:

0 commit comments

Comments
 (0)