CodeSampleX

Exemple

click 8.4.2: Test a click command without spawning a subprocess

Échantillon vérifié pour pypi click 8.4.2: Test a click command without spawning a subprocess. Le contrat s'est exécuté sur python 3.12 · linux alpine/x64 …

sha256:02f8c2f7fe825dcfb2135cbd593f008e36f7550ce68c3fcfa7c6de1bcfcbb02f

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
2
Clés de signature qui l’ont compilé
2
Environnement déclaré python 3.12 linux x64 python 3.12 python pip

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
python 3.12 · linux alpine/x64 · docker ed25519:a2ec939a4c60e243 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1
2026-08-13
python 3.12 · linux alpine/x64 · docker ed25519:d91480838ac982c9 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · python@1
2026-08-17

Cas

HOW
Objectif
Test a click command without spawning a subprocess
Paquets
Symboles
  • click.command
  • click.option
  • CliRunner
Environnement
python 3.12
Créé
2026-08-13T18:43:59Z

Contrat

  1. define a command with an option and an argument
  2. assert CliRunner invokes it in-process and captures stdout
  3. assert a bad value exits with code 2 and an error message
  4. assert the exit code is available without a subprocess

Fichiers

  • csx.json
  • requirements.txt
  • src/__init__.py
  • src/cli.py
  • test/contract.py

Télécharger l’artefact source (tar.gz)

Code source

csx.json
{"case":{"caseId":"case:sha256:d87471a05acabf2b3b96c01132c1c2c680e31e30a713feeb381357de113bb7ba","constraints":{"runtime":"python"},"contract":["define a command with an option and an argument","assert CliRunner invokes it in-process and captures stdout","assert a bad value exits with code 2 and an error message","assert the exit code is available without a subprocess"],"goal":"Test a click command without spawning a subprocess","kind":"HOW","packages":["pkg:pypi/click@8.4.2"],"schemaVersion":1,"symbols":["click.command","click.option","CliRunner"]},"contractCommand":["python","test/contract.py"],"environment":{"arch":"x64","ecosystem":"pypi","executionContext":"python","language":"python","os":"linux","packageManager":"pip","runtime":"python","runtimeVersion":"3.12","schemaVersion":1},"license":"MIT-0","packages":["pkg:pypi/click@8.4.2"],"schemaVersion":1,"symbols":["click.command","click.option","CliRunner"],"verifierAdapter":"python@1"}
requirements.txt
click==8.4.2
src/__init__.py
src/cli.py
"""A click command and the runner that tests it in-process."""

import click


@click.command()
@click.argument("name")
@click.option("--count", default=1, type=int, help="how many times to greet")
@click.option("--shout/--no-shout", default=False)
def greet(name, count, shout):
    """Greet NAME COUNT times."""
    line = "hello %s" % name
    if shout:
        line = line.upper()
    for _ in range(count):
        click.echo(line)
test/contract.py
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from click.testing import CliRunner

from src.cli import greet

runner = CliRunner()

# CliRunner calls the command in this process: no subprocess, no PATH, and
# the exit code and output are both returned rather than having to be
# scraped from a pipe.
result = runner.invoke(greet, ["codesamplex", "--count", "2"])
assert result.exit_code == 0, result.output
assert result.output == "hello codesamplex\nhello codesamplex\n", repr(result.output)

result = runner.invoke(greet, ["csx", "--shout"])
assert result.exit_code == 0
assert result.output.strip() == "HELLO CSX"

# Usage errors exit 2 — click's convention, distinct from an app error.
result = runner.invoke(greet, ["csx", "--count", "not-a-number"])
assert result.exit_code == 2, result.output
assert "not a valid integer" in result.output.lower(), result.output

# A missing required argument is also a usage error, not a crash.
result = runner.invoke(greet, [])
assert result.exit_code == 2
assert "missing argument" in result.output.lower(), result.output

print("CONTRACT PASS: click ran in-process and reported usage errors as exit 2")

Seeder d'origine

anonymous