Skip to content
Myr

test_cli.py

This file

Path

tests/test_cli.py

137 lines · 3.5 KiB · Python

Last changed by

Forward ported CLI tests from Flask-CLI and fixed a bug with the CLI's name. (#1806)Jannis Leidelbefore adoption88500f5

History of this file

Take it

1# -*- coding: utf-8 -*-2"""3    tests.test_cli4    ~~~~~~~~~~~~~~56    :copyright: (c) 2016 by the Flask Team, see AUTHORS for more details.7    :license: BSD, see LICENSE for more details.8"""9#10# This file was part of Flask-CLI and was modified under the terms its license,11# the Revised BSD License.12# Copyright (C) 2015 CERN.13#14from __future__ import absolute_import, print_function1516import click17import pytest18from click.testing import CliRunner19from flask import Flask, current_app2021from flask.cli import AppGroup, FlaskGroup, NoAppException, ScriptInfo, \22    find_best_app, locate_app, script_info_option, with_appcontext232425def test_cli_name(test_apps):26    "Make sure the CLI object's name is the app's name and not the app itself"27    from cliapp.app import testapp28    assert testapp.cli.name == testapp.name293031def test_find_best_app(test_apps):32    """Test of find_best_app."""33    class mod:34        app = Flask('appname')35    assert find_best_app(mod) == mod.app3637    class mod:38        application = Flask('appname')39    assert find_best_app(mod) == mod.application4041    class mod:42        myapp = Flask('appname')43    assert find_best_app(mod) == mod.myapp4445    class mod:46        myapp = Flask('appname')47        myapp2 = Flask('appname2')4849    pytest.raises(NoAppException, find_best_app, mod)505152def test_locate_app(test_apps):53    """Test of locate_app."""54    assert locate_app("cliapp.app").name == "testapp"55    assert locate_app("cliapp.app:testapp").name == "testapp"56    assert locate_app("cliapp.multiapp:app1").name == "app1"57    pytest.raises(RuntimeError, locate_app, "cliapp.app:notanapp")585960def test_scriptinfo(test_apps):61    """Test of ScriptInfo."""62    obj = ScriptInfo(app_import_path="cliapp.app:testapp")63    assert obj.load_app().name == "testapp"64    assert obj.load_app().name == "testapp"6566    def create_app(info):67        return Flask("createapp")6869    obj = ScriptInfo(create_app=create_app)70    app = obj.load_app()71    assert app.name == "createapp"72    assert obj.load_app() == app737475def test_with_appcontext():76    """Test of with_appcontext."""77    @click.command()78    @with_appcontext79    def testcmd():80        click.echo(current_app.name)8182    obj = ScriptInfo(create_app=lambda info: Flask("testapp"))8384    runner = CliRunner()85    result = runner.invoke(testcmd, obj=obj)86    assert result.exit_code == 087    assert result.output == 'testapp\n'888990def test_appgroup():91    """Test of with_appcontext."""92    @click.group(cls=AppGroup)93    def cli():94        pass9596    @cli.command(with_appcontext=True)97    def test():98        click.echo(current_app.name)99100    @cli.group()101    def subgroup():102        pass103104    @subgroup.command(with_appcontext=True)105    def test2():106        click.echo(current_app.name)107108    obj = ScriptInfo(create_app=lambda info: Flask("testappgroup"))109110    runner = CliRunner()111    result = runner.invoke(cli, ['test'], obj=obj)112    assert result.exit_code == 0113    assert result.output == 'testappgroup\n'114115    result = runner.invoke(cli, ['subgroup', 'test2'], obj=obj)116    assert result.exit_code == 0117    assert result.output == 'testappgroup\n'118119120def test_flaskgroup():121    """Test FlaskGroup."""122    def create_app(info):123        return Flask("flaskgroup")124125    @click.group(cls=FlaskGroup, create_app=create_app)126    @script_info_option('--config', script_info_key='config')127    def cli(**params):128        pass129130    @cli.command()131    def test():132        click.echo(current_app.name)133134    runner = CliRunner()135    result = runner.invoke(cli, ['test'])136    assert result.exit_code == 0137    assert result.output == 'flaskgroup\n'