From e4d75cd73fc74a90e9924497b8d1909bc66b8cea Mon Sep 17 00:00:00 2001 From: Andrew Pollock Date: Tue, 22 Oct 2024 11:36:05 +1000 Subject: [PATCH] test(ecosystems): add tests for ecosystem consistency (#2615) Test that ecosystem name hardcoding is internally consistent and consistent with the OSV Schema Requires OSV Schema submodule to be at or past https://github.com/ossf/osv-schema/commit/84969b48584c72424595f1d918865da865e03a45 (currently unreleased) to pass. --- osv/ecosystems/ecosystems_test.py | 58 +++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 osv/ecosystems/ecosystems_test.py diff --git a/osv/ecosystems/ecosystems_test.py b/osv/ecosystems/ecosystems_test.py new file mode 100644 index 00000000000..49ee117d0f4 --- /dev/null +++ b/osv/ecosystems/ecosystems_test.py @@ -0,0 +1,58 @@ +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Ecosystem helpers tests.""" + +import json + +import re + +import unittest + +from . import _ecosystems + + +class EcosystemsTest(unittest.TestCase): + """Ecosystem helpers tests.""" + + def setUp(self): + with open('osv/osv-schema/validation/schema.json') as schema_f: + schema = json.load(schema_f) + self.schema_ecosystems = re.compile(schema['$defs']['ecosystem']['pattern']) + self.canonical_ecosystems = _ecosystems._ecosystems.keys() # pylint: disable=protected-access + + def test_ecosystem_supported_by_schema(self): + """Test ecosystems referenced exist in schema definition""" + for ecosystem in self.canonical_ecosystems: + self.assertIsNotNone( + self.schema_ecosystems.match(ecosystem), + msg=f'"{ecosystem}" not defined in schema') + + for ecosystem in _ecosystems.SEMVER_ECOSYSTEMS: + self.assertIsNotNone( + self.schema_ecosystems.match(ecosystem), + msg=f'SEMVER ecosystem "{ecosystem}" not defined in schema') + + for ecosystem in _ecosystems.package_urls: + self.assertIsNotNone( + self.schema_ecosystems.match(ecosystem), + msg=f'Purl "{ecosystem}" not defined in schema') + + for ecosystem in _ecosystems._OSV_TO_DEPS_ECOSYSTEMS_MAP: # pylint: disable=protected-access + self.assertIsNotNone( + self.schema_ecosystems.match(ecosystem), + msg=f'"{ecosystem}" in deps.dev map not defined in schema') + + +if __name__ == '__main__': + unittest.main()