From ea949ae3bb60172aa6cf84680dff308df13686e7 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sat, 8 Sep 2012 15:46:54 +0100 Subject: GALL.OBJECT: Add tests to cover gall.object --- lib/gall/object.lua | 7 ++- test/create_test_repo.sh | 45 +++++++++++++++ test/test-gall.object.lua | 136 ++++++++++++++++++++++++++++++++++++++++++++++ test/withrepo.lua | 5 ++ 4 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 test/create_test_repo.sh create mode 100644 test/test-gall.object.lua diff --git a/lib/gall/object.lua b/lib/gall/object.lua index 3b904c5..2772587 100644 --- a/lib/gall/object.lua +++ b/lib/gall/object.lua @@ -13,6 +13,10 @@ local tag = require "gall.tag" local repos = setmetatable({}, {__mode="k"}) +local known_types = { + blob = true, commit = true, tree = true, tag = true, +} + local function _objectindex(obj, field) local ok, ret if field == "type" then @@ -22,6 +26,7 @@ local function _objectindex(obj, field) elseif field == "raw" then ok, ret = repos[obj]:rawgather("cat-file", (obj.type == "tag" and "tag" or "-p"), obj.sha) elseif field == "content" then + assert(known_types[obj.type], "Unknown type: " .. obj.type) if obj.type == "blob" then ok, ret = 0, obj.raw elseif obj.type == "commit" then @@ -30,8 +35,6 @@ local function _objectindex(obj, field) ok, ret = 0, tree.new(repos[obj], obj) elseif obj.type == "tag" then ok, ret = 0, tag.new(repos[obj], obj) - else - error("Unknown type <" .. obj.type .. "> for content parse") end else error("Unknown field <" .. tostring(field) .. ">") diff --git a/test/create_test_repo.sh b/test/create_test_repo.sh new file mode 100644 index 0000000..4d4793e --- /dev/null +++ b/test/create_test_repo.sh @@ -0,0 +1,45 @@ +#!/bin/sh + +mkdir -p test/test_repo + +cd test/test_repo + +__git () { git "$@" 2>/dev/null ; } +_git () { __git "$@" > /dev/null ; } + +_git init -q + +echo -n "FOOBAR" > testblob + +_git add testblob + +GIT_AUTHOR_NAME="Gall Test Suite Author" +GIT_AUTHOR_EMAIL="gall-author@gitano.org.uk" +GIT_AUTHOR_DATE="1347114766 +0000" +GIT_COMMITTER_NAME="Gall Test Suite Committer" +GIT_COMMITTER_EMAIL="gall-committer@gitano.org.uk" +GIT_COMMITTER_DATE="1347114766 +0100" + +export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE +export GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE + +_git commit -m "Initial" + +echo -n "BAZMETA" > testblob + +_git add testblob + +_git commit -m "Second" + +_git checkout -b branchy 'HEAD^' + +echo -n "NEW" > secondblob + +_git add secondblob +_git commit -m "branchy" + +_git checkout master + +_git merge branchy + +_git tag -a -m "Annotated Tag. Wahey" v1.0 diff --git a/test/test-gall.object.lua b/test/test-gall.object.lua new file mode 100644 index 0000000..2de69d3 --- /dev/null +++ b/test/test-gall.object.lua @@ -0,0 +1,136 @@ +-- test/test-gall.object.lua +-- +-- Git Abstraction layer for Lua - Object tests +-- +-- Copyright 2012 Daniel Silverstone +-- +-- For Licence terms, see COPYING +-- + +-- This test includes object 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}) + +local FOOBAR_SHA1 = "389865bb681b358c9b102d79abd8d5f941e96551" + +function suite.create_bad() + local repo = assert(gall.repository.create "test/test_repo") + local obj, msg = gall.object.create(repo, "BONGHIT", "FOOBAR") + assert(not obj) + assert(msg:find(" returned ")) +end + +function suite.create_blob() + local repo = assert(gall.repository.create "test/test_repo") + local obj, msg = gall.object.create(repo, "blob", "FOOBAR") + assert(obj, msg) +end + +function suite.object_tostring() + local repo = assert(gall.repository.create "test/test_repo") + local obj, msg = gall.object.create(repo, "blob", "FOOBAR") + assert(obj, msg) + local objstr = tostring(obj) + assert(objstr:find("GitObject")) + assert(objstr:find("test/test_repo")) + assert(objstr:find(FOOBAR_SHA1)) +end + +function suite.object_type() + local repo = assert(gall.repository.create "test/test_repo") + local obj, msg = gall.object.create(repo, "blob", "FOOBAR") + assert(obj, msg) + assert(obj.type == "blob") +end + +function suite.object_size() + local repo = assert(gall.repository.create "test/test_repo") + local obj, msg = gall.object.create(repo, "blob", "FOOBAR") + assert(obj, msg) + assert(obj.size == 6) +end + +function suite.object_raw() + local repo = assert(gall.repository.create "test/test_repo") + local obj, msg = gall.object.create(repo, "blob", "FOOBAR") + assert(obj, msg) + assert(obj.raw == "FOOBAR") +end + +function suite.object_blob_content() + local repo = assert(gall.repository.create "test/test_repo") + local obj, msg = gall.object.create(repo, "blob", "FOOBAR") + assert(obj, msg) + assert(obj.content == "FOOBAR") +end + +function suite.object_commit_content() + local repo = test_repo() + local obj = repo:get(repo.HEAD) + assert(obj.type == "commit") + local commit = obj.content + assert(commit.sha == obj.sha) +end + +function suite.object_tree_content() + local repo = test_repo() + local obj = repo:get(repo.HEAD) + assert(obj.type == "commit") + local commit = obj.content + local treeobj = commit.tree + local tree = treeobj.content + local treestr = tostring(tree) + assert(treestr:find("GitTree")) +end + +function suite.object_tag_content() + local repo = test_repo() + local obj = repo:get("refs/tags/v1.0") + assert(obj.type == "tag") + local tag = obj.content + assert(tag.sha == obj.sha) +end + +function suite.object_get_crap() + local repo = test_repo() + local obj = repo:get("refs/tags/v1.0") + assert(not xpcall(function() obj.trank() end)) +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 index 2343fae..bf90b0c 100644 --- a/test/withrepo.lua +++ b/test/withrepo.lua @@ -11,3 +11,8 @@ function xpcall(fn, tb) os.execute("rm -rf test/test_repo") return ok, msg end + +function test_repo() + os.execute("sh test/create_test_repo.sh") + return assert(require("gall.repository").new "test/test_repo") +end \ No newline at end of file -- cgit v1.2.1