diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/__init__.py | 0 | ||||
| -rw-r--r-- | tests/commit_test.py | 36 | ||||
| -rw-r--r-- | tests/log_test.py | 188 | ||||
| -rw-r--r-- | tests/overview_test.py | 9 | ||||
| -rw-r--r-- | tests/page_test.py | 19 | ||||
| -rw-r--r-- | tests/tree_test.py | 98 |
6 files changed, 350 insertions, 0 deletions
diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/__init__.py diff --git a/tests/commit_test.py b/tests/commit_test.py new file mode 100644 index 0000000..3a9871a --- /dev/null +++ b/tests/commit_test.py @@ -0,0 +1,36 @@ +from g2h.commit_page import CommitPage, redact_email +from unittest.mock import MagicMock + +def test_init(): + commit = MagicMock() + commit.hexsha = '123' + page = CommitPage('test/commit', 'commit test', commit, '') + assert(page.name == 'commit test') + assert(page.out_file_path == 'test/commit/123/index.md') + assert(page.template.name == 'commit.md') + +def test_context(): + commit = MagicMock() + commit.hexsha = '123' + commit.author.email = 'unit@te.st' + page = CommitPage('test/commit', 'commit test', commit, '') + context = page.context() + assert(context['title'] == 'commit test: 123') + assert(context['commit'] == commit) + assert(context['redacted_email'] == 'u...t@te...st') + +def test_redacting_email(): + assert(redact_email(None) == '') + assert(redact_email('') == '') + assert(redact_email('a') == 'a...a') + assert(redact_email('az') == 'a...z') + assert(redact_email('root') == 'r...t') + assert(redact_email('root@localhost') == 'r...t@localhost') + assert(redact_email('test@example.com') == 't...t@example...com') + assert(redact_email('test@sub.example.com') == 't...t@sub...example...com') + assert(redact_email('az@example.com') == 'a...z@example...com') + assert(redact_email('a@example.com') == 'a...a@example...com') + assert(redact_email('@example.com') == '@example...com') + assert(redact_email('test@') == 't...t@') + assert(redact_email('az@') == 'a...z@') + assert(redact_email('@') == '@') diff --git a/tests/log_test.py b/tests/log_test.py new file mode 100644 index 0000000..041544e --- /dev/null +++ b/tests/log_test.py @@ -0,0 +1,188 @@ +from g2h.log_page import LogPage +from unittest.mock import MagicMock + +def test_init(): + page = LogPage('test/log', 'log test', None, None, None, '') + assert(page.name == 'log test') + assert(page.out_file_path == 'test/log/l.md') + assert(page.template.name == 'log.md') + +def test_context_no_commits(): + page = LogPage('test/log', 'log test', None, None, None, '') + context = page.context() + assert(context['title'] == 'log test: Log') + assert(context['commits'] == []) + +def test_context_no_branches_and_tags(): + commit = MagicMock() + commit.summary = 'mock commit' + commit.hexsha = 1234; + commit.committed_datetime = 0 + page = LogPage('test/log', 'log test', [commit], None, None, '') + context = page.context() + assert(context['title'] == 'log test: Log') + assert(context['commits'] == [{'committed_datetime': 0, 'hexsha': 1234, 'refs': [], 'summary': 'mock commit'}]) + +def test_context_matching_branch_no_tags(): + commit = MagicMock() + commit.summary = 'mock commit' + commit.hexsha = 1234; + commit.committed_datetime = 0 + branch = MagicMock() + branch.commit.hexsha = commit.hexsha + branch.name = 'mock branch' + page = LogPage('test/log', 'log test', [commit], [branch], None, '') + context = page.context() + assert(context['title'] == 'log test: Log') + assert(context['commits'] == [{'committed_datetime': 0, 'hexsha': 1234, 'refs': ['mock branch'], 'summary': 'mock commit'}]) + +def test_context_unmatching_branch_no_tags(): + commit = MagicMock() + commit.summary = 'mock commit' + commit.hexsha = 1234; + commit.committed_datetime = 0 + branch = MagicMock() + branch.commit.hexsha = 4321 + branch.name = 'mock branch' + page = LogPage('test/log', 'log test', [commit], [branch], None, '') + context = page.context() + assert(context['title'] == 'log test: Log') + assert(context['commits'] == [{'committed_datetime': 0, 'hexsha': 1234, 'refs': [], 'summary': 'mock commit'}]) + +def test_context_no_branch_matching_tag(): + commit = MagicMock() + commit.summary = 'mock commit' + commit.hexsha = 1234; + commit.committed_datetime = 0 + tag = MagicMock() + tag.commit.hexsha = commit.hexsha + tag.name = 'mock tag' + page = LogPage('test/log', 'log test', [commit], None, [tag], '') + context = page.context() + assert(context['commits'] == [{'committed_datetime': 0, 'hexsha': 1234, 'refs': ['mock tag'], 'summary': 'mock commit'}]) + +def test_context_no_branch_no_matching_tag(): + commit = MagicMock() + commit.summary = 'mock commit' + commit.hexsha = 1234; + commit.committed_datetime = 0 + tag = MagicMock() + tag.commit.hexsha = 4321 + tag.name = 'mock tag' + page = LogPage('test/log', 'log test', [commit], None, [tag], '') + context = page.context() + assert(context['title'] == 'log test: Log') + assert(context['commits'] == [{'committed_datetime': 0, 'hexsha': 1234, 'refs': [], 'summary': 'mock commit'}]) + +def test_context_no_matching_branch_no_matching_tag(): + commit = MagicMock() + commit.summary = 'mock commit' + commit.hexsha = 1234; + commit.committed_datetime = 0 + branch = MagicMock() + branch.commit.hexsha = 321 + branch.name = 'mock branch' + tag = MagicMock() + tag.commit.hexsha = 4321 + tag.name = 'mock tag' + page = LogPage('test/log', 'log test', [commit], [branch], [tag], '') + context = page.context() + assert(context['title'] == 'log test: Log') + assert(context['commits'] == [{'committed_datetime': 0, 'hexsha': 1234, 'refs': [], 'summary': 'mock commit'}]) + +def test_context_matching_branch_matching_tag(): + commit = MagicMock() + commit.summary = 'mock commit' + commit.hexsha = 1234; + commit.committed_datetime = 0 + branch = MagicMock() + branch.commit.hexsha = commit.hexsha + branch.name = 'mock branch' + tag = MagicMock() + tag.commit.hexsha = commit.hexsha + tag.name = 'mock tag' + page = LogPage('test/log', 'log test', [commit], [branch], [tag], '') + context = page.context() + assert(context['title'] == 'log test: Log') + assert(context['commits'] == [{'committed_datetime': 0, 'hexsha': 1234, 'refs': ['mock branch', 'mock tag'], 'summary': 'mock commit'}]) + +def test_multiple_commits(): + commit1 = MagicMock() + commit1.summary = 'mock commit 1' + commit1.hexsha = 1; + commit1.committed_datetime = 0 + commit2 = MagicMock() + commit2.summary = 'mock commit 2' + commit2.hexsha = 2; + commit2.committed_datetime = 1 + page = LogPage('test/log', 'log test', [commit1, commit2], None, None, '') + context = page.context() + assert(context['title'] == 'log test: Log') + assert(context['commits'] == [ + {'committed_datetime': 0, 'hexsha': 1, 'refs': [], 'summary': 'mock commit 1'}, + {'committed_datetime': 1, 'hexsha': 2, 'refs': [], 'summary': 'mock commit 2'} + ]) + +def test_multiple_commits_branch(): + commit1 = MagicMock() + commit1.summary = 'mock commit 1' + commit1.hexsha = 1; + commit1.committed_datetime = 0 + commit2 = MagicMock() + commit2.summary = 'mock commit 2' + commit2.hexsha = 2; + commit2.committed_datetime = 1 + branch = MagicMock() + branch.commit.hexsha = 1 + branch.name = 'mock branch' + page = LogPage('test/log', 'log test', [commit1, commit2], [branch], None, '') + context = page.context() + assert(context['title'] == 'log test: Log') + assert(context['commits'] == [ + {'committed_datetime': 0, 'hexsha': 1, 'refs': ['mock branch'], 'summary': 'mock commit 1'}, + {'committed_datetime': 1, 'hexsha': 2, 'refs': [], 'summary': 'mock commit 2'} + ]) + +def test_multiple_commits_tag(): + commit1 = MagicMock() + commit1.summary = 'mock commit 1' + commit1.hexsha = 1; + commit1.committed_datetime = 0 + commit2 = MagicMock() + commit2.summary = 'mock commit 2' + commit2.hexsha = 2; + commit2.committed_datetime = 1 + tag = MagicMock() + tag.commit.hexsha = 1 + tag.name = 'mock tag' + page = LogPage('test/log', 'log test', [commit1, commit2], None, [tag], '') + context = page.context() + assert(context['title'] == 'log test: Log') + assert(context['commits'] == [ + {'committed_datetime': 0, 'hexsha': 1, 'refs': ['mock tag'], 'summary': 'mock commit 1'}, + {'committed_datetime': 1, 'hexsha': 2, 'refs': [], 'summary': 'mock commit 2'} + ]) + +def test_multiple_commits_branch_and_tag(): + commit1 = MagicMock() + commit1.summary = 'mock commit 1' + commit1.hexsha = 1; + commit1.committed_datetime = 0 + commit2 = MagicMock() + commit2.summary = 'mock commit 2' + commit2.hexsha = 2; + commit2.committed_datetime = 1 + branch = MagicMock() + branch.commit.hexsha = 1 + branch.name = 'mock branch' + tag = MagicMock() + tag.commit.hexsha = 2 + tag.name = 'mock tag' + page = LogPage('test/log', 'log test', [commit1, commit2], [branch], [tag], '') + context = page.context() + assert(context['title'] == 'log test: Log') + assert(context['commits'] == [ + {'committed_datetime': 0, 'hexsha': 1, 'refs': ['mock branch'], 'summary': 'mock commit 1'}, + {'committed_datetime': 1, 'hexsha': 2, 'refs': ['mock tag'], 'summary': 'mock commit 2'} + ]) + diff --git a/tests/overview_test.py b/tests/overview_test.py new file mode 100644 index 0000000..06eea97 --- /dev/null +++ b/tests/overview_test.py @@ -0,0 +1,9 @@ +from g2h.overview_page import OverviewPage + +def test_init(): + page = OverviewPage('test/overview', 'overview test', 'test branch', 'test tag', 'overview/sub/dir') + context = page.context() + assert(context['title'] == 'overview test') + assert(context['branches'] == 'test branch') + assert(context['tags'] == 'test tag') + assert(context['content_subdir'] == 'overview/sub/dir') diff --git a/tests/page_test.py b/tests/page_test.py new file mode 100644 index 0000000..c68e435 --- /dev/null +++ b/tests/page_test.py @@ -0,0 +1,19 @@ +from g2h.page import Page +from jinja2.exceptions import TemplateNotFound +import pytest + +def test_init(): + page = Page('overview.md', 'dir', 'file', 'name', 'sub/dir') + assert(page.name == 'name') + assert(page.out_file_path == 'dir/file') + assert(page.template.name == 'overview.md') + assert(page.content_subdir == 'sub/dir') + +def test_wrong_template(): + with pytest.raises(TemplateNotFound): + Page('not existant.md', 'dir', 'file', 'name', '') + +def test_no_context(): + page = Page('overview.md', 'dir', 'file', 'name', '') + with pytest.raises(NotImplementedError): + page.context() diff --git a/tests/tree_test.py b/tests/tree_test.py new file mode 100644 index 0000000..ebad5e5 --- /dev/null +++ b/tests/tree_test.py @@ -0,0 +1,98 @@ +import os +from pytest import fail +from unittest.mock import MagicMock + +from g2h.tree_page import TreePage + +def test_init(): + repo = MagicMock() + commit = MagicMock() + commit.hexsha = '123' + page = TreePage('test/tree', 'tree test', commit, 'test/sub/tree', repo, '') + assert(page.name == 'tree test') + assert(page.repo == repo) + assert(page.commit == commit) + commit.tree.join.assert_called_once_with('test/sub/tree') + commit_no_subtree = MagicMock() + commit_no_subtree.hexsha = '123' + commit_no_subtree.tree = 'no sub tree' + page_no_subtree = TreePage('test/tree', 'tree test', commit_no_subtree, '', repo, '') + assert(page_no_subtree.name == 'tree test') + assert(page_no_subtree.repo == repo) + assert(page_no_subtree.commit == commit_no_subtree) + assert(page_no_subtree.tree == 'no sub tree') + +def add_nodes(parent, paths): + nodes = [] + for node in paths: + nodes.append(add_node(parent, node)) + return nodes + +def add_node(parent, path): + node = MagicMock() + node.trees = [] + node.blobs = [] + node.name = path + node.path = os.path.join(parent.path, path) + if path.endswith('/'): + parent.trees.append(node) + else: + parent.blobs.append(node) + return node + +def test_context_empty(): + commit = MagicMock() + commit.hexsha = '123' + page = TreePage('test/tree', 'tree test', commit, '', None, '') + context = page.context() + assert(context['entries'] == []) + assert(context['title'] == 'tree test: Tree') + +def test_context_filled(): + tree = MagicMock() + tree.trees = [] + tree.blobs = [] + repo = MagicMock() + sub_dir = add_node(tree, 'sub/') + sub_file = add_node(sub_dir, 'file.txt') + readme = add_node(tree, 'README') + commit = MagicMock() + commit.hexsha = '123' + commit.author.email = 'unit@te.st' + commit.tree = tree + commit.committed_datetime = 'Date Time' + repo.iter_commits = MagicMock(return_value=iter([commit, commit])) + page = TreePage('test/tree', 'tree test', commit, '', repo, '') + context = page.context() + assert(repo.iter_commits.called) + assert(context['title'] == 'tree test: Tree') + assert(context['entries'] == [{'name': 'sub/', 'modified': 'Date Time'}, {'name': 'README', 'modified': 'Date Time'}]) + +def test_traversal(): + factory = MagicMock() + def new_tree_sideeffect(*args, **kwargs): + return args[1] + factory.new_tree.side_effect = new_tree_sideeffect + tree = MagicMock() + tree.trees = [] + tree.blobs = [] + tree.path = '' + root_tree = add_nodes(tree, ['README', 'sub/']) + sub_tree = add_nodes(root_tree[1], ['main.cc', 'lib.cc', 'include/']) + add_node(sub_tree[2], 'lib.h') + def tree_join_sideeffect(*args, **kwargs): + if args[0] == 'sub/': + return root_tree[1] + elif args[0] == 'sub/include/': + return sub_tree[2] + fail('Unexpected tree join: ' + args[0]) + tree.join.side_effect = tree_join_sideeffect + commit = MagicMock() + commit.tree = tree + page = TreePage('test/tree', 'tree test', commit, '', None, '') + pages = page.build_pages(factory) + assert(len(pages) == 3) + assert(pages[0] == '') + assert(pages[1] == 'sub/') + assert(pages[2] == 'sub/include/') + |