From a36141a1f0c3bca88d006f23e48ba523f8aa177e Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Tue, 7 Apr 2020 21:48:54 -0700 Subject: [PATCH] Unit test to keep README up-to-date, closes #13 Run this to rewrite the README in place to fix it: pytest --rewrite-readme --- README.md | 25 ++++++++----------------- tests/conftest.py | 7 +++++++ tests/test_publish_now.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 17 deletions(-) create mode 100644 tests/conftest.py diff --git a/README.md b/README.md index 22f034f..7291083 100644 --- a/README.md +++ b/README.md @@ -42,29 +42,20 @@ The `--project` argument is required - it specifies the project name that should ``` $ datasette publish now2 --help + Usage: datasette publish now2 [OPTIONS] [FILES]... Options: - -m, --metadata FILENAME Path to JSON/YAML file containing metadata - to publish - + -m, --metadata FILENAME Path to JSON/YAML file containing metadata to publish --extra-options TEXT Extra options to pass to datasette serve - --branch TEXT Install datasette from a GitHub branch e.g. - master - - --template-dir DIRECTORY Path to directory containing custom - templates - + --branch TEXT Install datasette from a GitHub branch e.g. master + --template-dir DIRECTORY Path to directory containing custom templates --plugins-dir DIRECTORY Path to directory containing custom plugins - --static MOUNT:DIRECTORY Serve static files from this directory at - /MOUNT/... - - --install TEXT Additional packages (e.g. plugins) to - install - + --static MOUNT:DIRECTORY Serve static files from this directory at /MOUNT/... + --install TEXT Additional packages (e.g. plugins) to install --plugin-secret ... - Secrets to pass to plugins, e.g. --plugin- - secret datasette-auth-github client_id xxx + Secrets to pass to plugins, e.g. --plugin-secret + datasette-auth-github client_id xxx --version-note TEXT Additional note to show on /-/versions --title TEXT Title for metadata diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..9db3e06 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,7 @@ +def pytest_addoption(parser): + parser.addoption( + "--rewrite-readme", + action="store_true", + default=False, + help="Rewrite README on error", + ) diff --git a/tests/test_publish_now.py b/tests/test_publish_now.py index d5a2b1a..202c3a1 100644 --- a/tests/test_publish_now.py +++ b/tests/test_publish_now.py @@ -1,6 +1,8 @@ from click.testing import CliRunner from datasette import cli from unittest import mock +import pathlib +import re import subprocess @@ -60,3 +62,30 @@ def test_publish_now_public(mock_run, mock_which): mock_run.assert_has_calls( [mock.call(["now", "--confirm", "--no-clipboard", "--prod", "--public"]),] ) + + +def test_help_in_readme(request): + # Ensure the --help output embedded in the README is up-to-date + readme_path = pathlib.Path(__file__).parent.parent / "README.md" + readme = readme_path.read_text() + block_re = re.compile("```(.*)```", re.DOTALL) + expected = block_re.search(readme).group(1).strip() + runner = CliRunner() + result = runner.invoke(cli.cli, ["publish", "now2", "--help"], terminal_width=88) + actual = "$ datasette publish now2 --help\n\n{}".format(result.output) + + if request.config.getoption("--rewrite-readme"): + readme_path.write_text( + block_re.sub( + "```\n{}```".format(actual).replace("Usage: cli", "Usage: datasette"), + readme, + ) + ) + return + + # actual has "Usage: cli package [OPTIONS] FILES" + # because it doesn't know that cli will be aliased to datasette + expected = expected.replace("Usage: datasette", "Usage: cli") + assert ( + expected.strip() == actual.strip() + ), "README out of date - try runnning: pytest --rewrite-readme"