Skip to content

Refactor: Move augmented_sys_path into tests directory #2610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading