Skip to content
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

more #7

Merged
merged 1 commit into from
Jan 16, 2024
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
5 changes: 5 additions & 0 deletions phlop/os.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import contextlib
import os
import platform
from pathlib import Path


Expand Down Expand Up @@ -45,3 +46,7 @@ def write_to_file(file, contents, mode="w", skip_if_empty=True):
f.write(contents)
except IOError as e:
raise RuntimeError(f"Failed to write to file {file}: {e}")


def env_sep():
return ";" if any(platform.win32_ver()) else ":"
2 changes: 2 additions & 0 deletions phlop/reflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@


def classes_in_file(file, subclasses_only=None, fail_on_import_error=True):
assert file
module = str(file).replace(os.path.sep, ".")[:-3]
assert module

if subclasses_only is not None and not isinstance(subclasses_only, list):
subclasses_only = [subclasses_only]
Expand Down
12 changes: 12 additions & 0 deletions phlop/sys.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#


import os
import sys
from contextlib import contextmanager

Expand All @@ -19,3 +20,14 @@ def extend_sys_path(paths):
yield
finally:
sys.path = old_path


@contextmanager
def extend_env(**environ):
old_env = dict(os.environ)
os.environ.update(environ)
try:
yield
finally:
os.environ.clear()
os.environ.update(old_env)
Comment on lines +25 to +33
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The extend_env function clears and then restores the entire environment. This could lead to race conditions in a multi-threaded context where other threads might see an empty or partially restored environment.

-        os.environ.clear()
-        os.environ.update(old_env)
+        for var in environ:
+            os.environ.pop(var, None)
+        os.environ.update(old_env)

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
@contextmanager
def extend_env(**environ):
old_env = dict(os.environ)
os.environ.update(environ)
try:
yield
finally:
os.environ.clear()
os.environ.update(old_env)
@contextmanager
def extend_env(**environ):
old_env = dict(os.environ)
os.environ.update(environ)
try:
yield
finally:
for var in environ:
os.environ.pop(var, None)
os.environ.update(old_env)

14 changes: 8 additions & 6 deletions phlop/testing/test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
from pathlib import Path

from phlop.app.cmake import list_tests as get_cmake_tests
from phlop.os import pushd
from phlop.os import env_sep
from phlop.proc import run
from phlop.reflection import classes_in_file
from phlop.sys import extend_sys_path

_LOG_DIR = Path(os.environ.get("PHLOP_LOG_DIR", os.getcwd()))

Expand All @@ -25,6 +26,9 @@ class TestCase:
working_dir: str = field(default_factory=lambda: None)
log_file_path: str = field(default_factory=lambda: None)

def __post_init__(self):
self.cmd = self.cmd.strip()


class TestBatch:
def __init__(self, tests, cores=1):
Expand Down Expand Up @@ -101,13 +105,11 @@ def load_test_cases_in(


def load_test_cases_from_cmake(ctest_test):
ppath = f"{ctest_test.working_dir}:{ctest_test.env.get('PYTHONPATH','')}"
ctest_test.env["PYTHONPATH"] = ppath
with pushd(ctest_test.working_dir):
print("ctest_test", ctest_test.cmd)
ppath = ctest_test.env.get("PYTHONPATH", "")
with extend_sys_path([ctest_test.working_dir] + ppath.split(env_sep())):
pyfile = ctest_test.cmd.split(" ")[-1]
return load_test_cases_in(
classes_in_file(pyfile, unittest.TestCase, fail_on_import_error=False),
classes_in_file(pyfile, unittest.TestCase, fail_on_import_error=True),
Comment on lines +108 to +112
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The extend_sys_path context manager is used with a list that includes ctest_test.working_dir and the split PYTHONPATH. However, the split method is called without considering the possibility of an empty PYTHONPATH, which could lead to an empty string in the list, potentially causing issues.

-    with extend_sys_path([ctest_test.working_dir] + ppath.split(env_sep())):
+    with extend_sys_path([ctest_test.working_dir] + (ppath.split(env_sep()) if ppath else [])):

Committable suggestion

IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
ppath = ctest_test.env.get("PYTHONPATH", "")
with extend_sys_path([ctest_test.working_dir] + ppath.split(env_sep())):
pyfile = ctest_test.cmd.split(" ")[-1]
return load_test_cases_in(
classes_in_file(pyfile, unittest.TestCase, fail_on_import_error=False),
classes_in_file(pyfile, unittest.TestCase, fail_on_import_error=True),
ppath = ctest_test.env.get("PYTHONPATH", "")
with extend_sys_path([ctest_test.working_dir] + (ppath.split(env_sep()) if ppath else [])):
pyfile = ctest_test.cmd.split(" ")[-1]
return load_test_cases_in(
classes_in_file(pyfile, unittest.TestCase, fail_on_import_error=True),

env=ctest_test.env,
working_dir=ctest_test.working_dir,
log_file_path=_LOG_DIR
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[project]
name = "phlop"
version = "0.0.7"
version = "0.0.8"

dependencies = [

]

description = "stuff and things"
readme = "README.md"
requires-python = ">=3.7"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name="phlop",
version="0.0.7",
version="0.0.8",
cmdclass={},
classifiers=[],
include_package_data=True,
Expand Down