From 76450d71a6b7e2294b81ab2256bbc68c3bd674c1 Mon Sep 17 00:00:00 2001 From: Kunal Vishwasrao <36107786+kunalvishwasrao@users.noreply.github.com> Date: Sun, 7 Jul 2024 02:05:33 +0530 Subject: [PATCH] Correctly loading the methods from relatively imported classes (#364) * Bump the pip-dependencies group with 2 updates (#358) Bumps the pip-dependencies group with 2 updates: [importlib-metadata](https://github.com/python/importlib_metadata) and [setuptools](https://github.com/pypa/setuptools). Updates `importlib-metadata` from 7.1.0 to 7.2.1 - [Release notes](https://github.com/python/importlib_metadata/releases) - [Changelog](https://github.com/python/importlib_metadata/blob/main/NEWS.rst) - [Commits](https://github.com/python/importlib_metadata/compare/v7.1.0...v7.2.1) Updates `setuptools` from 70.0.0 to 70.1.0 - [Release notes](https://github.com/pypa/setuptools/releases) - [Changelog](https://github.com/pypa/setuptools/blob/main/NEWS.rst) - [Commits](https://github.com/pypa/setuptools/compare/v70.0.0...v70.1.0) --- updated-dependencies: - dependency-name: importlib-metadata dependency-type: direct:production update-type: version-update:semver-minor dependency-group: pip-dependencies - dependency-name: setuptools dependency-type: direct:production update-type: version-update:semver-minor dependency-group: pip-dependencies ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Signed-off-by: Kunal Vishwasrao * Bump the pip-dependencies group across 1 directory with 3 updates (#362) Bumps the pip-dependencies group with 3 updates in the / directory: [debugpy](https://github.com/microsoft/debugpy), [importlib-metadata](https://github.com/python/importlib_metadata) and [setuptools](https://github.com/pypa/setuptools). Updates `debugpy` from 1.8.1 to 1.8.2 - [Release notes](https://github.com/microsoft/debugpy/releases) - [Commits](https://github.com/microsoft/debugpy/compare/v1.8.1...v1.8.2) Updates `importlib-metadata` from 7.2.1 to 8.0.0 - [Release notes](https://github.com/python/importlib_metadata/releases) - [Changelog](https://github.com/python/importlib_metadata/blob/main/NEWS.rst) - [Commits](https://github.com/python/importlib_metadata/compare/v7.2.1...v8.0.0) Updates `setuptools` from 70.1.0 to 70.1.1 - [Release notes](https://github.com/pypa/setuptools/releases) - [Changelog](https://github.com/pypa/setuptools/blob/main/NEWS.rst) - [Commits](https://github.com/pypa/setuptools/compare/v70.1.0...v70.1.1) --- updated-dependencies: - dependency-name: debugpy dependency-type: direct:production update-type: version-update:semver-patch dependency-group: pip-dependencies - dependency-name: importlib-metadata dependency-type: direct:production update-type: version-update:semver-major dependency-group: pip-dependencies - dependency-name: setuptools dependency-type: direct:production update-type: version-update:semver-patch dependency-group: pip-dependencies ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Signed-off-by: Kunal Vishwasrao * Support for relative imports to reuse step impls Signed-off-by: Kunal Vishwasrao * Update release dependencies Signed-off-by: BugDiver Signed-off-by: Kunal Vishwasrao * Correctly loading the methods from relatively imported classs (addition to #360) Signed-off-by: Kunal Vishwasrao * Update getgauge/impl_loader.py Correctly loading the methods from relatively imported classes (#365) Signed-off-by: Kunal Vishwasrao Co-authored-by: Zabil Cheriya Maliackal Signed-off-by: Kunal Vishwasrao * Returning method_list in update_step_resgistry_with_class method (#365) Signed-off-by: Kunal Vishwasrao * Adding test class for relative import unittest (#365) Signed-off-by: Kunal Vishwasrao * Adding unittest for relative import (#365) Signed-off-by: Kunal Vishwasrao * Renaming method to update_step_registry_with_class (#365) Signed-off-by: Kunal Vishwasrao * Updating unittest for impl loader (#365) Signed-off-by: Kunal Vishwasrao * Bumping up python package version (#365) Signed-off-by: Kunal Vishwasrao --------- Signed-off-by: dependabot[bot] Signed-off-by: Kunal Vishwasrao Signed-off-by: BugDiver Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Vinay Shukla Co-authored-by: Zabil Cheriya Maliackal --- getgauge/impl_loader.py | 11 +++++---- python.json | 2 +- test_relative_import/__init__.py | 0 test_relative_import/relative_import_class.py | 21 +++++++++++++++++ tests/test_impl_loader.py | 23 +++++++++++++++++++ 5 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 test_relative_import/__init__.py create mode 100644 test_relative_import/relative_import_class.py create mode 100644 tests/test_impl_loader.py 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()