def doctests(path):
    buf = StringIO()
    with(state(sys, stdout=buf)):
        doctest.testfile(path)
    res = buf.getvalue()
    if res != '':
        raise Exception(res)
Exemplo n.º 2
0
 def lint() -> Tuple[int, str, str]:
     print('     flake8...')
     from flake8.api import legacy as flake8  # type: ignore
     out, err = io.StringIO(), io.StringIO()
     guide = flake8.get_style_guide()
     with state(sys, stdout=out, stderr=err):
         report = guide.check_files('.')
     return (report.total_errors, out.getvalue(), err.getvalue())
Exemplo n.º 3
0
def doctests(path):
    buf = StringIO()
    with state(sys, stdout=buf):
        doctest.testfile(path)
    res = buf.getvalue()
    if res != '':
        # import ipdb; ipdb.set_trace()
        raise Exception(res)
Exemplo n.º 4
0
    def style() -> Tuple[int, str, str]:
        """Runs pycodestyle, unpleasantly white-boxy call.  ..but it doesn't
         seem to be written for integration, so:

        """
        print('     pycodestyle...')
        code = 0
        warn, err = io.StringIO(), io.StringIO()
        with state(sys, argv=[], stdout=warn, stderr=err):
            try:
                pycodestyle._main()
            except SystemExit as exc:
                code = exc.code
        return code, warn.getvalue(), err.getvalue()
Exemplo n.º 5
0
def test_state_dict_extra(dct):
    with state(dct, b=2, c=3):
        assert dct == {'a':1, 'b':2, 'c':3}
    assert dct == {'a':1}
Exemplo n.º 6
0
def test_state_obj_forget(obj):
    with state(obj, a=forget):
        assert not hasattr(obj, 'a')
    assert obj.a == 1
Exemplo n.º 7
0
def test_state_obj_extra(obj):
    with state(obj, b=2, c=3):
        assert obj.b == 2
        assert obj.c == 3
    assert not hasattr(obj, 'b')
    assert not hasattr(obj, 'c')
Exemplo n.º 8
0
def test_state_obj_raises(obj):
    with raises(CertainException):
        with state(obj, a=2):
            raise CertainException()
    assert obj.a == 1
Exemplo n.º 9
0
def test_deny_module():
    with pytest.raises(ImportError):
        with state(sys.modules, shutil=None):
            import shutil
Exemplo n.º 10
0
def test_os_environ():
    with state(os.environ, ALTERED='states'):
        assert os.environ['ALTERED'] == 'states'

    assert 'ALTERED' not in os.environ
Exemplo n.º 11
0
def test_state_obj_extra(obj):
    with state(obj, b=2, c=3):
        assert obj.b == 2
        assert obj.c == 3
    assert not hasattr(obj, 'b')
    assert not hasattr(obj, 'c')
Exemplo n.º 12
0
def test_nested_context():
    "Should handle nested context managers correctly"
    env = Expando(foo='bar', bar='baz')
    with state(env, foo='baz') as _, state(env, bar='foo') as _:
        assert env.foo == 'baz'
        assert env.bar == 'foo'
Exemplo n.º 13
0
def test_deny_module():
    with pytest.raises(ImportError):
        with state(sys.modules, shutil=None):
            import shutil
Exemplo n.º 14
0
def test_getitem_overridable(w_getitem):
    with state(w_getitem, any='changed'):
        assert w_getitem.any == 'changed'
Exemplo n.º 15
0
def test_os_environ():
    with state(os.environ, ALTERED='states'):
        assert os.environ['ALTERED'] == 'states'

    assert 'ALTERED' not in os.environ
Exemplo n.º 16
0
def test_state_dict_forget(dct):
    with state(dct, a=forget):
        assert dct == {}
    assert dct == {'a':1}
Exemplo n.º 17
0
def test_patch_sys_modules():
    with (state(sys.modules, fakey=Expando(foo='bar'))):
        import fakey
        assert fakey.foo == 'bar'
    with (pytest.raises(ImportError)):
        import fakey
Exemplo n.º 18
0
def test_state_obj_overwrite(obj):
    with state(obj, a=2):
        assert obj.a == 2
    assert obj.a == 1
Exemplo n.º 19
0
def test_getitem_overridable(w_getitem):
    with state(w_getitem, any='changed'):
        assert w_getitem.any == 'changed'
Exemplo n.º 20
0
def test_state_obj_forget(obj):
    with state(obj, a=forget):
        assert not hasattr(obj, 'a')
    assert obj.a == 1
Exemplo n.º 21
0
def test_nested_context():
    "Should handle nested context managers correctly"
    env = Expando(foo='bar', bar='baz')
    with state(env, foo='baz') as _, state(env, bar='foo') as _:
        assert env.foo == 'baz'
        assert env.bar == 'foo'
Exemplo n.º 22
0
def test_state_obj_raises(obj):
    with raises(CertainException):
        with state(obj, a=2):
            raise CertainException()
    assert obj.a == 1
Exemplo n.º 23
0
def test_state_dct_raises(dct):
    with raises(CertainException):
        with state(dct, a=2):
            raise CertainException()
    assert dct['a'] == 1
Exemplo n.º 24
0
def test_state_dict_extra(dct):
    with state(dct, b=2, c=3):
        assert dct == {'a': 1, 'b': 2, 'c': 3}
    assert dct == {'a': 1}
Exemplo n.º 25
0
def test_state_dict_overwrite(dct):
    with state(dct, a=2):
        assert dct == {'a': 2}
    assert dct == {'a': 1}
Exemplo n.º 26
0
def test_state_dict_forget(dct):
    with state(dct, a=forget):
        assert dct == {}
    assert dct == {'a': 1}
Exemplo n.º 27
0
def test_state_obj_overwrite(obj):
    with state(obj, a=2):
        assert obj.a == 2
    assert obj.a == 1
Exemplo n.º 28
0
def test_state_dct_raises(dct):
    with raises(CertainException):
        with state(dct, a=2):
            raise CertainException()
    assert dct['a'] == 1
Exemplo n.º 29
0
 def b0rked(badness):
     with state(obj, a=2):
         raise badness
Exemplo n.º 30
0
def test_capture_stdout():
    buf = py23compat.strio()
    print('Foo')
    with(state(sys, stdout=buf)):
        print('Bar')
    assert buf.getvalue() == 'Bar' + os.linesep
Exemplo n.º 31
0
def test_state_dict_overwrite(dct):
    with state(dct, a=2):
        assert dct == {'a':2}
    assert dct == {'a':1}
Exemplo n.º 32
0
def test_patch_sys_modules():
    with(state(sys.modules, fakey=Expando(foo='bar'))):
        import fakey
        assert fakey.foo == 'bar'
    with(pytest.raises(ImportError)):
        import fakey
Exemplo n.º 33
0
 def b0rked(badness):
     with state(dct, a=2):
         raise badness
Exemplo n.º 34
0
def test_capture_stdout():
    buf = py23compat.strio()
    print('Foo')
    with (state(sys, stdout=buf)):
        print('Bar')
    assert buf.getvalue() == 'Bar' + os.linesep