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

Exception during AST parsing when using context managers #104

Open
razorx89 opened this issue Mar 10, 2018 · 2 comments · Fixed by pinerylabs/mamba#2 · May be fixed by #142
Open

Exception during AST parsing when using context managers #104

razorx89 opened this issue Mar 10, 2018 · 2 comments · Fixed by pinerylabs/mamba#2 · May be fixed by #142

Comments

@razorx89
Copy link

I am currently experimenting with mamba for testing a flask application. However, I encountered a weired bug during test writing. When the app instance is imported directly into the spec file, I can use context mangers, for instance app.test_client(), without any problems. When importing the whole module, then the AST parsing seems to crash. See the attached code for a MWE and more details.

spec.py:

from mamba import after, before, context, description, it
import utils
from utils import app

with description('Bug') as self:
    with context('when importing instance from module directly'):
        with it('works'):
            with app.test_client():
                pass

    with context('when importing the whole module'):
        with it('fails to deal with context managers'):
            with utils.app.test_client():
                pass

utils.py:

from flask import Flask
app = Flask(__name__)
$ mamba spec.py 
Traceback (most recent call last):
  File "/usr/local/bin/mamba", line 9, in <module>
    load_entry_point('mamba==0.9.3', 'console_scripts', 'mamba')()
  File "/usr/local/lib/python3.5/dist-packages/mamba/cli.py", line 18, in main
    runner.run()
  File "/usr/local/lib/python3.5/dist-packages/mamba/runners.py", line 29, in run
    modules = self.example_collector.modules()
  File "/usr/local/lib/python3.5/dist-packages/mamba/example_collector.py", line 25, in modules
    with self._load_module_from(path) as module:
  File "/usr/lib/python3.5/contextlib.py", line 59, in __enter__
    return next(self.gen)
  File "/usr/local/lib/python3.5/dist-packages/mamba/example_collector.py", line 57, in _load_module_from
    yield self._module_from_ast(name, path)
  File "/usr/local/lib/python3.5/dist-packages/mamba/example_collector.py", line 60, in _module_from_ast
    tree = self._parse_and_transform_ast(path)
  File "/usr/local/lib/python3.5/dist-packages/mamba/example_collector.py", line 83, in _parse_and_transform_ast
    tree = self._node_transformer.visit(tree)
  File "/usr/lib/python3.5/ast.py", line 245, in visit
    return visitor(node)
  File "/usr/local/lib/python3.5/dist-packages/mamba/nodetransformers.py", line 18, in visit_Module
    super(TransformToSpecsNodeTransformer, self).generic_visit(node)
  File "/usr/lib/python3.5/ast.py", line 300, in generic_visit
    value = self.visit(value)
  File "/usr/lib/python3.5/ast.py", line 245, in visit
    return visitor(node)
  File "/usr/local/lib/python3.5/dist-packages/mamba/nodetransformers.py", line 28, in visit_With
    super(TransformToSpecsNodeTransformer, self).generic_visit(node)
  File "/usr/lib/python3.5/ast.py", line 300, in generic_visit
    value = self.visit(value)
  File "/usr/lib/python3.5/ast.py", line 245, in visit
    return visitor(node)
  File "/usr/local/lib/python3.5/dist-packages/mamba/nodetransformers.py", line 28, in visit_With
    super(TransformToSpecsNodeTransformer, self).generic_visit(node)
  File "/usr/lib/python3.5/ast.py", line 300, in generic_visit
    value = self.visit(value)
  File "/usr/lib/python3.5/ast.py", line 245, in visit
    return visitor(node)
  File "/usr/local/lib/python3.5/dist-packages/mamba/nodetransformers.py", line 28, in visit_With
    super(TransformToSpecsNodeTransformer, self).generic_visit(node)
  File "/usr/lib/python3.5/ast.py", line 300, in generic_visit
    value = self.visit(value)
  File "/usr/lib/python3.5/ast.py", line 245, in visit
    return visitor(node)
  File "/usr/local/lib/python3.5/dist-packages/mamba/nodetransformers.py", line 30, in visit_With
    name = self._get_name(node)
  File "/usr/local/lib/python3.5/dist-packages/mamba/nodetransformers.py", line 49, in _get_name
    return context_expr.func.value.id
AttributeError: 'Attribute' object has no attribute 'id'
@sakows
Copy link

sakows commented Apr 5, 2018

should not you use python2.7?

@nestorsalceda
Copy link
Owner

Let me check this behaviour.

It looks some kind of messing when transforming with [it/context] context manager and other ones.

Thanks!

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