|
16 | 16 |
|
17 | 17 | import pytest |
18 | 18 | from packaging.requirements import Requirement |
| 19 | +from unittest.mock import patch |
19 | 20 |
|
20 | 21 | from opentelemetry.instrumentation.dependencies import ( |
21 | 22 | DependencyConflict, |
22 | 23 | get_dependency_conflicts, |
23 | 24 | get_dist_dependency_conflicts, |
24 | 25 | ) |
25 | 26 | from opentelemetry.test.test_base import TestBase |
26 | | -from opentelemetry.util._importlib_metadata import Distribution |
| 27 | +from opentelemetry.util._importlib_metadata import ( |
| 28 | + Distribution, |
| 29 | + PackageNotFoundError, |
| 30 | +) |
27 | 31 |
|
28 | 32 |
|
29 | 33 | class TestDependencyConflicts(TestBase): |
@@ -97,8 +101,69 @@ def read_text(self, filename): |
97 | 101 |
|
98 | 102 | @property |
99 | 103 | def requires(self): |
| 104 | + # TODO: make another test for returning something with a blank list for both and and or |
100 | 105 | return None |
101 | 106 |
|
102 | 107 | dist = MockDistribution() |
103 | 108 | conflict = get_dist_dependency_conflicts(dist) |
104 | 109 | self.assertTrue(conflict is None) |
| 110 | + |
| 111 | + @patch("opentelemetry.instrumentation.dependencies.version") |
| 112 | + def test_get_dist_dependency_conflicts_either(self, version_mock): |
| 113 | + class MockDistribution(Distribution): |
| 114 | + def locate_file(self, path): |
| 115 | + pass |
| 116 | + |
| 117 | + def read_text(self, filename): |
| 118 | + pass |
| 119 | + |
| 120 | + @property |
| 121 | + def requires(self): |
| 122 | + return [ |
| 123 | + 'foo ~= 1.0; extra == "instruments_either"', |
| 124 | + 'bar ~= 1.0; extra == "instruments_either"' |
| 125 | + ] |
| 126 | + |
| 127 | + dist = MockDistribution() |
| 128 | + |
| 129 | + def version_side_effect(package_name): |
| 130 | + if package_name == "foo": |
| 131 | + raise PackageNotFoundError("foo not found") |
| 132 | + elif package_name == "bar": |
| 133 | + return "1.0.0" |
| 134 | + else: |
| 135 | + raise PackageNotFoundError(f"{package_name} not found") |
| 136 | + |
| 137 | + version_mock.side_effect = version_side_effect |
| 138 | + conflict = get_dist_dependency_conflicts(dist) |
| 139 | + print("CONFLICT", conflict) |
| 140 | + self.assertIsNone(conflict) |
| 141 | + |
| 142 | + @patch("opentelemetry.instrumentation.dependencies.version") |
| 143 | + def test_get_dist_dependency_conflicts_neither(self, version_mock): |
| 144 | + class MockDistribution(Distribution): |
| 145 | + def locate_file(self, path): |
| 146 | + pass |
| 147 | + |
| 148 | + def read_text(self, filename): |
| 149 | + pass |
| 150 | + |
| 151 | + @property |
| 152 | + def requires(self): |
| 153 | + return [ |
| 154 | + 'foo ~= 1.0; extra == "instruments_either"', |
| 155 | + 'bar ~= 1.0; extra == "instruments_either"' |
| 156 | + ] |
| 157 | + |
| 158 | + dist = MockDistribution() |
| 159 | + # version_mock.side_effect = lambda x: "1.0.0" if x == "foo" else "2.0.0" |
| 160 | + # version_mock("foo").return_value = "2.0.0" |
| 161 | + version_mock.side_effect = PackageNotFoundError("not found") |
| 162 | + conflict = get_dist_dependency_conflicts(dist) |
| 163 | + self.assertTrue(conflict is not None) |
| 164 | + self.assertTrue(isinstance(conflict, DependencyConflict)) |
| 165 | + self.assertEqual( |
| 166 | + str(conflict), |
| 167 | + # TODO: fix tests of either conflict |
| 168 | + '''DependencyConflict: requested any of the following: "['foo~=1.0; extra == "instruments-either"', 'bar~=1.0; extra == "instruments-either"']" but found: "[]"''', |
| 169 | + ) |
0 commit comments