Skip to content

Commit 8e3ed8a

Browse files
committed
Add tests against environment pollution
1 parent a7a9f10 commit 8e3ed8a

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import json
2+
import os
3+
import sys
4+
from subprocess import run
5+
from unittest import mock
6+
7+
import pytest
8+
9+
from jupyterlab_code_formatter.formatters import SERVER_FORMATTERS
10+
11+
12+
def test_env_pollution_on_import():
13+
# should not pollute environment on import
14+
code = "; ".join(
15+
[
16+
"from jupyterlab_code_formatter import formatters",
17+
"import json",
18+
"import os",
19+
"assert formatters",
20+
"print(json.dumps(os.environ.copy()))",
21+
]
22+
)
23+
result = run([sys.executable, "-c", f"{code}"], capture_output=True, text=True, check=True, env={})
24+
environ = json.loads(result.stdout)
25+
assert set(environ.keys()) - {"LC_CTYPE"} == set()
26+
27+
28+
@pytest.mark.parametrize("name", SERVER_FORMATTERS)
29+
def test_env_pollution_on_importable_check(name):
30+
formatter = SERVER_FORMATTERS[name]
31+
# should not pollute environment on `importable` check
32+
with mock.patch.dict(os.environ, {}, clear=True):
33+
# invoke the property getter
34+
is_importable = formatter.importable
35+
# the environment should have no extra keys
36+
assert set(os.environ.keys()) == set()
37+
if not is_importable:
38+
pytest.skip(f"{name} formatter was not importable, the test may yield false negatives")

0 commit comments

Comments
 (0)