[pre-commit.ci lite] apply automatic fixes

This commit is contained in:
pre-commit-ci-lite[bot] 2026-02-06 08:24:13 +00:00 committed by GitHub
parent fb763eaf61
commit 554fad132e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 139 additions and 0 deletions

View file

@ -32,16 +32,19 @@ from flask.cli import with_appcontext
cwd = Path.cwd()
test_path = (Path(__file__) / ".." / "test_apps").resolve()
@pytest.fixture
def runner():
return CliRunner()
def test_cli_name(test_apps):
"""Make sure the CLI object's name is the app's name and not the app itself"""
from cliapp.app import testapp
assert testapp.cli.name == testapp.name
def test_find_best_app(test_apps):
class Module:
app = Flask("appname")
@ -128,6 +131,7 @@ def test_find_best_app(test_apps):
pytest.raises(TypeError, find_best_app, Module)
@pytest.mark.parametrize(
"value,path,result",
(
@ -172,6 +176,7 @@ def test_prepare_import(request, value, path, result):
assert prepare_import(value) == result
assert sys.path[0] == str(path)
@pytest.mark.parametrize(
"iname,aname,result",
(
@ -190,6 +195,7 @@ def test_prepare_import(request, value, path, result):
def test_locate_app(test_apps, iname, aname, result):
assert locate_app(iname, aname).name == result
@pytest.mark.parametrize(
"iname,aname",
(
@ -212,6 +218,7 @@ def test_locate_app_raises(test_apps, iname, aname):
with pytest.raises(NoAppException):
locate_app(iname, aname)
def test_locate_app_suppress_raise(test_apps):
app = locate_app("notanapp.py", None, raise_if_not_found=False)
assert app is None
@ -220,6 +227,7 @@ def test_locate_app_suppress_raise(test_apps):
with pytest.raises(NoAppException):
locate_app("cliapp.importerrorapp", None, raise_if_not_found=False)
def test_get_version(test_apps, capsys):
class MockCtx:
resilient_parsing = False
@ -235,6 +243,7 @@ def test_get_version(test_apps, capsys):
assert f"Flask {importlib.metadata.version('flask')}" in out
assert f"Werkzeug {importlib.metadata.version('werkzeug')}" in out
def test_scriptinfo(test_apps, monkeypatch):
obj = ScriptInfo(app_import_path="cliapp.app:testapp")
app = obj.load_app()
@ -276,6 +285,7 @@ def test_scriptinfo(test_apps, monkeypatch):
app = obj.load_app()
assert app.name == "testapp"
def test_app_cli_has_app_context(app, runner):
def _param_cb(ctx, param, value):
# current_app should be available in parameter callbacks
@ -293,6 +303,7 @@ def test_app_cli_has_app_context(app, runner):
result = runner.invoke(cli, ["check", "x"], standalone_mode=False)
assert result.return_value == (True, True)
def test_with_appcontext(runner):
@click.command()
@with_appcontext
@ -305,6 +316,7 @@ def test_with_appcontext(runner):
assert result.exit_code == 0
assert result.output == "testapp\n"
def test_appgroup_app_context(runner):
@click.group(cls=AppGroup)
def cli():
@ -332,6 +344,7 @@ def test_appgroup_app_context(runner):
assert result.exit_code == 0
assert result.output == "testappgroup\n"
def test_flaskgroup_app_context(runner):
def create_app():
return Flask("flaskgroup")
@ -348,6 +361,7 @@ def test_flaskgroup_app_context(runner):
assert result.exit_code == 0
assert result.output == "flaskgroup\n"
@pytest.mark.parametrize("set_debug_flag", (True, False))
def test_flaskgroup_debug(runner, set_debug_flag):
def create_app():
@ -367,6 +381,7 @@ def test_flaskgroup_debug(runner, set_debug_flag):
assert result.exit_code == 0
assert result.output == f"{not set_debug_flag}\n"
def test_flaskgroup_nested(app, runner):
cli = click.Group("cli")
flask_group = FlaskGroup(name="flask", create_app=lambda: app)
@ -379,6 +394,7 @@ def test_flaskgroup_nested(app, runner):
result = runner.invoke(cli, ["flask", "show"])
assert result.output == "flask_test\n"
def test_no_command_echo_loading_error():
from flask.cli import cli
@ -393,6 +409,7 @@ def test_no_command_echo_loading_error():
assert "FLASK_APP" in result.stderr
assert "Usage:" in result.stderr
def test_help_echo_loading_error():
from flask.cli import cli
@ -407,6 +424,7 @@ def test_help_echo_loading_error():
assert "FLASK_APP" in result.stderr
assert "Usage:" in result.stdout
def test_help_echo_exception():
def create_app():
raise Exception("oh no")
@ -424,6 +442,7 @@ def test_help_echo_exception():
assert "Exception: oh no" in result.stderr
assert "Usage:" in result.stdout
class TestRoutes:
@pytest.fixture
def app(self):
@ -498,6 +517,7 @@ class TestRoutes:
assert result.exit_code == 0
assert "Host" in result.output
def dotenv_not_available():
try:
import dotenv # noqa: F401
@ -506,10 +526,12 @@ def dotenv_not_available():
return False
need_dotenv = pytest.mark.skipif(
dotenv_not_available(), reason="dotenv is not installed"
)
@need_dotenv
def test_load_dotenv(monkeypatch):
# can't use monkeypatch.delitem since the keys don't exist yet
@ -533,6 +555,7 @@ def test_load_dotenv(monkeypatch):
# Non existent file should not load
assert not load_dotenv("non-existent-file", load_defaults=False)
@need_dotenv
def test_dotenv_path(monkeypatch):
for item in ("FOO", "BAR", "EGGS"):
@ -542,12 +565,14 @@ def test_dotenv_path(monkeypatch):
assert Path.cwd() == cwd
assert "FOO" in os.environ
def test_dotenv_optional(monkeypatch):
monkeypatch.setitem(sys.modules, "dotenv", None)
monkeypatch.chdir(test_path)
load_dotenv()
assert "FOO" not in os.environ
@need_dotenv
def test_disable_dotenv_from_env(monkeypatch, runner):
monkeypatch.chdir(test_path)
@ -555,6 +580,7 @@ def test_disable_dotenv_from_env(monkeypatch, runner):
runner.invoke(FlaskGroup())
assert "FOO" not in os.environ
def test_run_cert_path():
# no key
with pytest.raises(click.BadParameter):
@ -572,6 +598,7 @@ def test_run_cert_path():
ctx = run_command.make_context("run", ["--key", __file__, "--cert", __file__])
assert ctx.params["cert"] == (__file__, __file__)
def test_run_cert_adhoc(monkeypatch):
monkeypatch.setitem(sys.modules, "cryptography", None)
@ -588,6 +615,7 @@ def test_run_cert_adhoc(monkeypatch):
with pytest.raises(click.BadParameter):
run_command.make_context("run", ["--cert", "adhoc", "--key", __file__])
def test_run_cert_import(monkeypatch):
monkeypatch.setitem(sys.modules, "not_here", None)
@ -609,12 +637,14 @@ def test_run_cert_import(monkeypatch):
with pytest.raises(click.BadParameter):
run_command.make_context("run", ["--cert", "ssl_context", "--key", __file__])
def test_run_cert_no_ssl(monkeypatch):
monkeypatch.setitem(sys.modules, "ssl", None)
with pytest.raises(click.BadParameter):
run_command.make_context("run", ["--cert", "not_here"])
def test_cli_blueprints(app):
"""Test blueprint commands register correctly to the application"""
custom = Blueprint("custom", __name__, cli_group="customized")
@ -657,6 +687,7 @@ def test_cli_blueprints(app):
result = app_runner.invoke(args=["late_registration", "late"])
assert "late_result" in result.output
def test_cli_empty(app):
"""If a Blueprint's CLI group is empty, do not register it."""
bp = Blueprint("blue", __name__, cli_group="blue")
@ -665,6 +696,7 @@ def test_cli_empty(app):
result = app.test_cli_runner().invoke(args=["blue", "--help"])
assert result.exit_code == 2, f"Unexpected success:\n\n{result.output}"
def test_run_exclude_patterns():
ctx = run_command.make_context("run", ["--exclude-patterns", __file__])
assert ctx.params["exclude_patterns"] == [__file__]