Skip to content

Commit 8b8d983

Browse files
committed
Resolve impl_loader issue if step_impl is the first part of the module to import
Signed-off-by: sschulz92 <[email protected]>
1 parent 9a063f7 commit 8b8d983

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed

getgauge/impl_loader.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import sys
99
import traceback
1010
from contextlib import contextmanager
11-
from importlib import util as importlib_util
1211
from os import path
1312
from pathlib import Path
1413
from typing import Optional
@@ -31,21 +30,22 @@
3130

3231
def load_impls(step_impl_dirs=impl_dirs, project_root=project_root):
3332
""" project_root can be overwritten in tests! """
33+
3434
os.chdir(project_root)
35-
logger.debug('Project root: {}'.format(project_root))
35+
3636
for impl_dir in step_impl_dirs:
37+
3738
resolved_impl_dir = Path(impl_dir).resolve()
3839
if not resolved_impl_dir.is_dir():
3940
logger.error('Cannot import step implementations. Error: {} does not exist.'.format(impl_dir))
4041
logger.error('Make sure `STEP_IMPL_DIR` env var is set to a valid directory path.')
4142
return
4243

43-
base_dir = project_root if str(resolved_impl_dir).startswith(project_root) else os.path.dirname(resolved_impl_dir)
44+
base_dir = os.path.commonpath([project_root, f"{resolved_impl_dir}"])
45+
logger.debug("Base directory '{}' of '{}'".format(base_dir, resolved_impl_dir))
4446

45-
# Add temporary sys path for imports outside the project root
4647
temporary_sys_path = None
47-
if base_dir != project_root:
48-
logger.debug('Found different base directory compared to the project root: {}'.format(base_dir, f"{resolved_impl_dir}"))
48+
if project_root != base_dir:
4949
temporary_sys_path = base_dir
5050

5151
_import_impl(base_dir, resolved_impl_dir, temporary_sys_path)
@@ -68,10 +68,8 @@ def copy_skel_files():
6868

6969
def _import_impl(base_dir: str, absolute_step_impl_dir: str, temporary_sys_path: Optional[str]):
7070
for python_file in glob.glob(f"{absolute_step_impl_dir}/**/*.py", recursive=True):
71-
if python_file.endswith("__init__.py"):
72-
continue
73-
relative_path = os.path.normpath(python_file.replace(base_dir + os.path.sep, ''))
74-
module_name = os.path.splitext(relative_path.replace(os.path.sep, '.'))[0]
71+
relative_path = Path(python_file).relative_to(base_dir)
72+
module_name = ".".join(relative_path.parts).replace(".py", "")
7573
_import_file(module_name, python_file, temporary_sys_path)
7674

7775
@contextmanager

tests/test_impl_loader.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@
55
from getgauge.impl_loader import load_impls
66
from getgauge.registry import registry
77

8+
DIRECTORY_NAME = "test_relative_import"
9+
810

911
class ImplLoaderTest(unittest.TestCase):
1012

1113
def test_update_step_registry_with_class(self):
1214

13-
tests_directory = str(Path(__file__).resolve().parent)
14-
relative_file_path = os.path.join('..', 'test_relative_import')
15+
test_relative_import_directory = str(Path(__file__).resolve().parent / DIRECTORY_NAME)
16+
relative_file_path = os.path.join('..', DIRECTORY_NAME)
1517

1618
load_impls(
1719
step_impl_dirs=[relative_file_path],
18-
project_root=tests_directory
20+
project_root=test_relative_import_directory
1921
)
2022

2123
loaded_steps = registry.get_steps_map()
@@ -35,7 +37,7 @@ def test_update_step_registry_with_class(self):
3537
def test_update_step_registry_with_class_one_level_above(self):
3638

3739
repo_root_directory = str(Path(__file__).resolve().parent.parent)
38-
relative_file_path_one_level_above = os.path.join('tests', '..', 'test_relative_import')
40+
relative_file_path_one_level_above = os.path.join('tests', '..', 'tests', DIRECTORY_NAME)
3941

4042
load_impls(
4143
step_impl_dirs=[relative_file_path_one_level_above],
@@ -44,7 +46,7 @@ def test_update_step_registry_with_class_one_level_above(self):
4446

4547
loaded_steps = registry.get_steps_map()
4648

47-
self.assertEqual(2, len(loaded_steps))
49+
self.assertEqual(2, len(loaded_steps), f"Steps found: {loaded_steps}")
4850

4951
step_infos_of_class_instance = loaded_steps["Greet {} from inside the class"]
5052

@@ -56,6 +58,9 @@ def test_update_step_registry_with_class_one_level_above(self):
5658
registry.steps()
5759
)
5860

61+
def tearDown(self):
62+
registry.clear()
63+
5964

6065
if __name__ == '__main__':
6166
unittest.main()
File renamed without changes.

0 commit comments

Comments
 (0)