summaryrefslogtreecommitdiffstats
path: root/tests/tree_page_test.py
blob: 891e940251d9c9de82cac73ff323015e738f01f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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)