Skip to content
Myr

test_cli.py

This file

Path

tests/test_cli.py

158 lines · 4.2 KiB · Python

Last changed by

Test side effect (#1889)Hyunchel Kimbefore adoption64a37bb

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_function15import os16import sys1718import click19import pytest20from click.testing import CliRunner21from flask import Flask, current_app2223from flask.cli import AppGroup, FlaskGroup, NoAppException, ScriptInfo, \24    find_best_app, locate_app, with_appcontext, prepare_exec_for_file252627def test_cli_name(test_apps):28    """Make sure the CLI object's name is the app's name and not the app itself"""29    from cliapp.app import testapp30    assert testapp.cli.name == testapp.name313233def test_find_best_app(test_apps):34    """Test of find_best_app."""35    class mod:36        app = Flask('appname')37    assert find_best_app(mod) == mod.app3839    class mod:40        application = Flask('appname')41    assert find_best_app(mod) == mod.application4243    class mod:44        myapp = Flask('appname')45    assert find_best_app(mod) == mod.myapp4647    class mod:48        myapp = Flask('appname')49        myapp2 = Flask('appname2')5051    pytest.raises(NoAppException, find_best_app, mod)525354def test_prepare_exec_for_file(test_apps):55    """Expect the correct path to be set and the correct module name to be returned.5657    :func:`prepare_exec_for_file` has a side effect, where58    the parent directory of given file is added to `sys.path`.59    """60    realpath = os.path.realpath('/tmp/share/test.py')61    dirname = os.path.dirname(realpath)62    assert prepare_exec_for_file('/tmp/share/test.py') == 'test'63    assert dirname in sys.path6465    realpath = os.path.realpath('/tmp/share/__init__.py')66    dirname = os.path.dirname(os.path.dirname(realpath))67    assert prepare_exec_for_file('/tmp/share/__init__.py') == 'share'68    assert dirname in sys.path6970    with pytest.raises(NoAppException):71        prepare_exec_for_file('/tmp/share/test.txt')727374def test_locate_app(test_apps):75    """Test of locate_app."""76    assert locate_app("cliapp.app").name == "testapp"77    assert locate_app("cliapp.app:testapp").name == "testapp"78    assert locate_app("cliapp.multiapp:app1").name == "app1"79    pytest.raises(RuntimeError, locate_app, "cliapp.app:notanapp")808182def test_scriptinfo(test_apps):83    """Test of ScriptInfo."""84    obj = ScriptInfo(app_import_path="cliapp.app:testapp")85    assert obj.load_app().name == "testapp"86    assert obj.load_app().name == "testapp"8788    def create_app(info):89        return Flask("createapp")9091    obj = ScriptInfo(create_app=create_app)92    app = obj.load_app()93    assert app.name == "createapp"94    assert obj.load_app() == app959697def test_with_appcontext():98    """Test of with_appcontext."""99    @click.command()100    @with_appcontext101    def testcmd():102        click.echo(current_app.name)103104    obj = ScriptInfo(create_app=lambda info: Flask("testapp"))105106    runner = CliRunner()107    result = runner.invoke(testcmd, obj=obj)108    assert result.exit_code == 0109    assert result.output == 'testapp\n'110111112def test_appgroup():113    """Test of with_appcontext."""114    @click.group(cls=AppGroup)115    def cli():116        pass117118    @cli.command(with_appcontext=True)119    def test():120        click.echo(current_app.name)121122    @cli.group()123    def subgroup():124        pass125126    @subgroup.command(with_appcontext=True)127    def test2():128        click.echo(current_app.name)129130    obj = ScriptInfo(create_app=lambda info: Flask("testappgroup"))131132    runner = CliRunner()133    result = runner.invoke(cli, ['test'], obj=obj)134    assert result.exit_code == 0135    assert result.output == 'testappgroup\n'136137    result = runner.invoke(cli, ['subgroup', 'test2'], obj=obj)138    assert result.exit_code == 0139    assert result.output == 'testappgroup\n'140141142def test_flaskgroup():143    """Test FlaskGroup."""144    def create_app(info):145        return Flask("flaskgroup")146147    @click.group(cls=FlaskGroup, create_app=create_app)148    def cli(**params):149        pass150151    @cli.command()152    def test():153        click.echo(current_app.name)154155    runner = CliRunner()156    result = runner.invoke(cli, ['test'])157    assert result.exit_code == 0158    assert result.output == 'flaskgroup\n'