def test_do_mission_correctly(self): oldfile = tempfile.NamedTemporaryFile(delete=False) file_to_patch = oldfile.name try: orig_response = self.client.get( reverse(views.patchsingle_get_original_file)) oldfile.write(orig_response.content) oldfile.close() patch_response = self.client.get( reverse(views.patchsingle_get_patch)) patch_process = subprocess.Popen(['patch', file_to_patch], stdin=subprocess.PIPE) patch_process.communicate(patch_response.content) self.assertEqual(patch_process.returncode, 0) submit_response = self.client.post( reverse(views.patchsingle_submit), {'patched_file': open(file_to_patch)}) self.assert_(submit_response.context['patchsingle_success']) paulproteus = Person.objects.get(user__username='******') self.assertEqual( len( StepCompletion.objects.filter( step__name='diffpatch_patchsingle', person=paulproteus)), 1) finally: os.unlink(file_to_patch)
def test_do_mission_correctly(self): oldfile = tempfile.NamedTemporaryFile(delete=False) file_to_patch = oldfile.name try: orig_response = self.client.get( reverse(views.patchsingle_get_original_file)) oldfile.write(orig_response.content) oldfile.close() patch_response = self.client.get( reverse(views.patchsingle_get_patch)) patch_process = subprocess.Popen( ['patch', file_to_patch], stdin=subprocess.PIPE) patch_process.communicate(patch_response.content) self.assertEqual(patch_process.returncode, 0) submit_response = self.client.post( reverse(views.patchsingle_submit), {'patched_file': open(file_to_patch)}) self.assert_(submit_response.context['patchsingle_success']) paulproteus = Person.objects.get(user__username='******') self.assertEqual(len(StepCompletion.objects.filter( step__name='diffpatch_patchsingle', person=paulproteus)), 1) finally: os.unlink(file_to_patch)
def test_do_mission_incorrectly_revdiff(self): orig_response = self.client.get( reverse(views.diffrecursive_get_original_tarball)) tfile = tarfile.open(fileobj=StringIO(orig_response.content), mode='r:gz') diff = StringIO() for fileinfo in tfile: if not fileinfo.isfile(): continue oldlines = tfile.extractfile(fileinfo).readlines() newlines = [] for line in oldlines: for old, new in view_helpers.DiffRecursiveMission.SUBSTITUTIONS: line = line.replace(old, new) newlines.append(line) # We're very similar to test_do_mission-correctly, but here we switch newlines and oldlines, to create a reverse patch diff.writelines( difflib.unified_diff(newlines, oldlines, 'orig-' + fileinfo.name, fileinfo.name)) diff.seek(0) diff.name = 'foo.patch' # Submit, and see if we get the same error message we expect. error = self.client.post(reverse(views.diffrecursive_submit), {'diff': diff}) self.assert_( 'You submitted a patch that would revert the correct changes back to the originals. You may have mixed the parameters for diff, or performed a reverse patch.' in utf8(error)) paulproteus = Person.objects.get(user__username='******') self.assertEqual( len( StepCompletion.objects.filter( step__name='diffpatch_diffrecursive', person=paulproteus)), 0)
def test_do_mission_correctly_with_old_filenames(self): orig_response = self.client.get( reverse(views.diffrecursive_get_original_tarball)) tfile = tarfile.open( fileobj=StringIO(orig_response.content), mode='r:gz') diff = StringIO() for fileinfo in tfile: if not fileinfo.isfile(): continue # calcualate the old name transformed_name = view_helpers.DiffRecursiveMission.name_new2old( fileinfo.name) oldlines = tfile.extractfile(fileinfo).readlines() newlines = [] for line in oldlines: for old, new in view_helpers.DiffRecursiveMission.SUBSTITUTIONS: line = line.replace(old, new) newlines.append(line) diff.writelines(difflib.unified_diff(oldlines, newlines, 'orig-' + transformed_name, transformed_name)) diff.seek(0) diff.name = 'foo.patch' submit_response = self.client.post( reverse(views.diffrecursive_submit), {'diff': diff}) self.assert_(submit_response.context['diffrecursive_success']) paulproteus = Person.objects.get(user__username='******') self.assertEqual(len(StepCompletion.objects.filter( step__name='diffpatch_diffrecursive', person=paulproteus)), 1)
def test_do_mission_incorrectly_revdiff(self): orig_response = self.client.get( reverse(views.diffrecursive_get_original_tarball)) tfile = tarfile.open( fileobj=StringIO(orig_response.content), mode='r:gz') diff = StringIO() for fileinfo in tfile: if not fileinfo.isfile(): continue oldlines = tfile.extractfile(fileinfo).readlines() newlines = [] for line in oldlines: for old, new in view_helpers.DiffRecursiveMission.SUBSTITUTIONS: line = line.replace(old, new) newlines.append(line) # We're very similar to test_do_mission-correctly, but here we # switch newlines and oldlines, to create a reverse patch diff.writelines( difflib.unified_diff(newlines, oldlines, 'orig-' + fileinfo.name, fileinfo.name)) diff.seek(0) diff.name = 'foo.patch' # Submit, and see if we get the same error message we expect. error = self.client.post( reverse(views.diffrecursive_submit), {'diff': diff}) self.assert_( 'You submitted a patch that would revert the correct changes back to the originals. You may have mixed the parameters for diff, or performed a reverse patch.' in utf8(error)) paulproteus = Person.objects.get(user__username='******') self.assertEqual(len(StepCompletion.objects.filter( step__name='diffpatch_diffrecursive', person=paulproteus)), 0)
def test_downloads_of_needed_files(self): orig_response = self.client.get( reverse(views.patchsingle_get_original_file)) self.assert_( orig_response['Content-Disposition'].startswith('attachment')) patch_response = self.client.get(reverse(views.patchsingle_get_patch)) self.assert_( patch_response['Content-Disposition'].startswith('attachment'))
def test_downloads_of_needed_files(self): orig_response = self.client.get( reverse(views.patchsingle_get_original_file)) self.assert_(orig_response['Content-Disposition'] .startswith('attachment')) patch_response = self.client.get(reverse(views.patchsingle_get_patch)) self.assert_(patch_response['Content-Disposition'] .startswith('attachment'))
def test_do_mission_correctly(self): orig_response = self.client.get(reverse(views.diffsingle_get_original_file)) orig_lines = StringIO(orig_response.content).readlines() result_lines = open(view_helpers.DiffSingleFileMission.NEW_FILE).readlines() diff = "".join(difflib.unified_diff(orig_lines, result_lines)) submit_response = self.client.post(reverse(views.diffsingle_submit), {"diff": diff}) self.assert_(submit_response.context["diffsingle_success"]) paulproteus = Person.objects.get(user__username="******") self.assertEqual(len(StepCompletion.objects.filter(step__name="diffpatch_diffsingle", person=paulproteus)), 1)
def _calculate_correct_recursive_diff(self, dos_line_endings=False): orig_response = self.client.get( reverse(views.diffrecursive_get_original_tarball)) tfile = tarfile.open( fileobj=StringIO(orig_response.content), mode='r:gz') diff = StringIO() for fileinfo in tfile: if not fileinfo.isfile(): continue oldlines = tfile.extractfile(fileinfo).readlines() newlines = [] for line in oldlines: for old, new in view_helpers.DiffRecursiveMission.SUBSTITUTIONS: line = line.replace(old, new) newlines.append(line) lines_for_output = list( difflib.unified_diff(oldlines, newlines, 'orig-' + fileinfo.name, fileinfo.name)) bytes_for_output = ''.join(lines_for_output) if dos_line_endings: bytes_for_output = bytes_for_output.replace('\n', '\r\n') diff.write(bytes_for_output) diff.seek(0) diff.name = 'foo.patch' return diff
def _calculate_correct_recursive_diff(self, dos_line_endings=False): orig_response = self.client.get( reverse(views.diffrecursive_get_original_tarball)) tfile = tarfile.open(fileobj=StringIO(orig_response.content), mode='r:gz') diff = StringIO() for fileinfo in tfile: if not fileinfo.isfile(): continue oldlines = tfile.extractfile(fileinfo).readlines() newlines = [] for line in oldlines: for old, new in view_helpers.DiffRecursiveMission.SUBSTITUTIONS: line = line.replace(old, new) newlines.append(line) lines_for_output = list( difflib.unified_diff(oldlines, newlines, 'orig-' + fileinfo.name, fileinfo.name)) bytes_for_output = ''.join(lines_for_output) if dos_line_endings: bytes_for_output = bytes_for_output.replace('\n', '\r\n') diff.write(bytes_for_output) diff.seek(0) diff.name = 'foo.patch' return diff
def test_do_mission_correctly(self): correct_diff = self._calculate_correct_recursive_diff() submit_response = self.client.post(reverse(views.diffrecursive_submit), {'diff': correct_diff}) self.assert_(submit_response.context['diffrecursive_success']) paulproteus = Person.objects.get(user__username='******') self.assertEqual(len(StepCompletion.objects.filter(step__name='diffpatch_diffrecursive', person=paulproteus)), 1)
def test_do_mission_incorrectly(self): submit_response = self.client.post( reverse(views.pip_list_submit), {"piplist_output": pip_freeze_output_with_requests} ) self.assertFalse(submit_response.context["piplist_success"]) paulproteus = Person.objects.get(user__username="******") self.assertEqual(len(StepCompletion.objects.filter(step__name="pipvirtualenv_piplist", person=paulproteus)), 0)
def test_do_mission_incorrectly(self): patched_file = StringIO("Some arbitrary contents so the file is incorrect.") patched_file.name = "foo.c" submit_response = self.client.post(reverse(views.patchsingle_submit), {"patched_file": patched_file}) self.assertFalse(submit_response.context["patchsingle_success"]) paulproteus = Person.objects.get(user__username="******") self.assertEqual(len(StepCompletion.objects.filter(step__name="diffpatch_patchsingle", person=paulproteus)), 0)
def test_do_mission_incorrectly_BOM(self): diff = StringIO('--- a/foo.txt\n+++ b/foo.txt\n@@ -0,0 +0,1 @@\n+\xef\xbb\xbfHello World\n') diff.name = 'foo.patch' submit_response = self.client.post(reverse(views.diffrecursive_submit), {'diff': diff}) self.assertFalse(submit_response.context['diffrecursive_success']) paulproteus = Person.objects.get(user__username='******') self.assertEqual(len(StepCompletion.objects.filter(step__name='diffpatch_diffrecursive', person=paulproteus)), 0)
def test_do_mission_correctly(self): response = self.client.post( reverse(views.patchrecursive_submit), view_helpers.PatchRecursiveMission.ANSWERS) self.assert_(response.status_code, 302) paulproteus = Person.objects.get(user__username='******') self.assertEqual(len(StepCompletion.objects.filter( step__name='diffpatch_patchrecursive', person=paulproteus)), 1)
def test_do_mission_incorrectly(self): answers = {} for key, value in controllers.PatchRecursiveMission.ANSWERS.iteritems(): answers[key] = value + 1 response = self.client.post(reverse(views.patchrecursive_submit), answers) self.assertFalse(response.context['patchrecursive_success']) paulproteus = Person.objects.get(user__username='******') self.assertEqual(len(StepCompletion.objects.filter(step__name='diffpatch_patchrecursive', person=paulproteus)), 0)
def test_do_mission_correctly(self): orig_response = self.client.get( reverse(views.diffsingle_get_original_file)) orig_lines = StringIO(orig_response.content).readlines() result_lines = open( view_helpers.DiffSingleFileMission.NEW_FILE).readlines() diff = ''.join(difflib.unified_diff(orig_lines, result_lines)) submit_response = self.client.post(reverse(views.diffsingle_submit), {'diff': diff}) self.assert_(submit_response.context['diffsingle_success']) paulproteus = Person.objects.get(user__username='******') self.assertEqual( len( StepCompletion.objects.filter( step__name='diffpatch_diffsingle', person=paulproteus)), 1)
def test_do_mission_backwards_diff(self): """ Submitting a backwards diff generates a special failure message. """ submit_response = self.client.post(reverse(views.diffsingle_submit), {"diff": self.make_swapped_patch()}) self.assert_("order of files passed to diff was flipped" in submit_response.content) self.assertFalse(submit_response.context["diffsingle_success"]) paulproteus = Person.objects.get(user__username="******") self.assertEqual(len(StepCompletion.objects.filter(step__name="diffpatch_diffsingle", person=paulproteus)), 0)
def test_do_mission_correctly_with_dos_line_endings(self): correct_diff = self._calculate_correct_recursive_diff(dos_line_endings=True) self.assertTrue("\r\n" in correct_diff.getvalue()) submit_response = self.client.post(reverse(views.diffrecursive_submit), {"diff": correct_diff}) self.assert_(submit_response.context["diffrecursive_success"]) paulproteus = Person.objects.get(user__username="******") self.assertEqual( len(StepCompletion.objects.filter(step__name="diffpatch_diffrecursive", person=paulproteus)), 1 )
def test_do_mission_incorrectly(self): diff = StringIO("--- a/foo.txt\n+++ b/foo.txt\n@@ -0,0 +0,1 @@\n+Hello World\n") diff.name = "foo.patch" submit_response = self.client.post(reverse(views.diffrecursive_submit), {"diff": diff}) self.assertFalse(submit_response.context["diffrecursive_success"]) paulproteus = Person.objects.get(user__username="******") self.assertEqual( len(StepCompletion.objects.filter(step__name="diffpatch_diffrecursive", person=paulproteus)), 0 )
def test_do_mission_incorrectly(self): submit_response = self.client.post( reverse(views.pip_list_submit), {'piplist_output': pip_freeze_output_with_requests} ) self.assertFalse(submit_response.context['piplist_success']) paulproteus = Person.objects.get(user__username='******') self.assertEqual(len(StepCompletion.objects.filter( step__name='pipvirtualenv_piplist', person=paulproteus)), 0)
def test_do_mission_incorrectly(self): """ Submitting a generically incorrect diff generates a generic failure message. """ submit_response = self.client.post(reverse(views.diffsingle_submit), {"diff": self.make_wrong_dest_patch()}) self.assert_("does not have the correct contents" in submit_response.content) self.assertFalse(submit_response.context["diffsingle_success"]) paulproteus = Person.objects.get(user__username="******") self.assertEqual(len(StepCompletion.objects.filter(step__name="diffpatch_diffsingle", person=paulproteus)), 0)
def test_do_mission_incorrectly_with_leading_dot_slash(self): file_path = get_mission_test_data_path('diffpatch') for i in range(1,4): filename = os.path.join(file_path, "leading_dot_slash_diff_" + str(i) + ".txt") with open(filename) as f: leading_dot_slash_diff = f.read() submit_response = self.client.post(reverse(views.diffrecursive_submit), {'diff': leading_dot_slash_diff}) self.assertFalse(submit_response.context['diffrecursive_success']) paulproteus = Person.objects.get(user__username='******') self.assertEqual(len(StepCompletion.objects.filter(step__name='diffpatch_diffrecursive', person=paulproteus)), 0)
def test_do_mission_correctly(self): response = self.client.post(reverse(views.patchrecursive_submit), view_helpers.PatchRecursiveMission.ANSWERS) self.assert_(response.status_code, 302) paulproteus = Person.objects.get(user__username='******') self.assertEqual( len( StepCompletion.objects.filter( step__name='diffpatch_patchrecursive', person=paulproteus)), 1)
def test_do_mission_correctly(self): correct_diff = self._calculate_correct_recursive_diff() submit_response = self.client.post(reverse(views.diffrecursive_submit), {'diff': correct_diff}) self.assert_(submit_response.context['diffrecursive_success']) paulproteus = Person.objects.get(user__username='******') self.assertEqual( len( StepCompletion.objects.filter( step__name='diffpatch_diffrecursive', person=paulproteus)), 1)
def test_do_mission_correctly_with_old_filenames(self): orig_response = self.client.get( reverse(views.diffrecursive_get_original_tarball)) tfile = tarfile.open(fileobj=StringIO(orig_response.content), mode='r:gz') diff = StringIO() for fileinfo in tfile: if not fileinfo.isfile(): continue # calcualate the old name transformed_name = view_helpers.DiffRecursiveMission.name_new2old( fileinfo.name) oldlines = tfile.extractfile(fileinfo).readlines() newlines = [] for line in oldlines: for old, new in ( view_helpers.DiffRecursiveMission.SUBSTITUTIONS): line = line.replace(old, new) newlines.append(line) diff.writelines( difflib.unified_diff(oldlines, newlines, 'orig-' + transformed_name, transformed_name)) diff.seek(0) diff.name = 'foo.patch' submit_response = self.client.post(reverse(views.diffrecursive_submit), {'diff': diff}) self.assert_(submit_response.context['diffrecursive_success']) paulproteus = Person.objects.get(user__username='******') self.assertEqual( len( StepCompletion.objects.filter( step__name='diffpatch_diffrecursive', person=paulproteus)), 1)
def test_do_mission_incorrectly(self): answers = {} for key, value in view_helpers.PatchRecursiveMission.ANSWERS.iteritems(): answers[key] = value + 1 response = self.client.post(reverse(views.patchrecursive_submit), answers) self.assertFalse(response.context["patchrecursive_success"]) self.assertIn("That is not the right number of garlic cloves", response.content) paulproteus = Person.objects.get(user__username="******") self.assertEqual( len(StepCompletion.objects.filter(step__name="diffpatch_patchrecursive", person=paulproteus)), 0 )
def test_do_mission_incorrectly(self): diff = StringIO( '--- a/foo.txt\n+++ b/foo.txt\n@@ -0,0 +0,1 @@\n+Hello World\n') diff.name = 'foo.patch' submit_response = self.client.post(reverse(views.diffrecursive_submit), {'diff': diff}) self.assertFalse(submit_response.context['diffrecursive_success']) paulproteus = Person.objects.get(user__username='******') self.assertEqual( len( StepCompletion.objects.filter( step__name='diffpatch_diffrecursive', person=paulproteus)), 0)
def test_do_mission_incorrectly(self): patched_file = StringIO( 'Some arbitrary contents so the file is incorrect.') patched_file.name = 'foo.c' submit_response = self.client.post(reverse(views.patchsingle_submit), {'patched_file': patched_file}) self.assertFalse(submit_response.context['patchsingle_success']) paulproteus = Person.objects.get(user__username='******') self.assertEqual( len( StepCompletion.objects.filter( step__name='diffpatch_patchsingle', person=paulproteus)), 0)
def test_do_mission_incorrectly(self): answers = {} for key, value in view_helpers.PatchRecursiveMission.ANSWERS.iteritems( ): answers[key] = value + 1 response = self.client.post(reverse(views.patchrecursive_submit), answers) self.assertFalse(response.context['patchrecursive_success']) paulproteus = Person.objects.get(user__username='******') self.assertEqual( len( StepCompletion.objects.filter( step__name='diffpatch_patchrecursive', person=paulproteus)), 0)
def test_do_mission_backwards_diff(self): """ Submitting a backwards diff generates a special failure message. """ submit_response = self.client.post(reverse(views.diffsingle_submit), {'diff': self.make_swapped_patch()}) self.assert_('order of files passed to diff was flipped' in submit_response.content) self.assertFalse(submit_response.context['diffsingle_success']) paulproteus = Person.objects.get(user__username='******') self.assertEqual( len( StepCompletion.objects.filter( step__name='diffpatch_diffsingle', person=paulproteus)), 0)
def test_do_mission_incorrectly(self): """ Submitting a generically incorrect diff generates a generic failure message. """ submit_response = self.client.post(reverse( views.diffsingle_submit), {'diff': self.make_wrong_dest_patch()}) self.assert_( 'does not have the correct contents' in submit_response.content) self.assertFalse(submit_response.context['diffsingle_success']) paulproteus = Person.objects.get(user__username='******') self.assertEqual( len( StepCompletion.objects.filter( step__name='diffpatch_diffsingle', person=paulproteus)), 0)
def _calculate_correct_recursive_diff(self): orig_response = self.client.get(reverse(views.diffrecursive_get_original_tarball)) tfile = tarfile.open(fileobj=StringIO(orig_response.content), mode='r:gz') diff = StringIO() for fileinfo in tfile: if not fileinfo.isfile(): continue oldlines = tfile.extractfile(fileinfo).readlines() newlines = [] for line in oldlines: for old, new in controllers.DiffRecursiveMission.SUBSTITUTIONS: line = line.replace(old, new) newlines.append(line) diff.writelines(difflib.unified_diff(oldlines, newlines, 'orig-'+fileinfo.name, fileinfo.name)) diff.seek(0) diff.name = 'foo.patch' return diff
def test_do_mission_incorrectly_with_leading_dot_slash(self): file_path = get_mission_test_data_path('diffpatch') for i in range(1, 4): filename = os.path.join( file_path, "leading_dot_slash_diff_" + str(i) + ".txt") with open(filename) as f: leading_dot_slash_diff = f.read() submit_response = self.client.post( reverse(views.diffrecursive_submit), {'diff': leading_dot_slash_diff}) self.assertFalse(submit_response.context['diffrecursive_success']) paulproteus = Person.objects.get(user__username='******') self.assertEqual( len( StepCompletion.objects.filter( step__name='diffpatch_diffrecursive', person=paulproteus)), 0)
def test_do_mission_correctly_with_extra_slashes(self): # If you pass weird-looking directory names to diff, with extra slashes # (for example: diff -urN old_dir/// new_dir// ) # you should still be able to pass the mission. # First we calculcate a normal diff. correct_diff = self._calculate_correct_recursive_diff() # Then we mutate it to have extra slashes. as_string = correct_diff.getvalue() as_string = as_string.replace('recipes/', 'recipes//') as_stringio = StringIO(as_string) as_stringio.name = 'foo.patch' submit_response = self.client.post(reverse(views.diffrecursive_submit), {'diff': as_stringio}) self.assert_(submit_response.context['diffrecursive_success']) paulproteus = Person.objects.get(user__username='******') self.assertEqual(len(StepCompletion.objects.filter(step__name='diffpatch_diffrecursive', person=paulproteus)), 1)
def test_do_mission_correctly_with_extra_slashes(self): # If you pass weird-looking directory names to diff, with extra slashes # (for example: diff -urN old_dir/// new_dir// ) # you should still be able to pass the mission. # First we calculcate a normal diff. correct_diff = self._calculate_correct_recursive_diff() # Then we mutate it to have extra slashes. as_string = correct_diff.getvalue() as_string = as_string.replace('recipes/', 'recipes//') as_stringio = StringIO(as_string) as_stringio.name = 'foo.patch' submit_response = self.client.post(reverse(views.diffrecursive_submit), {'diff': as_stringio}) self.assert_(submit_response.context['diffrecursive_success']) paulproteus = Person.objects.get(user__username='******') self.assertEqual( len( StepCompletion.objects.filter( step__name='diffpatch_diffrecursive', person=paulproteus)), 1)
def test_submit_nothing_causes_an_error(self): submit_response = self.client.post(reverse(views.diffrecursive_submit)) self.assertEqual(submit_response.status_code, 200) self.assert_('No file was uploaded' in utf8(submit_response))
def test_submit_nothing_causes_an_error(self): submit_response = self.client.post(reverse(views.diffrecursive_submit)) self.assertEqual(submit_response.status_code, 200) self.assert_('No file was uploaded' in utf8(submit_response))