Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make sure filedir is at the beginning of PYTHONPATH
  • Loading branch information
ocelotl committed Apr 29, 2020
commit 80d716738bb2f8da4d7fc5cc7d9323c482191bf2
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@

def run() -> None:

python_path = environ.get("PYTHONPATH", "")
python_path = environ.get("PYTHONPATH", [])

if python_path:
python_path = python_path.split(pathsep)

filedir_path = dirname(abspath(__file__))

if filedir_path not in python_path:
environ["PYTHONPATH"] = pathsep.join([filedir_path, python_path])
if filedir_path in python_path:
python_path.remove(filedir_path)

environ["PYTHONPATH"] = pathsep.join([filedir_path, *python_path])

executable = which(argv[1])

Expand Down
42 changes: 42 additions & 0 deletions opentelemetry-auto-instrumentation/tests/test_instrumentor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
# type: ignore

from logging import WARNING
from os import environ
from os.path import abspath, dirname, pathsep
from unittest import TestCase
from unittest.mock import patch

from opentelemetry.auto_instrumentation import auto_instrumentation
from opentelemetry.auto_instrumentation.instrumentor import BaseInstrumentor


Expand Down Expand Up @@ -44,3 +48,41 @@ def test_protect(self):

def test_singleton(self):
self.assertIs(self.Instrumentor(), self.Instrumentor())


class TestRun(TestCase):
auto_instrumentation_path = dirname(abspath(auto_instrumentation.__file__))

@patch.dict("os.environ", {"PYTHONPATH": ""})
@patch("opentelemetry.auto_instrumentation.auto_instrumentation.argv")
@patch("opentelemetry.auto_instrumentation.auto_instrumentation.execl")
def test_run_empty(
self, mock_execl, mock_argv
): # pylint: disable=unused-argument
auto_instrumentation.run()
assert environ["PYTHONPATH"] == self.auto_instrumentation_path

@patch.dict("os.environ", {"PYTHONPATH": "abc"})
@patch("opentelemetry.auto_instrumentation.auto_instrumentation.argv")
@patch("opentelemetry.auto_instrumentation.auto_instrumentation.execl")
def test_run_non_empty(
self, mock_execl, mock_argv
): # pylint: disable=unused-argument
auto_instrumentation.run()
assert environ["PYTHONPATH"] == pathsep.join(
[self.auto_instrumentation_path, "abc"]
)

@patch.dict(
"os.environ",
{"PYTHONPATH": pathsep.join(["abc", auto_instrumentation_path])},
)
@patch("opentelemetry.auto_instrumentation.auto_instrumentation.argv")
@patch("opentelemetry.auto_instrumentation.auto_instrumentation.execl")
def test_run_after_path(
self, mock_execl, mock_argv
): # pylint: disable=unused-argument
auto_instrumentation.run()
assert environ["PYTHONPATH"] == pathsep.join(
[self.auto_instrumentation_path, "abc"]
)