Skip to content

Commit 4da437d

Browse files
authored
Add a default script_file for function.json without scriptFile (#893)
1 parent 7630661 commit 4da437d

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

azure_functions_worker/loader.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
import pathlib
1212
import sys
1313
import typing
14+
from os import PathLike, fspath
1415

1516
from .constants import MODULE_NOT_FOUND_TS_URL
1617
from .utils.wrappers import attach_message_to_exception
17-
from os import PathLike, fspath
18-
1918

2019
_AZURE_NAMESPACE = '__app__'
20+
_DEFAULT_SCRIPT_FILENAME = '__init__.py'
21+
_DEFAULT_ENTRY_POINT = 'main'
2122

2223
_submodule_dirs = []
2324

@@ -50,9 +51,10 @@ def uninstall() -> None:
5051
def load_function(name: str, directory: str, script_file: str,
5152
entry_point: typing.Optional[str]):
5253
dir_path = pathlib.Path(directory)
53-
script_path = pathlib.Path(script_file)
54+
script_path = pathlib.Path(script_file) if script_file else pathlib.Path(
55+
_DEFAULT_SCRIPT_FILENAME)
5456
if not entry_point:
55-
entry_point = 'main'
57+
entry_point = _DEFAULT_ENTRY_POINT
5658

5759
register_function_dir(dir_path.parent)
5860

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"bindings": [
3+
{
4+
"type": "httpTrigger",
5+
"direction": "in",
6+
"name": "req"
7+
},
8+
{
9+
"type": "http",
10+
"direction": "out",
11+
"name": "$return"
12+
}
13+
]
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
5+
def main(req) -> str:
6+
return __name__

tests/unittests/test_loader.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ def test_loader_custom_entrypoint(self):
2525
self.assertEqual(r.status_code, 200)
2626
self.assertEqual(r.text, '__app__.entrypoint.main')
2727

28+
def test_loader_no_script_file(self):
29+
r = self.webhost.request('GET', 'no_script_file')
30+
self.assertEqual(r.status_code, 200)
31+
self.assertEqual(r.text, '__app__.no_script_file.main')
32+
2833
def test_loader_subdir(self):
2934
r = self.webhost.request('GET', 'subdir')
3035
self.assertEqual(r.status_code, 200)

0 commit comments

Comments
 (0)