示例#1
0
class ApplyFromGitRefTest(unittest.TestCase):
    def setUp(self):
        self.sourcetree = SourceTree()
        self.sourcetree.get_local_repo_path = lambda c: os.path.abspath(
            os.path.join(os.path.dirname(__file__), 'testrepo'))
        self.sourcetree.start_with_checkout(17)
        self.sourcetree.run_command('git checkout test-start')
        self.sourcetree.run_command('git reset')

    def test_from_real_git_stuff(self):
        listing = CodeListing(filename='file1.txt',
                              contents=dedent("""
            file 1 line 2 amended
            file 1 line 3
            """).lstrip())
        listing.commit_ref = 'ch17l021'

        self.sourcetree.apply_listing_from_commit(listing)

        with open(self.sourcetree.tempdir + '/superlists/file1.txt') as f:
            assert f.read() == dedent("""
                file 1 line 1
                file 1 line 2 amended
                file 1 line 3
                """).lstrip()

        assert listing.was_written

    def test_leaves_staging_empty(self):
        listing = CodeListing(filename='file1.txt',
                              contents=dedent("""
            file 1 line 2 amended
            file 1 line 3
            """).lstrip())
        listing.commit_ref = 'ch17l021'

        self.sourcetree.apply_listing_from_commit(listing)

        staged = self.sourcetree.run_command('git diff --staged')
        assert staged == ''

    def test_raises_if_wrong_file(self):
        listing = CodeListing(filename='file2.txt',
                              contents=dedent("""
            file 1 line 1
            file 1 line 2 amended
            file 1 line 3
            """).lstrip())
        listing.commit_ref = 'ch17l021'

        with self.assertRaises(ApplyCommitException):
            self.sourcetree.apply_listing_from_commit(listing)

    def _checkout_commit(self, commit):
        commit_spec = self.sourcetree.get_commit_spec(commit)
        self.sourcetree.run_command('git checkout ' + commit_spec)
        self.sourcetree.run_command('git reset')

    def test_raises_if_too_many_files_in_commit(self):
        listing = CodeListing(filename='file1.txt',
                              contents=dedent("""
            file 1 line 1
            file 1 line 2
            """).lstrip())
        listing.commit_ref = 'ch17l023'

        self._checkout_commit('ch17l022')
        with self.assertRaises(ApplyCommitException):
            self.sourcetree.apply_listing_from_commit(listing)

    def test_raises_if_listing_doesnt_show_all_new_lines_in_diff(self):
        listing = CodeListing(filename='file1.txt',
                              contents=dedent("""
            file 1 line 3
            """).lstrip())
        listing.commit_ref = 'ch17l021'

        with self.assertRaises(ApplyCommitException):
            self.sourcetree.apply_listing_from_commit(listing)

    def test_raises_if_listing_lines_in_wrong_order(self):
        listing = CodeListing(filename='file1.txt',
                              contents=dedent("""
            file 1 line 3
            file 1 line 2 amended
            """).lstrip())
        listing.commit_ref = 'ch17l021'

        with self.assertRaises(ApplyCommitException):
            self.sourcetree.apply_listing_from_commit(listing)

    def test_line_ordering_check_isnt_confused_by_dupe_lines(self):
        listing = CodeListing(filename='file2.txt',
                              contents=dedent("""
            another line changed
            some duplicate lines coming up...

            hello
            goodbye
            hello
            """).lstrip())
        listing.commit_ref = 'ch17l027'
        self._checkout_commit('ch17l026')

        self.sourcetree.apply_listing_from_commit(listing)

    def test_line_ordering_check_isnt_confused_by_new_lines_that_dupe_existing(
            self):
        listing = CodeListing(filename='file2.txt',
                              contents=dedent("""
            some duplicate lines coming up...

            hello

            one more line at end
            add a line with a dupe of existing
            hello
            goodbye
            """).lstrip())
        listing.commit_ref = 'ch17l031'
        self._checkout_commit('ch17l030')

        self.sourcetree.apply_listing_from_commit(listing)

    def test_non_dupes_are_still_order_checked(self):
        listing = CodeListing(filename='file2.txt',
                              contents=dedent("""
            some duplicate lines coming up...

            hello

            one more line at end
            add a line with a dupe of existing
            goodbye
            hello
            """).lstrip())
        listing.commit_ref = 'ch17l031'
        self._checkout_commit('ch17l030')

        with self.assertRaises(ApplyCommitException):
            self.sourcetree.apply_listing_from_commit(listing)

    def test_raises_if_any_other_listing_lines_not_in_before_version(self):
        listing = CodeListing(filename='file1.txt',
                              contents=dedent("""
            what is this?
            file 1 line 2 amended
            file 1 line 3
            """).lstrip())
        listing.commit_ref = 'ch17l021'

        with self.assertRaises(ApplyCommitException):
            self.sourcetree.apply_listing_from_commit(listing)

    def test_happy_with_lines_in_before_and_after_version(self):
        listing = CodeListing(filename='file2.txt',
                              contents=dedent("""
            file 2 line 1 changed
            [...]

            hello
            hello

            one more line at end
            """).lstrip())
        listing.commit_ref = 'ch17l028'
        self._checkout_commit('ch17l027')

        self.sourcetree.apply_listing_from_commit(listing)

    def test_raises_if_listing_line_not_in_after_version(self):
        listing = CodeListing(filename='file2.txt',
                              contents=dedent("""
            hello
            goodbye
            hello

            one more line at end
            """).lstrip())
        listing.commit_ref = 'ch17l028'

        with self.assertRaises(ApplyCommitException):
            self.sourcetree.apply_listing_from_commit(listing)

    def test_happy_with_lines_from_just_before_diff(self):
        listing = CodeListing(filename='file1.txt',
                              contents=dedent("""
            file 1 line 1
            file 1 line 2 amended
            file 1 line 3
            """).lstrip())
        listing.commit_ref = 'ch17l021'

        self.sourcetree.apply_listing_from_commit(listing)

    def test_listings_showing_a_move_mean_can_ignore_commit_lines_added_and_removed(
            self):
        listing = CodeListing(filename='pythonfile.py',
                              contents=dedent("""
            class NuKlass(object):

                def method1(self):
                    [...]
                    a = a + 3
                    [...]
            """).lstrip())
        listing.commit_ref = 'ch17l029'
        self._checkout_commit('ch17l028-1')

        self.sourcetree.apply_listing_from_commit(listing)

    def test_listings_showing_a_move_mean_can_ignore_commit_lines_added_and_removed_2(
            self):
        listing = CodeListing(filename='file2.txt',
                              contents=dedent("""
            hello

            one more line at end
            """).lstrip())
        listing.commit_ref = 'ch17l030'
        self._checkout_commit('ch17l029')

        self.sourcetree.apply_listing_from_commit(listing)

    def test_happy_with_elipsis(self):
        listing = CodeListing(filename='file1.txt',
                              contents=dedent("""
            [...]
            file 1 line 2 amended
            file 1 line 3
            """).lstrip())
        listing.commit_ref = 'ch17l021'

        self.sourcetree.apply_listing_from_commit(listing)

    def DONTtest_listings_must_use_elipsis_to_indicate_skipped_lines(self):
        # TODO!
        lines = [
            "file 1 line 1",
            "file 1 line 2 amended",
            "file 1 line 3",
            "file 1 line 4 inserted",
            "another line",
        ]
        listing = CodeListing(filename='file1.txt', contents='')
        listing.commit_ref = 'ch17l022'
        self._checkout_commit('ch17l021')

        listing.contents = '\n'.join(lines)
        self.sourcetree.apply_listing_from_commit(listing)  # should not raise

        lines[1] = "[...]"
        listing.contents = '\n'.join(lines)
        self.sourcetree.apply_listing_from_commit(listing)  # should not raise

        lines.pop(1)
        listing.contents = '\n'.join(lines)
        with self.assertRaises(ApplyCommitException):
            self.sourcetree.apply_listing_from_commit(listing)

    def test_happy_with_python_callouts(self):
        listing = CodeListing(filename='file1.txt',
                              contents=dedent("""
            [...]
            file 1 line 2 amended #
            file 1 line 3 #
            """).lstrip())
        listing.commit_ref = 'ch17l021'

        self.sourcetree.apply_listing_from_commit(listing)

    def test_happy_with_js_callouts(self):
        listing = CodeListing(filename='file1.txt',
                              contents=dedent("""
            [...]
            file 1 line 2 amended //
            file 1 line 3 //
            """).lstrip())
        listing.commit_ref = 'ch17l021'

        self.sourcetree.apply_listing_from_commit(listing)

    def test_happy_with_blank_lines(self):
        listing = CodeListing(filename='file2.txt',
                              contents=dedent("""
            file 2 line 1 changed

            another line changed
            """).lstrip())
        listing.commit_ref = 'ch17l024'
        self._checkout_commit('ch17l023')

        self.sourcetree.apply_listing_from_commit(listing)

    def test_handles_indents(self):
        listing = CodeListing(filename='pythonfile.py',
                              contents=dedent("""
            def method1(self):
                # amend method 1
                return 2

            [...]
            """).lstrip())
        listing.commit_ref = 'ch17l026'
        self._checkout_commit('ch17l025')

        self.sourcetree.apply_listing_from_commit(listing)

    def test_over_indentation_differences_are_picked_up(self):
        listing = CodeListing(filename='pythonfile.py',
                              contents=dedent("""
            def method1(self):
                    # amend method 1
                return 2

            [...]
            """).lstrip())
        listing.commit_ref = 'ch17l026'
        self._checkout_commit('ch17l025')

        with self.assertRaises(ApplyCommitException):
            self.sourcetree.apply_listing_from_commit(listing)

    def test_under_indentation_differences_are_picked_up(self):
        listing = CodeListing(filename='pythonfile.py',
                              contents=dedent("""
            def method1(self):
            # amend method 1
            return 2

            [...]
            """).lstrip())
        listing.commit_ref = 'ch17l026'
        self._checkout_commit('ch17l025')

        with self.assertRaises(ApplyCommitException):
            self.sourcetree.apply_listing_from_commit(listing)

    def test_with_diff_listing_passing_case(self):
        listing = CodeListing(filename='file2.txt',
                              contents=dedent("""
            diff --git a/file2.txt b/file2.txt
            index 93f054e..519d518 100644
            --- a/file2.txt
            +++ b/file2.txt
            @@ -4,6 +4,5 @@ another line changed
             some duplicate lines coming up...

             hello
            -hello

             one more line at end
             """).lstrip())
        listing.commit_ref = 'ch17l030'
        self._checkout_commit('ch17l029')

        self.sourcetree.apply_listing_from_commit(listing)

    def test_with_diff_listing_failure_case(self):
        listing = CodeListing(filename='file2.txt',
                              contents=dedent("""
            diff --git a/file2.txt b/file2.txt
            index 93f054e..519d518 100644
            --- a/file2.txt
            +++ b/file2.txt
            @@ -4,6 +4,5 @@ another line changed
             some duplicate lines coming up...

             hello
            -hello
            +something else

             one more line at end
             """).lstrip())
        listing.commit_ref = 'ch17l030'
        self._checkout_commit('ch17l029')

        with self.assertRaises(ApplyCommitException):
            self.sourcetree.apply_listing_from_commit(listing)
示例#2
0
class ApplyFromGitRefTest(unittest.TestCase):

    def setUp(self):
        self.sourcetree = SourceTree()
        self.sourcetree.get_local_repo_path = lambda c: os.path.abspath(os.path.join(
            os.path.dirname(__file__), 'testrepo'
        ))
        self.sourcetree.start_with_checkout(17)
        self.sourcetree.run_command('git checkout test-start')
        self.sourcetree.run_command('git reset')


    def test_from_real_git_stuff(self):
        listing = CodeListing(filename='file1.txt', contents=dedent(
            """
            file 1 line 2 amended
            file 1 line 3
            """).lstrip()
        )
        listing.commit_ref = 'ch17l021'

        self.sourcetree.apply_listing_from_commit(listing)


        with open(self.sourcetree.tempdir + '/superlists/file1.txt') as f:
            assert f.read() == dedent(
                """
                file 1 line 1
                file 1 line 2 amended
                file 1 line 3
                """).lstrip()

        assert listing.was_written


    def test_leaves_staging_empty(self):
        listing = CodeListing(filename='file1.txt', contents=dedent(
            """
            file 1 line 2 amended
            file 1 line 3
            """).lstrip()
        )
        listing.commit_ref = 'ch17l021'

        self.sourcetree.apply_listing_from_commit(listing)

        staged = self.sourcetree.run_command('git diff --staged')
        assert staged == ''


    def test_raises_if_wrong_file(self):
        listing = CodeListing(filename='file2.txt', contents=dedent(
            """
            file 1 line 1
            file 1 line 2 amended
            file 1 line 3
            """).lstrip()
        )
        listing.commit_ref = 'ch17l021'

        with self.assertRaises(ApplyCommitException):
            self.sourcetree.apply_listing_from_commit(listing)


    def _checkout_commit(self, commit):
        commit_spec = self.sourcetree.get_commit_spec(commit)
        self.sourcetree.run_command('git checkout ' + commit_spec)
        self.sourcetree.run_command('git reset')


    def test_raises_if_too_many_files_in_commit(self):
        listing = CodeListing(filename='file1.txt', contents=dedent(
            """
            file 1 line 1
            file 1 line 2
            """).lstrip()
        )
        listing.commit_ref = 'ch17l023'

        self._checkout_commit('ch17l022')
        with self.assertRaises(ApplyCommitException):
            self.sourcetree.apply_listing_from_commit(listing)


    def test_raises_if_listing_doesnt_show_all_new_lines_in_diff(self):
        listing = CodeListing(filename='file1.txt', contents=dedent(
            """
            file 1 line 3
            """).lstrip()
        )
        listing.commit_ref = 'ch17l021'

        with self.assertRaises(ApplyCommitException):
            self.sourcetree.apply_listing_from_commit(listing)


    def test_raises_if_listing_lines_in_wrong_order(self):
        listing = CodeListing(filename='file1.txt', contents=dedent(
            """
            file 1 line 3
            file 1 line 2 amended
            """).lstrip()
        )
        listing.commit_ref = 'ch17l021'

        with self.assertRaises(ApplyCommitException):
            self.sourcetree.apply_listing_from_commit(listing)


    def test_line_ordering_check_isnt_confused_by_dupe_lines(self):
        listing = CodeListing(filename='file2.txt', contents=dedent(
            """
            another line changed
            some duplicate lines coming up...

            hello
            goodbye
            hello
            """).lstrip()
        )
        listing.commit_ref = 'ch17l027'
        self._checkout_commit('ch17l026')

        self.sourcetree.apply_listing_from_commit(listing)


    def test_line_ordering_check_isnt_confused_by_new_lines_that_dupe_existing(self):
        listing = CodeListing(filename='file2.txt', contents=dedent(
            """
            some duplicate lines coming up...

            hello

            one more line at end
            add a line with a dupe of existing
            hello
            goodbye
            """).lstrip()
        )
        listing.commit_ref = 'ch17l031'
        self._checkout_commit('ch17l030')

        self.sourcetree.apply_listing_from_commit(listing)


    def test_non_dupes_are_still_order_checked(self):
        listing = CodeListing(filename='file2.txt', contents=dedent(
            """
            some duplicate lines coming up...

            hello

            one more line at end
            add a line with a dupe of existing
            goodbye
            hello
            """).lstrip()
        )
        listing.commit_ref = 'ch17l031'
        self._checkout_commit('ch17l030')

        with self.assertRaises(ApplyCommitException):
            self.sourcetree.apply_listing_from_commit(listing)


    def test_raises_if_any_other_listing_lines_not_in_before_version(self):
        listing = CodeListing(filename='file1.txt', contents=dedent(
            """
            what is this?
            file 1 line 2 amended
            file 1 line 3
            """).lstrip()
        )
        listing.commit_ref = 'ch17l021'

        with self.assertRaises(ApplyCommitException):
            self.sourcetree.apply_listing_from_commit(listing)


    def test_happy_with_lines_in_before_and_after_version(self):
        listing = CodeListing(filename='file2.txt', contents=dedent(
            """
            file 2 line 1 changed
            [...]

            hello
            hello

            one more line at end
            """).lstrip()
        )
        listing.commit_ref = 'ch17l028'
        self._checkout_commit('ch17l027')

        self.sourcetree.apply_listing_from_commit(listing)


    def test_raises_if_listing_line_not_in_after_version(self):
        listing = CodeListing(filename='file2.txt', contents=dedent(
            """
            hello
            goodbye
            hello

            one more line at end
            """).lstrip()
        )
        listing.commit_ref = 'ch17l028'

        with self.assertRaises(ApplyCommitException):
            self.sourcetree.apply_listing_from_commit(listing)


    def test_happy_with_lines_from_just_before_diff(self):
        listing = CodeListing(filename='file1.txt', contents=dedent(
            """
            file 1 line 1
            file 1 line 2 amended
            file 1 line 3
            """).lstrip()
        )
        listing.commit_ref = 'ch17l021'

        self.sourcetree.apply_listing_from_commit(listing)


    def test_listings_showing_a_move_mean_can_ignore_commit_lines_added_and_removed(self):
        listing = CodeListing(filename='pythonfile.py', contents=dedent(
            """
            class NuKlass(object):

                def method1(self):
                    [...]
                    a = a + 3
                    [...]
            """).lstrip()
        )
        listing.commit_ref = 'ch17l029'
        self._checkout_commit('ch17l028-1')

        self.sourcetree.apply_listing_from_commit(listing)


    def test_listings_showing_a_move_mean_can_ignore_commit_lines_added_and_removed_2(self):
        listing = CodeListing(filename='file2.txt', contents=dedent(
            """
            hello

            one more line at end
            """).lstrip()
        )
        listing.commit_ref = 'ch17l030'
        self._checkout_commit('ch17l029')

        self.sourcetree.apply_listing_from_commit(listing)



    def test_happy_with_elipsis(self):
        listing = CodeListing(filename='file1.txt', contents=dedent(
            """
            [...]
            file 1 line 2 amended
            file 1 line 3
            """).lstrip()
        )
        listing.commit_ref = 'ch17l021'

        self.sourcetree.apply_listing_from_commit(listing)


    def DONTtest_listings_must_use_elipsis_to_indicate_skipped_lines(self):
        # TODO!
        lines = [
            "file 1 line 1",
            "file 1 line 2 amended",
            "file 1 line 3",
            "file 1 line 4 inserted",
            "another line",
        ]
        listing = CodeListing(filename='file1.txt', contents='')
        listing.commit_ref = 'ch17l022'
        self._checkout_commit('ch17l021')

        listing.contents = '\n'.join(lines)
        self.sourcetree.apply_listing_from_commit(listing) # should not raise

        lines[1] = "[...]"
        listing.contents = '\n'.join(lines)
        self.sourcetree.apply_listing_from_commit(listing) # should not raise

        lines.pop(1)
        listing.contents = '\n'.join(lines)
        with self.assertRaises(ApplyCommitException):
            self.sourcetree.apply_listing_from_commit(listing)




    def test_happy_with_python_callouts(self):
        listing = CodeListing(filename='file1.txt', contents=dedent(
            """
            [...]
            file 1 line 2 amended #
            file 1 line 3 #
            """).lstrip()
        )
        listing.commit_ref = 'ch17l021'

        self.sourcetree.apply_listing_from_commit(listing)


    def test_happy_with_js_callouts(self):
        listing = CodeListing(filename='file1.txt', contents=dedent(
            """
            [...]
            file 1 line 2 amended //
            file 1 line 3 //
            """).lstrip()
        )
        listing.commit_ref = 'ch17l021'

        self.sourcetree.apply_listing_from_commit(listing)


    def test_happy_with_blank_lines(self):
        listing = CodeListing(filename='file2.txt', contents=dedent(
            """
            file 2 line 1 changed

            another line changed
            """).lstrip()
        )
        listing.commit_ref = 'ch17l024'
        self._checkout_commit('ch17l023')

        self.sourcetree.apply_listing_from_commit(listing)


    def test_handles_indents(self):
        listing = CodeListing(filename='pythonfile.py', contents=dedent(
            """
            def method1(self):
                # amend method 1
                return 2

            [...]
            """).lstrip()
        )
        listing.commit_ref = 'ch17l026'
        self._checkout_commit('ch17l025')

        self.sourcetree.apply_listing_from_commit(listing)


    def test_over_indentation_differences_are_picked_up(self):
        listing = CodeListing(filename='pythonfile.py', contents=dedent(
            """
            def method1(self):
                    # amend method 1
                return 2

            [...]
            """).lstrip()
        )
        listing.commit_ref = 'ch17l026'
        self._checkout_commit('ch17l025')

        with self.assertRaises(ApplyCommitException):
            self.sourcetree.apply_listing_from_commit(listing)


    def test_under_indentation_differences_are_picked_up(self):
        listing = CodeListing(filename='pythonfile.py', contents=dedent(
            """
            def method1(self):
            # amend method 1
            return 2

            [...]
            """).lstrip()
        )
        listing.commit_ref = 'ch17l026'
        self._checkout_commit('ch17l025')

        with self.assertRaises(ApplyCommitException):
            self.sourcetree.apply_listing_from_commit(listing)



    def test_with_diff_listing_passing_case(self):
        listing = CodeListing(filename='file2.txt', contents=dedent(
            """
            diff --git a/file2.txt b/file2.txt
            index 93f054e..519d518 100644
            --- a/file2.txt
            +++ b/file2.txt
            @@ -4,6 +4,5 @@ another line changed
             some duplicate lines coming up...

             hello
            -hello

             one more line at end
             """).lstrip()
        )
        listing.commit_ref = 'ch17l030'
        self._checkout_commit('ch17l029')

        self.sourcetree.apply_listing_from_commit(listing)


    def test_with_diff_listing_failure_case(self):
        listing = CodeListing(filename='file2.txt', contents=dedent(
            """
            diff --git a/file2.txt b/file2.txt
            index 93f054e..519d518 100644
            --- a/file2.txt
            +++ b/file2.txt
            @@ -4,6 +4,5 @@ another line changed
             some duplicate lines coming up...

             hello
            -hello
            +something else

             one more line at end
             """).lstrip()
        )
        listing.commit_ref = 'ch17l030'
        self._checkout_commit('ch17l029')

        with self.assertRaises(ApplyCommitException):
            self.sourcetree.apply_listing_from_commit(listing)