kriti21

Untitled

Jun 29th, 2018
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.80 KB | None | 0 0
  1. import os
  2. import platform
  3. import shutil
  4. import stat
  5. import unittest
  6. import unittest.mock
  7. from queue import Queue
  8. from tempfile import mkdtemp
  9.  
  10. from coalib.testing.BearTestHelper import generate_skip_decorator
  11. from bears.vcs.VCSCommitBear import VCSCommitBear, CommitResult
  12. from bears.vcs.git.GitCommitBear import GitCommitBear
  13. from coalib.settings.Section import Section
  14. from coalib.misc.Shell import run_shell_command
  15. from .git.GitCommitBearTest import GitCommitBearTest
  16.  
  17.  
  18. class FakeCommitBear(VCSCommitBear):
  19.     @classmethod
  20.     def check_prerequisites(cls):
  21.         return True
  22.  
  23.     def get_head_commit(self):
  24.         return ('This is the fake head commit', '')
  25.  
  26.  
  27. @generate_skip_decorator(VCSCommitBear)
  28. class VCSCommitBearTest(unittest.TestCase):
  29.  
  30.     def run_uut(self, *args, **kwargs):
  31.         """
  32.        Runs the unit-under-test (via `self.uut.run()`) and collects the
  33.        messages of the yielded results as a list.
  34.  
  35.        :param args:   Positional arguments to forward to the run function.
  36.        :param kwargs: Keyword arguments to forward to the run function.
  37.        :return:       A list of the message strings.
  38.        """
  39.         return list((result.message) for result in self.uut.run(*args,
  40.                                                                 **kwargs))
  41.  
  42.     def setUp(self):
  43.         self.msg_queue = Queue()
  44.         self.section = Section('')
  45.         self.uut = FakeCommitBear(None, self.section, self.msg_queue)
  46.  
  47.         self._old_cwd = os.getcwd()
  48.         self.gitdir = mkdtemp()
  49.         os.chdir(self.gitdir)
  50.         GitCommitBearTest.run_git_command('init')
  51.         GitCommitBearTest.run_git_command(
  52.             'config', 'user.email coala@coala.io')
  53.         GitCommitBearTest.run_git_command('config', 'user.name coala')
  54.  
  55.     def test_head_commit(self):
  56.         self.assertEqual(self.run_uut(), ['HEAD commit information'])
  57.  
  58.  
  59. @generate_skip_decorator(GitCommitBear)
  60. class AnalyzeGitCommitTest(unittest.TestCase):
  61.  
  62.     def run_uut(self, *args, **kwargs):
  63.         """
  64.        Runs the unit-under-test (via `self.uut.run()`) and collects the
  65.        messages of the yielded results as a list.
  66.  
  67.        :param args:   Positional arguments to forward to the run function.
  68.        :param kwargs: Keyword arguments to forward to the run function.
  69.        :return:       A list of result values that give information related
  70.                       to head commit.
  71.        """
  72.         return list((result.raw_commit_message,
  73.                      result.commit_sha,
  74.                      result.commit_type,
  75.                      result.added_files,
  76.                      result.modified_files,
  77.                      result.deleted_files) for result in self.uut.run(
  78.                      *args, **kwargs))
  79.  
  80.     def setUp(self):
  81.         self.msg_queue = Queue()
  82.         self.section = Section('')
  83.         self.uut = GitCommitBear(None, self.section, self.msg_queue)
  84.  
  85.         self._old_cwd = os.getcwd()
  86.         self.gitdir = mkdtemp()
  87.         os.chdir(self.gitdir)
  88.         GitCommitBearTest.run_git_command('init')
  89.         GitCommitBearTest.run_git_command(
  90.             'config', 'user.email coala@coala.io')
  91.         GitCommitBearTest.run_git_command('config', 'user.name coala')
  92.  
  93.     @staticmethod
  94.     def _windows_rmtree_remove_readonly(func, path, excinfo):
  95.         os.chmod(path, stat.S_IWRITE)
  96.         func(path)
  97.  
  98.     def tearDown(self):
  99.         os.chdir(self._old_cwd)
  100.         if platform.system() == 'Windows':
  101.             onerror = self._windows_rmtree_remove_readonly
  102.         else:
  103.             onerror = None
  104.         shutil.rmtree(self.gitdir, onerror=onerror)
  105.  
  106.     def test_analyze_git_commit(self):
  107.         run_shell_command('git checkout -b testbranch master')
  108.         run_shell_command('touch testfile1.txt ')
  109.         run_shell_command('git add testfile1.txt')
  110.         run_shell_command('git commit -m "Add testfile1"')
  111.         test_sha1 = run_shell_command('git rev-parse HEAD')[0].strip('\n')
  112.         self.assertEqual(self.run_uut(),
  113.                          [('Add testfile1\n\n', test_sha1, ['merge_commit'],
  114.                            ['testfile1.txt'], [], [])])
  115.  
  116.         run_shell_command('touch testfile2.txt')
  117.         run_shell_command('git add testfile2.txt')
  118.         run_shell_command('git commit -m "Add testfile2 [ci skip]"')
  119.         test_sha2 = run_shell_command('git rev-parse HEAD')[0].strip('\n')
  120.         self.assertEqual(self.run_uut(),
  121.                          [('Add testfile2 [ci skip]\n\n', test_sha2,
  122.                            ['ci_skip_commit', 'merge_commit'],
  123.                            ['testfile2.txt'], [], [])])
  124.  
  125.         file_handle = open('testfile2.txt', 'a+')
  126.         file_handle.write('Some text')
  127.         file_handle.close()
  128.         run_shell_command('git add testfile2.txt')
  129.         run_shell_command('git commit -m "Modify testfile2"')
  130.         test_sha3 = run_shell_command('git rev-parse HEAD')[0].strip('\n')
  131.         self.assertEqual(self.run_uut(),
  132.                          [('Modify testfile2\n\n', test_sha3,
  133.                            ['merge_commit'],
  134.                            [], ['testfile2.txt'], [])])
  135.  
  136.         run_shell_command('touch testfile3.txt')
  137.         run_shell_command('git add testfile3.txt')
  138.         run_shell_command('git commit -m "another commit [skip ci]"')
  139.         test_sha3 = run_shell_command('git rev-parse HEAD')[0].strip('\n')
  140.         run_shell_command('git revert HEAD --no-edit')
  141.         test_sha4 = run_shell_command('git rev-parse HEAD')[0].strip('\n')
  142.         test_raw_commit_msg = ('Revert "another commit [skip ci]"\n\n'
  143.                                'This reverts commit %s.\n\n' % (test_sha3))
  144.         self.assertEqual(self.run_uut(),
  145.                          [(test_raw_commit_msg, test_sha4,
  146.                            ['ci_skip_commit', 'merge_commit', 'revert_commit'],
  147.                              [], [], ['testfile3.txt'])])
  148.  
  149.         run_shell_command('git checkout master')
  150.         run_shell_command('git checkout -b new-feature master')
  151.         run_shell_command('touch testfile4.txt ')
  152.         run_shell_command('git add testfile4.txt')
  153.         run_shell_command('git commit -m "Commit in feature branch"')
  154.         run_shell_command('git checkout master')
  155.         run_shell_command('git merge --no-ff new-feature')
  156.         test_raw_commit_msg = "Merge branch 'new-feature'\n\n"
  157.         test_sha5 = run_shell_command('git rev-parse HEAD')[0].strip('\n')
  158.         self.assertEqual(self.run_uut(),
  159.                          [(test_raw_commit_msg, test_sha5,
  160.                            ['merge_commit'], [], [], [])])
  161.  
  162.  
  163. class CommitResultTest(unittest.TestCase):
  164.     def setUp(self):
  165.         self.raw_commit_message = 'raw_commit_message'
  166.         self.commit_sha = 'commit_sha'
  167.         self.commit_type = ['commit_type']
  168.         self.modified_files = ['modified_files']
  169.         self.added_files = ['added_files']
  170.         self.deleted_files = ['deleted_files']
  171.  
  172.     def test_commitresult_object_repr(self):
  173.         repr_result = repr(CommitResult(VCSCommitBear,
  174.                                         self.raw_commit_message,
  175.                                         self.commit_sha,
  176.                                         self.commit_type,
  177.                                         self.modified_files,
  178.                                         self.added_files,
  179.                                         self.deleted_files,))
  180.  
  181.         repr_regex = (
  182.             r"<CommitResult object\(id=.+, origin=\'bearclass\', "
  183.             r"raw_commit_message=\'.+\', "
  184.             r"commit_sha=\'.+\', "
  185.             r'commit_type=\[.+\], '
  186.             r'modified_files=\[.+\], '
  187.             r'added_files=\[.+\], '
  188.             r'deleted_files=\[.+\], '
  189.             r"message=\'HEAD commit information\'\) at .+>"
  190.             )
  191.         self.assertRegex(repr_result, repr_regex)
Add Comment
Please, Sign In to add comment