Skip to content
Myr

Merge pull request #4066 from dannysepler/pathlib-in-test

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Add pathlib in cli tests

authored by David Lord · committed by GitHub · parents 1c30523, 1b5f21e · Browse files · 1 file+16 added−27 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+16 added−27 removed

  • tests:+16 added−27 removed

1 file+16 added−27 removed

modified:tests/test_cli.py+16 added-27 removed
3 unmodified lines
4import ssl5import sys6import types7from functools import partial8from pathlib import Path9from unittest.mock import patch1011import click12import pytest
16 unmodified lines
29from flask.cli import run_command30from flask.cli import ScriptInfo31from flask.cli import with_appcontext3232cwd = os.getcwd()33test_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "test_apps"))33cwd = Path.cwd()34test_path = (Path(__file__) / ".." / "test_apps").resolve()353637@pytest.fixture38def runner():
113 unmodified lines
152 "value,path,result",153 (154 ("test", cwd, "test"),155 ("test.py", cwd, "test"),155 ("a/test", os.path.join(cwd, "a"), "test"),156 ("a/test", cwd / "a", "test"),157 ("test/__init__.py", cwd, "test"),158 ("test/__init__", cwd, "test"),159 # nested package160 (160 os.path.join(test_path, "cliapp", "inner1", "__init__"),161 test_path / "cliapp" / "inner1" / "__init__",162 test_path,163 "cliapp.inner1",164 ),165 (165 os.path.join(test_path, "cliapp", "inner1", "inner2"),166 test_path / "cliapp" / "inner1" / "inner2",167 test_path,168 "cliapp.inner1.inner2",169 ),170 # dotted name171 ("test.a.b", cwd, "test.a.b"),171 (os.path.join(test_path, "cliapp.app"), test_path, "cliapp.app"),172 (test_path / "cliapp.app", test_path, "cliapp.app"),173 # not a Python file, will be caught during import173 (174 os.path.join(test_path, "cliapp", "message.txt"),175 test_path,176 "cliapp.message.txt",177 ),174 (test_path / "cliapp" / "message.txt", test_path, "cliapp.message.txt"),175 ),176)177def test_prepare_import(request, value, path, result):178 """Expect the correct path to be set and the correct import and app names
10 unmodified lines
189190 request.addfinalizer(reset_path)191192 assert prepare_import(value) == result196 assert sys.path[0] == path193 assert sys.path[0] == str(path)194195196@pytest.mark.parametrize(197 "iname,aname,result",
76 unmodified lines
274 assert app.name == "testapp"275 assert obj.load_app() is app276277 # import app with module's absolute path281 cli_app_path = os.path.abspath(282 os.path.join(os.path.dirname(__file__), "test_apps", "cliapp", "app.py")283 )278 cli_app_path = str(test_path / "cliapp" / "app.py")279280 obj = ScriptInfo(app_import_path=cli_app_path)281 app = obj.load_app()282 assert app.name == "testapp"283 assert obj.load_app() is app
13 unmodified lines
297 obj = ScriptInfo()298 pytest.raises(NoAppException, obj.load_app)299300 # import app from wsgi.py in current directory305 monkeypatch.chdir(306 os.path.abspath(307 os.path.join(os.path.dirname(__file__), "test_apps", "helloworld")308 )309 )301 monkeypatch.chdir(test_path / "helloworld")302 obj = ScriptInfo()303 app = obj.load_app()304 assert app.name == "hello"305306 # import app from app.py in current directory315 monkeypatch.chdir(316 os.path.abspath(os.path.join(os.path.dirname(__file__), "test_apps", "cliapp"))317 )307 monkeypatch.chdir(test_path / "cliapp")308 obj = ScriptInfo()309 app = obj.load_app()310 assert app.name == "testapp"311
190 unmodified lines
502503 monkeypatch.setenv("EGGS", "3")504 monkeypatch.chdir(test_path)505 assert load_dotenv()516 assert os.getcwd() == test_path506 assert Path.cwd() == test_path507 # .flaskenv doesn't overwrite .env508 assert os.environ["FOO"] == "env"509 # set only in .flaskenv510 assert os.environ["BAR"] == "bar"
11 unmodified lines
522def test_dotenv_path(monkeypatch):523 for item in ("FOO", "BAR", "EGGS"):524 monkeypatch._setitem.append((os.environ, item, notset))525536 cwd = os.getcwd()537 load_dotenv(os.path.join(test_path, ".flaskenv"))538 assert os.getcwd() == cwd526 load_dotenv(test_path / ".flaskenv")527 assert Path.cwd() == cwd528 assert "FOO" in os.environ529530531def test_dotenv_optional(monkeypatch):
130 unmodified lines