summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/1px.webpbin0 -> 34 bytes
-rw-r--r--tests/base_page_test.py (renamed from tests/page_test.py)0
-rw-r--r--tests/commit_page_test.py (renamed from tests/commit_test.py)2
-rw-r--r--tests/file_page_test.py81
-rw-r--r--tests/file_test.py26
-rw-r--r--tests/log_page_test.py (renamed from tests/log_test.py)0
-rw-r--r--tests/overview_page_test.py (renamed from tests/overview_test.py)0
-rw-r--r--tests/tree_page_test.py120
-rw-r--r--tests/tree_test.py106
9 files changed, 237 insertions, 98 deletions
diff --git a/tests/1px.webp b/tests/1px.webp
new file mode 100644
index 0000000..15e1c70
--- /dev/null
+++ b/tests/1px.webp
Binary files differ
diff --git a/tests/page_test.py b/tests/base_page_test.py
index c68e435..c68e435 100644
--- a/tests/page_test.py
+++ b/tests/base_page_test.py
diff --git a/tests/commit_test.py b/tests/commit_page_test.py
index b32901e..839d493 100644
--- a/tests/commit_test.py
+++ b/tests/commit_page_test.py
@@ -6,7 +6,7 @@ def test_init():
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.out_file_path == 'test/commit/123/_index.md')
assert(page.template.name == 'commit.md')
def test_context():
diff --git a/tests/file_page_test.py b/tests/file_page_test.py
new file mode 100644
index 0000000..a1123de
--- /dev/null
+++ b/tests/file_page_test.py
@@ -0,0 +1,81 @@
+from unittest.mock import MagicMock
+
+from g2h.file_page import FilePage, bytes_to_content
+
+def test_init():
+ commit = MagicMock()
+ commit.hexsha = '123'
+ page = FilePage('test/file', 'file test', commit, '.test_file', 'path/test', '')
+ assert(page.name == 'file test')
+ assert(page.out_file_path == 'test/file/123/t/path/test/_test_file.md')
+ assert(page.template.name == 'file.md')
+ assert(page.commit == commit)
+ assert(page.file_name == '.test_file')
+ assert(page.file_path == 'path/test')
+ commit.tree.join.assert_called_with('path/test')
+
+def test_read_file():
+ file = MagicMock()
+ tree = MagicMock()
+ commit = MagicMock()
+ commit.hexsha = '123'
+ commit.tree = tree
+ commit.tree.join.return_value = file
+ page = FilePage('test/file', 'file test', commit, 'test_file', '', '')
+ page.read_file()
+ tree.join.assert_called_with('test_file')
+ file.data_stream.read.assert_called()
+
+def test_context():
+ file = MagicMock()
+ tree = MagicMock()
+ commit = MagicMock()
+ commit.hexsha = '123'
+ commit.tree = tree
+ commit.tree.join.return_value = file
+ page = FilePage('test/file', 'file test', commit, 'test_file.txt', '', 'file/content')
+ file.data_stream.read.return_value = 'test'.encode()
+ context = page.context()
+ assert(context['content'] == 'test')
+ assert(context['content_subdir'] == 'file/content')
+ assert(context['file_extension'] == 'txt')
+ assert(context['title'] == 'file test: test_file.txt')
+ assert(context['type'] == 'text')
+
+def test_context_dot_file():
+ file = MagicMock()
+ tree = MagicMock()
+ commit = MagicMock()
+ commit.hexsha = '123'
+ commit.tree = tree
+ commit.tree.join.return_value = file
+ page = FilePage('test/file', 'file test', commit, '.file', '', 'file/content')
+ file.data_stream.read.return_value = 'test'.encode()
+ context = page.context()
+ assert(context['content'] == 'test')
+ assert(context['content_subdir'] == 'file/content')
+ assert(context['file_extension'] == '')
+ assert(context['title'] == 'file test: .file')
+ assert(context['type'] == 'text')
+
+def test_bytes_to_content_text():
+ content, type = bytes_to_content('test'.encode())
+ assert(content == 'test')
+ assert(type == 'text')
+
+def test_bytes_to_content_pic():
+ webp_1px = bytes([0x52, 0x49, 0x46, 0x46, 0x1a, 0x00, 0x00, 0x00, 0x57, 0x45, 0x42, 0x50, 0x56, 0x50, 0x38, 0x4c, 0x0e, 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00, 0x00, 0x00, 0x07, 0x10, 0x11, 0xfd, 0x0f, 0x44, 0x44, 0xff, 0x03, 0x0a])
+ content, type = bytes_to_content(webp_1px)
+ assert(content == 'UklGRhoAAABXRUJQVlA4TA4AAAAvAAAAAAcQEf0PRET/Awo=')
+ assert(type == 'image')
+
+def test_bytes_to_content_empty():
+ content, type = bytes_to_content(bytes())
+ assert(content == None)
+ assert(type == 'empty')
+
+def test_bytes_to_content_bin():
+ content, type = bytes_to_content(bytes([0xc3, 0x28]))
+ assert(content == None)
+ assert(type == None)
+
diff --git a/tests/file_test.py b/tests/file_test.py
new file mode 100644
index 0000000..838df71
--- /dev/null
+++ b/tests/file_test.py
@@ -0,0 +1,26 @@
+import os
+import pytest
+
+from unittest.mock import Mock
+
+from g2h.file import sanitize_file_name, tree_join
+
+def test_sanitize_not_necessary():
+ assert(sanitize_file_name('foo', None) == 'foo')
+
+def test_sanitize_success():
+ assert(sanitize_file_name('.test', []) == '_test')
+
+def test_sanitize_collissions():
+ entries = []
+ for i in range(1, 10):
+ entry = Mock()
+ entry.name = '_' * i + 'test'
+ entries.append(entry)
+ with pytest.raises(RuntimeError):
+ sanitize_file_name('.test', entries)
+
+def test_tree_join():
+ commit = Mock()
+ commit.hexsha = '123'
+ assert(tree_join(commit, 'test') == os.path.join('123', 't', 'test'))
diff --git a/tests/log_test.py b/tests/log_page_test.py
index 041544e..041544e 100644
--- a/tests/log_test.py
+++ b/tests/log_page_test.py
diff --git a/tests/overview_test.py b/tests/overview_page_test.py
index 06eea97..06eea97 100644
--- a/tests/overview_test.py
+++ b/tests/overview_page_test.py
diff --git a/tests/tree_page_test.py b/tests/tree_page_test.py
new file mode 100644
index 0000000..891e940
--- /dev/null
+++ b/tests/tree_page_test.py
@@ -0,0 +1,120 @@
+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 = MagicMock()
+ commit_no_subtree.tree.path = '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.path == '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)
+ node.type = 'tree'
+ else:
+ parent.blobs.append(node)
+ node.type = 'blob'
+ return node
+
+def test_context_empty():
+ commit = MagicMock()
+ commit.hexsha = '123'
+ commit.tree.path = 'tree/path'
+ page = TreePage('test/tree', 'tree test', commit, '', None, '')
+ context = page.context()
+ assert(context['entries'] == [])
+ assert(context['content_subdir'] == '')
+ assert(context['subdir'] == '/123/t/tree/path')
+ assert(context['title'] == 'tree test: Tree')
+
+def test_context_entries():
+ tree = MagicMock()
+ tree.trees = []
+ tree.blobs = []
+ repo = MagicMock()
+ sub_dir = add_node(tree, 'sub/')
+ add_node(sub_dir, 'file.txt')
+ add_node(tree, 'README')
+ add_node(tree, '.gitignore')
+ commit = MagicMock()
+ commit.hexsha = '123'
+ commit.tree = tree
+ commit.committed_datetime = 'Date Time'
+ repo.iter_commits = MagicMock(return_value=iter([commit, commit, commit]))
+ page = TreePage('test/tree', 'tree test', commit, '', repo, '')
+ context = page.context()
+ assert(repo.iter_commits.called)
+ assert(context['entries'] == [{'file_name': 'sub//', 'name': 'sub//', 'modified': 'Date Time'}, {'file_name': '_gitignore', 'name': '.gitignore', 'modified': 'Date Time'}, {'file_name': 'README', '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/')
+
+def test_get_files():
+ tree = MagicMock()
+ tree.trees = []
+ tree.blobs = []
+ tree.path = ''
+ commit = MagicMock()
+ commit.hexsha = '123'
+ commit.tree = tree
+ expected = []
+ for case in [ None, '.file', 'README', 'main.cc' ]:
+ print(str(case))
+ if case:
+ add_node(tree, case)
+ expected.append(case)
+ page = TreePage('test/tree', 'tree test', commit, '', None, '')
+ assert(page.get_files() == expected)
+
diff --git a/tests/tree_test.py b/tests/tree_test.py
index e7d23a8..7ec0f0e 100644
--- a/tests/tree_test.py
+++ b/tests/tree_test.py
@@ -1,100 +1,12 @@
-import os
-from pytest import fail
-from unittest.mock import MagicMock
+from unittest.mock import Mock
-from g2h.tree_page import TreePage
+from g2h.tree import safe_join
-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'
- commit.tree.path = 'tree/path'
- page = TreePage('test/tree', 'tree test', commit, '', None, '')
- context = page.context()
- assert(context['entries'] == [])
- assert(context['content_subdir'] == '')
- assert(context['subdir'] == '/123/t/tree/path')
- assert(context['title'] == 'tree test: Tree')
-
-def test_context_entries():
- 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['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/')
+def test_safe_join_empty():
+ tree = Mock()
+ assert(safe_join(tree, '') == tree)
+def test_safe_join_non_empty():
+ tree = Mock()
+ safe_join(tree, 'test')
+ tree.join.assert_called_with('test')