Skip to content

Commit

Permalink
Unit test to keep README up-to-date, closes #13
Browse files Browse the repository at this point in the history
Run this to rewrite the README in place to fix it:

    pytest --rewrite-readme
  • Loading branch information
simonw committed Apr 8, 2020
1 parent b18a0c0 commit a36141a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 17 deletions.
25 changes: 8 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <TEXT TEXT TEXT>...
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
Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
def pytest_addoption(parser):
parser.addoption(
"--rewrite-readme",
action="store_true",
default=False,
help="Rewrite README on error",
)
29 changes: 29 additions & 0 deletions tests/test_publish_now.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from click.testing import CliRunner
from datasette import cli
from unittest import mock
import pathlib
import re
import subprocess


Expand Down Expand Up @@ -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"

0 comments on commit a36141a

Please sign in to comment.