Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 04447c8

Browse files
Capture pytest conftest warnings (#1008)
# Pull Request ## Title Capture pytest conftest warnings ______________________________________________________________________ ## Description Followup to #1007. Copilot suggestions were correct. The `logger.warning()` messages generated in `__init__.py` and `conftest.py` are captured, but not emitted by default when *per-test* captures are done. This switches those calls to `UserWarnings` so they are always captured. This was the only location I could find for those. Additionally, I adjust some checks for the `docker.sock` on Windows to avoid some warnings. ______________________________________________________________________ ## Type of Change - 🛠️ Bug fix - 🧪 Tests ______________________________________________________________________ ## Testing Manual. ______________________________________________________________________ --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 0152cb6 commit 04447c8

2 files changed

Lines changed: 32 additions & 18 deletions

File tree

mlos_bench/mlos_bench/tests/__init__.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
import shutil
1414
import socket
1515
import stat
16+
import sys
1617
from datetime import tzinfo
17-
from logging import warning
1818
from subprocess import run
19+
from warnings import warn
1920

2021
import pytest
2122
import pytz
@@ -43,7 +44,12 @@
4344
DOCKER = shutil.which("docker")
4445
if DOCKER:
4546
# Gathering info about Github CI docker.sock permissions for debugging purposes.
46-
DOCKER_SOCK_PATH = "/var/run/docker.sock"
47+
DOCKER_SOCK_PATH: str
48+
if sys.platform == "win32":
49+
DOCKER_SOCK_PATH = "//./pipe/docker_engine"
50+
else:
51+
DOCKER_SOCK_PATH = "/var/run/docker.sock"
52+
4753
mode: str | None = None
4854
uid: int | None = None
4955
gid: int | None = None
@@ -56,13 +62,16 @@
5662
uid = st.st_uid
5763
gid = st.st_gid
5864
except Exception as e: # pylint: disable=broad-except
59-
warning(f"Could not stat {DOCKER_SOCK_PATH}: {e}")
65+
warn(f"Could not stat {DOCKER_SOCK_PATH}: {e}", UserWarning)
6066
try:
61-
current_uid = os.getuid()
62-
current_gid = os.getgid()
63-
gids = os.getgroups()
67+
if sys.platform != "win32":
68+
current_uid = os.getuid()
69+
current_gid = os.getgid()
70+
gids = os.getgroups()
71+
if not os.access(DOCKER_SOCK_PATH, os.W_OK):
72+
warn(f"Docker socket {DOCKER_SOCK_PATH} is not writable.", UserWarning)
6473
except Exception as e: # pylint: disable=broad-except
65-
warning(f"Could not get current user info: {e}")
74+
warn(f"Could not get current user info: {e}", UserWarning)
6675

6776
cmd = run(
6877
"docker builder inspect default || docker buildx inspect default",
@@ -76,16 +85,17 @@
7685
line for line in stdout.splitlines() if "Platform" in line and "linux" in line
7786
):
7887
DOCKER = None
79-
warning(
88+
warn(
8089
"Docker is available but missing buildx support for targeting linux platform:\n"
8190
+ f"stdout:\n{stdout}\n"
8291
+ f"stderr:\n{stderr}\n"
8392
+ f"sock_path: {DOCKER_SOCK_PATH} sock mode: {mode} sock uid: {uid} gid: {gid}\n"
84-
+ f"current_uid: {current_uid} groups: {gids}\n"
93+
+ f"current_uid: {current_uid} groups: {gids}\n",
94+
UserWarning,
8595
)
8696

8797
if not DOCKER:
88-
warning("Docker is not available on this system. Some tests will be skipped.")
98+
warn("Docker is not available on this system. Some tests will be skipped.", UserWarning)
8999

90100
# A decorator for tests that require docker.
91101
# Use with @requires_docker above a test_...() function.
@@ -98,7 +108,7 @@
98108
# Use with @requires_ssh above a test_...() function.
99109
SSH = shutil.which("ssh")
100110
if not SSH:
101-
warning("ssh is not available on this system. Some tests will be skipped.")
111+
warn("ssh is not available on this system. Some tests will be skipped.", UserWarning)
102112
requires_ssh = pytest.mark.skipif(not SSH, reason="ssh is not available on this system.")
103113

104114
# A common seed to use to avoid tracking down race conditions and intermingling
@@ -223,14 +233,16 @@ def are_dir_trees_equal(dir1: str, dir2: str) -> bool:
223233
or len(dirs_cmp.right_only) > 0
224234
or len(dirs_cmp.funny_files) > 0
225235
):
226-
warning(
227-
f"Found differences in dir trees {dir1}, {dir2}:\n"
228-
f"{dirs_cmp.diff_files}\n{dirs_cmp.funny_files}"
236+
warn(
237+
UserWarning(
238+
f"Found differences in dir trees {dir1}, {dir2}:\n"
239+
f"{dirs_cmp.diff_files}\n{dirs_cmp.funny_files}"
240+
)
229241
)
230242
return False
231243
(_, mismatch, errors) = filecmp.cmpfiles(dir1, dir2, dirs_cmp.common_files, shallow=False)
232244
if len(mismatch) > 0 or len(errors) > 0:
233-
warning(f"Found differences in files:\n{mismatch}\n{errors}")
245+
warn(f"Found differences in files:\n{mismatch}\n{errors}", UserWarning)
234246
return False
235247
for common_dir in dirs_cmp.common_dirs:
236248
new_dir1 = os.path.join(dir1, common_dir)

mlos_viz/mlos_viz/tests/conftest.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
import os
88
import sys
99
from glob import glob
10-
from logging import warning
1110
from pathlib import Path
11+
from warnings import warn
1212

1313
from mlos_bench.tests import tunable_groups_fixtures
1414
from mlos_bench.tests.storage.sql import fixtures as sql_storage_fixtures
@@ -51,6 +51,8 @@
5151
)
5252
).parent
5353
)
54-
warning(f"""Setting {env_var} to {os.environ[env_var]}""")
54+
warn(f"""Setting {env_var} to {os.environ[env_var]}""", UserWarning)
5555
except StopIteration:
56-
warning(f"{env_var} not found, some Tcl/Tk functionality may be limited.")
56+
warn(
57+
UserWarning(f"{env_var} not found, some Tcl/Tk functionality may be limited.")
58+
)

0 commit comments

Comments
 (0)