Skip to content

Commit a238a87

Browse files
author
Emanuele Palazzetti
committed
[core] add sitecustomize.py test when -S is used
1 parent 300aa1f commit a238a87

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
from __future__ import print_function
22

3+
import sys
34
from ddtrace import tracer
45
from nose.tools import ok_
56

67

78
if __name__ == '__main__':
9+
# detect if `-S` is used
10+
suppress = len(sys.argv) == 2 and sys.argv[1] is '-S'
11+
if suppress:
12+
ok_('sitecustomize' not in sys.modules)
13+
else:
14+
ok_('sitecustomize' in sys.modules)
15+
16+
# ensure the right `sitecustomize` will be imported
817
import sitecustomize
918
ok_(sitecustomize.CORRECT_IMPORT)
1019
print('Test success')

tests/commands/test_runner.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import subprocess
66
import unittest
77

8+
from ..util import inject_sitecustomize
9+
810

911
class DdtraceRunTest(unittest.TestCase):
1012
def tearDown(self):
@@ -152,20 +154,18 @@ def test_sitecustomize_run(self):
152154
# [Regression test]: ensure users `sitecustomize.py` is properly loaded,
153155
# so that our `bootstrap/sitecustomize.py` doesn't override the one
154156
# defined in users' PYTHONPATH.
155-
#
156-
# Copy the current environment and replace the PYTHONPATH. This is
157-
# required otherwise `ddtrace-run` is not found: when `env` kwarg is
158-
# passed, the environment is entirely replaced
159-
env = os.environ.copy()
160-
sitecustomize = os.path.join(os.path.dirname(__file__), 'bootstrap')
161-
162-
# Add `boostrap` module so that `sitecustomize.py` is at the bottom
163-
# of the PYTHONPATH
164-
python_path = list(sys.path) + [sitecustomize]
165-
env['PYTHONPATH'] = ':'.join(python_path)[1:]
166-
157+
env = inject_sitecustomize('tests/commands/bootstrap')
167158
out = subprocess.check_output(
168159
['ddtrace-run', 'python', 'tests/commands/ddtrace_run_sitecustomize.py'],
169160
env=env,
170161
)
171162
assert out.startswith(b"Test success")
163+
164+
def test_sitecustomize_run_suppressed(self):
165+
# ensure `sitecustomize.py` is not loaded if `-S` is used
166+
env = inject_sitecustomize('tests/commands/bootstrap')
167+
out = subprocess.check_output(
168+
['ddtrace-run', 'python', 'tests/commands/ddtrace_run_sitecustomize.py', '-S'],
169+
env=env,
170+
)
171+
assert out.startswith(b"Test success")

tests/util.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import os
2+
import sys
23
import mock
34
import ddtrace
45

6+
from ddtrace import __file__ as root_file
57
from nose.tools import ok_
68
from contextlib import contextmanager
79

@@ -75,3 +77,26 @@ def set_env(**environ):
7577
finally:
7678
os.environ.clear()
7779
os.environ.update(old_environ)
80+
81+
82+
def inject_sitecustomize(path):
83+
"""Creates a new environment, injecting a ``sitecustomize.py`` module in
84+
the current PYTHONPATH.
85+
86+
:param path: package path containing ``sitecustomize.py`` module, starting
87+
from the ddtrace root folder
88+
:returns: a cloned environment that includes an altered PYTHONPATH with
89+
the given `sitecustomize.py`
90+
"""
91+
root_folder = os.path.dirname(root_file)
92+
# Copy the current environment and replace the PYTHONPATH. This is
93+
# required otherwise `ddtrace` scripts are not found when `env` kwarg is
94+
# passed
95+
env = os.environ.copy()
96+
sitecustomize = os.path.join(root_folder, '..', path)
97+
98+
# Add `boostrap` module so that `sitecustomize.py` is at the bottom
99+
# of the PYTHONPATH
100+
python_path = list(sys.path) + [sitecustomize]
101+
env['PYTHONPATH'] = ':'.join(python_path)[1:]
102+
return env

0 commit comments

Comments
 (0)