This commit is contained in:
@@ -67,19 +67,27 @@ def test_import_logging_once_per_module():
|
||||
import random
|
||||
""")
|
||||
|
||||
# Write the script to a temp file and run it
|
||||
# Write the script to a temp file
|
||||
with tempfile.NamedTemporaryFile("w", suffix=".py", delete=False) as tmp:
|
||||
tmp.write(code)
|
||||
tmp_path = tmp.name
|
||||
|
||||
result = subprocess.run([sys.executable, tmp_path], capture_output=True, text=True)
|
||||
# Define the environment with correct PYTHONPATH
|
||||
env = os.environ.copy()
|
||||
root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
|
||||
env["PYTHONPATH"] = root_path + os.pathsep + env.get("PYTHONPATH", "")
|
||||
|
||||
stderr = result.stderr
|
||||
# Run the script
|
||||
result = subprocess.run([sys.executable, tmp_path], capture_output=True, text=True, env=env)
|
||||
|
||||
# Clean up temp file
|
||||
os.remove(tmp_path)
|
||||
|
||||
# Assert that each module is logged only once
|
||||
for mod in ["json", "math", "io", "random"]:
|
||||
assert stderr.count(f"importing: {mod}") == 1, f"Module '{mod}' logged multiple times"
|
||||
# Check for errors
|
||||
assert result.returncode == 0, f"Subprocess failed:\n{result.stderr}"
|
||||
|
||||
stderr = result.stderr
|
||||
|
||||
# Assert that each top-level import is logged only once
|
||||
for mod in ["json", "math", "io", "random"]:
|
||||
assert stderr.count(f"importing: {mod}") == 1, f"Module '{mod}' logged multiple times or missing"
|
||||
|
||||
Reference in New Issue
Block a user