diff --git a/tests/vcs/actions/AddNewlineActionTest.py b/tests/vcs/actions/AddNewlineActionTest.py new file mode 100644 index 0000000000..d99dacfa92 --- /dev/null +++ b/tests/vcs/actions/AddNewlineActionTest.py @@ -0,0 +1,67 @@ +import unittest +import os +import platform +import shutil +from tempfile import mkdtemp, mkstemp + +from coalib.results.Result import Result +from bears.vcs.actions.AddNewlineAction import AddNewlineAction +from coala_utils.ContextManagers import retrieve_stdout +from coalib.misc.Shell import run_shell_command + + +class AddNewlineActionTest(unittest.TestCase): + + @staticmethod + def run_git_command(*args, stdin=None): + return run_shell_command(' '.join(('git',) + args), stdin) + + def setUp(self): + self.shortlog = 'file.py: Add something' + self.body = 'Added something, wrote some things' + self.uut = AddNewlineAction(self.shortlog, self.body) + self.result = Result('origin', 'message') + + self._old_cwd = os.getcwd() + self.gitdir = mkdtemp() + os.chdir(self.gitdir) + self.gitfile = mkstemp(dir=self.gitdir) + self.run_git_command('init') + self.run_git_command('config', 'user.email coala@coala.io') + self.run_git_command('config', 'user.name coala') + msg = self.shortlog + '\n' + self.body + self.run_git_command('add .') + self.run_git_command('commit', + '--file=-', + stdin=msg) + + def tearDown(self): + os.chdir(self._old_cwd) + if platform.system() == 'Windows': + onerror = self._windows_rmtree_remove_readonly + else: + onerror = None + shutil.rmtree(self.gitdir, onerror=onerror) + + def test_is_applicable(self): + applicable = self.uut.is_applicable(self.result, + {}, + {'filename': 'Diff'}) + self.assertTrue(applicable) + + applicable = self.uut.is_applicable(self.result, + {}, + {'filename': 'Diff', + 'AddNewlineAction': True}) + self.assertFalse(applicable) + + def test_apply(self): + with retrieve_stdout() as stdout: + file_diff_dict = self.uut.apply(self.result, {}, {}) + self.assertEqual(file_diff_dict, {'AddNewlineAction': True}) + self.assertEqual(stdout.getvalue(), '') + + new_message, _ = run_shell_command('git log -1 --pretty=%B') + new_message = new_message.rstrip('\n') + self.assertEqual(new_message, + self.shortlog + '\n\n' + self.body) diff --git a/tests/vcs/actions/__init__.py b/tests/vcs/actions/__init__.py new file mode 100644 index 0000000000..e69de29bb2