nestorsalceda / mamba

The definitive testing tool for Python. Born under the banner of Behavior Driven Development (BDD).
http://nestorsalceda.github.io/mamba
MIT License
518 stars 65 forks source link

Exception during AST parsing when using context managers #104

Open razorx89 opened 6 years ago

razorx89 commented 6 years ago

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 commented 6 years ago

should not you use python2.7?

nestorsalceda commented 6 years ago

Let me check this behaviour.

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

Thanks!