def test_edit_file_with_invalid_filename(self): """Testing edit_file with invalid filename""" message = ('The file "blargh-bad-filename" does not exist or is not ' 'accessible.') with self.assertRaisesMessage(EditorError, message): edit_file('blargh-bad-filename')
def test_edit_file_with_invalid_editor(self): """Testing edit_file with invalid filename""" message = ( 'The editor "./bad-rbtools-editor" was not found or could not ' 'be run. Make sure the EDITOR environment variable is set ' 'to your preferred editor.') os.environ[str('RBTOOLS_EDITOR')] = './bad-rbtools-editor' with self.assertRaisesMessage(EditorError, message): edit_file(make_tempfile(b'Test content'))
def test_edit_file_with_file_deleted(self): """Testing edit_file with file deleted during edit""" def _subprocess_call(*args, **kwargs): os.unlink(filename) filename = make_tempfile(b'Test content') message = 'The edited file "%s" was deleted during edit.' % filename self.spy_on(subprocess.call, call_fake=_subprocess_call) with self.assertRaisesMessage(EditorError, message): edit_file(filename)
def test_edit_file_with_editor_priority(self): """Testing edit_file editor priority""" self.spy_on(subprocess.call, call_original=False) # Save these so we can restore after the tests. We don't need to # save RBTOOLS_EDITOR, because this is taken care of in the base # TestCase class. old_visual = os.environ.get(str('VISUAL')) old_editor = os.environ.get(str('EDITOR')) filename = make_tempfile(b'Test content') try: os.environ[str('RBTOOLS_EDITOR')] = 'rbtools-editor' os.environ[str('VISUAL')] = 'visual' os.environ[str('EDITOR')] = 'editor' edit_file(filename) self.assertTrue( subprocess.call.last_called_with(['rbtools-editor', filename])) os.environ[str('RBTOOLS_EDITOR')] = '' edit_file(filename) self.assertTrue( subprocess.call.last_called_with(['visual', filename])) os.environ[str('VISUAL')] = '' edit_file(filename) self.assertTrue( subprocess.call.last_called_with(['editor', filename])) os.environ[str('EDITOR')] = '' edit_file(filename) self.assertTrue(subprocess.call.last_called_with(['vi', filename])) finally: if old_visual: os.environ[str('VISUAL')] = old_visual if old_editor: os.environ[str('EDITOR')] = old_editor
def test_edit_file(self): """Testing edit_file""" result = edit_file(make_tempfile(b'Test content')) self.assertEqual(result, 'TEST CONTENT')
def create_commit(self, message, author, run_editor, files=[], all_files=False): """Commit the given modified files. This is expected to be called after applying a patch. This commits the patch using information from the review request, opening the commit message in $EDITOR to allow the user to update it. Args: message (unicode): The commit message to use. author (object): The author of the commit. This is expected to have ``fullname`` and ``email`` attributes. run_editor (bool): Whether to run the user's editor on the commmit message before committing. files (list of unicode, optional): The list of filenames to commit. all_files (bool, optional): Whether to commit all changed files, ignoring the ``files`` argument. Raises: rbtools.clients.errors.CreateCommitError: The commit message could not be created. It may have been aborted by the user. """ if run_editor: filename = make_tempfile(message.encode('utf-8'), prefix='hg-editor-', suffix='.txt') try: modified_message = edit_file(filename) except EditorError as e: raise CreateCommitError(six.text_type(e)) finally: try: os.unlink(filename) except OSError: pass else: modified_message = message if not modified_message.strip(): raise CreateCommitError( "A commit message wasn't provided. The patched files are in " "your tree but haven't been committed.") hg_command = [self._exe, 'commit', '-m', modified_message] try: hg_command += ['-u', '%s <%s>' % (author.fullname, author.email)] except AttributeError: # Users who have marked their profile as private won't include the # fullname or email fields in the API payload. Just commit as the # user running RBTools. logging.warning('The author has marked their Review Board profile ' 'information as private. Committing without ' 'author attribution.') if all_files: hg_command.append('-A') else: hg_command += files try: self._execute(hg_command) except Exception as e: raise CreateCommitError(six.text_type(e))