aboutsummaryrefslogtreecommitdiff
path: root/spec/unit/build_spec.lua
diff options
context:
space:
mode:
authorHisham Muhammad <hisham@gobolinux.org>2024-02-26 17:47:28 -0300
committerHisham Muhammad <hisham@gobolinux.org>2024-02-27 13:28:33 -0300
commit5cba4b83f60966045b86ac615df2692c953ebba7 (patch)
tree445d1f58e589b53e4ee8cda5984f430f54fd2b36 /spec/unit/build_spec.lua
parent6bc6ede826843c3692971c14c27c3d27714b2126 (diff)
downloadluarocks-5cba4b83f60966045b86ac615df2692c953ebba7.tar.gz
luarocks-5cba4b83f60966045b86ac615df2692c953ebba7.tar.bz2
luarocks-5cba4b83f60966045b86ac615df2692c953ebba7.zip
fix(fs): make current_dir always return 1 arg only
Diffstat (limited to 'spec/unit/build_spec.lua')
-rw-r--r--spec/unit/build_spec.lua369
1 files changed, 369 insertions, 0 deletions
diff --git a/spec/unit/build_spec.lua b/spec/unit/build_spec.lua
new file mode 100644
index 00000000..6ab143c6
--- /dev/null
+++ b/spec/unit/build_spec.lua
@@ -0,0 +1,369 @@
1local test_env = require("spec.util.test_env")
2local lfs = require("lfs")
3local get_tmp_path = test_env.get_tmp_path
4local run = test_env.run
5local testing_paths = test_env.testing_paths
6local write_file = test_env.write_file
7local P = test_env.P
8
9test_env.unload_luarocks()
10
11test_env.unload_luarocks()
12test_env.setup_specs()
13local cfg = require("luarocks.core.cfg")
14local deps = require("luarocks.deps")
15local fs = require("luarocks.fs")
16local path = require("luarocks.path")
17local rockspecs = require("luarocks.rockspecs")
18local build_builtin = require("luarocks.build.builtin")
19
20local c_module_source = [[
21 #include <lua.h>
22 #include <lauxlib.h>
23
24 int luaopen_c_module(lua_State* L) {
25 lua_newtable(L);
26 lua_pushinteger(L, 1);
27 lua_setfield(L, -2, "c_module");
28 return 1;
29 }
30]]
31
32describe("LuaRocks build #unit", function()
33 local runner
34
35 lazy_setup(function()
36 runner = require("luacov.runner")
37 runner.init(testing_paths.testrun_dir .. "/luacov.config")
38 runner.tick = true
39 cfg.init()
40 fs.init()
41 deps.check_lua_incdir(cfg.variables)
42 deps.check_lua_libdir(cfg.variables)
43 end)
44
45 lazy_teardown(function()
46 runner.shutdown()
47 end)
48
49 describe("build.builtin", function()
50 it("builtin auto installs files in lua subdir", function()
51 test_env.run_in_tmp(function(tmpdir)
52 lfs.mkdir("lua")
53 write_file("lua_module-1.0-1.rockspec", [[
54 package = "lua_module"
55 version = "1.0-1"
56 source = {
57 url = "http://example.com/lua_module"
58 }
59 build = {
60 type = "builtin",
61 modules = {}
62 }
63 ]], finally)
64 write_file("lua/lua_module.lua", "return 123", finally)
65
66 assert.is_true(run.luarocks_bool("build"))
67 assert.match("[\\/]lua_module%.lua", run.luarocks("show lua_module"))
68 end, finally)
69 end)
70
71 describe("builtin.autodetect_modules", function()
72 local tmpdir
73 local olddir
74
75 before_each(function()
76 tmpdir = get_tmp_path()
77 olddir = lfs.currentdir()
78 lfs.mkdir(tmpdir)
79 lfs.chdir(tmpdir)
80 fs.change_dir(tmpdir)
81 end)
82
83 after_each(function()
84 if olddir then
85 lfs.chdir(olddir)
86 fs.change_dir(olddir)
87 if tmpdir then
88 lfs.rmdir(tmpdir)
89 end
90 end
91 end)
92
93 local libs = { "foo1", "foo2" }
94 local incdirs = { "$(FOO1_INCDIR)", "$(FOO2_INCDIR)" }
95 local libdirs = { "$(FOO1_LIBDIR)", "$(FOO2_LIBDIR)" }
96
97 it("returns a table of the modules having as location the current directory", function()
98 write_file("module1.lua", "", finally)
99 write_file("module2.c", "", finally)
100 write_file("module3.c", "int luaopen_my_module()", finally)
101 write_file("test.lua", "", finally)
102 write_file("tests.lua", "", finally)
103
104 local modules = build_builtin.autodetect_modules(libs, incdirs, libdirs)
105 assert.same(modules, {
106 module1 = "module1.lua",
107 module2 = {
108 sources = "module2.c",
109 libraries = libs,
110 incdirs = incdirs,
111 libdirs = libdirs
112 },
113 my_module = {
114 sources = "module3.c",
115 libraries = libs,
116 incdirs = incdirs,
117 libdirs = libdirs
118 }
119 })
120 end)
121
122 local test_with_location = function(location)
123 lfs.mkdir(location)
124 lfs.mkdir(location .. "/dir1")
125 lfs.mkdir(location .. "/dir1/dir2")
126
127 write_file(location .. "/module1.lua", "", finally)
128 write_file(location .. "/dir1/module2.c", "", finally)
129 write_file(location .. "/dir1/dir2/module3.c", "int luaopen_my_module()", finally)
130 write_file(location .. "/test.lua", "", finally)
131 write_file(location .. "/tests.lua", "", finally)
132
133 local modules = build_builtin.autodetect_modules(libs, incdirs, libdirs)
134 assert.same(modules, {
135 module1 = location .. "/module1.lua",
136 ["dir1.module2"] = {
137 sources = location .. "/dir1/module2.c",
138 libraries = libs,
139 incdirs = incdirs,
140 libdirs = libdirs
141 },
142 my_module = {
143 sources = location .. "/dir1/dir2/module3.c",
144 libraries = libs,
145 incdirs = incdirs,
146 libdirs = libdirs
147 }
148 })
149
150 lfs.rmdir(location .. "/dir1/dir2")
151 lfs.rmdir(location .. "/dir1")
152 lfs.rmdir(location)
153 end
154
155 it("returns a table of the modules having as location the src directory", function()
156 test_with_location("src")
157 end)
158
159 it("returns a table of the modules having as location the lua directory", function()
160 test_with_location("lua")
161 end)
162
163 it("returns as second and third argument tables of the bin files and copy directories", function()
164 lfs.mkdir("doc")
165 lfs.mkdir("docs")
166 lfs.mkdir("samples")
167 lfs.mkdir("tests")
168 lfs.mkdir("bin")
169 write_file("bin/binfile", "", finally)
170
171 local _, install, copy_directories = build_builtin.autodetect_modules({}, {}, {})
172 assert.same(install, { bin = { P"bin/binfile" } })
173 assert.same(copy_directories, { "doc", "docs", "samples", "tests" })
174
175 lfs.rmdir("doc")
176 lfs.rmdir("docs")
177 lfs.rmdir("samples")
178 lfs.rmdir("tests")
179 lfs.rmdir("bin")
180 end)
181 end)
182
183 describe("builtin.run", function()
184 local tmpdir
185 local olddir
186
187 before_each(function()
188 tmpdir = get_tmp_path()
189 olddir = lfs.currentdir()
190 lfs.mkdir(tmpdir)
191 lfs.chdir(tmpdir)
192 fs.change_dir(tmpdir)
193 path.use_tree(lfs.currentdir())
194 end)
195
196 after_each(function()
197 if olddir then
198 lfs.chdir(olddir)
199 fs.change_dir(olddir)
200 if tmpdir then
201 lfs.rmdir(tmpdir)
202 end
203 end
204 end)
205
206 it("returns false if the rockspec has no build modules and its format does not support autoextraction", function()
207 local rockspec = {
208 package = "test",
209 version = "1.0-1",
210 source = {
211 url = "http://example.com/test"
212 },
213 build = {}
214 }
215
216 rockspecs.from_persisted_table("test-1.0-1.rockspec", rockspec)
217 assert.falsy(build_builtin.run(rockspec))
218 rockspec.rockspec_format = "1.0"
219 assert.falsy(build_builtin.run(rockspec))
220 end)
221
222 it("returns false if lua.h could not be found", function()
223 local rockspec = {
224 package = "c_module",
225 version = "1.0-1",
226 source = {
227 url = "http://example.com/c_module"
228 },
229 build = {
230 type = "builtin",
231 modules = {
232 c_module = "c_module.c"
233 }
234 }
235 }
236 write_file("c_module.c", c_module_source, finally)
237
238 rockspecs.from_persisted_table("c_module-1.0-1.rockspec", rockspec)
239 rockspec.variables = { LUA_INCDIR = "invalid" }
240 assert.falsy(build_builtin.run(rockspec))
241 end)
242
243 it("returns false if the build fails", function()
244 local rockspec = {
245 package = "c_module",
246 version = "1.0-1",
247 source = {
248 url = "http://example.com/c_module"
249 },
250 build = {
251 type = "builtin",
252 modules = {
253 c_module = "c_module.c"
254 }
255 }
256 }
257 write_file("c_module.c", c_module_source .. "invalid", finally)
258
259 rockspecs.from_persisted_table("c_module-1.0-1.rockspec", rockspec)
260 assert.falsy(build_builtin.run(rockspec))
261 end)
262
263 it("returns true if the build succeeds with C module", function()
264 local rockspec = {
265 package = "c_module",
266 version = "1.0-1",
267 source = {
268 url = "http://example.com/c_module"
269 },
270 build = {
271 type = "builtin",
272 modules = {
273 c_module = "c_module.c"
274 }
275 }
276 }
277 write_file("c_module.c", c_module_source, finally)
278
279 rockspecs.from_persisted_table("c_module-1.0-1.rockspec", rockspec)
280 assert.truthy(build_builtin.run(rockspec))
281 assert.truthy(lfs.attributes("lib/luarocks/rocks-" .. test_env.lua_version .. "/c_module/1.0-1/lib/c_module." .. test_env.lib_extension))
282 end)
283
284 it("returns true if the build succeeds with Lua module", function()
285 local rockspec = {
286 rockspec_format = "1.0",
287 package = "test",
288 version = "1.0-1",
289 source = {
290 url = "http://example.com/test"
291 },
292 build = {
293 type = "builtin",
294 modules = {
295 test = "test.lua"
296 }
297 }
298 }
299 write_file("test.lua", "return {}", finally)
300
301 rockspecs.from_persisted_table("test-1.0-1.rockspec", rockspec)
302 assert.truthy(build_builtin.run(rockspec))
303 assert.truthy(lfs.attributes("lib/luarocks/rocks-" .. test_env.lua_version .. "/test/1.0-1/lua/test.lua"))
304 end)
305
306 it("automatically extracts the modules and libraries if they are not given and builds against any external dependencies", function()
307 local fdir = testing_paths.fixtures_dir
308 if test_env.TEST_TARGET_OS == "windows" then
309 if test_env.MINGW then
310 os.execute("gcc -shared -o " .. fdir .. "/libfixturedep.dll -Wl,--out-implib," .. fdir .."/libfixturedep.a " .. fdir .. "/fixturedep.c")
311 else
312 os.execute("cl " .. fdir .. "\\fixturedep.c /link /export:fixturedep_fn /out:" .. fdir .. "\\fixturedep.dll /implib:" .. fdir .. "\\fixturedep.lib")
313 end
314 elseif test_env.TEST_TARGET_OS == "linux" then
315 os.execute("gcc -shared -o " .. fdir .. "/libfixturedep.so " .. fdir .. "/fixturedep.c")
316 elseif test_env.TEST_TARGET_OS == "osx" then
317 os.execute("cc -dynamiclib -o " .. fdir .. "/libfixturedep.dylib " .. fdir .. "/fixturedep.c")
318 end
319
320 local rockspec = {
321 rockspec_format = "3.0",
322 package = "c_module",
323 version = "1.0-1",
324 source = {
325 url = "http://example.com/c_module"
326 },
327 external_dependencies = {
328 FIXTUREDEP = {
329 library = "fixturedep"
330 }
331 },
332 build = {
333 type = "builtin"
334 }
335 }
336 write_file("c_module.c", c_module_source, finally)
337
338 rockspecs.from_persisted_table("c_module-1.0-1.rockspec", rockspec)
339 rockspec.variables["FIXTUREDEP_LIBDIR"] = testing_paths.fixtures_dir
340 assert.truthy(build_builtin.run(rockspec))
341 end)
342
343 it("returns false if any external dependency is missing", function()
344 local rockspec = {
345 rockspec_format = "3.0",
346 package = "c_module",
347 version = "1.0-1",
348 source = {
349 url = "https://example.com/c_module"
350 },
351 external_dependencies = {
352 EXTDEP = {
353 library = "missing"
354 }
355 },
356 build = {
357 type = "builtin"
358 }
359 }
360 write_file("c_module.c", c_module_source, finally)
361
362 rockspecs.from_persisted_table("c_module-1.0-1.rockspec", rockspec)
363 rockspec.variables["EXTDEP_INCDIR"] = lfs.currentdir()
364 rockspec.variables["EXTDEP_LIBDIR"] = lfs.currentdir()
365 assert.falsy(build_builtin.run(rockspec))
366 end)
367 end)
368 end)
369end)