Plain is headed towards 1.0! Subscribe for development updates →

 1import os
 2import subprocess
 3import sys
 4
 5import click
 6from dotenv import load_dotenv
 7
 8from plain.cli import register_cli
 9
10
11@register_cli("test")
12@click.command(
13    context_settings={
14        "ignore_unknown_options": True,
15    }
16)
17@click.argument("pytest_args", nargs=-1, type=click.UNPROCESSED)
18def cli(pytest_args: tuple[str, ...]) -> None:
19    """Run pytest with .env.test loaded"""
20
21    if os.path.exists(".env.test"):
22        click.secho(
23            "Loading environment variables from .env.test...", dim=True, italic=True
24        )
25        # plain.dev may load .env files first, so make sure we override any existing variables
26        load_dotenv(".env.test", override=True)
27
28    cmd = [
29        sys.executable,
30        "-m",
31        "pytest",
32    ] + list(pytest_args)
33
34    result = subprocess.run(cmd, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)
35
36    if returncode := result.returncode:
37        sys.exit(returncode)