Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
template using Jinja2.
"""

import os
import sys
from pathlib import Path
import subprocess
import shutil

import click
import jinja2

from execution_testing.config.docs import DocsConfig
from execution_testing.config import AppConfig
from execution_testing.forks import get_development_forks, get_forks

from ....input import input_select, input_text
Expand All @@ -28,6 +30,26 @@
)


def _ruff_cmd() -> list[str]:
r = shutil.which("ruff")
return [r] if r else [sys.executable, "-m", "ruff"]


def _project_pyproject() -> Path:
# ROOT_DIR -> packages/testing/src/execution_testing
# pyproject -> packages/testing/pyproject.toml
return (AppConfig().ROOT_DIR.parent.parent / "pyproject.toml").resolve()


def _ruff_format_file(target: Path) -> None:
cfg = _project_pyproject()
cmd = _ruff_cmd() + ["format"]
if cfg.exists():
cmd += ["--config", str(cfg)]
cmd += [str(target)]
subprocess.run(cmd, check=True)


def exit_now() -> None:
"""Interrupt execution instantly via ctrl+C."""
print("Ctrl+C detected, exiting..")
Expand Down Expand Up @@ -146,7 +168,7 @@ def test() -> None:
)
sys.exit(1)

os.makedirs(directory_path, exist_ok=True)
directory_path.mkdir(parents=True, exist_ok=True)

template = template_env.get_template(f"{test_type.lower()}_test.py.j2")
rendered_template = template.render(
Expand All @@ -159,6 +181,9 @@ def test() -> None:
with open(module_path, "w") as file:
file.write(rendered_template)

_ruff_format_file(module_path)
_ruff_format_file(module_path)

click.echo(
click.style(
f"\n 🎉 Success! Test file created at: {module_path}",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from __future__ import annotations

from pathlib import Path
from typing import Any
import importlib
import sys

# Import the module object explicitly to avoid grabbing the Click command
make_test = importlib.import_module(
"execution_testing.cli.eest.make.commands.test"
)


def test_ruff_called_with_project_config(
monkeypatch: Any, tmp_path: Path
) -> None:
called: dict[str, list[str]] = {}

# Force a predictable Ruff invocation
monkeypatch.setattr(
make_test, "_ruff_cmd", lambda: [sys.executable, "-m", "ruff"]
)

# Capture the subprocess invocation
def fake_run(cmd: list[str], check: bool = True) -> int:
called["cmd"] = cmd
return 0

monkeypatch.setattr(make_test.subprocess, "run", fake_run)

# Point to a dummy pyproject.toml
dummy_cfg = tmp_path / "pyproject.toml"
dummy_cfg.write_text("")

monkeypatch.setattr(make_test, "_project_pyproject", lambda: dummy_cfg)

target = tmp_path / "sample.py"
target.write_text("x='1'\n")

make_test._ruff_format_file(target)

cmd = called["cmd"]
assert cmd[:3] == [sys.executable, "-m", "ruff"]
assert "format" in cmd
idx = cmd.index("--config")
assert cmd[idx + 1] == str(dummy_cfg)
assert cmd[-1] == str(target)