diff --git a/getgauge/impl_loader.py b/getgauge/impl_loader.py index 30f39bb..07b04cf 100644 --- a/getgauge/impl_loader.py +++ b/getgauge/impl_loader.py @@ -91,17 +91,20 @@ def _import_file(base_dir, file_path): file = inspect.getfile(c[1]) # Create instance of step implementation class. if _has_methods_with_gauge_decoratores(c[1]): - update_step_resgistry_with_class(c[1](), file_path) # c[1]() will create a new instance of the class + update_step_registry_with_class(c[1](), file_path) # c[1]() will create a new instance of the class except: logger.fatal('Exception occurred while loading step implementations from file: {}.\n{}'.format(rel_path, traceback.format_exc())) # Inject instace in each class method (hook/step) -def update_step_resgistry_with_class(instance, file_path): - for info in registry.get_all_methods_in(file_path): +def update_step_registry_with_class(instance, file_path): + # Resolve the absolute path from relative path + file_path = os.path.abspath(file_path) if '..' in file_path else file_path + method_list = registry.get_all_methods_in(file_path) + for info in method_list: class_methods = [x[0] for x in inspect.getmembers(instance, inspect.ismethod)] if info.impl.__name__ in class_methods: info.instance = instance - + return method_list def _get_version(): json_data = open(PLUGIN_JSON).read() diff --git a/python.json b/python.json index b42e11f..7d143cc 100644 --- a/python.json +++ b/python.json @@ -1,6 +1,6 @@ { "id": "python", - "version": "0.4.4", + "version": "0.4.5", "description": "Python support for gauge", "run": { "windows": [ diff --git a/test_relative_import/__init__.py b/test_relative_import/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/test_relative_import/relative_import_class.py b/test_relative_import/relative_import_class.py new file mode 100644 index 0000000..35662f5 --- /dev/null +++ b/test_relative_import/relative_import_class.py @@ -0,0 +1,21 @@ +from getgauge.python import step, Messages + + +class BaseSample: + def __init__(self) -> None: + pass + +class Sample(BaseSample): + def __init__(self) -> None: + pass + + # Gauge step implementation in a class + @step('Greet from inside the class') + def greetings_from_class(self, name): + Messages.write_message("Hello from inside the class, {0}".format(name)) + + +# Gauge step implementation outside class +@step('Greet from outside the class') +def greetings_from_outside_the_class(name): + Messages.write_message("Hello from outside the class, {0}".format(name)) \ No newline at end of file diff --git a/tests/test_impl_loader.py b/tests/test_impl_loader.py new file mode 100644 index 0000000..8d82d32 --- /dev/null +++ b/tests/test_impl_loader.py @@ -0,0 +1,23 @@ +import os +import unittest + +from test_relative_import.relative_import_class import Sample +from getgauge.impl_loader import update_step_registry_with_class + + +class ImplLoaderTest(unittest.TestCase): + def setUp(self): + self.relative_file_path = os.path.join('..', 'test_relative_import', 'relative_import_class.py') + + def test_update_step_resgistry_with_class(self): + curr_dir = os.getcwd() + os.chdir('tests') + method_list = update_step_registry_with_class(Sample(), self.relative_file_path) + os.chdir(curr_dir) + self.assertEqual(["Greet from inside the class", + "Greet from outside the class"], + [method.step_text for method in method_list]) + + +if __name__ == '__main__': + unittest.main()