diff --git a/dev_cmd/invoke.py b/dev_cmd/invoke.py index 5f42160..7e563ed 100644 --- a/dev_cmd/invoke.py +++ b/dev_cmd/invoke.py @@ -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, diff --git a/tests/test_exec.py b/tests/test_exec.py index c64a5ad..e43cf2d 100644 --- a/tests/test_exec.py +++ b/tests/test_exec.py @@ -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( @@ -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") @@ -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() + )