Skip to content

Commit

Permalink
Correctly loading the methods from relatively imported classes (#364)
Browse files Browse the repository at this point in the history
* 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](python/importlib_metadata@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](pypa/setuptools@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] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: Kunal Vishwasrao <kunal.vishwasrao@gmail.com>

* 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](microsoft/debugpy@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](python/importlib_metadata@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](pypa/setuptools@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] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: Kunal Vishwasrao <kunal.vishwasrao@gmail.com>

* Support for relative imports to reuse step impls

Signed-off-by: Kunal Vishwasrao <kunal.vishwasrao@gmail.com>

* Update release dependencies

Signed-off-by: BugDiver <vinayshankar00@gmail.com>
Signed-off-by: Kunal Vishwasrao <kunal.vishwasrao@gmail.com>

* Correctly loading the methods from relatively imported classs (addition to #360)

Signed-off-by: Kunal Vishwasrao <kunal.vishwasrao@gmail.com>

* Update getgauge/impl_loader.py

Correctly loading the methods from relatively imported classes (#365)

Signed-off-by: Kunal Vishwasrao <kunal.vishwasrao@gmail.com>

Co-authored-by: Zabil Cheriya Maliackal <zabil@users.noreply.github.com>
Signed-off-by: Kunal Vishwasrao <kunal.vishwasrao@gmail.com>

* Returning method_list in update_step_resgistry_with_class method (#365)

Signed-off-by: Kunal Vishwasrao <kunal.vishwasrao@gmail.com>

* Adding test class for relative import unittest (#365)

Signed-off-by: Kunal Vishwasrao <kunal.vishwasrao@gmail.com>

* Adding unittest for relative import (#365)

Signed-off-by: Kunal Vishwasrao <kunal.vishwasrao@gmail.com>

* Renaming method to update_step_registry_with_class (#365)

Signed-off-by: Kunal Vishwasrao <kunal.vishwasrao@gmail.com>

* Updating unittest for impl loader (#365)

Signed-off-by: Kunal Vishwasrao <kunal.vishwasrao@gmail.com>

* Bumping up python package version (#365)

Signed-off-by: Kunal Vishwasrao <kunal.vishwasrao@gmail.com>

---------

Signed-off-by: dependabot[bot] <support@github.com>
Signed-off-by: Kunal Vishwasrao <kunal.vishwasrao@gmail.com>
Signed-off-by: BugDiver <vinayshankar00@gmail.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Vinay Shukla <vinayshankar00@gmail.com>
Co-authored-by: Zabil Cheriya Maliackal <zabil@users.noreply.github.com>
  • Loading branch information
4 people authored Jul 6, 2024
1 parent aca13b1 commit 76450d7
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 5 deletions.
11 changes: 7 additions & 4 deletions getgauge/impl_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion python.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "python",
"version": "0.4.4",
"version": "0.4.5",
"description": "Python support for gauge",
"run": {
"windows": [
Expand Down
Empty file.
21 changes: 21 additions & 0 deletions test_relative_import/relative_import_class.py
Original file line number Diff line number Diff line change
@@ -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 <name> 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 <name> from outside the class')
def greetings_from_outside_the_class(name):
Messages.write_message("Hello from outside the class, {0}".format(name))
23 changes: 23 additions & 0 deletions tests/test_impl_loader.py
Original file line number Diff line number Diff line change
@@ -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 <name> from inside the class",
"Greet <name> from outside the class"],
[method.step_text for method in method_list])


if __name__ == '__main__':
unittest.main()

0 comments on commit 76450d7

Please sign in to comment.