|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +import sys |
| 5 | + |
| 6 | + |
| 7 | +EXPERIMENT_ROOT = Path(__file__).resolve().parents[1] |
| 8 | +REPO_ROOT = EXPERIMENT_ROOT.parents[1] |
| 9 | +sys.path.insert(0, str(EXPERIMENT_ROOT / "src")) |
| 10 | + |
| 11 | +from rg_nanogpt_one_head.config import DEFAULT_ROOT, roots |
| 12 | + |
| 13 | + |
| 14 | +ROOT_ENV_VARS = ( |
| 15 | + "RG_NANOGPT_ONE_HEAD_ROOT", |
| 16 | + "RG_NANOGPT_ONE_HEAD_DATA_ROOT", |
| 17 | + "RG_NANOGPT_ONE_HEAD_RESULTS_ROOT", |
| 18 | + "RG_NANOGPT_ONE_HEAD_PLOTS_ROOT", |
| 19 | +) |
| 20 | +FORBIDDEN_HOME_TOKENS = ( |
| 21 | + "$HOME", |
| 22 | + "${HOME}", |
| 23 | + "Path.home(", |
| 24 | + ".expanduser(", |
| 25 | + "/home/", |
| 26 | + "~/", |
| 27 | +) |
| 28 | + |
| 29 | + |
| 30 | +def test_default_root_is_tmp(monkeypatch): |
| 31 | + for name in ROOT_ENV_VARS: |
| 32 | + monkeypatch.delenv(name, raising=False) |
| 33 | + |
| 34 | + assert DEFAULT_ROOT == Path("/tmp/rg-nanogpt-one-head") |
| 35 | + resolved = roots() |
| 36 | + assert resolved == { |
| 37 | + "root": Path("/tmp/rg-nanogpt-one-head"), |
| 38 | + "data": Path("/tmp/rg-nanogpt-one-head/data"), |
| 39 | + "results": Path("/tmp/rg-nanogpt-one-head/results"), |
| 40 | + "plots": Path("/tmp/rg-nanogpt-one-head/plots"), |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | +def test_nanogpt_sources_docs_and_notebooks_do_not_reference_home(): |
| 45 | + paths = [EXPERIMENT_ROOT / "README.md"] |
| 46 | + paths.extend((EXPERIMENT_ROOT / "src").rglob("*.py")) |
| 47 | + paths.extend((EXPERIMENT_ROOT / "notebooks").glob("*.ipynb")) |
| 48 | + paths.extend((EXPERIMENT_ROOT / "configs").glob("*.yaml")) |
| 49 | + |
| 50 | + violations = [] |
| 51 | + for path in paths: |
| 52 | + text = path.read_text(encoding="utf-8") |
| 53 | + for token in FORBIDDEN_HOME_TOKENS: |
| 54 | + if token in text: |
| 55 | + violations.append(f"{path.relative_to(REPO_ROOT)}: {token}") |
| 56 | + |
| 57 | + assert not violations, "home-directory references found:\n" + "\n".join(violations) |
| 58 | + |
| 59 | + |
| 60 | +def test_repo_shell_scripts_never_reference_home(): |
| 61 | + violations = [] |
| 62 | + for path in REPO_ROOT.rglob("*.sh"): |
| 63 | + text = path.read_text(encoding="utf-8") |
| 64 | + for token in FORBIDDEN_HOME_TOKENS: |
| 65 | + if token in text: |
| 66 | + violations.append(f"{path.relative_to(REPO_ROOT)}: {token}") |
| 67 | + |
| 68 | + assert not violations, "home-directory references found in shell scripts:\n" + "\n".join(violations) |
| 69 | + |
| 70 | + |
| 71 | +def test_removed_nanogpt_wrapper_scripts_do_not_return(): |
| 72 | + assert not (EXPERIMENT_ROOT / "scripts").exists() |
0 commit comments