Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly loading the methods from relatively imported classes #364

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
zabil marked this conversation as resolved.
Show resolved Hide resolved
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()
Loading