summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2012-09-08 15:17:50 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2012-09-08 15:17:50 +0100
commit7b6f77c46b65b44ed8d69f4b6a4859893a7542c6 (patch)
tree6646f5401036deca474855f716fa6b756d5e0e53
parent988de21ab9ad0cceac9ced9059b6462f33c67439 (diff)
downloadgall-7b6f77c46b65b44ed8d69f4b6a4859893a7542c6.tar.gz
GALL.REPOSITORY: Start to test repository
-rw-r--r--.gitignore1
-rw-r--r--Makefile2
-rw-r--r--lib/gall/repository.lua19
-rw-r--r--test/test-gall.repository.lua150
-rw-r--r--test/withrepo.lua13
5 files changed, 174 insertions, 11 deletions
diff --git a/.gitignore b/.gitignore
index f63b65d..6556034 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
*~
luacov.*.out
+test/test_repo
diff --git a/Makefile b/Makefile
index 5bf34f6..32d4af7 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
all: test
-MODULES := gall gall.util gall.ll
+MODULES := gall gall.util gall.ll gall.repository
LUA_VER := 5.1
INST_BASE := /usr/local
diff --git a/lib/gall/repository.lua b/lib/gall/repository.lua
index af1ded4..bd047b6 100644
--- a/lib/gall/repository.lua
+++ b/lib/gall/repository.lua
@@ -136,12 +136,14 @@ function repomethod:normalise(sha)
local ref, err = self:get_ref(sha)
return ref, err
else
- local ok, out = self:_run_with_input(sha, chomp, "cat-file", "--batch-check")
- if not ok then
- error(out)
+ local ok, out, err = self:_run_with_input(sha, chomp, "cat-file", "--batch-check")
+ if ok ~= 0 then
+ error((out or "") .. "\n" .. (err or ""))
+ end
+ local fullsha = out:match("^("..string.rep("[0-9a-f]", 40)..")")
+ if fullsha then
+ return fullsha
end
- sha = out:match("^("..string.rep("[0-9a-f]", 40)..")")
- return sha
end
return nil, "Unable to normalise " .. tostring(sha)
end
@@ -255,11 +257,8 @@ end
local function _create(path, full)
-- Cause a bare repository to be created (or a non-bare if full is true)
- local args = {
- stderr = true,
- repo = path,
- "-q"
- }
+ local args = { stderr = true, repo = path, "-q" }
+
if not full then
args[#args+1] = "--bare"
end
diff --git a/test/test-gall.repository.lua b/test/test-gall.repository.lua
new file mode 100644
index 0000000..41aa63b
--- /dev/null
+++ b/test/test-gall.repository.lua
@@ -0,0 +1,150 @@
+-- test/test-gall.repository.lua
+--
+-- Git Abstraction layer for Lua - Repository tests
+--
+-- Copyright 2012 Daniel Silverstone <dsilvers@digital-scurf.org>
+--
+-- For Licence terms, see COPYING
+--
+
+-- This test includes repository 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.find_current_dot_git()
+ assert(gall.repository.new("./.git"))
+end
+
+function suite.find_current()
+ assert(gall.repository.new("."))
+end
+
+function suite.fail_to_find()
+ local ok, msg = gall.repository.new("/DOES_NOT_EXIST")
+ assert(not ok)
+ assert(msg:find("DOES_NOT_EXIST"))
+end
+
+function suite.make_repo()
+ assert(gall.repository.create("test/test_repo"))
+end
+
+function suite.make_repo_full()
+ assert(gall.repository.create("test/test_repo", true))
+end
+
+function suite.make_fail()
+ local ok, msg = gall.repository.create("/dev/null/CANNOT_CREATE")
+ assert(not ok)
+ assert(msg:find("CANNOT_CREATE"))
+end
+
+function suite.repo_tostring()
+ local repo = assert(gall.repository.new("."))
+ local repstr = tostring(repo)
+ assert(repstr:find("GitRepository"))
+ assert(repstr:find("%./%.git"))
+end
+
+function suite.repo_hash_blob()
+ local repo = assert(gall.repository.create("test/test_repo"))
+ local sha = repo:hash_object("blob", "FOOBAR")
+ assert(sha == "389865bb681b358c9b102d79abd8d5f941e96551")
+end
+
+function suite.repo_create_blob()
+ local repo = assert(gall.repository.create("test/test_repo"))
+ local sha = repo:hash_object("blob", "FOOBAR", true)
+ assert(sha == "389865bb681b358c9b102d79abd8d5f941e96551")
+end
+
+function suite.repo_create_retrieve_blob()
+ local repo = assert(gall.repository.create("test/test_repo"))
+ local sha = repo:hash_object("blob", "FOOBAR", true)
+ assert(sha == "389865bb681b358c9b102d79abd8d5f941e96551")
+ local repo2 = assert(gall.repository.new("test/test_repo"))
+ local obj = repo2:get("389865bb681b358c9b102d79abd8d5f941e96551")
+ assert(obj)
+ assert(obj.type == "blob")
+ assert(obj.content == "FOOBAR")
+end
+
+function suite.normalise_bad_ref()
+ local repo = assert(gall.repository.new("."))
+ local ok, msg = repo:normalise("PANTS")
+ assert(not ok)
+ assert(msg:find("PANTS"))
+end
+
+function suite.normalise_good_ref()
+ local repo = assert(gall.repository.new("."))
+ assert(repo:normalise("refs/heads/master"))
+end
+
+function suite.normalise_short_sha()
+ local repo = assert(gall.repository.new("."))
+ assert(repo:normalise("988de21"))
+end
+
+function suite.error_normalise()
+ local repo = assert(gall.repository.new("."))
+ repo.path = "/DOES_NOT_EXIST"
+ assert(not xpcall(function()repo:normalise("PANTS")end))
+end
+
+function suite.get_bad()
+ local repo = assert(gall.repository.new("."))
+ local ok, msg = repo:get("PANTS")
+ assert(not ok)
+ assert(msg:find("PANTS"))
+end
+
+function suite.get_ref()
+ local repo = assert(gall.repository.new("."))
+ assert(repo:get_ref("refs/heads/master"))
+end
+
+function suite.all_refs()
+ local repo = assert(gall.repository.new("."))
+ local refs, msg = repo:all_refs()
+ assert(refs, msg)
+ assert(refs["refs/heads/master"])
+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/withrepo.lua b/test/withrepo.lua
new file mode 100644
index 0000000..2343fae
--- /dev/null
+++ b/test/withrepo.lua
@@ -0,0 +1,13 @@
+-- test/withrepo.lua
+--
+-- Gall -- Test utilities for when you have repositories
+--
+
+local _xpcall = xpcall
+
+function xpcall(fn, tb)
+ os.execute("rm -rf test/test_repo")
+ local ok, msg = _xpcall(fn, tb)
+ os.execute("rm -rf test/test_repo")
+ return ok, msg
+end