Skip to content

Add unit tests for _current_commit and _get_list_of_committed_files #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 13, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# pylint: disable=missing-docstring

import os
import shutil
import subprocess
import tempfile
import unittest

from git_pylint_commit_hook import commit_hook

class TestHook(unittest.TestCase):
# pylint: disable=protected-access,too-many-public-methods,invalid-name

def setUp(self):
# Create temporary directory
self.tmp_dir = tempfile.mkdtemp(prefix='pylint_hook_test_')

# Set current working directory to the temporary directory for
# all the commands run in the test
os.chdir(self.tmp_dir)

# Initialize temporary git repository
self.cmd('git init')

def tearDown(self):
shutil.rmtree(self.tmp_dir)

def write_file(self, filename, contents):
with open(os.path.join(self.tmp_dir, filename), 'w') as wfile:
wfile.write(contents)
return filename

def cmd(self, args):
return subprocess.check_output(args.split(), cwd=self.tmp_dir)

def test_current_commit(self):
"""Test commit_hook._current_commit"""

# Test empty tree
empty_hash = '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
self.assertEquals(commit_hook._current_commit(), empty_hash)

# Test after commit
self.cmd('git commit --allow-empty -m msg')
self.assertEquals(commit_hook._current_commit(), 'HEAD')

def test_list_of_committed_files(self):
"""Test commit_hook._get_list_of_committed_files"""

# Test empty tree
self.assertEquals(commit_hook._get_list_of_committed_files(), [])

# Create file 'a'
a = self.write_file('a', 'foo')
self.assertEquals(commit_hook._get_list_of_committed_files(), [])

# Add 'a'
self.cmd('git add ' + a)
self.assertEquals(commit_hook._get_list_of_committed_files(), [a])

# Commit 'a'
self.cmd('git commit -m msg')
self.assertEquals(commit_hook._get_list_of_committed_files(), [])

# Edit 'a'
self.write_file('a', 'bar')
self.assertEquals(commit_hook._get_list_of_committed_files(), [])

# Add 'a'
self.cmd('git add ' + a)
self.assertEquals(commit_hook._get_list_of_committed_files(), [a])