-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from pomalley/master
Add git_commit_detail directive.
- Loading branch information
Showing
4 changed files
with
292 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import os | ||
from shutil import rmtree | ||
from tempfile import mkdtemp, mkstemp | ||
from BeautifulSoup import BeautifulStoneSoup | ||
|
||
from git import Repo | ||
from mock import Mock | ||
|
||
from sphinx_git import GitCommitDetail | ||
|
||
from tests.assertions import ( | ||
assert_equal, | ||
assert_greater, | ||
assert_less_equal, | ||
assert_in, | ||
assert_not_in, | ||
assert_raises, | ||
assert_is, | ||
assert_is_not, | ||
) | ||
|
||
|
||
class TestableGitCommitDetail(GitCommitDetail): | ||
github_nonce_url = 'https://github.com/no_user/no_repo.git/' | ||
github_nonce_commit_base = 'https://github.com/no_user/no_repo/commit/' | ||
|
||
def __init__(self): | ||
self.lineno = 123 | ||
self.options = {} | ||
self.state = Mock() | ||
|
||
|
||
class TempDirTestCase(object): | ||
def setup(self): | ||
self.root = mkdtemp() | ||
self.commit_detail = TestableGitCommitDetail() | ||
self.commit_detail.state.document.settings.env.srcdir = self.root | ||
|
||
def teardown(self): | ||
rmtree(self.root) | ||
|
||
|
||
class TestWithRepository(TempDirTestCase): | ||
def setup(self): | ||
super(TestWithRepository, self).setup() | ||
self.repo = Repo.init(self.root) | ||
self.repo.config_writer().set_value('user', 'name', 'Test User') | ||
|
||
def test_commit_only(self): | ||
self.repo.index.commit('my root commit') | ||
self.commit_detail.options = {'commit': True} | ||
nodes = self.commit_detail.run() | ||
node_p = nodes[0] # <p> node | ||
node_fl = node_p[0] # field list | ||
node_f = node_fl[0] # field | ||
assert_equal(1, len(node_fl)) | ||
assert_equal('Commit', node_f[0].astext()) | ||
assert_equal( | ||
self.repo.commit().hexsha[:GitCommitDetail.default_sha_length], | ||
node_f[1].astext() | ||
) | ||
|
||
def test_branch_only(self): | ||
self.repo.index.commit('my root commit') | ||
self.commit_detail.options = {'branch': True} | ||
nodes = self.commit_detail.run() | ||
node_p = nodes[0] # <p> node | ||
node_fl = node_p[0] # field list | ||
node_f = node_fl[0] # field | ||
assert_equal(1, len(node_fl)) | ||
assert_equal('Branch', node_f[0].astext()) | ||
assert_equal('master', node_f[1].astext()) | ||
|
||
def test_commit_and_branch(self): | ||
self.repo.index.commit('my root commit') | ||
self.commit_detail.options = {'commit': True, 'branch': True} | ||
nodes = self.commit_detail.run() | ||
node_p = nodes[0] # <p> node | ||
node_fl = node_p[0] # field list | ||
node_f_b = node_fl[0] # field--branch | ||
node_f_c = node_fl[1] # field--commit | ||
assert_equal(2, len(node_fl)) | ||
assert_equal('Commit', node_f_c[0].astext()) | ||
assert_equal('Branch', node_f_b[0].astext()) | ||
|
||
def test_github_link(self): | ||
self.repo.index.commit('my root commit') | ||
self.commit_detail.options = {'commit': True} | ||
self.repo.create_remote('origin', self.commit_detail.github_nonce_url) | ||
nodes = self.commit_detail.run() | ||
list_markup = BeautifulStoneSoup(str(nodes[0])) | ||
assert_is_not(list_markup.reference, None) | ||
assert_equal( | ||
self.commit_detail.github_nonce_commit_base + | ||
self.repo.commit().hexsha, | ||
list_markup.reference['refuri'] | ||
) | ||
assert_equal( | ||
self.repo.commit().hexsha[:GitCommitDetail.default_sha_length], | ||
list_markup.reference.text | ||
) | ||
|
||
def test_no_github_link(self): | ||
self.repo.index.commit('my root commit') | ||
self.commit_detail.options = {'commit': True, 'no_github_link': True} | ||
self.repo.create_remote('origin', self.commit_detail.github_nonce_url) | ||
nodes = self.commit_detail.run() | ||
list_markup = BeautifulStoneSoup(str(nodes[0])) | ||
assert_is(list_markup.reference, None) | ||
|
||
def test_sha_length(self): | ||
self.repo.index.commit('my root commit') | ||
self.commit_detail.options = {'commit': True, 'sha_length': 4} | ||
nodes = self.commit_detail.run() | ||
node_p = nodes[0] # <p> node | ||
node_fl = node_p[0] # field list | ||
node_f = node_fl[0] # field | ||
assert_equal(1, len(node_fl)) | ||
assert_equal('Commit', node_f[0].astext()) | ||
assert_equal(self.repo.commit().hexsha[:4], node_f[1].astext()) | ||
|
||
def test_untracked_files(self): | ||
self.repo.index.commit('my root commit') | ||
self.commit_detail.options = {'untracked': True} | ||
fd, name = mkstemp(dir=self.root) | ||
os.close(fd) | ||
nodes = self.commit_detail.run() | ||
node_p = nodes[0] # <p> node | ||
assert_equal(2, len(node_p)) | ||
node_w = node_p[1] # nodes.warning | ||
node_i = node_w[0] # inline | ||
assert_in('untracked', node_i.astext()) | ||
|
||
def test_uncommitted_changes(self): | ||
fd, name = mkstemp(dir=self.root) | ||
self.repo.index.add([name]) | ||
self.repo.index.commit('my root commit') | ||
os.write(fd, "some change") | ||
os.close(fd) | ||
self.commit_detail.options = {'uncommitted': True} | ||
nodes = self.commit_detail.run() | ||
node_p = nodes[0] # <p> node | ||
assert_equal(2, len(node_p)) | ||
node_w = node_p[1] # nodes.warning | ||
node_i = node_w[0] # inline | ||
assert_in('uncommitted', node_i.astext()) |