|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | import os |
| 16 | +import sys |
16 | 17 | from unittest import mock |
17 | 18 |
|
18 | 19 | import click |
19 | | -import pytest |
20 | 20 | from click.testing import CliRunner |
21 | 21 |
|
22 | | -try: |
23 | | - from flask import cli as flask_cli |
24 | | -except ImportError: |
25 | | - flask_cli = None |
26 | | - |
27 | 22 | from opentelemetry.instrumentation.click import ClickInstrumentor |
28 | 23 | from opentelemetry.test.test_base import TestBase |
29 | 24 | from opentelemetry.trace import SpanKind |
@@ -168,24 +163,35 @@ def command_raises(): |
168 | 163 | }, |
169 | 164 | ) |
170 | 165 |
|
171 | | - def test_uvicorn_cli_command_ignored(self): |
172 | | - @click.command("uvicorn") |
173 | | - def command_uvicorn(): |
| 166 | + @mock.patch("sys.argv", ["command.py"]) |
| 167 | + def test_disabled_when_asgi_instrumentation_loaded(self): |
| 168 | + @click.command() |
| 169 | + def command(): |
174 | 170 | pass |
175 | 171 |
|
176 | 172 | runner = CliRunner() |
177 | | - result = runner.invoke(command_uvicorn) |
| 173 | + with mock.patch.dict( |
| 174 | + sys.modules, |
| 175 | + {**sys.modules, "opentelemetry.instrumentation.asgi": mock.Mock()}, |
| 176 | + ): |
| 177 | + result = runner.invoke(command) |
178 | 178 | self.assertEqual(result.exit_code, 0) |
179 | 179 |
|
180 | 180 | self.assertFalse(self.memory_exporter.get_finished_spans()) |
181 | 181 |
|
182 | | - @pytest.mark.skipif(flask_cli is None, reason="requires flask") |
183 | | - def test_flask_run_command_ignored(self): |
| 182 | + @mock.patch("sys.argv", ["command.py"]) |
| 183 | + def test_disabled_when_wsgi_instrumentation_loaded(self): |
| 184 | + @click.command() |
| 185 | + def command(): |
| 186 | + pass |
| 187 | + |
184 | 188 | runner = CliRunner() |
185 | | - result = runner.invoke( |
186 | | - flask_cli.run_command, obj=flask_cli.ScriptInfo() |
187 | | - ) |
188 | | - self.assertEqual(result.exit_code, 2) |
| 189 | + with mock.patch.dict( |
| 190 | + sys.modules, |
| 191 | + {**sys.modules, "opentelemetry.instrumentation.wsgi": mock.Mock()}, |
| 192 | + ): |
| 193 | + result = runner.invoke(command) |
| 194 | + self.assertEqual(result.exit_code, 0) |
189 | 195 |
|
190 | 196 | self.assertFalse(self.memory_exporter.get_finished_spans()) |
191 | 197 |
|
|
0 commit comments