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

Step is not found when the py script with @step function is imported by another script #8

Closed
kbfu opened this issue Aug 12, 2016 · 6 comments

Comments

@kbfu
Copy link

kbfu commented Aug 12, 2016

Hi,
I was running my gauge python code, and I found a problem.
Let's say there is an a.py, it has an @step decorator like below:

from getgauge.python import step


@step('run a')
def this_is_a():
    print('running a')


def this_is_another_a():
    print('running another a')

And there is another script, let's say b.py that imports a.py as below:

from step_impl.ZeusLibrary import a
from getgauge.python import step


@step('run b')
def this_is_b():
    a.this_is_another_a()

Now let's run the gauge spec as below shows:

Spec
=====================


Case
------------------
* run a
* run b

, it will throw ValidationError because it cannot find @step('run a').

The workaround is if I want to use the function in a.py, I must import it inside a function in b.py instead of importing it globally.

Console output as below:
Python: 3.5.2
[ValidationError] /Users/xxx/Documents/xxx/specs/1.spec:7: 1 => 'run a'

@kbfu
Copy link
Author

kbfu commented Aug 12, 2016

Run the same code on my Windows machie, it shows:
Python: 3.5.2
[ValidationError] D:\PycharmProjects\g\specs\1.spec:6: Duplicate step implementation => 'run a'

Looks like @step will be run twice when imported on the head of the py script, so gauge got the two same step text.

@kbfu
Copy link
Author

kbfu commented Aug 12, 2016

Ok I think I have found the root cause.
When the python plugin use importlib to dynamic import py scripts, if there is a py script with @step is imported by another one, the decorator @step in the first script will be loaded twice, that why I got this duplicate error.
But import it within the function will not cause this problem.
Maybe we can remove the duplicate one from the list?

@kashishm
Copy link
Collaborator

@kbfu I think the root cause you have found is the right one. But we cannot remove the duplicate step implementation because Gauge skips a scenario which has a step with multiple step implementation. And if we remove the duplicate one, then Gauge will never know that there was duplicate step implementation found which will result in inconsistent behaviour between different languages supported by Gauge.

The ideal solution would be to not import the file twice and that responsibility lies with the python runner. Currently, I am not sure how will we implement it.

Thoughts?

@kbfu
Copy link
Author

kbfu commented Aug 14, 2016

Hey @kashishm ,
I tried to remove the duplicate step from the step_map dict, it works when I import the file.
However when I have two duplicate steps in a py script, no exception throws.
Maybe add a new mechanism for the python runner to check the duplicate steps in a script file before sending the message to gauge?

@kashishm
Copy link
Collaborator

kashishm commented Sep 3, 2016

@kbfu I have released a new version(0.1.6) of the plugin which has the fix for this issue.

Run the following commands to update the plugin:

gauge --update python
pip install --upgrade getgauge

@kbfu
Copy link
Author

kbfu commented Sep 5, 2016

@kashishm Thanks, it works.

@kbfu kbfu closed this as completed Sep 5, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants