Skip to content

Commit

Permalink
Accept cookiecutter arguments on the command line for update
Browse files Browse the repository at this point in the history
This commit adds support for providing cookiecutter arguments on the
command line for `cruft update`. If any arguments are provided on the
command line `cruft update` will always render the template regardless
of whether the user has requested to update to a new template version or
not.

Ideally this change will be applied after
cruft#159 as it's been developed on top
of that PR originally.
  • Loading branch information
simu committed Jul 14, 2022
1 parent 6ad8fc3 commit e778256
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
7 changes: 7 additions & 0 deletions cruft/_cli.py
Expand Up @@ -240,6 +240,12 @@ def update(
" repository (but no other changes)"
),
),
extra_context: str = typer.Option(
"{}",
"--extra-context",
help="A JSON string describing any extra context to pass to cookiecutter.",
show_default=False,
),
) -> None:
if not _commands.update(
project_dir=project_dir,
Expand All @@ -250,6 +256,7 @@ def update(
checkout=checkout,
strict=strict,
allow_untracked_files=allow_untracked_files,
extra_context=json.loads(extra_context),
):
raise typer.Exit(1)

Expand Down
13 changes: 10 additions & 3 deletions cruft/_commands/update.py
@@ -1,7 +1,7 @@
import json
from pathlib import Path
from subprocess import DEVNULL, PIPE, CalledProcessError, run # nosec
from typing import Optional, Set
from typing import Any, Dict, Optional, Set

import click
import typer
Expand All @@ -22,6 +22,7 @@ def update(
checkout: Optional[str] = None,
strict: bool = True,
allow_untracked_files: bool = False,
extra_context: Optional[Dict[str, Any]] = None,
) -> bool:
"""Update specified project's cruft to the latest and greatest release."""
cruft_file = utils.cruft.get_cruft_file(project_dir)
Expand Down Expand Up @@ -53,15 +54,15 @@ def update(

# Bail early if the repo is already up to date and no inputs are asked
if not (
cookiecutter_input or refresh_private_variables
extra_content or cookiecutter_input or refresh_private_variables
) and utils.cruft.is_project_updated(repo, cruft_state["commit"], last_commit, strict):
typer.secho(
"Nothing to do, project's cruft is already up to date!", fg=typer.colors.GREEN
)
return True

# Generate clean outputs via the cookiecutter
# from the current cruft state commit of the cookiectter and the updated
# from the current cruft state commit of the cookiecutter and the updated
# cookiecutter.
# For the current cruft state, we do not try to update the cookiecutter_input
# because we want to keep the current context input intact.
Expand All @@ -79,6 +80,12 @@ def update(
if refresh_private_variables:
_clean_cookiecutter_private_variables(cruft_state)

# Add new input data from command line to cookiecutter context
if extra_context:
extra = cruft_state["context"]["cookiecutter"]
for k, v in extra_context.items():
extra[k] = v

new_context = utils.generate.cookiecutter_template(
output_dir=new_template_dir,
repo=repo,
Expand Down
28 changes: 28 additions & 0 deletions tests/test_cli.py
Expand Up @@ -378,6 +378,34 @@ def test_update_refresh_private_variables_from_template(
assert result.exit_code == 0


@pytest.mark.parametrize(
"template_version",
[
"input",
"input-updated",
],
)
def test_update_extra_context(
cruft_runner,
cookiecutter_dir_input,
capfd,
template_version,
):
result = cruft_runner(
["update", "--project-dir", cookiecutter_dir_input.as_posix()]
+ ["-c", template_version, '--extra-context={"input":"extra-context"}'],
input="v\ny\n",
)
git_diff_captured = capfd.readouterr()
if template_version == "input_updated":
assert "-Initial" in git_diff_captured.out
assert "+Updated" in git_diff_captured.out
assert "-Input from cookiecutter: some-input" in git_diff_captured.out
assert "+Input from cookiecutter: extra-context" in git_diff_captured.out
assert "cruft has been updated" in result.stdout
assert result.exit_code == 0


@pytest.mark.parametrize("args,expected_exit_code", [([], 0), (["--exit-code"], 1), (["-e"], 1)])
def test_diff_has_diff(args, expected_exit_code, cruft_runner, cookiecutter_dir):
(cookiecutter_dir / "README.md").write_text("changed content\n")
Expand Down

0 comments on commit e778256

Please sign in to comment.