Run cog with --extra test

Previously it kept on adding stuff to cli-reference.rst
that came from other plugins installed for my global environment
This commit is contained in:
Simon Willison 2025-11-04 18:11:20 -08:00
commit 9f74dc22a8
2 changed files with 26 additions and 6 deletions

View file

@ -21,11 +21,11 @@ export DATASETTE_SECRET := "not_a_secret"
@lint: codespell
uv run black . --check
uv run flake8
uv run cog --check README.md docs/*.rst
uv run --extra test cog --check README.md docs/*.rst
# Rebuild docs with cog
@cog:
uv run cog -r README.md docs/*.rst
uv run --extra test cog -r README.md docs/*.rst
# Serve live docs on localhost:8000
@docs: cog blacken-docs

View file

@ -442,6 +442,11 @@ def uninstall(packages, yes):
"--get",
help="Run an HTTP GET request against this path, print results and exit",
)
@click.option(
"--headers",
is_flag=True,
help="Include HTTP headers in --get output (requires --get)",
)
@click.option(
"--token",
help="API token to send with --get requests",
@ -510,6 +515,7 @@ def serve(
secret,
root,
get,
headers,
token,
actor,
version_note,
@ -658,19 +664,33 @@ def serve(
# Run async soundness checks - but only if we're not under pytest
run_sync(lambda: check_databases(ds))
if headers and not get:
raise click.ClickException("--headers can only be used with --get")
if token and not get:
raise click.ClickException("--token can only be used with --get")
if get:
client = TestClient(ds)
headers = {}
request_headers = {}
if token:
headers["Authorization"] = "Bearer {}".format(token)
request_headers["Authorization"] = "Bearer {}".format(token)
cookies = {}
if actor:
cookies["ds_actor"] = client.actor_cookie(json.loads(actor))
response = client.get(get, headers=headers, cookies=cookies)
click.echo(response.text)
response = client.get(get, headers=request_headers, cookies=cookies)
if headers:
# Output HTTP status code, headers, two newlines, then the response body
click.echo(f"HTTP/1.1 {response.status}")
for key, value in response.headers.items():
click.echo(f"{key}: {value}")
if response.text:
click.echo()
click.echo(response.text)
else:
click.echo(response.text)
exit_code = 0 if response.status == 200 else 1
sys.exit(exit_code)
return