Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix import module order issue #652

Merged
merged 4 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion aiohttp_devtools/runserver/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def import_app_factory(self) -> AppFactory:
rel_py_file = self.py_file.relative_to(self.python_path)
module_path = '.'.join(rel_py_file.with_suffix('').parts)

sys.path.append(str(self.python_path))
sys.path.insert(0, str(self.python_path))
module = import_module(module_path)
# Rewrite the package name, so it will appear the same as running the app.
if module.__package__:
Expand Down
39 changes: 39 additions & 0 deletions tests/test_runserver_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
from aiohttp import ClientTimeout
from pytest_toolbox import mktree

from multiprocessing import set_start_method

from aiohttp_devtools.runserver import runserver
from aiohttp_devtools.runserver.config import Config
from aiohttp_devtools.runserver.serve import (
WS, create_auxiliary_app, create_main_app, modify_main_app, src_reload, start_main_app)
from aiohttp_devtools.runserver.watch import AppTask

from .conftest import SIMPLE_APP, forked

Expand Down Expand Up @@ -112,6 +115,42 @@ async def hello(request):
assert len(aux_app.cleanup_ctx) == 1


@forked
def test_start_runserver_with_multi_app_modules(tmpworkdir, event_loop, capfd):
mktree(tmpworkdir, {
"app.py": f"""\
from aiohttp import web
import sys
sys.path.insert(0, "{tmpworkdir}/libs/l1")

async def hello(request):
return web.Response(text="<h1>hello world</h1>", content_type="text/html")

async def create_app():
a = web.Application()
a.router.add_get("/", hello)
return a
""",
"libs": {
"l1": {
"__init__.py": "",
"app.py": "print('wrong_import')"
}
}
})

set_start_method("spawn")
config = Config(app_path="app.py", root_path=tmpworkdir, main_port=0, app_factory_name="create_app")
config.import_app_factory()
app_task = AppTask(config)

app_task._start_dev_server()
app_task._process.join(2)

captured = capfd.readouterr()
assert captured.out == ""


@forked
async def test_run_app_aiohttp_client(tmpworkdir, aiohttp_client):
mktree(tmpworkdir, SIMPLE_APP)
Expand Down