Skip to content

Commit

Permalink
Work around windows "python" lookup when in venv - try 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
jsirois committed Jan 5, 2025
1 parent 4792eb0 commit 9d31a91
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 29 deletions.
12 changes: 6 additions & 6 deletions dev_cmd/invoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ async def _invoke_command(
env.update(command.extra_env)
if USE_COLOR and not any(color_env in env for color_env in ("PYTHON_COLORS", "NO_COLOR")):
env.setdefault("FORCE_COLOR", "1")
# if "windows" == platform.system().lower():
# existing_path = env.get("PATH", os.defpath).split(os.pathsep)
# venv_bin_dir = os.path.join(os.environ["VIRTUAL_ENV"], "Scripts")
# if venv_bin_dir not in existing_path:
# env["PATH"] = os.pathsep.join([venv_bin_dir] + existing_path)

if args[0].endswith(".py"):
args.insert(0, sys.executable)
elif "python" == args[0]:
args[0] = sys.executable

process = await asyncio.create_subprocess_exec(
sys.executable if "python" == args[0] else args[0],
args[0],
*args[1:],
cwd=command.cwd,
env=env,
Expand Down
85 changes: 62 additions & 23 deletions tests/test_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,29 @@ def project_dir() -> PurePath:


@pytest.fixture
def pyproject_toml(tmp_path: Path, monkeypatch: MonkeyPatch) -> Path:
def pyproject_toml(monkeypatch: MonkeyPatch, tmp_path: Path, project_dir: PurePath) -> Path:
monkeypatch.chdir(tmp_path)
return tmp_path / "pyproject.toml"
pyproject_toml_file = tmp_path / "pyproject.toml"
pyproject_toml_file.write_text(
dedent(
f"""
[project]
name = "script-test"
version = "0.1.0"
[dependency-groups]
dev = [
"ansicolors",
"dev-cmd @ {project_dir.as_posix()}",
]
"""
)
)
return pyproject_toml_file


def test_exec_python(tmp_path: Path, pyproject_toml: Path, project_dir: PurePath) -> None:
@pytest.fixture
def script(tmp_path: Path) -> PurePath:
script = tmp_path / "script.py"
script.write_text(
dedent(
Expand All @@ -38,31 +55,30 @@ def test_exec_python(tmp_path: Path, pyproject_toml: Path, project_dir: PurePath
import colors
print(colors.cyan(" ".join(sys.argv[1:])))
if sys.argv[1].endswith(":"):
color = sys.argv[1][:-1]
args = sys.argv[2:]
else:
color = "cyan"
args = sys.argv[1:]
print(colors.color(" ".join(args), fg=color))
"""
)
)
return script

pyproject_toml.write_text(
dedent(
f"""
[project]
name = "script-test"
version = "0.1.0"
[dependency-groups]
dev = [
"ansicolors",
"dev-cmd @ {project_dir.as_posix()}",
]
[tool.dev-cmd.commands.test]
args = ["python", "{script.as_posix()}"]
accepts-extra-args = true
"""

def test_exec_python(script: PurePath, pyproject_toml: Path) -> None:
with pyproject_toml.open("a") as fp:
fp.write(
dedent(
f"""
[tool.dev-cmd.commands.test]
args = ["python", "{script.as_posix()}"]
accepts-extra-args = true
"""
)
)
)

assert (
colors.cyan("Slartibartfast 42")
Expand All @@ -73,3 +89,26 @@ def test_exec_python(tmp_path: Path, pyproject_toml: Path, project_dir: PurePath
check=True,
).stdout.strip()
)


def test_exec_script(script: PurePath, pyproject_toml: Path) -> None:
with pyproject_toml.open("a") as fp:
fp.write(
dedent(
f"""
[tool.dev-cmd.commands.test]
args = ["{script.as_posix()}"]
accepts-extra-args = true
"""
)
)

assert (
colors.magenta("Ford Marvin")
== subprocess.run(
args=["uv", "run", "dev-cmd", "test", "--", "magenta:", "Ford", "Marvin"],
stdout=subprocess.PIPE,
text=True,
check=True,
).stdout.strip()
)

0 comments on commit 9d31a91

Please sign in to comment.