Skip to content

Commit

Permalink
remove deprecated _URL stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabien Coelho committed May 20, 2024
1 parent 9974a61 commit 474517f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 24 deletions.
20 changes: 11 additions & 9 deletions FlaskTester.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,28 +535,30 @@ def _ft_client(authenticator):
default_login = os.environ.get("FLASK_TESTER_DEFAULT", None)
client: Client

app_def = os.environ.get("FLASK_TESTER_URL", "app")
app_def = os.environ.get("FLASK_TESTER_APP", app_def)
test_app = os.environ.get("FLASK_TESTER_APP", "app")

if app_def.startswith("http://") or app_def.startswith("https://"):
client = RequestClient(authenticator, app_def, default_login)
if test_app.startswith("http://") or test_app.startswith("https://"):
client = RequestClient(authenticator, test_app, default_login)
else:
# load app package
pkg_name, app = app_def, None
app_names = ["app", "application", "create_app", "make_app"]
if ":" in pkg_name: # override defaults
pkg_name, app_name = pkg_name.split(":", 1)
if ":" in test_app: # override defaults
pkg_name, app_name = test_app.split(":", 1)
app_names = [app_name]
else:
pkg_name = test_app
app_names = ["app", "application", "create_app", "make_app"]
pkg = importlib.import_module(pkg_name)
# find app in package
app = None
for name in app_names:
if hasattr(pkg, name):
app = getattr(pkg, name)
if callable(app) and not hasattr(app, "test_client"):
app = app()
break
# none found
if not app:
raise FlaskTesterError(f"cannot find Flask app in {pkg_name}")
raise FlaskTesterError(f"cannot find Flask app in {pkg_name} ({test_app})")
client = FlaskClient(authenticator, app.test_client(), default_login)

return client
Expand Down
7 changes: 2 additions & 5 deletions docs/versions.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ Packages are distributed from [PyPI](https://pypi.org/project/FlaskTester/),
see also the [documentation](https://zx80.github.io/flask-tester/),
please report any [issues](https://github.com/zx80/flask-tester/issues).

## TODO

- remove deprecated features on 4.0

## ? on ?
## 4.0 on ?

Improved documentation and tests.
Remove deprecated `FLASK_TESTER_URL`, simplifying code in passing.

## 3.6 on 2024-03-30

Expand Down
2 changes: 1 addition & 1 deletion tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ SEED := $(shell head -c 33 /dev/urandom | base64)
check.external:
source $(VENV)/bin/activate
export TEST_SEED="$(SEED)"
export FLASK_TESTER_URL="http://localhost:$(PORT)"
export FLASK_TESTER_APP="http://localhost:$(PORT)"
# app
flask --app app run --port=$(PORT) &
flask_pid=$$!
Expand Down
12 changes: 3 additions & 9 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

def test_sanity():
# must provide url or package of Flask application to test
assert "FLASK_TESTER_URL" in os.environ or "FLASK_TESTER_APP" in os.environ
assert "FLASK_TESTER_APP" in os.environ
# log.debug(f"TEST_SEED={os.environ.get('TEST_SEED')}")

# example from README.md
Expand Down Expand Up @@ -309,21 +309,17 @@ def test_client_fixture():
# ft_client coverage
auth = ft._ft_authenticator()
# check and save env
url = None
if "FLASK_TESTER_URL" in os.environ: # pragma: no cover
url = os.environ["FLASK_TESTER_URL"]
del os.environ["FLASK_TESTER_URL"]
app = None
if "FLASK_TESTER_APP" in os.environ:
app = os.environ["FLASK_TESTER_APP"]
del os.environ["FLASK_TESTER_APP"]
# no url nor app, defaults to "app"
init = ft._ft_client(auth)
# url
os.environ["FLASK_TESTER_URL"] = "http://localhost:5000"
os.environ["FLASK_TESTER_APP"] = "http://localhost:5000"
init = ft._ft_client(auth)
assert isinstance(init, ft.RequestClient)
del os.environ["FLASK_TESTER_URL"]
del os.environ["FLASK_TESTER_APP"]
# bad package
os.environ["FLASK_TESTER_APP"] = "no_such_package"
try:
Expand All @@ -340,7 +336,5 @@ def test_client_fixture():
assert True, "expected error raised"
del os.environ["FLASK_TESTER_APP"]
# reset env
if url: # pragma: no cover
os.environ["FLASK_TESTER_URL"] = url
if app:
os.environ["FLASK_TESTER_APP"] = app

0 comments on commit 474517f

Please sign in to comment.