def doctests(path): buf = StringIO() with(state(sys, stdout=buf)): doctest.testfile(path) res = buf.getvalue() if res != '': raise Exception(res)
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())
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)
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()
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}
def test_state_obj_forget(obj): with state(obj, a=forget): assert not hasattr(obj, 'a') assert obj.a == 1
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')
def test_state_obj_raises(obj): with raises(CertainException): with state(obj, a=2): raise CertainException() assert obj.a == 1
def test_deny_module(): with pytest.raises(ImportError): with state(sys.modules, shutil=None): import shutil
def test_os_environ(): with state(os.environ, ALTERED='states'): assert os.environ['ALTERED'] == 'states' assert 'ALTERED' not in os.environ
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'
def test_getitem_overridable(w_getitem): with state(w_getitem, any='changed'): assert w_getitem.any == 'changed'
def test_state_dict_forget(dct): with state(dct, a=forget): assert dct == {} assert dct == {'a':1}
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
def test_state_obj_overwrite(obj): with state(obj, a=2): assert obj.a == 2 assert obj.a == 1
def test_state_dct_raises(dct): with raises(CertainException): with state(dct, a=2): raise CertainException() assert dct['a'] == 1
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}
def test_state_dict_overwrite(dct): with state(dct, a=2): assert dct == {'a': 2} assert dct == {'a': 1}
def test_state_dict_forget(dct): with state(dct, a=forget): assert dct == {} assert dct == {'a': 1}
def b0rked(badness): with state(obj, a=2): raise badness
def test_capture_stdout(): buf = py23compat.strio() print('Foo') with(state(sys, stdout=buf)): print('Bar') assert buf.getvalue() == 'Bar' + os.linesep
def test_state_dict_overwrite(dct): with state(dct, a=2): assert dct == {'a':2} assert dct == {'a':1}
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
def b0rked(badness): with state(dct, a=2): raise badness
def test_capture_stdout(): buf = py23compat.strio() print('Foo') with (state(sys, stdout=buf)): print('Bar') assert buf.getvalue() == 'Bar' + os.linesep