|
| 1 | +from __future__ import annotations |
| 2 | +import pytest |
| 3 | +from unittest.mock import MagicMock |
| 4 | +from licensecheck.types import PackageInfo, ucstr, License |
| 5 | +from licensecheck.packageinfo import PackageInfoManager |
| 6 | +from licensecheck.checker import check |
| 7 | + |
| 8 | + |
| 9 | +@pytest.fixture |
| 10 | +def mock_package_info_manager() -> PackageInfoManager: |
| 11 | + """Fixture to provide a mocked PackageInfoManager.""" |
| 12 | + return MagicMock(spec=PackageInfoManager) |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize( |
| 16 | + ("ignore_packages", "fail_packages", "fail_licenses", "expected_incompatible"), |
| 17 | + [ |
| 18 | + (None, None, None, False), # No fail conditions, should pass |
| 19 | + (["PACKAGE_B"], None, None, False), # Ignored package should not cause failure |
| 20 | + (["PACKAGE*"], None, None, False), # Globs are supported in ignore_packages |
| 21 | + (None, ["PACKAGE_A"], None, True), # PACKAGE_A in fail_packages should fail |
| 22 | + (None, ["PACKAGE*"], None, True), # Globs are supported in fail_packages |
| 23 | + (None, None, ["GPL-3.0"], True), # GPL-3.0 should be marked as incompatible |
| 24 | + (None, None, ["GPL*"], False), # Globs are not supported on licenses! |
| 25 | + ], |
| 26 | +) |
| 27 | +def test_check( |
| 28 | + mock_package_info_manager: PackageInfoManager, |
| 29 | + ignore_packages: list | None, |
| 30 | + fail_packages: list | None, |
| 31 | + fail_licenses: list | None, |
| 32 | + expected_incompatible: bool, |
| 33 | +): |
| 34 | + """Parametrized test for different license check scenarios.""" |
| 35 | + mock_packages = { |
| 36 | + PackageInfo(name="PACKAGE_A", license=ucstr("MIT"), licenseCompat=True), |
| 37 | + PackageInfo(name="PACKAGE_B", license=ucstr("GPL-3.0"), licenseCompat=False), |
| 38 | + } |
| 39 | + mock_package_info_manager.getPackages.return_value = mock_packages |
| 40 | + |
| 41 | + incompatible, packages = check( |
| 42 | + requirements_paths=["requirements.txt"], |
| 43 | + groups=[], |
| 44 | + this_license=License.GPL_3_PLUS, |
| 45 | + package_info_manager=mock_package_info_manager, |
| 46 | + ignore_packages=ignore_packages, |
| 47 | + fail_packages=fail_packages, |
| 48 | + fail_licenses=fail_licenses, |
| 49 | + ) |
| 50 | + |
| 51 | + assert incompatible == expected_incompatible, packages |
0 commit comments