Пример #1
0
    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)
Пример #2
0
    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
        )
Пример #3
0
 def test_backwards_diff(self):
     """
     A backwards diff generates a special IncorrectPatch exception.
     """
     try:
         view_helpers.DiffSingleFileMission.validate_patch(self.make_swapped_patch())
     except view_helpers.IncorrectPatch, e:
         self.assert_("order of files passed to diff was flipped" in utf8(e))
Пример #4
0
 def test_copy_paste_white_space_error(self):
     """
     A diff that is corrupted by the removal of white space while copying from terminal (mostly in the case of windows)
     """
     try:
         view_helpers.DiffSingleFileMission.validate_patch(self.make_copy_paste_error_patch())
     except view_helpers.IncorrectPatch, e:
         self.assert_('copy and paste from the terminal' in utf8(e))
Пример #5
0
 def test_copy_paste_white_space_error(self):
     """
     A diff that is corrupted by the removal of white space while copying from terminal (mostly in the case of windows)
     """
     try:
         view_helpers.DiffSingleFileMission.validate_patch(
             self.make_copy_paste_error_patch())
     except view_helpers.IncorrectPatch, e:
         self.assert_('copy and paste from the terminal' in utf8(e))
Пример #6
0
 def test_remove_trailing_whitespace(self):
     """
     A diff that is corrupted by the removal of line-end whitespace
     generates a special IncorrectPatch exception.
     """
     try:
         view_helpers.DiffSingleFileMission.validate_patch(self.make_patch_without_trailing_whitespace())
     except view_helpers.IncorrectPatch, e:
         self.assert_("removed the space" in utf8(e))
Пример #7
0
 def test_diff_missing_headers(self):
     """
     If a diff submission lacks headers, raise a special IncorrectPatch
     exception with a hint that the headers should be included.
     """
     try:
         view_helpers.DiffSingleFileMission.validate_patch("I lack headers.")
     except view_helpers.IncorrectPatch, e:
         self.assert_("Make sure you are including the diff headers" in utf8(e))
Пример #8
0
 def test_remove_trailing_whitespace(self):
     """
     A diff that is corrupted by the removal of line-end whitespace
     generates a special IncorrectPatch exception.
     """
     try:
         view_helpers.DiffSingleFileMission.validate_patch(
             self.make_patch_without_trailing_whitespace())
     except view_helpers.IncorrectPatch, e:
         self.assert_('removed the space' in utf8(e))
Пример #9
0
 def test_backwards_diff(self):
     """
     A backwards diff generates a special IncorrectPatch exception.
     """
     try:
         view_helpers.DiffSingleFileMission.validate_patch(
             self.make_swapped_patch())
     except view_helpers.IncorrectPatch, e:
         self.assert_(
             'order of files passed to diff was flipped' in utf8(e))
Пример #10
0
 def test_diff_missing_headers(self):
     """
     If a diff submission lacks headers, raise a special IncorrectPatch
     exception with a hint that the headers should be included.
     """
     try:
         view_helpers.DiffSingleFileMission.validate_patch(
             "I lack headers.")
     except view_helpers.IncorrectPatch, e:
         self.assert_(
             'Make sure you are including the diff headers' in utf8(e))
Пример #11
0
def diffrecursive_submit(request):
    # Initialize data array and some default values.
    data = {}
    data['diffrecursive_form'] = forms.DiffRecursiveUploadForm()
    data['diffrecursive_success'] = False
    data['diffrecursive_error_message'] = ''
    if request.method == 'POST':
        form = forms.DiffRecursiveUploadForm(request.POST, request.FILES)
        if form.is_valid():
            try:
                view_helpers.DiffRecursiveMission.validate_patch(form.cleaned_data['diff'].read())
                set_mission_completed(request.user.get_profile(), 'diffpatch_diffrecursive')
                data['diffrecursive_success'] = True
            except view_helpers.IncorrectPatch, e:
                data['diffrecursive_error_message'] = utf8(e)
        else:
            errors = list(form['diff'].errors)
            if errors:
                data['diffrecursive_error_message'] = (
                    data.get('diffrecursive_error_message', '') +
                    utf8(' '.join(errors)))
        data['diffrecursive_form'] = form
Пример #12
0
    def handle(self, *args, **options):
        # This management command is called from the mission svn repositories
        # as the pre-commit hook.  It receives the repository path and transaction
        # ID as arguments, and it receives a description of applicable lock
        # tokens on stdin.  Its environment and current directory are undefined.
        if len(args) != 2:
            raise CommandError, 'Exactly two arguments are expected.'
        repo_path, txn_id = args

        try:
            view_helpers.SvnCommitMission.pre_commit_hook(repo_path, txn_id)
        except view_helpers.IncorrectPatch, e:
            sys.stderr.write('\n    ' + utf8(e) + '\n\n')
            raise CommandError, 'The commit failed to validate.'
Пример #13
0
def diffrecursive_submit(request):
    # Initialize data array and some default values.
    data = {}
    data['diffrecursive_form'] = forms.DiffRecursiveUploadForm()
    data['diffrecursive_success'] = False
    data['diffrecursive_error_message'] = ''
    if request.method == 'POST':
        form = forms.DiffRecursiveUploadForm(request.POST, request.FILES)
        if form.is_valid():
            try:
                view_helpers.DiffRecursiveMission.validate_patch(
                    form.cleaned_data['diff'].read())
                set_mission_completed(request.user.get_profile(),
                                      'diffpatch_diffrecursive')
                data['diffrecursive_success'] = True
            except view_helpers.IncorrectPatch, e:
                data['diffrecursive_error_message'] = utf8(e)
        else:
            errors = list(form['diff'].errors)
            if errors:
                data['diffrecursive_error_message'] = (
                    data.get('diffrecursive_error_message', '') +
                    utf8(' '.join(errors)))
        data['diffrecursive_form'] = form
Пример #14
0
    def handle(self, *args, **options):
        # This management command is called from the mission svn repositories
        # as the pre-commit hook.  It receives the repository path and transaction
        # ID as arguments, and it receives a description of applicable lock
        # tokens on stdin.  Its environment and current directory are
        # undefined.
        if len(args) != 2:
            raise CommandError, 'Exactly two arguments are expected.'
        repo_path, txn_id = args

        try:
            view_helpers.SvnCommitMission.pre_commit_hook(repo_path, txn_id)
        except view_helpers.IncorrectPatch, e:
            sys.stderr.write('\n    ' + utf8(e) + '\n\n')
            raise CommandError, 'The commit failed to validate.'
Пример #15
0
def diffsingle_submit(request):
    # Initialize data array and some default values.
    data = {}
    data['diffsingle_form'] = forms.DiffSingleUploadForm()
    data['diffsingle_success'] = False
    data['diffsingle_error_message'] = ''
    if request.method == 'POST':
        form = forms.DiffSingleUploadForm(request.POST)
        if form.is_valid():
            try:
                view_helpers.DiffSingleFileMission.validate_patch(form.cleaned_data['diff'])
                set_mission_completed(request.user.get_profile(), 'diffpatch_diffsingle')
                data['diffsingle_success'] = True
            except view_helpers.IncorrectPatch, e:
                data['diffsingle_error_message'] = utf8(e)
        data['diffsingle_form'] = form
Пример #16
0
def upload(request):
    # Initialize data array and some default values.
    data = {}
    data['create_form'] = forms.UploadForm()
    data['create_success'] = False
    data['what_was_wrong_with_the_tarball'] = ''
    if request.method == 'POST':
        form = forms.UploadForm(request.POST, request.FILES)
        if form.is_valid():
            try:
                view_helpers.TarMission.check_tarfile(form.cleaned_data['tarfile'].read())
                data['create_success'] = True
                view_helpers.set_mission_completed(request.user.get_profile(), 'tar')
            except view_helpers.IncorrectTarFile, e:
                data['what_was_wrong_with_the_tarball'] = utf8(e)
        data['create_form'] = form
Пример #17
0
 def test_do_mission_incorrectly_leading_dot_slash_error_message(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()
         try:
             view_helpers.DiffRecursiveMission.validate_patch(
                 leading_dot_slash_diff)
         except view_helpers.IncorrectPatch, e:
             self.assertEqual(
                 'The diff you submitted will not apply properly because it has filename(s) starting with "./". Make sure that when you run the diff command, you do not add unnecessary "./" path prefixes. This is important because it affects the arguments needed to run the patch command for whoever applies the patch.',
                 utf8(e))
         else:
             self.fail('no exception raised')
Пример #18
0
def pip_list_submit(request):
    # Initialize data array and some default values.
    data = {}
    data['piplist_form'] = forms.PipListOutputForm()
    data['piplist_success'] = False
    data['piplist_error_message'] = ''
    if request.method == 'POST':
        form = forms.PipListOutputForm(request.POST)
        if form.is_valid():
            try:
                view_helpers.validate_pip_list_output(
                    form.cleaned_data['piplist_output'])
                set_mission_completed(
                    request.user.get_profile(), 'pipvirtualenv_piplist')
                data['piplist_success'] = True
            except view_helpers.IncorrectPipOutput, e:
                data['piplist_error_message'] = utf8(e)
        data['piplist_form'] = form
Пример #19
0
def diffsingle_submit(request):
    # Initialize data array and some default values.
    data = {}
    data['diffsingle_form'] = forms.DiffSingleUploadForm()
    data['diffsingle_success'] = False
    data['diffsingle_error_message'] = ''
    if request.method == 'POST':
        form = forms.DiffSingleUploadForm(request.POST)
        if form.is_valid():
            try:
                view_helpers.DiffSingleFileMission.validate_patch(
                    form.cleaned_data['diff'])
                set_mission_completed(request.user.get_profile(),
                                      'diffpatch_diffsingle')
                data['diffsingle_success'] = True
            except view_helpers.IncorrectPatch, e:
                data['diffsingle_error_message'] = utf8(e)
        data['diffsingle_form'] = form
Пример #20
0
def upload(request):
    # Initialize data array and some default values.
    data = {}
    data['create_form'] = forms.UploadForm()
    data['create_success'] = False
    data['what_was_wrong_with_the_tarball'] = ''
    if request.method == 'POST':
        form = forms.UploadForm(request.POST, request.FILES)
        if form.is_valid():
            try:
                view_helpers.TarMission.check_tarfile(
                    form.cleaned_data['tarfile'].read())
                data['create_success'] = True
                view_helpers.set_mission_completed(request.user.get_profile(),
                                                   'tar')
            except view_helpers.IncorrectTarFile, e:
                data['what_was_wrong_with_the_tarball'] = utf8(e)
        data['create_form'] = form
Пример #21
0
def pip_list_submit(request):
    # Initialize data array and some default values.
    data = {}
    data['piplist_form'] = forms.PipListOutputForm()
    data['piplist_success'] = False
    data['piplist_error_message'] = ''
    if request.method == 'POST':
        form = forms.PipListOutputForm(request.POST)
        if form.is_valid():
            try:
                view_helpers.validate_pip_list_output(
                    form.cleaned_data['piplist_output'])
                set_mission_completed(request.user.get_profile(),
                                      'pipvirtualenv_piplist')
                data['piplist_success'] = True
            except view_helpers.IncorrectPipOutput, e:
                data['piplist_error_message'] = utf8(e)
        data['piplist_form'] = form
Пример #22
0
 def test_do_mission_incorrectly_leading_dot_slash_error_message(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()
         try:
             view_helpers.DiffRecursiveMission.validate_patch(leading_dot_slash_diff)
         except view_helpers.IncorrectPatch, e:
             self.assertEqual(
                 (
                     "The diff you submitted will not apply properly because "
                     'it has filename(s) starting with "./". Make sure that '
                     "when you run the diff command, you do not add "
                     'unnecessary "./" path prefixes. This is important '
                     "because it affects the arguments needed to run the "
                     "patch command for whoever applies the patch."
                 ),
                 utf8(e),
             )
         else:
             self.fail("no exception raised")
Пример #23
0
 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))
Пример #24
0
 def test_produces_wrong_file(self):
     try:
         view_helpers.DiffSingleFileMission.validate_patch(
             self.make_wrong_dest_patch())
     except view_helpers.IncorrectPatch, e:
         self.assert_('does not have the correct contents' in utf8(e))
Пример #25
0
 def test_not_single_file(self):
     try:
         view_helpers.DiffSingleFileMission.validate_patch(
             self.make_good_patch() * 2)
     except view_helpers.IncorrectPatch, e:
         self.assert_('affects more than one file' in utf8(e))
Пример #26
0
 def test_not_single_file(self):
     try:
         view_helpers.DiffSingleFileMission.validate_patch(
             self.make_good_patch() * 2)
     except view_helpers.IncorrectPatch, e:
         self.assert_('affects more than one file' in utf8(e))
Пример #27
0
 def test_produces_wrong_file(self):
     try:
         view_helpers.DiffSingleFileMission.validate_patch(
             self.make_wrong_dest_patch())
     except view_helpers.IncorrectPatch, e:
         self.assert_('does not have the correct contents' in utf8(e))
Пример #28
0
 def test_not_single_file(self):
     try:
         controllers.DiffSingleFileMission.validate_patch(self.make_good_patch() * 2)
     except controllers.IncorrectPatch, e:
         self.assert_("affects more than one file" in utf8(e))
Пример #29
0
 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))