|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | + tests.test_cli |
| 4 | + ~~~~~~~~~~~~~~ |
| 5 | +
|
| 6 | + :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 | +# |
| 14 | +from __future__ import absolute_import, print_function |
| 15 | + |
| 16 | +import click |
| 17 | +import pytest |
| 18 | +from click.testing import CliRunner |
| 19 | +from flask import Flask, current_app |
| 20 | + |
| 21 | +from flask.cli import AppGroup, FlaskGroup, NoAppException, ScriptInfo, \ |
| 22 | + find_best_app, locate_app, script_info_option, with_appcontext |
| 23 | + |
| 24 | + |
| 25 | +def 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 testapp |
| 28 | + assert testapp.cli.name == testapp.name |
| 29 | + |
| 30 | + |
| 31 | +def 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.app |
| 36 | + |
| 37 | + class mod: |
| 38 | + application = Flask('appname') |
| 39 | + assert find_best_app(mod) == mod.application |
| 40 | + |
| 41 | + class mod: |
| 42 | + myapp = Flask('appname') |
| 43 | + assert find_best_app(mod) == mod.myapp |
| 44 | + |
| 45 | + class mod: |
| 46 | + myapp = Flask('appname') |
| 47 | + myapp2 = Flask('appname2') |
| 48 | + |
| 49 | + pytest.raises(NoAppException, find_best_app, mod) |
| 50 | + |
| 51 | + |
| 52 | +def 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") |
| 58 | + |
| 59 | + |
| 60 | +def 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" |
| 65 | + |
| 66 | + def create_app(info): |
| 67 | + return Flask("createapp") |
| 68 | + |
| 69 | + obj = ScriptInfo(create_app=create_app) |
| 70 | + app = obj.load_app() |
| 71 | + assert app.name == "createapp" |
| 72 | + assert obj.load_app() == app |
| 73 | + |
| 74 | + |
| 75 | +def test_with_appcontext(): |
| 76 | + """Test of with_appcontext.""" |
| 77 | + @click.command() |
| 78 | + @with_appcontext |
| 79 | + def testcmd(): |
| 80 | + click.echo(current_app.name) |
| 81 | + |
| 82 | + obj = ScriptInfo(create_app=lambda info: Flask("testapp")) |
| 83 | + |
| 84 | + runner = CliRunner() |
| 85 | + result = runner.invoke(testcmd, obj=obj) |
| 86 | + assert result.exit_code == 0 |
| 87 | + assert result.output == 'testapp\n' |
| 88 | + |
| 89 | + |
| 90 | +def test_appgroup(): |
| 91 | + """Test of with_appcontext.""" |
| 92 | + @click.group(cls=AppGroup) |
| 93 | + def cli(): |
| 94 | + pass |
| 95 | + |
| 96 | + @cli.command(with_appcontext=True) |
| 97 | + def test(): |
| 98 | + click.echo(current_app.name) |
| 99 | + |
| 100 | + @cli.group() |
| 101 | + def subgroup(): |
| 102 | + pass |
| 103 | + |
| 104 | + @subgroup.command(with_appcontext=True) |
| 105 | + def test2(): |
| 106 | + click.echo(current_app.name) |
| 107 | + |
| 108 | + obj = ScriptInfo(create_app=lambda info: Flask("testappgroup")) |
| 109 | + |
| 110 | + runner = CliRunner() |
| 111 | + result = runner.invoke(cli, ['test'], obj=obj) |
| 112 | + assert result.exit_code == 0 |
| 113 | + assert result.output == 'testappgroup\n' |
| 114 | + |
| 115 | + result = runner.invoke(cli, ['subgroup', 'test2'], obj=obj) |
| 116 | + assert result.exit_code == 0 |
| 117 | + assert result.output == 'testappgroup\n' |
| 118 | + |
| 119 | + |
| 120 | +def test_flaskgroup(): |
| 121 | + """Test FlaskGroup.""" |
| 122 | + def create_app(info): |
| 123 | + return Flask("flaskgroup") |
| 124 | + |
| 125 | + @click.group(cls=FlaskGroup, create_app=create_app) |
| 126 | + @script_info_option('--config', script_info_key='config') |
| 127 | + def cli(**params): |
| 128 | + pass |
| 129 | + |
| 130 | + @cli.command() |
| 131 | + def test(): |
| 132 | + click.echo(current_app.name) |
| 133 | + |
| 134 | + runner = CliRunner() |
| 135 | + result = runner.invoke(cli, ['test']) |
| 136 | + assert result.exit_code == 0 |
| 137 | + assert result.output == 'flaskgroup\n' |
0 commit comments