Skip to content

testsuite fails if virtualenv is not activated when running it #2492

@RonnyPfannschmidt

Description

@RonnyPfannschmidt

context is that im reworking the downstream testin in pluggy

when i run the testsuite using .venv/bin/pytest, i get a failure due to the ds_localhost_http_server expecting to be able to directly use the datasette command

i experimented with adding it to the PATH env var for the relevant subprocess but didnt get positive results yet

# /// script
# requires-python = ">=3.11"
# dependencies = [
#   "rich",
# ]
# ///
from __future__ import annotations

import os.path
import shlex
import subprocess
import sys
from pathlib import Path
from contextlib import ContextDecorator, ExitStack, chdir
from dataclasses import dataclass, field

from collections.abc import Sequence


def ensure(args: Sequence[str], cwd: str|None = None):
    print("$", shlex.join(args))
    if os.path.isdir(".venv"):

        new_path = f".venv/bin{os.pathsep}{os.environ['PATH']}"
    else:
        new_path = os.environ["PATH"]
    res = subprocess.run(args, cwd=cwd, env={**os.environ, "PATH": new_path})
    if res.returncode:
        print("$", shlex.join(args), "failed")
        sys.exit(res.returncode)

def ensure_install(deps: Sequence[str]):
    ensure(["uv", "venv"])
    ensure([
        *("uv", "pip", "install"),
        *("--python", ".venv/bin/python"),
        *("-e", "../.."),

        *deps,
    ])



@dataclass
class WithDownstreamProjectDirectory(ContextDecorator):
    name: str
    git_url: str
    _exit_stack: ExitStack = field(repr=False, default_factory=ExitStack)

    def __enter__(self):
        if os.path.isdir(self.name):
            ensure(["git", "pull"], cwd=self.name)
        else:
            ensure(["git", "clone", "--depth=1", self.git_url, self.name])
        self._exit_stack.enter_context(chdir(self.name))

    def __exit__(self, exc_type, exc_val, exc_tb):
        self._exit_stack.close()
...

@WithDownstreamProjectDirectory("datasette", "https://github.com/simonw/datasette")
def datasette(extra_args: Sequence[str]):
    ensure_install(["-e", ".[test]", "click<8.2", "pytest-asyncio<1"])
    ensure([".venv/bin/pytest", *extra_args])


...
if __name__ == "__main__":
    globals()[sys.argv[1]](sys.argv[2:])

triggers `FileNotFoundError: [Errno 2] No such file or directory: 'datasette' in the fixture

if theres nothing that comes to mind off hand i'll start to dig out more detailed debug info

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions