Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix commands with an argv0 of "python" on Windows. #5

Merged
merged 1 commit into from
Jan 5, 2025
Merged
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
6 changes: 5 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: CI
on: [push, pull_request]
on:
push:
branches:
- main
pull_request:
defaults:
run:
shell: bash
Expand Down
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## 0.11.0

Fix commands with an argv0 of "python" on Windows and add support for argv0 being a Python script.

## 0.10.4

Fix `-p` / `--parallel` handling of tasks - no longer flatten them.
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ greet = ["python", "-c", "import os; print(f'Hello from {os.getcwd()!r}.')"]
More on execution in various environments [below](#Execution), but you can run the greet command
with, for example `uv run dev-cmd greet`.

There are two special argv0's you can use in your commands:
1. "python": This will be mapped to the Python interpreter that is executing `dev-cmd`.
2. A file name ending in ".py": This will be assumed to be a python script, and it will be run using
the Python interpreter that is executing `dev-cmd`.

You can define as many commands as you want. They will all run from the project root directory (the
directory containing the `pyproject.toml` the commands are defined in) and accept no arguments
besides those defined in the command. You can gain further control over the command by defining it
Expand Down
2 changes: 1 addition & 1 deletion dev_cmd/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2024 John Sirois.
# Licensed under the Apache License, Version 2.0 (see LICENSE).

__version__ = "0.10.4"
__version__ = "0.11.0"
5 changes: 5 additions & 0 deletions dev_cmd/invoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ async def _invoke_command(
if USE_COLOR and not any(color_env in env for color_env in ("PYTHON_COLORS", "NO_COLOR")):
env.setdefault("FORCE_COLOR", "1")

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

process = await asyncio.create_subprocess_exec(
args[0],
*args[1:],
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ include = ["dev_cmd*"]

[dependency-groups]
dev = [
"ansicolors",
"mypy",
"pytest",
"ruff",
Expand Down
114 changes: 114 additions & 0 deletions tests/test_exec.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Copyright 2025 John Sirois.
# Licensed under the Apache License, Version 2.0 (see LICENSE).

import subprocess
from pathlib import Path, PurePath
from textwrap import dedent

import colors
import pytest
from pytest import MonkeyPatch


@pytest.fixture
def project_dir() -> PurePath:
return PurePath(
subprocess.run(
args=["git", "rev-parse", "--show-toplevel"],
cwd=PurePath(__file__).parent,
stdout=subprocess.PIPE,
text=True,
check=True,
).stdout.strip()
)


@pytest.fixture
def pyproject_toml(monkeypatch: MonkeyPatch, tmp_path: Path, project_dir: PurePath) -> Path:
monkeypatch.chdir(tmp_path)
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


@pytest.fixture
def script(tmp_path: Path) -> PurePath:
script = tmp_path / "script.py"
script.write_text(
dedent(
"""\
import sys

import colors

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


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")
== subprocess.run(
args=["uv", "run", "dev-cmd", "test", "--", "Slartibartfast", "42"],
stdout=subprocess.PIPE,
text=True,
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()
)
4 changes: 3 additions & 1 deletion uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.