|
9 | 9 | from .utils import run_cli_command |
10 | 10 |
|
11 | 11 |
|
12 | | -class MockWorkingSet(list): |
13 | | - """Mocks a pkg_resources.workingset object |
| 12 | +class MockDistribution: |
| 13 | + """Mocks an importlib.metadata.Distribution object""" |
14 | 14 |
|
15 | | - Basically, a list of mock packages representing all of the packages |
16 | | - installed in the current environment |
17 | | - """ |
18 | | - |
19 | | - def __init__(self, *package_names): |
20 | | - super().__init__() |
21 | | - for package_name in package_names: |
22 | | - pkg = mock.Mock() |
23 | | - pkg.key = package_name |
24 | | - self.append(pkg) |
| 15 | + def __init__(self, name): |
| 16 | + self.name = name |
25 | 17 |
|
26 | 18 |
|
27 | 19 | def mock_Command(returncodes={}): |
@@ -49,16 +41,18 @@ def run(): |
49 | 41 | return the_real_mock |
50 | 42 |
|
51 | 43 |
|
52 | | -@mock.patch("cumulusci.cli.robot.pkg_resources") |
53 | | -def test_is_package_installed(pkg_resources): |
| 44 | +@mock.patch("cumulusci.cli.robot.importlib.metadata.distribution") |
| 45 | +def test_is_package_installed(mock_distribution): |
54 | 46 | """Verify that the helper _is_package_installed returns the correct result""" |
55 | | - pkg_resources.working_set = MockWorkingSet( |
56 | | - "robotframework", "robotframework-browser", "snowfakery" |
57 | | - ) |
| 47 | + # Test when package is installed |
| 48 | + mock_distribution.return_value = MockDistribution("robotframework-browser") |
58 | 49 | result = _is_package_installed("robotframework-browser") |
59 | 50 | assert result is True |
60 | 51 |
|
61 | | - pkg_resources.working_set = MockWorkingSet("robotframework", "snowfakery") |
| 52 | + # Test when package is not installed |
| 53 | + from importlib.metadata import PackageNotFoundError |
| 54 | + |
| 55 | + mock_distribution.side_effect = PackageNotFoundError("Package not found") |
62 | 56 | result = _is_package_installed("robotframework-browser") |
63 | 57 | assert result is False |
64 | 58 |
|
|
0 commit comments