|
| 1 | +import unittest |
| 2 | +import os |
| 3 | +import platform |
| 4 | +import shutil |
| 5 | +from tempfile import mkdtemp, mkstemp |
| 6 | + |
| 7 | +from coalib.results.Result import Result |
| 8 | +from bears.vcs.actions.AddNewlineAction import AddNewlineAction |
| 9 | +from coala_utils.ContextManagers import retrieve_stdout |
| 10 | +from coalib.misc.Shell import run_shell_command |
| 11 | + |
| 12 | + |
| 13 | +class AddNewlineActionTest(unittest.TestCase): |
| 14 | + |
| 15 | + @staticmethod |
| 16 | + def run_git_command(*args, stdin=None): |
| 17 | + return run_shell_command(' '.join(('git',) + args), stdin) |
| 18 | + |
| 19 | + def setUp(self): |
| 20 | + self.shortlog = 'file.py: Add something' |
| 21 | + self.body = 'Added something, wrote some things' |
| 22 | + self.uut = AddNewlineAction(self.shortlog, self.body) |
| 23 | + self.result = Result('origin', 'message') |
| 24 | + |
| 25 | + self._old_cwd = os.getcwd() |
| 26 | + self.gitdir = mkdtemp() |
| 27 | + os.chdir(self.gitdir) |
| 28 | + self.gitfile = mkstemp(dir=self.gitdir) |
| 29 | + self.run_git_command('init') |
| 30 | + self. run_git_command( 'config', 'user.email [email protected]') |
| 31 | + self.run_git_command('config', 'user.name coala') |
| 32 | + msg = self.shortlog + '\n' + self.body |
| 33 | + self.run_git_command('add .') |
| 34 | + self.run_git_command('commit', |
| 35 | + '--file=-', |
| 36 | + stdin=msg) |
| 37 | + |
| 38 | + def tearDown(self): |
| 39 | + os.chdir(self._old_cwd) |
| 40 | + if platform.system() == 'Windows': |
| 41 | + onerror = self._windows_rmtree_remove_readonly |
| 42 | + else: |
| 43 | + onerror = None |
| 44 | + shutil.rmtree(self.gitdir, onerror=onerror) |
| 45 | + |
| 46 | + def test_is_applicable(self): |
| 47 | + applicable = self.uut.is_applicable(self.result, |
| 48 | + {}, |
| 49 | + {'filename': 'Diff'}) |
| 50 | + self.assertTrue(applicable) |
| 51 | + |
| 52 | + applicable = self.uut.is_applicable(self.result, |
| 53 | + {}, |
| 54 | + {'filename': 'Diff', |
| 55 | + 'AddNewlineAction': True}) |
| 56 | + self.assertFalse(applicable) |
| 57 | + |
| 58 | + def test_apply(self): |
| 59 | + with retrieve_stdout() as stdout: |
| 60 | + file_diff_dict = self.uut.apply(self.result, {}, {}) |
| 61 | + self.assertEqual(file_diff_dict, {'AddNewlineAction': True}) |
| 62 | + self.assertEqual(stdout.getvalue(), '') |
| 63 | + |
| 64 | + new_message, _ = run_shell_command('git log -1 --pretty=%B') |
| 65 | + new_message = new_message.rstrip('\n') |
| 66 | + self.assertEqual(new_message, |
| 67 | + self.shortlog + '\n\n' + self.body) |
0 commit comments