Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Render Engine CLI is the CLI tool for the Render Engine static site generator.

## Installing the Render Engine CLI

To use the render engine, you must have Python 3.10 or greater installed. You can download Python from [python.org](https://python.org).
To use the render engine, you must have Python 3.11 or greater installed. You can download Python from [python.org](https://python.org).

- Linux/MacOS: [python.org](https://python.org)
- Windows: [Microsoft Store](https://apps.microsoft.com/store/detail/python-311/9NRWMJP3717K)
Expand Down Expand Up @@ -78,6 +78,17 @@ eval (env _RENDER_ENGINE_COMPLETE=fish_source render-engine)

After adding the appropriate line to your shell configuration file, restart your shell or source the configuration file to enable completion.

## Create a New Site

Create a new Render Engine site and answer a few questions about it:

```shell
render-engine init
```

Use `render-engine init --no-input` to skip the questions and use the default answers,
or `render-engine init --help` to see all options.

## Getting Started

Check out the [Getting Started](https://render-engine.readthedocs.io/en/latest/page/) Section in the [Documentation](https://render-engine.readthedocs.io)
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ readme = "README.md"
requires-python = ">=3.11"
dependencies = [
"click>=8.2.1",
"cookiecutter>=2.6.0",
"render-engine>=2025.11.1a1",
"rich>=14.0.0",
"toml>=0.10.2",
Expand Down
73 changes: 7 additions & 66 deletions src/render_engine_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
]

import datetime
import json
from pathlib import Path

import click
Expand Down Expand Up @@ -66,73 +65,15 @@ def get_version_string() -> str:
def app(): ...


@app.command
@click.option(
"-e",
"--extra-context",
help="Extra context to pass to the cookiecutter template. This must be a JSON string.",
default=None,
type=click.STRING,
)
@click.option(
"-t",
"--template",
help="Template to use for creating a new site.",
default="https://github.com/render-engine/cookiecutter-render-engine-site",
type=click.STRING,
)
@click.option(
"--no-input",
is_flag=True,
default=False,
help="Do not prompt for parameters.",
@app.command(
context_settings={"ignore_unknown_options": True, "allow_extra_args": True, "help_option_names": []},
)
@click.option(
"-o",
"--output-dir",
type=click.Path(
path_type=Path,
exists=False,
),
default=Path("./"),
)
@click.option(
"-c",
"--config-file",
type=click.Path(
path_type=Path,
exists=True,
readable=True,
),
default=None,
)
def init(template: str, extra_context: str, no_input: bool, output_dir: Path, config_file: Path):
"""
Create a new site configuration. You can provide extra_context to the cookiecutter template.
@click.argument("args", nargs=-1, type=click.UNPROCESSED)
def init(args: tuple[str, ...]) -> None:
"""Create a new Render Engine site."""
from render_engine.init.__main__ import main

Also, any argument that cookiecutter accepts can be passed to this command.

The template can be a local path or a git repository.
"""

# Check if cookiecutter is installed
try:
from cookiecutter.main import cookiecutter
except ImportError:
click.secho(
"You need to install cookiecutter to use this command. Run `pip install cookiecutter` to install it.",
err=True,
fg="red",
)
raise click.Exit(0)
extra_context = json.loads(extra_context) if extra_context else None
cookiecutter(
template=template,
extra_context=extra_context,
output_dir=output_dir,
config_file=config_file,
no_input=no_input,
)
main(list(args), prog="render-engine init")


@app.command
Expand Down
134 changes: 39 additions & 95 deletions tests/test_render_engine_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,109 +6,53 @@
from render_engine_cli.cli import app


@pytest.fixture(scope="module", autouse=True)
def get_test_template(tmp_path_factory):
path = tmp_path_factory.getbasetemp()
project_slug = path.joinpath("{{cookiecutter.project_slug}}")
project_slug.mkdir()
project_slug.joinpath("app.py").write_text("hello {{cookiecutter.world}}")
path.joinpath("cookiecutter.json").write_text(json.dumps({"world": "world", "project_slug": "testslug"}))

return path.resolve()


@pytest.fixture
def runner():
"""CLI test runner fixture"""
yield CliRunner()


def tests_error_raised_if_cookiecutter_not_installed(request, mocker, runner, get_test_template):
"""Tests that an error is raised if cookiecutter is not installed"""
def test_init_is_listed_in_the_help(runner):
"""`render-engine --help` lists the init command."""
result = runner.invoke(app, ["--help"])

# Mock importing cookiecutter
mocker.patch("cookiecutter.main.cookiecutter", side_effect=ImportError)
assert result.exit_code == 0
assert "Create a new Render Engine site." in result.output

result = runner.invoke(
app,
[
"init",
"--extra-context",
json.dumps(
{
"project_slug": request.node.name,
}
),
"--template",
get_test_template,
"--no-input",
],
)
assert isinstance(result.exception, ImportError)


def test_init_local_path_extra_context(request, tmp_path, get_test_template, runner):
"""
Tests that you can call init using a local path

It also tests that the extra_context is passed to the template.
"""
runner.invoke(
app,
[
"init",
"--extra-context",
json.dumps(
{
"project_slug": request.node.name,
}
),
"--template",
get_test_template,
"--no-input",
"--output-dir",
tmp_path,
],
)

template_path = tmp_path.joinpath(request.node.originalname)
app_py = template_path.joinpath("app.py")
assert app_py.exists()
assert app_py.read_bytes() == b"hello world"


def test_init_called_with_config(request, tmp_path, tmp_path_factory, runner):
"""
Tests that you can call init with cookiecutter configs passed in through a config file.
"""
temp_file = tmp_path.joinpath("test_cookiecutter_args.json")
temp_file.write_text(
f"""
default_context:
world: "Earth"
project_slug: {request.node.originalname}
"""
def test_init_passes_the_arguments_to_render_engine(runner, mocker):
"""All arguments after `init` are passed to render-engine's init, with the CLI's command name."""
main = mocker.patch("render_engine.init.__main__.main")

runner.invoke(app, ["init", "--no-input", "-o", "sites", "-e", '{"site_title": "My Blog"}'])

main.assert_called_once_with(
["--no-input", "-o", "sites", "-e", '{"site_title": "My Blog"}'], prog="render-engine init"
)
runner.invoke(
app,
[
"init",
"--extra-context",
json.dumps(
{
"project_slug": request.node.name,
}
),
"--template",
str(tmp_path_factory.getbasetemp().resolve()),
"--no-input",
"--output-dir",
tmp_path,
"--config-file",
temp_file,
],


def test_init_help_shows_the_init_options(runner):
"""`render-engine init --help` shows the options of render-engine's init, under the CLI's command name."""
result = runner.invoke(app, ["init", "--help"])

assert result.exit_code == 0
assert "usage: render-engine init" in result.output
assert "--no-input" in result.output


def test_init_creates_a_site(runner, tmp_path):
"""`render-engine init --no-input` creates a site from the answers in --extra-context."""
result = runner.invoke(
app, ["init", "--no-input", "-o", str(tmp_path), "-e", json.dumps({"site_title": "My Blog"})]
)
output_path = tmp_path.joinpath(request.node.originalname)
app_py = output_path.joinpath("app.py")
assert app_py.exists()
assert app_py.read_bytes() == b"hello Earth"

assert result.exit_code == 0, result.output
assert (tmp_path / "my-blog" / "app.py").is_file()


def test_init_template_option_is_rejected(runner):
"""Custom templates are no longer supported, so --template is an error."""
result = runner.invoke(app, ["init", "--template", "https://github.com/me/my-template"])

assert result.exit_code == 2
assert "unrecognized arguments: --template" in result.output
Loading
Loading