def create_file_content(patch, exp_path, filename): """Create file content of the patch. :param patch: `Patch` object to create content for :param exp_path: `str` base export folder path to use for file reading :param filename: `str` filename for file reading :return: `Patch` object with the content read from given filename. """ file_path = os.path.join(exp_path, filename).encode('utf-8') args = {'text': ''} if os.path.exists(file_path): if patch.is_binary: storage_name = uuid.uuid4().hex if not os.path.exists(BINARY_FILES_PATH): os.makedirs(BINARY_FILES_PATH) shutil.copy2( file_path, os.path.join(BINARY_FILES_PATH, storage_name)) args['data'] = storage_name else: try: with open(file_path) as fd: text = fd.read() except IOError: text = '' args['text'] = text content = models.Content(is_uploaded=True, **args) content.put() return content
def test_draft_details_no_base_file(self): request = MockRequest(User('*****@*****.**'), issue=self.issue) # add a comment and render cmt1 = models.Comment(patch_key=self.patches[0].key, parent=self.patches[0].key) cmt1.text = 'test comment' cmt1.lineno = 1 cmt1.left = False cmt1.draft = True cmt1.author = self.user cmt1.put() # Add a second comment cmt2 = models.Comment(patch_key=self.patches[1].key, parent=self.patches[1].key) cmt2.text = 'test comment 2' cmt2.lineno = 2 cmt2.left = False cmt2.draft = True cmt2.author = self.user cmt2.put() # Add fake content content1 = models.Content(text="foo\nbar\nbaz\nline\n") content1.put() content2 = models.Content(text="foo\nbar\nbaz\nline\n") content2.put() cmt1_patch = cmt1.patch_key.get() cmt1_patch.content_key = content1.key cmt1_patch.put() cmt2_patch = cmt2.patch_key.get() cmt2_patch.content_key = content2.key cmt2_patch.put() # Mock get content calls. The first fails with an FetchError, # the second succeeds (see issue384). def raise_err(): raise models.FetchError() cmt1.patch_key.get().get_content = raise_err cmt2.patch_key.get().get_patched_content = lambda: content2 tbd, comments = views._get_draft_comments(request, self.issue) self.assertEqual(len(comments), 2) # Try to render draft details using the patched Comment # instances from here. views._get_draft_details(request, [cmt1, cmt2])
original_patched_content = original_patch.get_patched_content() # Allocate keys for content and patched_content. content_id, _ = models.Content.allocate_ids(1, parent=patch.key) content_key = ndb.Key(models.Content, content_id, parent=patch.key) patched_content_id, _ = models.Content.allocate_ids(1, parent=patch.key) patched_content_key = ndb.Key(models.Content, patched_content_id, parent=patch.key) if original_patched_content: content = models.Content( key=content_key, text=original_patched_content.text, data=original_patched_content.data, checksum=original_patched_content.checksum, is_uploaded=original_patched_content.is_uploaded, is_bad=original_patched_content.is_bad, file_too_large=original_patched_content.file_too_large) elif (original_content and invert_patches.is_patch_binary_copy_modify_with_no_change( original_patch)): # For binary patches with 100% similarity indexes use the original content # as the content. See crbug.com/525625 content = models.Content( key=content_key, text=original_content.text, data=original_content.data, checksum=original_content.checksum, is_uploaded=original_content.is_uploaded, is_bad=original_content.is_bad,