|
|
@ -14,17 +14,23 @@ |
|
|
|
from __future__ import absolute_import, print_function |
|
|
|
from __future__ import absolute_import, print_function |
|
|
|
import os |
|
|
|
import os |
|
|
|
import sys |
|
|
|
import sys |
|
|
|
|
|
|
|
from functools import partial |
|
|
|
|
|
|
|
|
|
|
|
import click |
|
|
|
import click |
|
|
|
import pytest |
|
|
|
import pytest |
|
|
|
from click.testing import CliRunner |
|
|
|
from click.testing import CliRunner |
|
|
|
from flask import Flask, current_app |
|
|
|
from flask import Flask, current_app |
|
|
|
|
|
|
|
|
|
|
|
from flask.cli import AppGroup, FlaskGroup, NoAppException, ScriptInfo, \ |
|
|
|
from flask.cli import cli, AppGroup, FlaskGroup, NoAppException, ScriptInfo, \ |
|
|
|
find_best_app, locate_app, with_appcontext, prepare_exec_for_file, \ |
|
|
|
find_best_app, locate_app, with_appcontext, prepare_exec_for_file, \ |
|
|
|
find_default_import_path, get_version |
|
|
|
find_default_import_path, get_version |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture |
|
|
|
|
|
|
|
def runner(): |
|
|
|
|
|
|
|
return CliRunner() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_cli_name(test_apps): |
|
|
|
def test_cli_name(test_apps): |
|
|
|
"""Make sure the CLI object's name is the app's name and not the app itself""" |
|
|
|
"""Make sure the CLI object's name is the app's name and not the app itself""" |
|
|
|
from cliapp.app import testapp |
|
|
|
from cliapp.app import testapp |
|
|
@ -129,7 +135,7 @@ def test_scriptinfo(test_apps): |
|
|
|
assert obj.load_app() == app |
|
|
|
assert obj.load_app() == app |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_with_appcontext(): |
|
|
|
def test_with_appcontext(runner): |
|
|
|
"""Test of with_appcontext.""" |
|
|
|
"""Test of with_appcontext.""" |
|
|
|
@click.command() |
|
|
|
@click.command() |
|
|
|
@with_appcontext |
|
|
|
@with_appcontext |
|
|
@ -138,13 +144,12 @@ def test_with_appcontext(): |
|
|
|
|
|
|
|
|
|
|
|
obj = ScriptInfo(create_app=lambda info: Flask("testapp")) |
|
|
|
obj = ScriptInfo(create_app=lambda info: Flask("testapp")) |
|
|
|
|
|
|
|
|
|
|
|
runner = CliRunner() |
|
|
|
|
|
|
|
result = runner.invoke(testcmd, obj=obj) |
|
|
|
result = runner.invoke(testcmd, obj=obj) |
|
|
|
assert result.exit_code == 0 |
|
|
|
assert result.exit_code == 0 |
|
|
|
assert result.output == 'testapp\n' |
|
|
|
assert result.output == 'testapp\n' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_appgroup(): |
|
|
|
def test_appgroup(runner): |
|
|
|
"""Test of with_appcontext.""" |
|
|
|
"""Test of with_appcontext.""" |
|
|
|
@click.group(cls=AppGroup) |
|
|
|
@click.group(cls=AppGroup) |
|
|
|
def cli(): |
|
|
|
def cli(): |
|
|
@ -164,7 +169,6 @@ def test_appgroup(): |
|
|
|
|
|
|
|
|
|
|
|
obj = ScriptInfo(create_app=lambda info: Flask("testappgroup")) |
|
|
|
obj = ScriptInfo(create_app=lambda info: Flask("testappgroup")) |
|
|
|
|
|
|
|
|
|
|
|
runner = CliRunner() |
|
|
|
|
|
|
|
result = runner.invoke(cli, ['test'], obj=obj) |
|
|
|
result = runner.invoke(cli, ['test'], obj=obj) |
|
|
|
assert result.exit_code == 0 |
|
|
|
assert result.exit_code == 0 |
|
|
|
assert result.output == 'testappgroup\n' |
|
|
|
assert result.output == 'testappgroup\n' |
|
|
@ -174,7 +178,7 @@ def test_appgroup(): |
|
|
|
assert result.output == 'testappgroup\n' |
|
|
|
assert result.output == 'testappgroup\n' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_flaskgroup(): |
|
|
|
def test_flaskgroup(runner): |
|
|
|
"""Test FlaskGroup.""" |
|
|
|
"""Test FlaskGroup.""" |
|
|
|
def create_app(info): |
|
|
|
def create_app(info): |
|
|
|
return Flask("flaskgroup") |
|
|
|
return Flask("flaskgroup") |
|
|
@ -187,13 +191,12 @@ def test_flaskgroup(): |
|
|
|
def test(): |
|
|
|
def test(): |
|
|
|
click.echo(current_app.name) |
|
|
|
click.echo(current_app.name) |
|
|
|
|
|
|
|
|
|
|
|
runner = CliRunner() |
|
|
|
|
|
|
|
result = runner.invoke(cli, ['test']) |
|
|
|
result = runner.invoke(cli, ['test']) |
|
|
|
assert result.exit_code == 0 |
|
|
|
assert result.exit_code == 0 |
|
|
|
assert result.output == 'flaskgroup\n' |
|
|
|
assert result.output == 'flaskgroup\n' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_print_exceptions(): |
|
|
|
def test_print_exceptions(runner): |
|
|
|
"""Print the stacktrace if the CLI.""" |
|
|
|
"""Print the stacktrace if the CLI.""" |
|
|
|
def create_app(info): |
|
|
|
def create_app(info): |
|
|
|
raise Exception("oh no") |
|
|
|
raise Exception("oh no") |
|
|
@ -203,8 +206,65 @@ def test_print_exceptions(): |
|
|
|
def cli(**params): |
|
|
|
def cli(**params): |
|
|
|
pass |
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
runner = CliRunner() |
|
|
|
|
|
|
|
result = runner.invoke(cli, ['--help']) |
|
|
|
result = runner.invoke(cli, ['--help']) |
|
|
|
assert result.exit_code == 0 |
|
|
|
assert result.exit_code == 0 |
|
|
|
assert 'Exception: oh no' in result.output |
|
|
|
assert 'Exception: oh no' in result.output |
|
|
|
assert 'Traceback' in result.output |
|
|
|
assert 'Traceback' in result.output |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestRoutes: |
|
|
|
|
|
|
|
@pytest.fixture |
|
|
|
|
|
|
|
def invoke(self, runner): |
|
|
|
|
|
|
|
def create_app(info): |
|
|
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
|
|
app.testing = True |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/get_post/<int:x>/<int:y>', methods=['GET', 'POST']) |
|
|
|
|
|
|
|
def yyy_get_post(x, y): |
|
|
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.route('/zzz_post', methods=['POST']) |
|
|
|
|
|
|
|
def aaa_post(): |
|
|
|
|
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return app |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cli = FlaskGroup(create_app=create_app) |
|
|
|
|
|
|
|
return partial(runner.invoke, cli) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def expect_order(self, order, output): |
|
|
|
|
|
|
|
# skip the header and match the start of each row |
|
|
|
|
|
|
|
for expect, line in zip(order, output.splitlines()[2:]): |
|
|
|
|
|
|
|
# do this instead of startswith for nicer pytest output |
|
|
|
|
|
|
|
assert line[:len(expect)] == expect |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_simple(self, invoke): |
|
|
|
|
|
|
|
result = invoke(['routes']) |
|
|
|
|
|
|
|
assert result.exit_code == 0 |
|
|
|
|
|
|
|
self.expect_order( |
|
|
|
|
|
|
|
['aaa_post', 'static', 'yyy_get_post'], |
|
|
|
|
|
|
|
result.output |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_sort(self, invoke): |
|
|
|
|
|
|
|
default_output = invoke(['routes']).output |
|
|
|
|
|
|
|
endpoint_output = invoke(['routes', '-s', 'endpoint']).output |
|
|
|
|
|
|
|
assert default_output == endpoint_output |
|
|
|
|
|
|
|
self.expect_order( |
|
|
|
|
|
|
|
['static', 'yyy_get_post', 'aaa_post'], |
|
|
|
|
|
|
|
invoke(['routes', '-s', 'methods']).output |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
self.expect_order( |
|
|
|
|
|
|
|
['yyy_get_post', 'static', 'aaa_post'], |
|
|
|
|
|
|
|
invoke(['routes', '-s', 'rule']).output |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
self.expect_order( |
|
|
|
|
|
|
|
['aaa_post', 'yyy_get_post', 'static'], |
|
|
|
|
|
|
|
invoke(['routes', '-s', 'match']).output |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_all_methods(self, invoke): |
|
|
|
|
|
|
|
output = invoke(['routes']).output |
|
|
|
|
|
|
|
assert 'GET, HEAD, OPTIONS, POST' not in output |
|
|
|
|
|
|
|
output = invoke(['routes', '--all-methods']).output |
|
|
|
|
|
|
|
assert 'GET, HEAD, OPTIONS, POST' in output |
|
|
|