Skip to content
Myr

test_cli.py

This file

Path

tests/test_cli.py

175 lines · 5.0 KiB · Python

Last changed by

Enhance tests.test_cli.test_find_best_app (#1882)Hyunchel Kimbefore adoptionf19d3bd

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_file, \25    find_default_import_path262728def test_cli_name(test_apps):29    """Make sure the CLI object's name is the app's name and not the app itself"""30    from cliapp.app import testapp31    assert testapp.cli.name == testapp.name323334def test_find_best_app(test_apps):35    """Test if `find_best_app` behaves as expected with different combinations of input."""36    class Module:37        app = Flask('appname')38    assert find_best_app(Module) == Module.app3940    class Module:41        application = Flask('appname')42    assert find_best_app(Module) == Module.application4344    class Module:45        myapp = Flask('appname')46    assert find_best_app(Module) == Module.myapp4748    class Module:49        pass50    pytest.raises(NoAppException, find_best_app, Module)5152    class Module:53        myapp1 = Flask('appname1')54        myapp2 = Flask('appname2')55    pytest.raises(NoAppException, find_best_app, Module)565758def test_prepare_exec_for_file(test_apps):59    """Expect the correct path to be set and the correct module name to be returned.6061    :func:`prepare_exec_for_file` has a side effect, where62    the parent directory of given file is added to `sys.path`.63    """64    realpath = os.path.realpath('/tmp/share/test.py')65    dirname = os.path.dirname(realpath)66    assert prepare_exec_for_file('/tmp/share/test.py') == 'test'67    assert dirname in sys.path6869    realpath = os.path.realpath('/tmp/share/__init__.py')70    dirname = os.path.dirname(os.path.dirname(realpath))71    assert prepare_exec_for_file('/tmp/share/__init__.py') == 'share'72    assert dirname in sys.path7374    with pytest.raises(NoAppException):75        prepare_exec_for_file('/tmp/share/test.txt')767778def test_locate_app(test_apps):79    """Test of locate_app."""80    assert locate_app("cliapp.app").name == "testapp"81    assert locate_app("cliapp.app:testapp").name == "testapp"82    assert locate_app("cliapp.multiapp:app1").name == "app1"83    pytest.raises(RuntimeError, locate_app, "cliapp.app:notanapp")848586def test_find_default_import_path(test_apps, monkeypatch, tmpdir):87    """Test of find_default_import_path."""88    monkeypatch.delitem(os.environ, 'FLASK_APP', raising=False)89    assert find_default_import_path() == None90    monkeypatch.setitem(os.environ, 'FLASK_APP', 'notanapp')91    assert find_default_import_path() == 'notanapp'92    tmpfile = tmpdir.join('testapp.py')93    tmpfile.write('')94    monkeypatch.setitem(os.environ, 'FLASK_APP', str(tmpfile))95    expect_rv = prepare_exec_for_file(str(tmpfile))96    assert find_default_import_path() == expect_rv979899def test_scriptinfo(test_apps):100    """Test of ScriptInfo."""101    obj = ScriptInfo(app_import_path="cliapp.app:testapp")102    assert obj.load_app().name == "testapp"103    assert obj.load_app().name == "testapp"104105    def create_app(info):106        return Flask("createapp")107108    obj = ScriptInfo(create_app=create_app)109    app = obj.load_app()110    assert app.name == "createapp"111    assert obj.load_app() == app112113114def test_with_appcontext():115    """Test of with_appcontext."""116    @click.command()117    @with_appcontext118    def testcmd():119        click.echo(current_app.name)120121    obj = ScriptInfo(create_app=lambda info: Flask("testapp"))122123    runner = CliRunner()124    result = runner.invoke(testcmd, obj=obj)125    assert result.exit_code == 0126    assert result.output == 'testapp\n'127128129def test_appgroup():130    """Test of with_appcontext."""131    @click.group(cls=AppGroup)132    def cli():133        pass134135    @cli.command(with_appcontext=True)136    def test():137        click.echo(current_app.name)138139    @cli.group()140    def subgroup():141        pass142143    @subgroup.command(with_appcontext=True)144    def test2():145        click.echo(current_app.name)146147    obj = ScriptInfo(create_app=lambda info: Flask("testappgroup"))148149    runner = CliRunner()150    result = runner.invoke(cli, ['test'], obj=obj)151    assert result.exit_code == 0152    assert result.output == 'testappgroup\n'153154    result = runner.invoke(cli, ['subgroup', 'test2'], obj=obj)155    assert result.exit_code == 0156    assert result.output == 'testappgroup\n'157158159def test_flaskgroup():160    """Test FlaskGroup."""161    def create_app(info):162        return Flask("flaskgroup")163164    @click.group(cls=FlaskGroup, create_app=create_app)165    def cli(**params):166        pass167168    @cli.command()169    def test():170        click.echo(current_app.name)171172    runner = CliRunner()173    result = runner.invoke(cli, ['test'])174    assert result.exit_code == 0175    assert result.output == 'flaskgroup\n'