def open_mock(content, **kwargs): """Mock's mock_open only supports read() and write() which is not very useful. This context manager adds support for getting the value of what was written out and for iterating through a file line by line.""" global file_spec if file_spec is None: # set on first use if PY2: file_spec = file else: import _io file_spec = list(set(dir(_io.TextIOWrapper)).union(set(dir(_io.BytesIO)))) m = MagicMock(name='open', spec=open) handle = MagicMock(spec=file_spec) handle.__enter__.return_value = handle m.return_value = handle content_out = StringIO() if PY2: patch_module = "__builtin__.open" else: patch_module = "builtins.open" with patch(patch_module, m, create=True, **kwargs) as mo: stream = StringIO(content) rv = mo.return_value rv.write = lambda x: content_out.write(bytes(x, "utf-8")) rv.content_out = lambda: content_out.getvalue() rv.__iter__.return_value = iter(stream.readlines()) rv.read.return_value = stream.read() yield rv
def _update_setup_py(self, new_version): """ If this project has a setup.py, attempt to update it's version. """ self._update_version_file(new_version) setup_file = os.path.join(self.full_project_dir, "setup.py") if not os.path.exists(setup_file): return debug("Found setup.py, attempting to update version.") # We probably don't want version-release in setup.py as release is # an rpm concept. Hopefully this assumption on py_new_version = new_version.split('-')[0] f = open(setup_file, 'r') buf = StringIO() for line in f.readlines(): buf.write(replace_version(line, py_new_version)) f.close() # Write out the new setup.py file contents: f = open(setup_file, 'w') f.write(buf.getvalue()) f.close() buf.close() run_command("git add %s" % setup_file)
def _update_changelog(self, new_version): """ Update the changelog with the new version. """ # Not thrilled about having to re-read the file here but we need to # check for the changelog entry before making any modifications, then # bump the version, then update the changelog. f = open(self.changes_file, 'r') buf = StringIO() found_match = False done = False empty_line_regex = re.compile('^\s*$') for line in f.readlines(): if not done and not found_match and self.changelog_regex.match( line): buf.write(line) found_match = True elif not done and found_match and empty_line_regex.match(line): buf.write("\n- version %s\n" % new_version) done = True else: buf.write(line) f.close() # Write out the new file contents with our modified changelog entry: f = open(self.changes_file, 'w') f.write(buf.getvalue()) f.close() buf.close()
def setUp(self): self.out = StringIO() self.tarfixer = TarFixer(None, self.out, EXPECTED_TIMESTAMP, EXPECTED_REF) self.test_file = os.path.join(os.path.dirname(__file__), 'resources', 'archive.tar') self.reference_file = os.path.join(os.path.dirname(__file__), 'resources', 'archive-fixed.tar') self.reference_hash = self.hash_file(self.reference_file)
def test_conformance(self): tests = [ # http://pep8.readthedocs.org/en/latest/intro.html#error-codes 'E101', # indentation contains mixed spaces and tabs 'E111', # indentation is not a multiple of four 'E112', # expected an indented block 'E113', # unexpected indentation 'E121', # continuation line indentation is not a multiple of four 'E122', # continuation line missing indentation or outdented 'E126', # continuation line over-indented for hanging indent 'E2', # whitespace errors 'E3', # blank line errors 'E4', # import errors 'E502', # the backslash is redundant between brackets 'E9', # runtime errors (SyntaxError, IndentationError, IOError) 'W1', # indentation warnings 'W2', # whitespace warnings 'W3', # blank line warnings # @FIXME we currently have a lot of these errors introduced to our # codebase. Let's temporarily disable the check, so we can get travis # working again. # 'E7', # statement errors # 'W6', # deprecated features ] try: checker = pep8.StyleGuide(select=tests, paths=[REPO_DIR], reporter=pep8.StandardReport) # Unfortunatelly, IMHO the most interesting information # `checker.check_files()` prints to the STDOUT and doesn't provide # API to get it - the list of _bad_ files. Let's just read it from # the output with StringIO() as buf, redirect_stdout(buf): report = checker.check_files() result = report.total_errors output = buf.getvalue() except AttributeError: # We don't have pep8.StyleGuide, so we must be # using pep8 older than git tag 1.1-72-gf20d656. os.chdir(REPO_DIR) checks = ','.join(tests) cmd = "pep8 --select=%s %s | wc -l" % (checks, '.') result = int(getoutput(cmd)) output = getoutput("pep8 --select %s %s" % (checks, '.')) if result != 0: self.fail("Found PEP8 errors that may break your code in Python 3:\n%s" % output)
def _update_changelog(self, new_version): """ Update the changelog with the new version. """ # Not thrilled about having to re-read the file here but we need to # check for the changelog entry before making any modifications, then # bump the version, then update the changelog. f = open(self.spec_file, 'r') buf = StringIO() found_match = False for line in f.readlines(): match = self.changelog_regex.match(line) if match and not found_match: buf.write("%s %s\n" % (match.group(), new_version)) found_match = True else: buf.write(line) f.close() # Write out the new file contents with our modified changelog entry: f = open(self.spec_file, 'w') f.write(buf.getvalue()) f.close() buf.close()
def test_colors(self, mock_user_conf): mock_user_conf.return_value = {} stream = StringIO() _out('Hello world', None, Terminal().red, stream) # RHEL 6 doesn't have self.assertRegexpMatches unfortunately self.assertTrue(re.match('.+Hello world.+\n', stream.getvalue()))
def test_turn_off_colors(self, mock_user_conf): mock_user_conf.return_value = {'COLOR': '0'} stream = StringIO() _out('Hello world', None, Terminal().red, stream) self.assertEquals('Hello world\n', stream.getvalue())
def __init__(self, stream, silent): self.buf = StringIO() self.stream = stream self.silent = silent
def test_full_read_eventual_buffer_underflow(self): items = [StringIO("1" * 5), StringIO("1" * 2), StringIO("1" * 2)] self.tarfixer.fh = self._irregular_reader(items) self.assertRaises(IOError, self.tarfixer.full_read, 10)
def test_full_read_buffer_underflow(self): input = StringIO("1" * 9) self.tarfixer.fh = input self.assertRaises(IOError, self.tarfixer.full_read, 10)
def test_full_read(self): items = [StringIO("1" * 5), StringIO("1" * 2), StringIO("1" * 6)] self.tarfixer.fh = self._irregular_reader(items) self.assertEqual("1" * 10, self.tarfixer.full_read(10))