summaryrefslogtreecommitdiff
path: root/test/test-gall.tree.lua
blob: 1720aabd48e353caa4b4b83d8534f6194ca175c3 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
-- test/test-gall.tree.lua
--
-- Git Abstraction layer for Lua - Tree object tests
--
-- Copyright 2012 Daniel Silverstone <dsilvers@digital-scurf.org>
--
-- For Licence terms, see COPYING
--

-- This test includes tree wibbling
dofile "test/withrepo.lua"

-- Step one, start coverage

local luacov = require 'luacov'

local gall = require 'gall'

local testnames = {}

local real_assert = assert
local total_asserts = 0
local function assert(...)
   local retval = real_assert(...)
   total_asserts = total_asserts + 1
   return retval
end

local function add_test(suite, name, value)
   rawset(suite, name, value)
   testnames[#testnames+1] = name
end

local suite = setmetatable({}, {__newindex = add_test})

function suite.get_tree_ok()
   local repo = test_repo()
   local commit = repo:get("HEAD").content
   local tree = commit.tree.content
   assert(tree)
end

function suite.tree_str()
   local repo = test_repo()
   local commit = repo:get("HEAD").content
   local treeobj = commit.tree
   local tree = treeobj.content
   local treestr = tostring(tree)
   assert(treestr:find("GitTree"))
   assert(treestr:find(treeobj.sha))
end

function suite.flatten_tree()
   local repo = test_repo()
   local commit = repo:get("refs/heads/heirarchy").content
   local tree = commit.tree.content
   local flat = gall.tree.flatten(tree)
   assert(flat)
   assert(flat.testblob)
   assert(flat["blobs/second"])
end

function suite.bad_diff()
   local repo = test_repo()
   local commit = repo:get("refs/heads/heirarchy").content
   local tree = commit.tree.content
   local ok, msg = tree:diff_to()
   assert(not ok)
   assert(msg:find("e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"))
end

function suite.diff_trees()
   local repo = test_repo()
   local heir_tree = repo:get("refs/heads/heirarchy").content.tree.content
   local master_tree = repo:get("refs/heads/master").content.tree.content
   local diff = assert(master_tree:diff_to(heir_tree))
end

function suite.flatten_then_create_tree()
   local repo = test_repo()
   local commit = repo:get("refs/heads/heirarchy").content
   local tree = commit.tree.content
   local flat = gall.tree.flatten(tree)
   assert(flat)
   assert(flat.testblob)
   assert(flat["blobs/second"])
   local newtree = gall.tree.create(repo, flat)
   assert(newtree == commit.tree)
end

function suite.create_bad_tree_type()
   local repo = test_repo()
   local bad_tree = {
      thingy = { obj = repo:get("HEAD") }
   }
   local ok, msg = gall.tree.create(repo, bad_tree)
   assert(not ok)
   assert(msg:find("commit"))
end

function suite.create_bad_tree_type_flat()
   local repo = test_repo()
   local bad_tree = {
      thingy = { type = "FISH" }
   }
   local ok, msg = gall.tree.create(repo, bad_tree)
   assert(not ok)
   assert(msg:find("FISH"))
end

function suite.create_bad_tree_type_flat_deep()
   local repo = test_repo()
   local bad_tree = {
      ["thingy/wotsit"] = { type = "FISH" }
   }
   local ok, msg = gall.tree.create(repo, bad_tree)
   assert(not ok)
   assert(msg:find("FISH"))
end

function suite.create_bad_sha()
   local repo = test_repo()
   local bad_tree = {
      ["thingy/wotsit"] = { type = "blob", sha = "FISHFISH" }
   }
   local ok, msg = gall.tree.create(repo, bad_tree)
   assert(not ok)
   assert(msg:find("mktree returned"))
end


local count_ok = 0
for _, testname in ipairs(testnames) do
--   print("Run: " .. testname)
   local ok, err = xpcall(suite[testname], debug.traceback)
   if not ok then
      print(err)
      print()
   else
      count_ok = count_ok + 1
   end
end

print(tostring(count_ok) .. "/" .. tostring(#testnames) .. " [" .. tostring(total_asserts) .. "] OK")

os.exit(count_ok == #testnames and 0 or 1)