|
| 1 | +import unittest |
| 2 | +import os |
| 3 | +import platform |
| 4 | +import shutil |
| 5 | +from tempfile import mkdtemp, mkstemp |
| 6 | +from unittest.mock import Mock |
| 7 | + |
| 8 | +from coalib.results.Result import Result |
| 9 | +from bears.vcs.actions.AddNewlineAction import AddNewlineAction |
| 10 | +from coala_utils.ContextManagers import retrieve_stdout |
| 11 | +from coalib.misc.Shell import run_shell_command |
| 12 | + |
| 13 | + |
| 14 | +class AddNewlineActionTest(unittest.TestCase): |
| 15 | + |
| 16 | + @staticmethod |
| 17 | + def run_git_command(*args, stdin=None): |
| 18 | + return run_shell_command(' '.join(('git',) + args), stdin) |
| 19 | + |
| 20 | + def setUp(self): |
| 21 | + self.shortlog = 'file.py: Add something' |
| 22 | + self.body = ('Added something, wrote some things\n' |
| 23 | + 'Wrote tests\n' |
| 24 | + '\n' |
| 25 | + 'Fixes #issue') |
| 26 | + self.uut = AddNewlineAction() |
| 27 | + self.result = Result('origin', 'message') |
| 28 | + |
| 29 | + # Creating a temporary git repository and |
| 30 | + # adding a commit to test |
| 31 | + self._old_cwd = os.getcwd() |
| 32 | + self.gitdir = mkdtemp() |
| 33 | + os.chdir(self.gitdir) |
| 34 | + self.gitfile = mkstemp(dir=self.gitdir) |
| 35 | + self.run_git_command('init') |
| 36 | + self. run_git_command( 'config', 'user.email [email protected]') |
| 37 | + self.run_git_command('config', 'user.name coala') |
| 38 | + self.msg = self.shortlog + '\n' + self.body |
| 39 | + self.run_git_command('add .') |
| 40 | + self.run_git_command('commit', |
| 41 | + '--file=-', |
| 42 | + stdin=self.msg) |
| 43 | + |
| 44 | + def tearDown(self): |
| 45 | + # Deleting the temporary repository |
| 46 | + os.chdir(self._old_cwd) |
| 47 | + if platform.system() == 'Windows': |
| 48 | + onerror = self._windows_rmtree_remove_readonly |
| 49 | + else: |
| 50 | + onerror = None |
| 51 | + shutil.rmtree(self.gitdir, onerror=onerror) |
| 52 | + |
| 53 | + def test_is_applicable_apply(self): |
| 54 | + # Applicable because there is no newline between shortlog and body |
| 55 | + self.assertTrue(self.uut.is_applicable(self.result, {}, {})) |
| 56 | + |
| 57 | + with retrieve_stdout() as stdout: |
| 58 | + self.uut.apply(self.result, {}, {}) |
| 59 | + new_message, _ = run_shell_command('git log -1 --pretty=%B') |
| 60 | + new_message = new_message.rstrip('\n') |
| 61 | + self.assertEqual(new_message, |
| 62 | + self.shortlog + '\n\n' + self.body) |
| 63 | + self.assertEqual(stdout.getvalue(), '') |
| 64 | + |
| 65 | + # Not applicable after action is applied |
| 66 | + self.assertFalse(self.uut.is_applicable(self.result, {}, {})) |
| 67 | + |
| 68 | + # Undoing the amend done by applying the action |
| 69 | + self.run_git_command('commit', |
| 70 | + '--amend', |
| 71 | + '--file=-', |
| 72 | + stdin=self.msg) |
| 73 | + |
| 74 | + def test_is_applicable_edited_message(self): |
| 75 | + # Applicable because there is no newline between shortlog and body |
| 76 | + self.assertTrue(self.uut.is_applicable(self.result, {}, {})) |
| 77 | + |
| 78 | + # Mocking EditCommitMessageAction to test cases where user first |
| 79 | + # changes commit message by appying EditCommitMessageAction, then |
| 80 | + # checking the applicability of AddNewlineAction |
| 81 | + EditCommitMessageAction = Mock() |
| 82 | + edited_msg1 = ('This is new commit message\n' |
| 83 | + 'Still no new line') |
| 84 | + edited_msg2 = ('This is lastest commit message\n' |
| 85 | + '\n' |
| 86 | + 'Finally a new line!!') |
| 87 | + |
| 88 | + EditCommitMessageAction.apply.side_effect = self.run_git_command( |
| 89 | + 'commit', '--amend', '--file=-', stdin=edited_msg1) |
| 90 | + EditCommitMessageAction.apply() |
| 91 | + self.assertTrue(self.uut.is_applicable(self.result, {}, {})) |
| 92 | + |
| 93 | + EditCommitMessageAction.apply.side_effect = self.run_git_command( |
| 94 | + 'commit', '--amend', '--file=-', stdin=edited_msg2) |
| 95 | + EditCommitMessageAction.apply() |
| 96 | + self.assertFalse(self.uut.is_applicable(self.result, {}, {})) |
0 commit comments