Download tests/test_webgpu_installer.py from PYTHAI/drAIML: direct link, hf CLI and curl.
- Browser
- Download file 3.11 kB
-
https://huggingface.co/spaces/PYTHAI/drAIML/resolve/main/tests/test_webgpu_installer.py
- Command line
-
hf download hf://spaces/PYTHAI/drAIML/tests/test_webgpu_installer.py
-
curl -L -o test_webgpu_installer.py https://huggingface.co/spaces/PYTHAI/drAIML/resolve/main/tests/test_webgpu_installer.py
3.11 kB
| """drAIML WebGPU installer: adds the flags, is idempotent, skips a running browser, and undo removes only its own.""" | |
| import json | |
| import os | |
| import subprocess | |
| from pathlib import Path | |
| SH = Path(__file__).resolve().parents[1] / "webgpu" / "installer" / "webgpu-enable.sh" | |
| def run(*a): | |
| return subprocess.run(["bash", str(SH), *a], capture_output=True, text=True) | |
| def labs(d): | |
| return json.loads((d / "Local State").read_text())["browser"]["enabled_labs_experiments"] | |
| def test_chromium_flags_round_trip(tmp_path): | |
| (tmp_path / "Local State").write_text(json.dumps({"browser": {"enabled_labs_experiments": ["smooth-scrolling@2"]}, "x": 1})) | |
| assert "would add" in run("--dir", str(tmp_path), "--dry-run").stdout and labs(tmp_path) == ["smooth-scrolling@2"] | |
| assert run("--dir", str(tmp_path)).returncode == 0 | |
| added = labs(tmp_path) | |
| assert added[0] == "smooth-scrolling@2" and "enable-unsafe-webgpu" in added | |
| assert "unchanged" in run("--dir", str(tmp_path)).stdout and labs(tmp_path) == added | |
| assert list(tmp_path.glob("Local State.bak-webgpu-*")) | |
| run("--dir", str(tmp_path), "--blocklist") # a second run adding one more flag: undo must remove both | |
| assert "ignore-gpu-blocklist" in labs(tmp_path) | |
| run("--dir", str(tmp_path), "--undo") | |
| assert labs(tmp_path) == ["smooth-scrolling@2"] and json.loads((tmp_path / "Local State").read_text())["x"] == 1 | |
| def test_running_browser_is_skipped(tmp_path): | |
| (tmp_path / "Local State").write_text("{}") | |
| os.symlink(f"{os.uname().nodename}-{os.getpid()}", tmp_path / "SingletonLock") # this test process: alive | |
| r = run("--dir", str(tmp_path)) | |
| assert r.returncode == 3 and "running" in r.stdout and json.loads((tmp_path / "Local State").read_text()) == {} | |
| ff = tmp_path / "ff"; ff.mkdir(); (ff / "prefs.js").write_text("") | |
| os.symlink(f"127.0.1.1:+{os.getpid()}", ff / "lock") | |
| assert run("--dir", str(ff)).returncode == 3 and not (ff / "user.js").exists() | |
| def test_firefox_profile(tmp_path): | |
| os.symlink("127.0.1.1:+999999999", tmp_path / "lock") # Firefox's lock format; a pid that is not running | |
| (tmp_path / "prefs.js").write_text('user_pref("other", 1);\n') | |
| run("--dir", str(tmp_path)) | |
| assert 'user_pref("dom.webgpu.enabled", true);' in (tmp_path / "user.js").read_text() | |
| with open(tmp_path / "prefs.js", "a") as f: # Firefox copies the value into prefs.js on exit | |
| f.write('user_pref("dom.webgpu.enabled", true);\n') | |
| run("--dir", str(tmp_path), "--undo") | |
| assert "dom.webgpu" not in (tmp_path / "user.js").read_text() and "dom.webgpu" not in (tmp_path / "prefs.js").read_text() | |
| assert 'user_pref("other", 1);' in (tmp_path / "prefs.js").read_text() | |
| def test_firefox_undo_keeps_a_pref_that_was_yours(tmp_path): | |
| (tmp_path / "prefs.js").write_text('user_pref("dom.webgpu.enabled", true);\n') | |
| run("--dir", str(tmp_path)) | |
| run("--dir", str(tmp_path), "--undo") | |
| assert "dom.webgpu" not in (tmp_path / "user.js").read_text() | |
| assert 'user_pref("dom.webgpu.enabled", true);' in (tmp_path / "prefs.js").read_text() | |