From b80edbf10b277115c97a28354a88da97f74d0767 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sat, 8 Sep 2012 17:32:03 +0100 Subject: GALL.TREE: Test trees as much as possible --- Makefile | 2 +- lib/gall/tree.lua | 9 +-- test/test-gall.tree.lua | 146 ++++++++++++++++++++++++++++++++++++++++++++++++ test/test_repo.tar | Bin 81920 -> 92160 bytes 4 files changed, 152 insertions(+), 5 deletions(-) create mode 100644 test/test-gall.tree.lua diff --git a/Makefile b/Makefile index ca74156..01985be 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ all: test MODULES := gall gall.util gall.ll \ - gall.repository gall.object gall.commit gall.tag + gall.repository gall.object gall.commit gall.tag gall.tree LUA_VER := 5.1 INST_BASE := /usr/local diff --git a/lib/gall/tree.lua b/lib/gall/tree.lua index ed7ba4b..43f76f1 100644 --- a/lib/gall/tree.lua +++ b/lib/gall/tree.lua @@ -44,10 +44,11 @@ end function tree_method:diff_to(other) -- Generate a diff from self to other local repo = repos[self] - local ok, streediff = repo:rawgather("diff-tree", "-r", "-M", "-C", - objs[self].sha, objs[other].sha) + local ok, streediff, err = repo:rawgather("diff-tree", "-r", "-M", "-C", + ((objs[self] or {}).sha) or "???", + ((objs[other] or {}).sha) or "???") if ok ~= 0 then - return nil, streediff + return nil, (streediff or "") .. "\n" .. (err or "") end local treediff = {} for startmode, endmode, startsha, endsha, action, score, filenames in @@ -176,7 +177,7 @@ local function _create(repo, flat_tree) for k, v in pairs(t) do if k ~= "" then local ok, obj = pcall(function() return v.obj end) - if ok then + if ok and obj then v = obj end if not mode[v.type] then diff --git a/test/test-gall.tree.lua b/test/test-gall.tree.lua new file mode 100644 index 0000000..e1d5449 --- /dev/null +++ b/test/test-gall.tree.lua @@ -0,0 +1,146 @@ +-- test/test-gall.tree.lua +-- +-- Git Abstraction layer for Lua - Tree object tests +-- +-- Copyright 2012 Daniel Silverstone +-- +-- 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("%?%?%?")) +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) diff --git a/test/test_repo.tar b/test/test_repo.tar index 405b779..614b8aa 100644 Binary files a/test/test_repo.tar and b/test/test_repo.tar differ -- cgit v1.2.1