tests: fix two environment-dependent failures found by the wider CI matrix

Both surfaced only once the staging matrix ran these suites on runners the
org queue does not cover. Neither is a product defect; both are tests
asserting something their environment cannot supply.

test_unsloth_pip_shim.py::test_forwarded_install_carries_protected_constraints
reads the ambient environment through importlib.metadata.distributions.
_protected_constraints_file correctly returns None when no protected
package is installed, so no --constraint pair is appended, and the test
then indexed execd[-2] unconditionally:

    E   IndexError: list index out of range
    1 failed, 115 passed, 2 skipped

It failed on all four docker-test legs and in any bare venv, and passed
upstream only because studio-backend-ci installs torch and transformers
first. Its own sibling at line 93 already guards with len(execd) >= 2.

Guarding the index alone would have left the test measuring whatever
happened to be installed, so distributions() is now stubbed and the test
asserts the real contract deterministically. A second case covers the
other half of that contract, which is what a bare venv actually hits: with
nothing protected installed the install must still be forwarded, just
without the pair.

test_select_cuda_jit_tools.sh stages libnvrtc as symlinks and asserts
through readlink, because retargeting that symlink is what the function
under test does. git-bash copies instead of symlinking unless
MSYS=winsymlinks:nativestrict and the user is elevated, so readlink comes
back empty and all 14 assertions fail on both Windows runners, taking
tests/run_all.sh down with them for any Windows contributor. The code only
ever runs inside a Linux container, so probe for real symlink support and
skip when it is absent rather than assert something the filesystem cannot
represent.

Verified: the shim suite is 87 passed / 2 skipped in both a bare venv and
a full one; the shell suite still reports 14 passed on Linux and skips
with exit 0 under a simulated no-symlink filesystem.
This commit is contained in:
Daniel Han 2026-07-26 17:34:13 +00:00
commit 4123190b82
2 changed files with 57 additions and 2 deletions

View file

@ -511,9 +511,36 @@ def _raw_execd(shim, tool, args):
return exc.argv[exc.argv.index("install") + 1 :]
def test_forwarded_install_carries_protected_constraints(shim):
class _FakeDist:
"""Minimal stand-in for an importlib.metadata Distribution."""
def __init__(self, name, version):
self.metadata = {"Name": name}
self.version = version
def _fake_distributions(monkeypatch, *pairs):
"""Pin what _protected_constraints_file sees as INSTALLED.
It reads the ambient environment via importlib.metadata.distributions, so
without this the outcome depends on whatever happens to be in the venv:
with no protected package installed it correctly returns None (see its
docstring) and no --constraint pair is appended. That made the assertion
below environment-dependent, and it surfaced as an IndexError on execd[-2]
rather than a readable failure. The shim imports the symbol inside the
function, so patch it at its source.
"""
monkeypatch.setattr(
"importlib.metadata.distributions",
lambda: [_FakeDist(n, v) for n, v in pairs],
)
def test_forwarded_install_carries_protected_constraints(shim, monkeypatch):
_fake_distributions(monkeypatch, ("transformers", "5.14.1"), ("trl", "0.24.0"))
execd = _raw_execd(shim, "pip", ["snac"])
assert execd is not None and execd[-2] == "--constraint", execd
assert execd is not None, "an unprotected target must still be forwarded"
assert len(execd) >= 2 and execd[-2] == "--constraint", execd
pins = Path(execd[-1]).read_text(encoding = "utf-8").strip().splitlines()
assert pins, "constraints file must pin the installed protected packages"
assert all("==" in pin for pin in pins), pins
@ -524,6 +551,16 @@ def test_forwarded_install_carries_protected_constraints(shim):
), names
def test_forwarded_install_without_protected_packages_has_no_constraints(shim, monkeypatch):
# The other half of the contract: with nothing protected installed there is
# nothing to pin, so the install must still be forwarded, just bare. This is
# the case a bare venv actually hits.
_fake_distributions(monkeypatch, ("snac", "1.2.1"))
execd = _raw_execd(shim, "pip", ["snac"])
assert execd is not None, "the install must still be forwarded"
assert "--constraint" not in execd, execd
def test_noop_install_gets_no_constraints(shim):
# A cell whose only target is protected still no-ops (no exec at all).
execd = _raw_execd(shim, "pip", ["torch"])