Exemple #1
0
    def hook(self, pytestconfig, monkeypatch, testdir):
        """Returns a patched AssertionRewritingHook instance so we can configure its initial paths and track
        if imp.find_module has been called.
        """
        import imp

        self.find_module_calls = []
        self.initial_paths = set()

        class StubSession(object):
            _initialpaths = self.initial_paths

            def isinitpath(self, p):
                return p in self._initialpaths

        def spy_imp_find_module(name, path):
            self.find_module_calls.append(name)
            return imp.find_module(name, path)

        hook = AssertionRewritingHook(pytestconfig)
        # use default patterns, otherwise we inherit pytest's testing config
        hook.fnpats[:] = ["test_*.py", "*_test.py"]
        monkeypatch.setattr(hook, "_imp_find_module", spy_imp_find_module)
        hook.set_session(StubSession())
        testdir.syspathinsert()
        return hook
Exemple #2
0
def test_rewrite_infinite_recursion(testdir, pytestconfig, monkeypatch):
    """Fix infinite recursion when writing pyc files: if an import happens to be triggered when writing the pyc
    file, this would cause another call to the hook, which would trigger another pyc writing, which could
    trigger another import, and so on. (#3506)"""
    from _pytest.assertion import rewrite

    testdir.syspathinsert()
    testdir.makepyfile(test_foo="def test_foo(): pass")
    testdir.makepyfile(test_bar="def test_bar(): pass")

    original_write_pyc = rewrite._write_pyc

    write_pyc_called = []

    def spy_write_pyc(*args, **kwargs):
        # make a note that we have called _write_pyc
        write_pyc_called.append(True)
        # try to import a module at this point: we should not try to rewrite this module
        assert hook.find_module("test_bar") is None
        return original_write_pyc(*args, **kwargs)

    monkeypatch.setattr(rewrite, "_write_pyc", spy_write_pyc)
    monkeypatch.setattr(sys, "dont_write_bytecode", False)

    hook = AssertionRewritingHook(pytestconfig)
    assert hook.find_module("test_foo") is not None
    assert len(write_pyc_called) == 1
Exemple #3
0
 def test_rewrite_warning(self, pytestconfig, monkeypatch):
     hook = AssertionRewritingHook(pytestconfig)
     warnings = []
     def mywarn(code, msg):
         warnings.append((code, msg))
     monkeypatch.setattr(hook.config, 'warn', mywarn)
     hook.mark_rewrite('_pytest')
     assert '_pytest' in warnings[0][1]
 def test_remember_rewritten_modules(self, pytestconfig, testdir, monkeypatch):
     """
     AssertionRewriteHook should remember rewritten modules so it
     doesn't give false positives (#2005).
     """
     monkeypatch.syspath_prepend(testdir.tmpdir)
     testdir.makepyfile(test_remember_rewritten_modules='')
     warnings = []
     hook = AssertionRewritingHook(pytestconfig)
     monkeypatch.setattr(hook.config, 'warn', lambda code, msg: warnings.append(msg))
     hook.find_module('test_remember_rewritten_modules')
     hook.load_module('test_remember_rewritten_modules')
     hook.mark_rewrite('test_remember_rewritten_modules')
     hook.mark_rewrite('test_remember_rewritten_modules')
     assert warnings == []