Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion scripts/docs_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@
DIRENV_ENV_MARKER = "DIGITAL_ASSET_DOCS_DIRENV"


def already_reentered_direnv() -> bool:
return os.environ.get(DIRENV_ENV_MARKER) == "1"


def repo_direnv_command(repo_root: Path, *args: str) -> list[str]:
if already_reentered_direnv():
if args and args[0] == "x2mdx":
return ["python3", "-m", "x2mdx.cli", *args[1:]]
return list(args)

if args and args[0] == "x2mdx":
return [
"direnv",
Expand All @@ -25,7 +34,7 @@ def repo_direnv_command(repo_root: Path, *args: str) -> list[str]:


def ensure_repo_direnv(*, repo_root: Path, script_path: Path, argv: Sequence[str]) -> None:
if os.environ.get(DIRENV_ENV_MARKER) == "1":
if already_reentered_direnv():
return

env = os.environ.copy()
Expand Down
34 changes: 34 additions & 0 deletions tests/test_docs_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from __future__ import annotations

import importlib
from pathlib import Path


def load_docs_env():
return importlib.import_module("scripts.docs_env")


def test_repo_direnv_command_runs_x2mdx_directly_after_direnv_reentry(monkeypatch) -> None:
docs_env = load_docs_env()
monkeypatch.setenv(docs_env.DIRENV_ENV_MARKER, "1")

assert docs_env.repo_direnv_command(Path("/repo"), "x2mdx", "openrpc") == [
"python3",
"-m",
"x2mdx.cli",
"openrpc",
]


def test_repo_direnv_command_uses_direnv_even_inside_nix_shell(monkeypatch) -> None:
docs_env = load_docs_env()
monkeypatch.delenv(docs_env.DIRENV_ENV_MARKER, raising=False)
monkeypatch.setenv("IN_NIX_SHELL", "pure")

assert docs_env.repo_direnv_command(Path("/repo"), "python3", "script.py") == [
"direnv",
"exec",
"/repo",
"python3",
"script.py",
]