Add Blueprint level cli command registration

Implements #1357.
Adds ability to register click cli commands onto blueprint.
This commit is contained in:
Anthony Plunkett 2018-05-14 22:05:54 -04:00 committed by David Lord
parent 855d59b68b
commit ec1ccd7530
No known key found for this signature in database
GPG key ID: 7A1C87E3F5BC42A8
6 changed files with 136 additions and 8 deletions

View file

@ -23,7 +23,7 @@ import pytest
from _pytest.monkeypatch import notset
from click.testing import CliRunner
from flask import Flask, current_app
from flask import Flask, current_app, Blueprint
from flask.cli import (
AppGroup,
FlaskGroup,
@ -609,3 +609,46 @@ def test_run_cert_import(monkeypatch):
# no --key with SSLContext
with pytest.raises(click.BadParameter):
run_command.make_context("run", ["--cert", "ssl_context", "--key", __file__])
def test_cli_blueprints(app):
"""Test blueprint commands register correctly to the application"""
custom = Blueprint("custom", __name__, cli_group="customized")
nested = Blueprint("nested", __name__)
merged = Blueprint("merged", __name__, cli_group=None)
late = Blueprint("late", __name__)
@custom.cli.command("custom")
def custom_command():
click.echo("custom_result")
@nested.cli.command("nested")
def nested_command():
click.echo("nested_result")
@merged.cli.command("merged")
def merged_command():
click.echo("merged_result")
@late.cli.command("late")
def late_command():
click.echo("late_result")
app.register_blueprint(custom)
app.register_blueprint(nested)
app.register_blueprint(merged)
app.register_blueprint(late, cli_group="late_registration")
app_runner = app.test_cli_runner()
result = app_runner.invoke(args=["customized", "custom"])
assert "custom_result" in result.output
result = app_runner.invoke(args=["nested", "nested"])
assert "nested_result" in result.output
result = app_runner.invoke(args=["merged"])
assert "merged_result" in result.output
result = app_runner.invoke(args=["late_registration", "late"])
assert "late_result" in result.output