test_cli.py
This file
Path
tests/test_cli.py
193 lines · 5.6 KiB · Python
Last changed by
Merge branch '0.12-maintenance'Markus Unterwaditzerbefore adoption71c534d
History of this fileTake it
Forward ported CLI tests from Flask-CLI and fixed a bug with the CLI's name. (#1806)Jannis Leidelbefore adoption88500f5
lines 1–14why, one step back1# -*- 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_functionTest side effect (#1889)Hyunchel Kimbefore adoption64a37bb
lines 15–16why, one step back15import os16import sysForward ported CLI tests from Flask-CLI and fixed a bug with the CLI's name. (#1806)Jannis Leidelbefore adoption88500f5
lines 17–23why, one step back1718import click19import pytest20from click.testing import CliRunner21from flask import Flask, current_app2223from flask.cli import AppGroup, FlaskGroup, NoAppException, ScriptInfo, \Merge pull request #1881 from antsar/masterMarkus Unterwaditzerbefore adoption4f83f70
line 24why, one step back24 find_best_app, locate_app, with_appcontext, prepare_exec_for_file, \Add test for get_version (CLI) (#1884)Anton Sarukhanovbefore adoption9121e10
line 25why, one step back25 find_default_import_path, get_versionForward ported CLI tests from Flask-CLI and fixed a bug with the CLI's name. (#1806)Jannis Leidelbefore adoption88500f5
lines 26–28why, one step back262728def test_cli_name(test_apps):fix docstring (#1818)dataforgerbefore adoption92f63a1
line 29why, one step back29 """Make sure the CLI object's name is the app's name and not the app itself"""Forward ported CLI tests from Flask-CLI and fixed a bug with the CLI's name. (#1806)Jannis Leidelbefore adoption88500f5
lines 30–34why, one step back30 from cliapp.app import testapp31 assert testapp.cli.name == testapp.name323334def test_find_best_app(test_apps):Enhance tests.test_cli.test_find_best_app (#1882)Hyunchel Kimbefore adoptionf19d3bd
lines 35–36why, one step back35 """Test if `find_best_app` behaves as expected with different combinations of input."""36 class Module:Forward ported CLI tests from Flask-CLI and fixed a bug with the CLI's name. (#1806)Jannis Leidelbefore adoption88500f5
line 37why, one step back37 app = Flask('appname')Enhance tests.test_cli.test_find_best_app (#1882)Hyunchel Kimbefore adoptionf19d3bd
line 38why, one step back38 assert find_best_app(Module) == Module.appForward ported CLI tests from Flask-CLI and fixed a bug with the CLI's name. (#1806)Jannis Leidelbefore adoption88500f5
line 39why, one step back39Enhance tests.test_cli.test_find_best_app (#1882)Hyunchel Kimbefore adoptionf19d3bd
line 40why, one step back40 class Module:Forward ported CLI tests from Flask-CLI and fixed a bug with the CLI's name. (#1806)Jannis Leidelbefore adoption88500f5
line 41why, one step back41 application = Flask('appname')Enhance tests.test_cli.test_find_best_app (#1882)Hyunchel Kimbefore adoptionf19d3bd
line 42why, one step back42 assert find_best_app(Module) == Module.applicationForward ported CLI tests from Flask-CLI and fixed a bug with the CLI's name. (#1806)Jannis Leidelbefore adoption88500f5
line 43why, one step back43Enhance tests.test_cli.test_find_best_app (#1882)Hyunchel Kimbefore adoptionf19d3bd
line 44why, one step back44 class Module:Forward ported CLI tests from Flask-CLI and fixed a bug with the CLI's name. (#1806)Jannis Leidelbefore adoption88500f5
line 45why, one step back45 myapp = Flask('appname')Enhance tests.test_cli.test_find_best_app (#1882)Hyunchel Kimbefore adoptionf19d3bd
line 46why, one step back46 assert find_best_app(Module) == Module.myappForward ported CLI tests from Flask-CLI and fixed a bug with the CLI's name. (#1806)Jannis Leidelbefore adoption88500f5
line 47why, one step back47Enhance tests.test_cli.test_find_best_app (#1882)Hyunchel Kimbefore adoptionf19d3bd
lines 48–50why, one step back48 class Module:49 pass50 pytest.raises(NoAppException, find_best_app, Module)Forward ported CLI tests from Flask-CLI and fixed a bug with the CLI's name. (#1806)Jannis Leidelbefore adoption88500f5
line 51why, one step back51Enhance tests.test_cli.test_find_best_app (#1882)Hyunchel Kimbefore adoptionf19d3bd
lines 52–55why, one step back52 class Module:53 myapp1 = Flask('appname1')54 myapp2 = Flask('appname2')55 pytest.raises(NoAppException, find_best_app, Module)fixed unmatched elif (#1872)jphilipsen05before adoptionfe5f714
lines 56–58why, one step back565758def test_prepare_exec_for_file(test_apps):Test side effect (#1889)Hyunchel Kimbefore adoption64a37bb
lines 59–73why, one step back59 """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.path73fixed unmatched elif (#1872)jphilipsen05before adoptionfe5f714
line 74why, one step back74 with pytest.raises(NoAppException):Test side effect (#1889)Hyunchel Kimbefore adoption64a37bb
line 75why, one step back75 prepare_exec_for_file('/tmp/share/test.txt')Forward ported CLI tests from Flask-CLI and fixed a bug with the CLI's name. (#1806)Jannis Leidelbefore adoption88500f5
lines 76–82why, one step back767778def 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"adding in try around __import__ to catch invalid files/paths (#1950)Nate Prewittbefore adoption0f1cf50
lines 83–84why, one step back83 pytest.raises(NoAppException, locate_app, "notanpp.py")84 pytest.raises(NoAppException, locate_app, "cliapp/app")Forward ported CLI tests from Flask-CLI and fixed a bug with the CLI's name. (#1806)Jannis Leidelbefore adoption88500f5
line 85why, one step back85 pytest.raises(RuntimeError, locate_app, "cliapp.app:notanapp")Merge branch '0.12-maintenance'Markus Unterwaditzerbefore adoption71c534d
line 86why, one step back86 pytest.raises(ImportError, locate_app, "cliapp.importerrorapp")Merge pull request #1881 from antsar/masterMarkus Unterwaditzerbefore adoption4f83f70
lines 87–99why, one step back878889def test_find_default_import_path(test_apps, monkeypatch, tmpdir):90 """Test of find_default_import_path."""91 monkeypatch.delitem(os.environ, 'FLASK_APP', raising=False)92 assert find_default_import_path() == None93 monkeypatch.setitem(os.environ, 'FLASK_APP', 'notanapp')94 assert find_default_import_path() == 'notanapp'95 tmpfile = tmpdir.join('testapp.py')96 tmpfile.write('')97 monkeypatch.setitem(os.environ, 'FLASK_APP', str(tmpfile))98 expect_rv = prepare_exec_for_file(str(tmpfile))99 assert find_default_import_path() == expect_rvAdd test for get_version (CLI) (#1884)Anton Sarukhanovbefore adoption9121e10
lines 100–114why, one step back100101102def test_get_version(test_apps, capsys):103 """Test of get_version."""104 from flask import __version__ as flask_ver105 from sys import version as py_ver106 class MockCtx(object):107 resilient_parsing = False108 color = None109 def exit(self): return110 ctx = MockCtx()111 get_version(ctx, None, "test")112 out, err = capsys.readouterr()113 assert flask_ver in out114 assert py_ver in outForward ported CLI tests from Flask-CLI and fixed a bug with the CLI's name. (#1806)Jannis Leidelbefore adoption88500f5
lines 115–193why, one step back115116117def test_scriptinfo(test_apps):118 """Test of ScriptInfo."""119 obj = ScriptInfo(app_import_path="cliapp.app:testapp")120 assert obj.load_app().name == "testapp"121 assert obj.load_app().name == "testapp"122123 def create_app(info):124 return Flask("createapp")125126 obj = ScriptInfo(create_app=create_app)127 app = obj.load_app()128 assert app.name == "createapp"129 assert obj.load_app() == app130131132def test_with_appcontext():133 """Test of with_appcontext."""134 @click.command()135 @with_appcontext136 def testcmd():137 click.echo(current_app.name)138139 obj = ScriptInfo(create_app=lambda info: Flask("testapp"))140141 runner = CliRunner()142 result = runner.invoke(testcmd, obj=obj)143 assert result.exit_code == 0144 assert result.output == 'testapp\n'145146147def test_appgroup():148 """Test of with_appcontext."""149 @click.group(cls=AppGroup)150 def cli():151 pass152153 @cli.command(with_appcontext=True)154 def test():155 click.echo(current_app.name)156157 @cli.group()158 def subgroup():159 pass160161 @subgroup.command(with_appcontext=True)162 def test2():163 click.echo(current_app.name)164165 obj = ScriptInfo(create_app=lambda info: Flask("testappgroup"))166167 runner = CliRunner()168 result = runner.invoke(cli, ['test'], obj=obj)169 assert result.exit_code == 0170 assert result.output == 'testappgroup\n'171172 result = runner.invoke(cli, ['subgroup', 'test2'], obj=obj)173 assert result.exit_code == 0174 assert result.output == 'testappgroup\n'175176177def test_flaskgroup():178 """Test FlaskGroup."""179 def create_app(info):180 return Flask("flaskgroup")181182 @click.group(cls=FlaskGroup, create_app=create_app)183 def cli(**params):184 pass185186 @cli.command()187 def test():188 click.echo(current_app.name)189190 runner = CliRunner()191 result = runner.invoke(cli, ['test'])192 assert result.exit_code == 0193 assert result.output == 'flaskgroup\n'