| 1 |
"""Audit F13: the CLI entry point must set telemetry-off env vars before any |
| 2 |
downstream imports. We test this by spawning a fresh subprocess (so we get a |
| 3 |
clean env) and asserting the vars are set after `dlm --version` returns. |
| 4 |
""" |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
import subprocess |
| 9 |
import sys |
| 10 |
import textwrap |
| 11 |
|
| 12 |
|
| 13 |
def test_cli_entry_forces_telemetry_off_env_vars() -> None: |
| 14 |
probe = textwrap.dedent( |
| 15 |
"""\ |
| 16 |
import os |
| 17 |
# Intentionally unset — we want to see if `import dlm.cli.app` sets them. |
| 18 |
for v in ("HF_HUB_DISABLE_TELEMETRY", "DO_NOT_TRACK", |
| 19 |
"TRANSFORMERS_NO_ADVISORY_WARNINGS"): |
| 20 |
os.environ.pop(v, None) |
| 21 |
import dlm.cli.app # noqa: F401 |
| 22 |
assert os.environ["HF_HUB_DISABLE_TELEMETRY"] == "1" |
| 23 |
assert os.environ["DO_NOT_TRACK"] == "1" |
| 24 |
assert os.environ["TRANSFORMERS_NO_ADVISORY_WARNINGS"] == "1" |
| 25 |
print("ok") |
| 26 |
""" |
| 27 |
) |
| 28 |
result = subprocess.run( |
| 29 |
[sys.executable, "-c", probe], |
| 30 |
check=False, |
| 31 |
capture_output=True, |
| 32 |
text=True, |
| 33 |
timeout=10, |
| 34 |
) |
| 35 |
assert result.returncode == 0, result.stderr |
| 36 |
assert result.stdout.strip() == "ok" |
| 37 |
|
| 38 |
|
| 39 |
def test_user_preset_telemetry_vars_are_respected() -> None: |
| 40 |
"""If a user has explicitly set one of these vars to "0", we must NOT |
| 41 |
overwrite them — `setdefault` semantics. |
| 42 |
""" |
| 43 |
probe = textwrap.dedent( |
| 44 |
"""\ |
| 45 |
import os |
| 46 |
os.environ["DO_NOT_TRACK"] = "0" |
| 47 |
import dlm.cli.app # noqa: F401 |
| 48 |
assert os.environ["DO_NOT_TRACK"] == "0" |
| 49 |
print("ok") |
| 50 |
""" |
| 51 |
) |
| 52 |
result = subprocess.run( |
| 53 |
[sys.executable, "-c", probe], |
| 54 |
check=False, |
| 55 |
capture_output=True, |
| 56 |
text=True, |
| 57 |
timeout=10, |
| 58 |
) |
| 59 |
assert result.returncode == 0, result.stderr |
| 60 |
assert result.stdout.strip() == "ok" |