Skip to content

Commit

Permalink
Move augmented_sys_path under tests (#2610)
Browse files Browse the repository at this point in the history
  • Loading branch information
akamat10 authored Oct 14, 2024
1 parent 5ef4bf8 commit bafda34
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 28 deletions.
26 changes: 0 additions & 26 deletions astroid/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@

from __future__ import annotations

import contextlib
import sys
import warnings
from collections.abc import Iterator, Sequence
from typing import TYPE_CHECKING, Any, Final, Literal

from astroid.exceptions import InferenceError
Expand Down Expand Up @@ -160,26 +157,3 @@ def safe_infer(
return None # there is some kind of ambiguity
except StopIteration:
return value


def _augment_sys_path(additional_paths: Sequence[str]) -> list[str]:
original = list(sys.path)
changes = []
seen = set()
for additional_path in additional_paths:
if additional_path not in seen:
changes.append(additional_path)
seen.add(additional_path)

sys.path[:] = changes + sys.path
return original


@contextlib.contextmanager
def augmented_sys_path(additional_paths: Sequence[str]) -> Iterator[None]:
"""Augment 'sys.path' by adding entries from additional_paths."""
original = _augment_sys_path(additional_paths)
try:
yield
finally:
sys.path[:] = original
25 changes: 25 additions & 0 deletions tests/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

from __future__ import annotations

import contextlib
import os
import sys
from collections.abc import Iterator, Sequence
from pathlib import Path

from astroid import builder
Expand Down Expand Up @@ -33,3 +35,26 @@ def tearDown(self) -> None:
for key in list(sys.path_importer_cache):
if key.startswith(datadir):
del sys.path_importer_cache[key]


def _augment_sys_path(additional_paths: Sequence[str]) -> list[str]:
original = list(sys.path)
changes = []
seen = set()
for additional_path in additional_paths:
if additional_path not in seen:
changes.append(additional_path)
seen.add(additional_path)

sys.path[:] = changes + sys.path
return original


@contextlib.contextmanager
def augmented_sys_path(additional_paths: Sequence[str]) -> Iterator[None]:
"""Augment 'sys.path' by adding entries from additional_paths."""
original = _augment_sys_path(additional_paths)
try:
yield
finally:
sys.path[:] = original
3 changes: 1 addition & 2 deletions tests/test_modutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from astroid import modutils
from astroid.const import PY310_PLUS
from astroid.interpreter._import import spec
from astroid.util import augmented_sys_path

from . import resources

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

0 comments on commit bafda34

Please sign in to comment.