aboutsummaryrefslogtreecommitdiff
path: root/spec/util/git_repo.lua
blob: b3ddd9eff57f728bbe1cc4f9891792c3596123c3 (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
local git_repo = {}

local test_env = require("spec.util.test_env")
local lfs = require("lfs")

local files = {
----------------------------------------
["testrock-dev-1.rockspec"] = [[
package = "testrock"
version = "dev-1"
source = {
   url = "git://localhost:20000/testrock"
}
description = {
   homepage = "https://localhost",
   license = "MIT"
}
dependencies = {}
build = {
   type = "builtin",
   modules = {
      testrock = "testrock.lua"
   }
}
]],
----------------------------------------
["testrock.lua"] = [[
local testrock = {}

function testrock.say()
   return "Hello, world!"
end

return testrock
]],
----------------------------------------
["foo.c"] = [[
#include <lua.h>
int luaopen_foo(lua_State* L) {
   lua_pushnumber(L, 42);
   return 1;
}
]],
----------------------------------------
["test.lua"] = [[
print("this should be ignored!")
]],
}

local function write_file(filename, contents)
   local fd = assert(io.open(filename, "w"))
   assert(fd:write(contents))
   fd:close()
end

local function handling(args)
   local pok, ret = pcall(args.try)
   if not pok then
      pok, ret = pcall(args.catch, ret)
   end
   args.finally()
   if not pok then
      error(ret)
   end
   return ret
end

function git_repo.start()
   local dir = lfs.currentdir()
   return handling {
      try = function()
         local pidfile = os.tmpname()
         local basedir = test_env.testing_paths.testrun_dir .. "/git_repo"
         local repodir = basedir .. "/testrock"
         test_env.remove_dir(basedir)
         lfs.mkdir(basedir)
         lfs.mkdir(repodir)
         lfs.chdir(repodir)
         assert(test_env.execute("git init"))
         for name, contents in pairs(files) do
            write_file(name, contents)
            test_env.execute("git add " .. name)
         end
         assert(test_env.execute("git commit -a -m 'initial commit'"))
         assert(test_env.execute("git branch test-branch"))
         print("git daemon --reuseaddr --pid-file="..pidfile.." --base-path="..basedir.." --export-all "..repodir.." &")
         assert(test_env.execute("git daemon --reuseaddr --pid-file="..pidfile.." --base-path="..basedir.." --export-all "..repodir.." &"))
         assert(test_env.execute("sleep 0.1; netstat -ln | grep '0.0.0.0:9418 .* LISTEN'"))
         return {
            stop = function()
               local fd = io.open(pidfile)
               local pid = fd:read("*a")
               fd:close()
               assert(test_env.execute("kill -HUP " .. pid))
               test_env.remove_dir(basedir)
            end
         }
      end,
      catch = function(err)
         error(err)
      end,
      finally = function()
         lfs.chdir(dir)
      end,
   }
end

return git_repo