Skip to content

Commit

Permalink
Add tests for AddNewlineAction
Browse files Browse the repository at this point in the history
This adds tests for AddNewlineAction
  • Loading branch information
akshatkarani committed Jun 25, 2019
1 parent be8d255 commit b17afae
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions tests/vcs/actions/AddNewlineActionTest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
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\n'
'Wrote tests\n'
'\n'
'Fixes #issue')
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)
Empty file added tests/vcs/actions/__init__.py
Empty file.

0 comments on commit b17afae

Please sign in to comment.