def test_failed_read(): import StringIO captured = StringIO.StringIO() import mock open_mock = mock.MagicMock() handle = mock.MagicMock() handle.read.side_effect = IOError('Stubbed read error') open_mock.__enter__.return_value = handle open_mock.return_value = handle with capture_stdout(captured): with mock.patch('__builtin__.open', open_mock, create=True): # Double nesting to support older versions of Python import pep257 pep257.main(*pep257.parse_options(['dummy-file.py'])) open_mock.assert_called_once_with(('dummy-file.py')) handle.close.assert_called_once_with() captured_lines = captured.getvalue().split('\n') captured_lines = [line.strip() for line in captured_lines] assert len(captured_lines) == 4 assert captured_lines[0] == ('=' * 80) assert captured_lines[1] == ('Note: checks are relaxed for scripts' ' (with #!) compared to modules') assert captured_lines[2] == 'Error reading file dummy-file.py' assert captured_lines[3] == ''
def test_failed_open(): filename = "non-existent-file.py" assert not os.path.exists(filename) captured = StringIO() with capture_stdout(captured): pep257.main(default_options, [filename]) captured_lines = captured.getvalue().strip().split('\n') assert captured_lines == [ '=' * 80, 'Note: checks are relaxed for scripts (with #!) compared to modules', 'Error opening file non-existent-file.py' ]
def test_opened_files_are_closed(): files_opened = [] real_open = open def open_wrapper(*args, **kw): opened_file = mock.MagicMock(wraps=real_open(*args, **kw)) files_opened.append(args[0]) return opened_file open_mock = mock.MagicMock(side_effect=open_wrapper) open_mock.__enter__.side_effect = open_wrapper with mock.patch('__builtin__.open', open_mock, create=True): pep257.main(default_options, ['pep257.py']) assert len(files_opened) == 1 assert files_opened[0].endswith('pep257.py')
def test_failed_open(): import os.path filename = "non-existent-file.py" assert not os.path.exists(filename) import StringIO captured = StringIO.StringIO() with capture_stdout(captured): import pep257 pep257.main(*pep257.parse_options([filename])) captured_lines = captured.getvalue().split('\n') captured_lines = [line.strip() for line in captured_lines] assert len(captured_lines) == 4 assert captured_lines[0] == ('=' * 80) assert captured_lines[1] == ('Note: checks are relaxed for scripts' ' (with #!) compared to modules') assert captured_lines[2] == 'Error opening file non-existent-file.py' assert captured_lines[3] == ''
def test_opened_files_are_closed(): import mock files_opened = [] real_open = open def open_wrapper(*args, **kw): opened_file = mock.MagicMock(wraps=real_open(*args, **kw)) files_opened.append(opened_file) return opened_file open_mock = mock.MagicMock(side_effect=open_wrapper) open_mock.__enter__.side_effect = open_wrapper with mock.patch('__builtin__.open', open_mock, create=True): import pep257 pep257.main(*pep257.parse_options(['pep257.py'])) open_mock.assert_called_once_with(('pep257.py')) assert len(files_opened) == 1 for opened_file in files_opened: opened_file.close.assert_called_once_with()
def run_pep257(): print('Running pep257...') pep257_options = namedtuple('PEP257Options', [ 'explain', 'source', 'ignore', 'match', 'match_dir', ]) # Default options in the option parser options = pep257_options(False, False, '', '(?!test_).*\.py', '[^\.].*') return pep257.main(options, [CODE_DIR])
def check_pep257(target): """Run pep257 checker with args passed.""" args = _get_args('pep257') sys.argv = ['pep257', target] if args is not None: sys.argv += args try: status = pep257.main(*pep257.parse_options()) print() return status except Exception: traceback.print_exc() return None
def SKIP_test_failed_read(): captured = StringIO() open_mock = mock.MagicMock() handle = mock.MagicMock() handle.read.side_effect = IOError('Stubbed read error') open_mock.__enter__.return_value = handle open_mock.return_value = handle with capture_stdout(captured): with mock.patch('__builtin__.open', open_mock, create=True): pep257.main(default_options, ['dummy-file.py']) open_mock.assert_called_once_with('dummy-file.py') handle.close.assert_called_once_with() captured_lines = captured.getvalue().strip().split('\n') assert captured_lines == [ '=' * 80, 'Note: checks are relaxed for scripts (with #!) compared to modules', 'Error reading file dummy-file.py', ]
"""This is a shim to access pep257. Windows only. Under ActiveState Pytyon 3.4.10, when pep257 installs (via pip) it doesn't lay down an exe, but rather a shell script. But that file has no extension and is a bit suspect. I created this file to do something similar, but more reliable w.r.t the lint.ps1 script. """ from pep257 import main if __name__ == '__main__': main()
"""This is a shim to access pep257. Windows only. Under ActiveState Pytyon 3.4.10, when pep257 installs (via pip) it doesn't lay down an exe, but rather a shell script. But that file has no extension and is a bit suspect. I created this file to do something similar, but more reliable w.r.t the lint.ps1 script. """ from pep257 import main if __name__ == "__main__": main()