Skip to content

Commit b17afae

Browse files
committed
Add tests for AddNewlineAction
This adds tests for AddNewlineAction
1 parent be8d255 commit b17afae

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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\n'
22+
'Wrote tests\n'
23+
'\n'
24+
'Fixes #issue')
25+
self.uut = AddNewlineAction(self.shortlog, self.body)
26+
self.result = Result('origin', 'message')
27+
28+
self._old_cwd = os.getcwd()
29+
self.gitdir = mkdtemp()
30+
os.chdir(self.gitdir)
31+
self.gitfile = mkstemp(dir=self.gitdir)
32+
self.run_git_command('init')
33+
self.run_git_command('config', 'user.email [email protected]')
34+
self.run_git_command('config', 'user.name coala')
35+
msg = self.shortlog + '\n' + self.body
36+
self.run_git_command('add .')
37+
self.run_git_command('commit',
38+
'--file=-',
39+
stdin=msg)
40+
41+
def tearDown(self):
42+
os.chdir(self._old_cwd)
43+
if platform.system() == 'Windows':
44+
onerror = self._windows_rmtree_remove_readonly
45+
else:
46+
onerror = None
47+
shutil.rmtree(self.gitdir, onerror=onerror)
48+
49+
def test_is_applicable(self):
50+
applicable = self.uut.is_applicable(self.result,
51+
{},
52+
{'filename': 'Diff'})
53+
self.assertTrue(applicable)
54+
55+
applicable = self.uut.is_applicable(self.result,
56+
{},
57+
{'filename': 'Diff',
58+
'AddNewlineAction': True})
59+
self.assertFalse(applicable)
60+
61+
def test_apply(self):
62+
with retrieve_stdout() as stdout:
63+
file_diff_dict = self.uut.apply(self.result, {}, {})
64+
self.assertEqual(file_diff_dict, {'AddNewlineAction': True})
65+
self.assertEqual(stdout.getvalue(), '')
66+
67+
new_message, _ = run_shell_command('git log -1 --pretty=%B')
68+
new_message = new_message.rstrip('\n')
69+
self.assertEqual(new_message,
70+
self.shortlog + '\n\n' + self.body)

tests/vcs/actions/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)