getgauge / gauge-python

Python language runner for Gauge
MIT License
90 stars 38 forks source link

Unable to correctly loading the methods from relatively imported classes #365

Closed kunalvishwasrao closed 4 months ago

kunalvishwasrao commented 5 months ago

Description: After merging #360 all the previous issues regarding relatively importing step implementations from another project were fixed but when I created step implementations inside a class in different project and tried use those steps inside specs by using the relative import feature, I am getting an error saying missing 1 required positional argument despite of having everything correctly present.

Gauge-Main-Project/step_impl/step_impl.py

from getgauge.python import step, Messages

class BaseMainClass:
    def __init__(self) -> None:
        pass

class MainClass(BaseMainClass):
    @step("Greet <name> from Main Project")
    def greet_main_project(self, name):
        print(f"Hello from the Main Project, {name}")
        Messages.write_message(f"Hello from the Main Project - {name}")

Gauge-Sub-Project/step_impl/step_impl.py

from getgauge.python import step, Messages

class BaseSubClass:
    def __init__(self) -> None:
        pass

class SubClass(BaseSubClass):
    @step("Greet <name> from Sub Project")
    def greet_sub_project(sub, name):
        print(f"Hello from the Sub Project, {name}")
        Messages.write_message(f"Hello from the Main Project - {name}")

Sample Spec File

# Test Relative Imports
> Sample spec from Sub Project

## Test Relative Imports From Class

* Greet "Kunal" from Sub Project
* Greet "Kunal" from Main Project

To Reproduce: Steps (or project) to reproduce the behavior:

  1. Initialize 2 gauge projects with gauge init python command
  2. Update the step_impl.py files for both the projects like shown above
  3. Setup the Gauge-Sub-Project/env/default/python.properties file like: STEP_IMPL_DIR = step_impl, ../Gauge-Main-Project/step_impl
  4. Create a spec file with the above mentioned steps
  5. Run the spec
  6. See error

Logs

Traceback (most recent call last):
  File "<PATH_TO_VENV>\Lib\site-packages\getgauge\executor.py", line 33, in execute_method
    step.impl(*params)
TypeError: MainClass.greet_main_project() missing 1 required positional argument: 'name'

Possible Fix: This issue arises because the update_step_resgistry_with_class() method inside the impl_loader.py file is unable to fetch all the methods inside that step implementation file inside get_all_methods_in() method due to the file_path being a relative file path. Resolving this relative file path into an absolute file path fixes this issue.

Versions:

Project Structure:

Project-Root
  ├── Gauge-Main-Project
  │    ├── env
  │    │   └── default
  │    │       └── default.properties
  │    │       └── python.properties
  │    ├── specs
  │    │   └── example.spec
  │    └── step_impl
  │        └── step_impl.py
  │
  └── Gauge-Sub-Project
      ├── env
      │   └── default
      │       └── default.properties
      │       └── python.properties
      ├── specs
      │   └── example.spec
      └── step_impl
          └── step_impl.py

Thanks!😄