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

Unable to reuse step implementations from another project #359

Closed
kunalvishwasrao opened this issue Jun 26, 2024 · 7 comments · Fixed by #360
Closed

Unable to reuse step implementations from another project #359

kunalvishwasrao opened this issue Jun 26, 2024 · 7 comments · Fixed by #360

Comments

@kunalvishwasrao
Copy link
Contributor

kunalvishwasrao commented Jun 26, 2024

Description:
I have 2 Gauge projects - Gauge-Project-Main and Gauge-Project-Sub. I want to use the step implementations created within Gauge-Project-Main inside Gauge-Project-Sub. To do so, I have tried updating the Gauge-Project-Sub's env/default/python.properties file with both absolute and relative paths to Gauge-Project-Main's step_impl folder but I keep getting different errors in both scenarios:

Adding relative path to python.properties file:
STEP_IMPL_DIR = step_impl, ../Gauge-Main-Project/step_impl

26-06-2024 18:34:44.479 [python] [CRITICAL] Exception occurred while loading step implementations from file: ..\Gauge-Main-Project\step_impl\step_impl.py.
Traceback (most recent call last):
File "<PYTHON_VENV_PATH>\Lib\site-packages\getgauge\impl_loader.py", line 65, in import_file
m = importlib.import_module(module_name)
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "\AppData\Local\Programs\Python\Python311\Lib\importlib_init
.py", line 121, in import_module
raise TypeError(msg.format(name))
TypeError: the 'package' argument is required to perform a relative import for '...Gauge-Main-Project.step_impl.step_impl'

Adding absolute path to python.properties file:
STEP_IMPL_DIR = step_impl, <ABSOLUTE_PATH_TO_PROJECT>/Gauge-Main-Project/step_impl

Traceback (most recent call last):
File "<PYTHON_VENV_PATH>\Lib\site-packages\getgauge\executor.py", line 32, in execute_method
params = _get_args(params, step)
^^^^^^^^^^^^^^^^^^^^^^^
File "<PYTHON_VENV_PATH>\Lib\site-packages\getgauge\executor.py", line 50, in _get_args
args_length = len(inspect.signature(hook_or_step.impl).parameters)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "\AppData\Local\Programs\Python\Python311\Lib\inspect.py", line 3280, in signature
return Signature.from_callable(obj, follow_wrapped=follow_wrapped,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "\AppData\Local\Programs\Python\Python311\Lib\inspect.py", line 3028, in from_callable
return _signature_from_callable(obj, sigcls=cls,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "\AppData\Local\Programs\Python\Python311\Lib\inspect.py", line 2449, in _signature_from_callable
raise TypeError('{!r} is not a callable object'.format(obj))
TypeError: None is not a callable object

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

  1. Initialize 2 gauge projects with gauge init python command
  2. Setup the python.properties file like shown above
  3. Create a spec file with one step from current project and one from another project
  4. Run the spec
  5. See error

Versions:

  • OS - Windows 10 (10.0.19045)
  • Python version - 3.11.4
  • Gauge version - 1.6.7

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 😄

@chadlwilson
Copy link
Contributor

chadlwilson commented Jun 26, 2024

Are you sure you didn't just typo things? Your tree structure shows Gauge-Project-Main whereas your config shows Gauge-Main-Project

Other than that, I have a feeling that might not work, and you might want to instead try to organise your projects and config differently to avoid paths like that, and be able to run from the root as working directly with different other config. On Linux/Mac you could probably just symlink, but not going to send you down the path of junction points on Windows....

@kunalvishwasrao
Copy link
Contributor Author

Hello @chadlwilson,

Apologies for the typo in the tree structure (will update it). Its actually correctly mentioned in the config as Gauge-Main-Project, despite of which it doesn't seem to work. Re-organizing the project will be the last resort, please let me know if you come across any solution.

Thanks.😄

@chadlwilson
Copy link
Contributor

I'm not an expert with python or the python plugin, but from the errors it looks like it is translaring the impl dirs to packages directly, so suspect only regular forward paths will work.

@BugDiver
Copy link
Member

I just tried to test it quickly, and It's working as expected on macOS. Unfortunately I don't have access to windows device ATM, so can't say if it's windows specific issue

@kunalvishwasrao
Copy link
Contributor Author

Hello @BugDiver,

The path format that worked for you on macOS, was it an absolute path or relative path in the STEP_IMPL_DIR variable?

@chadlwilson
Copy link
Contributor

Did the relative or absolute case work for you? I wonder if it makes any difference as to direction of slashes :-)

The code on the python side is:

def _import_impl(base_dir, step_impl_dir):
for f in os.listdir(step_impl_dir):
file_path = os.path.join(step_impl_dir, f)
if f.endswith('.py'):
_import_file(base_dir, file_path)
elif path.isdir(file_path):
_import_impl(base_dir, file_path)
def _import_file(base_dir, file_path):
rel_path = os.path.normpath(file_path.replace(base_dir + os.path.sep, ''))
try:
module_name = os.path.splitext(rel_path.replace(os.path.sep, '.'))[0]
m = importlib.import_module(module_name)
# Get all classes in the imported module

Didn't look to me like it'd work in either case unless the base path is the root (not the subproject)

@BugDiver
Copy link
Member

Forget what I said earlier, I'm able to see the error on macOS as well. I will try to look into the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants