Skip to content

Commit 76450d7

Browse files
kunalvishwasraodependabot[bot]BugDiverzabil
authored
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](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] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Signed-off-by: Kunal Vishwasrao <[email protected]> * 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] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Signed-off-by: Kunal Vishwasrao <[email protected]> * Support for relative imports to reuse step impls Signed-off-by: Kunal Vishwasrao <[email protected]> * Update release dependencies Signed-off-by: BugDiver <[email protected]> Signed-off-by: Kunal Vishwasrao <[email protected]> * Correctly loading the methods from relatively imported classs (addition to #360) Signed-off-by: Kunal Vishwasrao <[email protected]> * Update getgauge/impl_loader.py Correctly loading the methods from relatively imported classes (#365) Signed-off-by: Kunal Vishwasrao <[email protected]> Co-authored-by: Zabil Cheriya Maliackal <[email protected]> Signed-off-by: Kunal Vishwasrao <[email protected]> * Returning method_list in update_step_resgistry_with_class method (#365) Signed-off-by: Kunal Vishwasrao <[email protected]> * Adding test class for relative import unittest (#365) Signed-off-by: Kunal Vishwasrao <[email protected]> * Adding unittest for relative import (#365) Signed-off-by: Kunal Vishwasrao <[email protected]> * Renaming method to update_step_registry_with_class (#365) Signed-off-by: Kunal Vishwasrao <[email protected]> * Updating unittest for impl loader (#365) Signed-off-by: Kunal Vishwasrao <[email protected]> * Bumping up python package version (#365) Signed-off-by: Kunal Vishwasrao <[email protected]> --------- Signed-off-by: dependabot[bot] <[email protected]> Signed-off-by: Kunal Vishwasrao <[email protected]> Signed-off-by: BugDiver <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Vinay Shukla <[email protected]> Co-authored-by: Zabil Cheriya Maliackal <[email protected]>
1 parent aca13b1 commit 76450d7

File tree

5 files changed

+52
-5
lines changed

5 files changed

+52
-5
lines changed

getgauge/impl_loader.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,17 +91,20 @@ def _import_file(base_dir, file_path):
9191
file = inspect.getfile(c[1])
9292
# Create instance of step implementation class.
9393
if _has_methods_with_gauge_decoratores(c[1]):
94-
update_step_resgistry_with_class(c[1](), file_path) # c[1]() will create a new instance of the class
94+
update_step_registry_with_class(c[1](), file_path) # c[1]() will create a new instance of the class
9595
except:
9696
logger.fatal('Exception occurred while loading step implementations from file: {}.\n{}'.format(rel_path, traceback.format_exc()))
9797

9898
# Inject instace in each class method (hook/step)
99-
def update_step_resgistry_with_class(instance, file_path):
100-
for info in registry.get_all_methods_in(file_path):
99+
def update_step_registry_with_class(instance, file_path):
100+
# Resolve the absolute path from relative path
101+
file_path = os.path.abspath(file_path) if '..' in file_path else file_path
102+
method_list = registry.get_all_methods_in(file_path)
103+
for info in method_list:
101104
class_methods = [x[0] for x in inspect.getmembers(instance, inspect.ismethod)]
102105
if info.impl.__name__ in class_methods:
103106
info.instance = instance
104-
107+
return method_list
105108

106109
def _get_version():
107110
json_data = open(PLUGIN_JSON).read()

python.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "python",
3-
"version": "0.4.4",
3+
"version": "0.4.5",
44
"description": "Python support for gauge",
55
"run": {
66
"windows": [

test_relative_import/__init__.py

Whitespace-only changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from getgauge.python import step, Messages
2+
3+
4+
class BaseSample:
5+
def __init__(self) -> None:
6+
pass
7+
8+
class Sample(BaseSample):
9+
def __init__(self) -> None:
10+
pass
11+
12+
# Gauge step implementation in a class
13+
@step('Greet <name> from inside the class')
14+
def greetings_from_class(self, name):
15+
Messages.write_message("Hello from inside the class, {0}".format(name))
16+
17+
18+
# Gauge step implementation outside class
19+
@step('Greet <name> from outside the class')
20+
def greetings_from_outside_the_class(name):
21+
Messages.write_message("Hello from outside the class, {0}".format(name))

tests/test_impl_loader.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import os
2+
import unittest
3+
4+
from test_relative_import.relative_import_class import Sample
5+
from getgauge.impl_loader import update_step_registry_with_class
6+
7+
8+
class ImplLoaderTest(unittest.TestCase):
9+
def setUp(self):
10+
self.relative_file_path = os.path.join('..', 'test_relative_import', 'relative_import_class.py')
11+
12+
def test_update_step_resgistry_with_class(self):
13+
curr_dir = os.getcwd()
14+
os.chdir('tests')
15+
method_list = update_step_registry_with_class(Sample(), self.relative_file_path)
16+
os.chdir(curr_dir)
17+
self.assertEqual(["Greet <name> from inside the class",
18+
"Greet <name> from outside the class"],
19+
[method.step_text for method in method_list])
20+
21+
22+
if __name__ == '__main__':
23+
unittest.main()

0 commit comments

Comments
 (0)