Skip to content

Commit bafda34

Browse files
authored
Move augmented_sys_path under tests (#2610)
1 parent 5ef4bf8 commit bafda34

File tree

3 files changed

+26
-28
lines changed

3 files changed

+26
-28
lines changed

astroid/util.py

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@
55

66
from __future__ import annotations
77

8-
import contextlib
9-
import sys
108
import warnings
11-
from collections.abc import Iterator, Sequence
129
from typing import TYPE_CHECKING, Any, Final, Literal
1310

1411
from astroid.exceptions import InferenceError
@@ -160,26 +157,3 @@ def safe_infer(
160157
return None # there is some kind of ambiguity
161158
except StopIteration:
162159
return value
163-
164-
165-
def _augment_sys_path(additional_paths: Sequence[str]) -> list[str]:
166-
original = list(sys.path)
167-
changes = []
168-
seen = set()
169-
for additional_path in additional_paths:
170-
if additional_path not in seen:
171-
changes.append(additional_path)
172-
seen.add(additional_path)
173-
174-
sys.path[:] = changes + sys.path
175-
return original
176-
177-
178-
@contextlib.contextmanager
179-
def augmented_sys_path(additional_paths: Sequence[str]) -> Iterator[None]:
180-
"""Augment 'sys.path' by adding entries from additional_paths."""
181-
original = _augment_sys_path(additional_paths)
182-
try:
183-
yield
184-
finally:
185-
sys.path[:] = original

tests/resources.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
from __future__ import annotations
66

7+
import contextlib
78
import os
89
import sys
10+
from collections.abc import Iterator, Sequence
911
from pathlib import Path
1012

1113
from astroid import builder
@@ -33,3 +35,26 @@ def tearDown(self) -> None:
3335
for key in list(sys.path_importer_cache):
3436
if key.startswith(datadir):
3537
del sys.path_importer_cache[key]
38+
39+
40+
def _augment_sys_path(additional_paths: Sequence[str]) -> list[str]:
41+
original = list(sys.path)
42+
changes = []
43+
seen = set()
44+
for additional_path in additional_paths:
45+
if additional_path not in seen:
46+
changes.append(additional_path)
47+
seen.add(additional_path)
48+
49+
sys.path[:] = changes + sys.path
50+
return original
51+
52+
53+
@contextlib.contextmanager
54+
def augmented_sys_path(additional_paths: Sequence[str]) -> Iterator[None]:
55+
"""Augment 'sys.path' by adding entries from additional_paths."""
56+
original = _augment_sys_path(additional_paths)
57+
try:
58+
yield
59+
finally:
60+
sys.path[:] = original

tests/test_modutils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from astroid import modutils
2323
from astroid.const import PY310_PLUS
2424
from astroid.interpreter._import import spec
25-
from astroid.util import augmented_sys_path
2625

2726
from . import resources
2827

@@ -185,7 +184,7 @@ def test_modpath_from_file_path_order(self) -> None:
185184
should be relative to the subdirectory since additional directory has
186185
higher precedence."""
187186
with tempfile.TemporaryDirectory() as tmp_dir:
188-
with augmented_sys_path([tmp_dir]):
187+
with resources.augmented_sys_path([tmp_dir]):
189188
mod_name = "module"
190189
sub_dirname = "subdir"
191190
sub_dir = tmp_dir + "/" + sub_dirname

0 commit comments

Comments
 (0)