Skip to content
Myr

test_cli.py

This file

Path

tests/test_cli.py

143 lines · 3.7 KiB · Python

Last changed by

fixed unmatched elifJosiah Philipsenbefore adoption41e08f4

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, with_appcontext, prepare_exec_for_file232425def 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_prepare_exec_for_file(test_apps):53    assert prepare_exec_for_file('test.py') == 'test'54    assert prepare_exec_for_file('/usr/share/__init__.py') == 'share'55    with pytest.raises(NoAppException):56        prepare_exec_for_file('test.txt')575859def test_locate_app(test_apps):60    """Test of locate_app."""61    assert locate_app("cliapp.app").name == "testapp"62    assert locate_app("cliapp.app:testapp").name == "testapp"63    assert locate_app("cliapp.multiapp:app1").name == "app1"64    pytest.raises(RuntimeError, locate_app, "cliapp.app:notanapp")656667def test_scriptinfo(test_apps):68    """Test of ScriptInfo."""69    obj = ScriptInfo(app_import_path="cliapp.app:testapp")70    assert obj.load_app().name == "testapp"71    assert obj.load_app().name == "testapp"7273    def create_app(info):74        return Flask("createapp")7576    obj = ScriptInfo(create_app=create_app)77    app = obj.load_app()78    assert app.name == "createapp"79    assert obj.load_app() == app808182def test_with_appcontext():83    """Test of with_appcontext."""84    @click.command()85    @with_appcontext86    def testcmd():87        click.echo(current_app.name)8889    obj = ScriptInfo(create_app=lambda info: Flask("testapp"))9091    runner = CliRunner()92    result = runner.invoke(testcmd, obj=obj)93    assert result.exit_code == 094    assert result.output == 'testapp\n'959697def test_appgroup():98    """Test of with_appcontext."""99    @click.group(cls=AppGroup)100    def cli():101        pass102103    @cli.command(with_appcontext=True)104    def test():105        click.echo(current_app.name)106107    @cli.group()108    def subgroup():109        pass110111    @subgroup.command(with_appcontext=True)112    def test2():113        click.echo(current_app.name)114115    obj = ScriptInfo(create_app=lambda info: Flask("testappgroup"))116117    runner = CliRunner()118    result = runner.invoke(cli, ['test'], obj=obj)119    assert result.exit_code == 0120    assert result.output == 'testappgroup\n'121122    result = runner.invoke(cli, ['subgroup', 'test2'], obj=obj)123    assert result.exit_code == 0124    assert result.output == 'testappgroup\n'125126127def test_flaskgroup():128    """Test FlaskGroup."""129    def create_app(info):130        return Flask("flaskgroup")131132    @click.group(cls=FlaskGroup, create_app=create_app)133    def cli(**params):134        pass135136    @cli.command()137    def test():138        click.echo(current_app.name)139140    runner = CliRunner()141    result = runner.invoke(cli, ['test'])142    assert result.exit_code == 0143    assert result.output == 'flaskgroup\n'