From 65842a4938d14b661ad357c1a5b1c52a27691fdc Mon Sep 17 00:00:00 2001 From: Dustin Ingram Date: Wed, 17 Nov 2021 13:04:10 -0500 Subject: [PATCH 1/2] Add a failing test --- tests/test_functions.py | 11 ++++++++++ tests/test_functions/relative_imports/main.py | 22 +++++++++++++++++++ tests/test_functions/relative_imports/test.py | 1 + 3 files changed, 34 insertions(+) create mode 100644 tests/test_functions/relative_imports/main.py create mode 100644 tests/test_functions/relative_imports/test.py diff --git a/tests/test_functions.py b/tests/test_functions.py index 64ecc794..c343205f 100644 --- a/tests/test_functions.py +++ b/tests/test_functions.py @@ -604,3 +604,14 @@ def tests_cloud_to_background_event_client_invalid_source( resp = background_event_client.post("/", headers=headers, json=tempfile_payload) assert resp.status_code == 500 + + +def test_relative_imports(): + source = TEST_FUNCTIONS_DIR / "relative_imports" / "main.py" + target = "function" + + client = create_app(target, source).test_client() + + resp = client.get("/") + assert resp.status_code == 200 + assert resp.data == b"success" diff --git a/tests/test_functions/relative_imports/main.py b/tests/test_functions/relative_imports/main.py new file mode 100644 index 00000000..3c28ff1a --- /dev/null +++ b/tests/test_functions/relative_imports/main.py @@ -0,0 +1,22 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Function used in test for relative imports.""" + +from .test import foo + + +def function(request): + """Test HTTP function who returns a value from a relative import""" + return foo diff --git a/tests/test_functions/relative_imports/test.py b/tests/test_functions/relative_imports/test.py new file mode 100644 index 00000000..862c735d --- /dev/null +++ b/tests/test_functions/relative_imports/test.py @@ -0,0 +1 @@ +foo = "success" From f4e709620bf133b4367683bc4516b43851d7acf3 Mon Sep 17 00:00:00 2001 From: Dustin Ingram Date: Wed, 17 Nov 2021 13:06:00 -0500 Subject: [PATCH 2/2] Support relative imports for submodules --- src/functions_framework/_function_registry.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/functions_framework/_function_registry.py b/src/functions_framework/_function_registry.py index f7869b24..cedb7e15 100644 --- a/src/functions_framework/_function_registry.py +++ b/src/functions_framework/_function_registry.py @@ -62,7 +62,9 @@ def load_function_module(source): directory, filename = os.path.split(realpath) name, extension = os.path.splitext(filename) # 2. Create a new module - spec = importlib.util.spec_from_file_location(name, realpath) + spec = importlib.util.spec_from_file_location( + name, realpath, submodule_search_locations=[directory] + ) source_module = importlib.util.module_from_spec(spec) # 3. Add the directory of the source to sys.path to allow the function to # load modules relative to its location