|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import os |
| 16 | +import pathlib |
15 | 17 | import sys |
16 | 18 |
|
17 | 19 | import pretend |
18 | 20 | import pytest |
19 | 21 |
|
20 | 22 | from click.testing import CliRunner |
| 23 | +from starlette.applications import Starlette |
21 | 24 |
|
22 | 25 | import functions_framework |
23 | 26 | import functions_framework._function_registry as _function_registry |
|
26 | 29 | from functions_framework._cli import _cli |
27 | 30 |
|
28 | 31 |
|
| 32 | +@pytest.fixture |
| 33 | +def clean_registries(): |
| 34 | + """Clean up both REGISTRY_MAP and ASGI_FUNCTIONS registries.""" |
| 35 | + original_registry_map = _function_registry.REGISTRY_MAP.copy() |
| 36 | + original_asgi = _function_registry.ASGI_FUNCTIONS.copy() |
| 37 | + _function_registry.REGISTRY_MAP.clear() |
| 38 | + _function_registry.ASGI_FUNCTIONS.clear() |
| 39 | + yield |
| 40 | + _function_registry.REGISTRY_MAP.clear() |
| 41 | + _function_registry.REGISTRY_MAP.update(original_registry_map) |
| 42 | + _function_registry.ASGI_FUNCTIONS.clear() |
| 43 | + _function_registry.ASGI_FUNCTIONS.update(original_asgi) |
| 44 | + |
| 45 | + |
29 | 46 | def test_cli_no_arguments(): |
30 | 47 | runner = CliRunner() |
31 | 48 | result = runner.invoke(_cli) |
@@ -128,47 +145,17 @@ def test_asgi_cli(monkeypatch): |
128 | 145 | assert asgi_server.run.calls == [pretend.call("0.0.0.0", 8080)] |
129 | 146 |
|
130 | 147 |
|
131 | | -@pytest.fixture |
132 | | -def clean_registry(): |
133 | | - """Save and restore function registry state.""" |
134 | | - original_asgi = _function_registry.ASGI_FUNCTIONS.copy() |
135 | | - _function_registry.ASGI_FUNCTIONS.clear() |
136 | | - yield |
137 | | - _function_registry.ASGI_FUNCTIONS.clear() |
138 | | - _function_registry.ASGI_FUNCTIONS.update(original_asgi) |
139 | | - |
140 | | - |
141 | | -def test_auto_asgi_for_aio_decorated_functions(monkeypatch, clean_registry): |
142 | | - _function_registry.ASGI_FUNCTIONS.add("my_aio_func") |
143 | | - |
144 | | - asgi_app = pretend.stub() |
145 | | - create_asgi_app = pretend.call_recorder(lambda *a, **k: asgi_app) |
146 | | - aio_module = pretend.stub(create_asgi_app=create_asgi_app) |
147 | | - monkeypatch.setitem(sys.modules, "functions_framework.aio", aio_module) |
148 | | - |
149 | | - asgi_server = pretend.stub(run=pretend.call_recorder(lambda host, port: None)) |
150 | | - create_server = pretend.call_recorder(lambda app, debug: asgi_server) |
151 | | - monkeypatch.setattr(functions_framework._cli, "create_server", create_server) |
152 | | - |
153 | | - runner = CliRunner() |
154 | | - result = runner.invoke(_cli, ["--target", "my_aio_func"]) |
155 | | - |
156 | | - assert create_asgi_app.calls == [pretend.call("my_aio_func", None, "http")] |
157 | | - assert asgi_server.run.calls == [pretend.call("0.0.0.0", 8080)] |
158 | | - |
159 | | - |
160 | | -def test_no_auto_asgi_for_regular_functions(monkeypatch, clean_registry): |
| 148 | +def test_cli_auto_detects_asgi_decorator(clean_registries): |
| 149 | + """Test that CLI auto-detects @aio decorated functions without --asgi flag.""" |
| 150 | + # Use the actual async_decorator.py test file which has @aio.http decorated functions |
| 151 | + test_functions_dir = pathlib.Path(__file__).parent / "test_functions" / "decorators" |
| 152 | + source = test_functions_dir / "async_decorator.py" |
161 | 153 |
|
162 | | - app = pretend.stub() |
163 | | - create_app = pretend.call_recorder(lambda *a, **k: app) |
164 | | - monkeypatch.setattr(functions_framework._cli, "create_app", create_app) |
165 | | - |
166 | | - flask_server = pretend.stub(run=pretend.call_recorder(lambda host, port: None)) |
167 | | - create_server = pretend.call_recorder(lambda app, debug: flask_server) |
168 | | - monkeypatch.setattr(functions_framework._cli, "create_server", create_server) |
| 154 | + # Call create_app without any asgi flag - should auto-detect |
| 155 | + app = functions_framework.create_app(target="function_http", source=str(source)) |
169 | 156 |
|
170 | | - runner = CliRunner() |
171 | | - result = runner.invoke(_cli, ["--target", "regular_func"]) |
| 157 | + # Verify it created a Starlette app (ASGI) |
| 158 | + assert isinstance(app, Starlette) |
172 | 159 |
|
173 | | - assert create_app.calls == [pretend.call("regular_func", None, "http")] |
174 | | - assert flask_server.run.calls == [pretend.call("0.0.0.0", 8080)] |
| 160 | + # Verify the function was registered in ASGI_FUNCTIONS |
| 161 | + assert "function_http" in _function_registry.ASGI_FUNCTIONS |
0 commit comments