aboutsummaryrefslogtreecommitdiff
path: root/spec/fetch_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/fetch_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/fetch_spec.lua')
-rw-r--r--spec/fetch_spec.lua488
1 files changed, 0 insertions, 488 deletions
diff --git a/spec/fetch_spec.lua b/spec/fetch_spec.lua
deleted file mode 100644
index 38da379e..00000000
--- a/spec/fetch_spec.lua
+++ /dev/null
@@ -1,488 +0,0 @@
1local test_env = require("spec.util.test_env")
2
3test_env.unload_luarocks()
4test_env.setup_specs()
5local cfg = require("luarocks.core.cfg")
6local fetch = require("luarocks.fetch")
7local fs = require("luarocks.fs")
8local dir = require("luarocks.dir")
9local path = require("luarocks.path")
10local rockspecs = require("luarocks.rockspecs")
11local lfs = require("lfs")
12local get_tmp_path = test_env.get_tmp_path
13local testing_paths = test_env.testing_paths
14local write_file = test_env.write_file
15local P = test_env.P
16
17describe("luarocks fetch #unit", function()
18 local are_same_files = function(file1, file2)
19 return file1 == file2 or lfs.attributes(file1).ino == lfs.attributes(file2).ino
20 end
21
22 local runner
23
24 setup(function()
25 cfg.init()
26 fs.init()
27
28 -- mock network access
29 fs.download = function(url, destfile)
30 local mockfile = P(url:gsub("http://localhost:8080/file", testing_paths.fixtures_dir))
31 if not destfile then
32 destfile = dir.base_name(mockfile)
33 end
34 destfile = fs.absolute_name(destfile)
35
36 local fdr = io.open(mockfile, "rb")
37 if not fdr then
38 return nil, "mock failed opening for reading"
39 end
40
41 local fdw = io.open(destfile, "wb")
42 if not fdr then
43 return nil, "mock failed opening for writing"
44 end
45
46 local data = fdr:read("*a")
47 if not data then
48 return nil, "mock failed reading"
49 end
50
51 local ok = fdw:write(data)
52 if not ok then
53 return nil, "mock failed writing"
54 end
55
56 fdr:close()
57 fdw:close()
58
59 return true, destfile
60 end
61
62 runner = require("luacov.runner")
63 runner.init(testing_paths.testrun_dir .. "/luacov.config")
64 runner.tick = true
65 end)
66
67 teardown(function()
68 runner.shutdown()
69 end)
70
71
72 describe("fetch.fetch_url", function()
73
74 it("fetches the url argument and returns the absolute path of the fetched file", function()
75 test_env.run_in_tmp(function()
76 local fetchedfile, err = fetch.fetch_url("http://localhost:8080/file/a_rock.lua")
77 assert(fetchedfile, err)
78 assert.truthy(are_same_files(fetchedfile, lfs.currentdir() .. "/a_rock.lua"))
79 local fd = assert(io.open(fetchedfile, "r"))
80 local fetchedcontent = assert(fd:read("*a"))
81 fd:close()
82 fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r"))
83 local filecontent = assert(fd:read("*a"))
84 fd:close()
85 assert.same(fetchedcontent, filecontent)
86 end, finally)
87 end)
88
89 it("returns the absolute path of the filename argument if the url represents a file", function()
90 test_env.run_in_tmp(function()
91 write_file("test.lua", "return {}")
92
93 local file, err = fetch.fetch_url("file://test.lua")
94 assert.truthy(file, err)
95 assert.truthy(are_same_files(file, lfs.currentdir() .. "/test.lua"))
96 fs.pop_dir()
97 end, finally)
98 end)
99
100 it("fails if local path is invalid and returns a helpful hint for relative paths", function()
101 test_env.run_in_tmp(function()
102 local ok, err = fetch.fetch_url("file://boo.lua")
103 assert.falsy(ok)
104 assert.match("note that given path in rockspec is not absolute: file://boo.lua", err)
105 end, finally)
106 end)
107
108 it("returns false and does nothing if the url argument contains a nonexistent file", function()
109 assert.falsy(fetch.fetch_url("http://localhost:8080/file/nonexistent"))
110 end)
111
112 it("returns false and does nothing if the url argument is invalid", function()
113 assert.falsy(fetch.fetch_url("invalid://url", "file"))
114 end)
115 end)
116
117 describe("fetch.fetch_url_at_temp_dir", function()
118
119 it("returns the absolute path and the parent directory of the file specified by the url", function()
120 test_env.run_in_tmp(function(tmpdir)
121 local tmpfile = tmpdir .. "/tmpfile"
122 assert(io.open(tmpfile, "w"))
123 local pathname, dirname = fetch.fetch_url_at_temp_dir("file://" .. tmpfile, "test")
124 assert.truthy(are_same_files(tmpfile, pathname))
125 assert.truthy(are_same_files(tmpdir, dirname))
126 end, finally)
127 end)
128
129 it("returns true and fetches the url into a temporary dir", function()
130 test_env.run_in_tmp(function()
131 local fetchedfile, tmpdir = fetch.fetch_url_at_temp_dir("http://localhost:8080/file/a_rock.lua", "test")
132 assert(fetchedfile, tmpdir)
133 assert.truthy(are_same_files(fetchedfile, tmpdir .. "/a_rock.lua"))
134 local fd = assert(io.open(fetchedfile, "r"))
135 local fetchedcontent = assert(fd:read("*a"))
136 fd:close()
137 fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r"))
138 local filecontent = assert(fd:read("*a"))
139 fd:close()
140 assert.same(fetchedcontent, filecontent)
141 end, finally)
142 end)
143
144 it("returns true and fetches the url into a temporary dir with custom filename", function()
145 test_env.run_in_tmp(function()
146 local fetchedfile, tmpdir = fetch.fetch_url_at_temp_dir("http://localhost:8080/file/a_rock.lua", "test", "my_a_rock.lua")
147 assert(fetchedfile, tmpdir)
148 assert.truthy(are_same_files(fetchedfile, tmpdir .. "/my_a_rock.lua"))
149 assert.truthy(string.find(tmpdir, "test"))
150 local fd = assert(io.open(fetchedfile, "r"))
151 local fetchedcontent = assert(fd:read("*a"))
152 fd:close()
153 fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r"))
154 local filecontent = assert(fd:read("*a"))
155 fd:close()
156 assert.same(fetchedcontent, filecontent)
157 end, finally)
158 end)
159
160 it("returns false and does nothing if the file specified in the url is nonexistent", function()
161 assert.falsy(fetch.fetch_url_at_temp_dir("file://nonexistent", "test"))
162 assert.falsy(fetch.fetch_url_at_temp_dir("http://localhost:8080/file/nonexistent", "test"))
163 end)
164
165 it("returns false and does nothing if the url is invalid", function()
166 assert.falsy(fetch.fetch_url_at_temp_dir("url://invalid", "test"))
167 end)
168 end)
169
170 describe("fetch.find_base_dir", function()
171 it("extracts the archive given by the file argument and returns the inferred and the actual root directory in the archive", function()
172 test_env.run_in_tmp(function()
173 local url = "http://localhost:8080/file/an_upstream_tarball-0.1.tar.gz"
174 local file, tmpdir = assert(fetch.fetch_url_at_temp_dir(url, "test"))
175 local inferreddir, founddir = fetch.find_base_dir(file, tmpdir, url)
176 assert.truthy(are_same_files(inferreddir, founddir))
177 assert.truthy(lfs.attributes(tmpdir .. "/" .. founddir))
178 end, finally)
179 end)
180
181 it("extracts the archive given by the file argument with given base directory and returns the inferred and the actual root directory in the archive", function()
182 test_env.run_in_tmp(function()
183 local url = "http://localhost:8080/file/an_upstream_tarball-0.1.tar.gz"
184 local file, tmpdir = assert(fetch.fetch_url_at_temp_dir(url, "test"))
185 local inferreddir, founddir = fetch.find_base_dir(file, tmpdir, url, "basedir")
186 assert.truthy(are_same_files(inferreddir, "basedir"))
187 assert.truthy(are_same_files(founddir, "an_upstream_tarball-0.1"))
188 assert.truthy(lfs.attributes(tmpdir .. "/" .. founddir))
189 end, finally)
190 end)
191
192 it("returns false and does nothing if the temporary directory doesn't exist", function()
193 assert.falsy(fetch.find_base_dir("file", "nonexistent", "url"))
194 end)
195 end)
196
197 describe("fetch.fetch_and_unpack_rock", function()
198
199 it("unpacks the rock file from the url and returns its resulting temporary parent directory", function()
200 test_env.run_in_tmp(function()
201 local tmpdir = fetch.fetch_and_unpack_rock("http://localhost:8080/file/a_rock-1.0-1.src.rock")
202 assert.truthy(string.find(tmpdir, "a_rock%-1%.0%-1"))
203 assert.truthy(lfs.attributes(tmpdir .. "/a_rock-1.0-1.rockspec"))
204 assert.truthy(lfs.attributes(tmpdir .. "/a_rock.lua"))
205 end, finally)
206 end)
207
208 it("unpacks the rock file from the url with custom unpacking directory", function()
209 test_env.run_in_tmp(function()
210 local tmpdir = get_tmp_path()
211 lfs.mkdir(tmpdir)
212 local resultingdir = fetch.fetch_and_unpack_rock("http://localhost:8080/file/a_rock-1.0-1.src.rock", tmpdir)
213 assert.truthy(are_same_files(resultingdir, tmpdir))
214 assert.truthy(lfs.attributes(resultingdir .. "/a_rock-1.0-1.rockspec"))
215 assert.truthy(lfs.attributes(resultingdir .. "/a_rock.lua"))
216 end, finally)
217 end)
218
219 it("does nothing if the url doesn't represent a rock file", function()
220 assert.falsy(pcall(fetch.fetch_and_unpack_rock, "http://localhost:8080/file/a_rock.lua"))
221 end)
222
223 it("does nothing if the rock file url is invalid", function()
224 assert.falsy(pcall(fetch.fetch_and_unpack_rock, "url://invalid"))
225 end)
226
227 it("does nothing if the rock file url represents a nonexistent file", function()
228 assert.falsy(pcall(fetch.fetch_and_unpack_rock, "url://invalid"))
229 assert.falsy(pcall(fetch.fetch_and_unpack_rock, "http://localhost:8080/file/nonexistent"))
230 end)
231 end)
232
233 describe("fetch.load_local_rockspec", function()
234 it("returns a table representing the rockspec from the given file skipping some checks if the quick argument is enabled", function()
235 test_env.run_in_tmp(function()
236 local rockspec = fetch.load_local_rockspec(testing_paths.fixtures_dir .. "/a_rock-1.0-1.rockspec", true)
237 assert.same(rockspec.name, "a_rock")
238 assert.same(rockspec.version, "1.0-1")
239 assert.same(rockspec.source.url, "http://localhost:8080/file/a_rock.lua")
240 assert.same(rockspec.description, { summary = "An example rockspec" })
241
242 write_file("missing_mandatory_field-1.0-1.rockspec", [[
243 package="missing_mandatory_field"
244 version="1.0-1"
245 source = {
246 url = "http://example.com/foo.tar.gz"
247 }
248 ]])
249 rockspec = fetch.load_local_rockspec("missing_mandatory_field-1.0-1.rockspec", true)
250 assert.same(rockspec.name, "missing_mandatory_field")
251 assert.same(rockspec.version, "1.0-1")
252 assert.same(rockspec.source.url, "http://example.com/foo.tar.gz")
253
254 write_file("unknown_field-1.0-1.rockspec", [[
255 package="unknown_field"
256 version="1.0-1"
257 source = {
258 url = "http://example.com/foo.tar.gz"
259 }
260 unknown="foo"
261 ]])
262 rockspec = fetch.load_local_rockspec("unknown_field-1.0-1.rockspec", true)
263 assert.same(rockspec.name, "unknown_field")
264 assert.same(rockspec.version, "1.0-1")
265 assert.same(rockspec.source.url, "http://example.com/foo.tar.gz")
266
267 -- The previous calls fail if the detailed checking is done
268 path.use_tree(testing_paths.testing_tree)
269 assert.falsy(fetch.load_local_rockspec("missing_mandatory_field-1.0-1.rockspec"))
270 assert.falsy(fetch.load_local_rockspec("unknown_field-1.0-1.rockspec"))
271 end, finally)
272 end)
273
274 it("returns a table representing the rockspec from the given file", function()
275 test_env.run_in_tmp(function()
276 path.use_tree(testing_paths.testing_tree)
277 local rockspec = fetch.load_local_rockspec(testing_paths.fixtures_dir .. "/a_rock-1.0-1.rockspec")
278 assert.same(rockspec.name, "a_rock")
279 assert.same(rockspec.version, "1.0-1")
280 assert.same(rockspec.description, { summary = "An example rockspec" })
281 assert.same(rockspec.source.url, "http://localhost:8080/file/a_rock.lua")
282 end, finally)
283 end)
284
285 it("returns false if the rockspec in invalid", function()
286 assert.falsy(fetch.load_local_rockspec(testing_paths.fixtures_dir .. "/invalid_say-1.3-1.rockspec"))
287 end)
288
289 it("returns false if the rockspec version is not supported", function()
290 assert.falsy(fetch.load_local_rockspec("invalid_version.rockspec"))
291 end)
292
293 it("returns false if the rockspec doesn't pass the type checking", function()
294 test_env.run_in_tmp(function()
295 write_file("type_mismatch_string-1.0-1.rockspec", [[
296 package="type_mismatch_version"
297 version=1.0
298 ]])
299 assert.falsy(fetch.load_local_rockspec("type_mismatch_string-1.0-1.rockspec"))
300 end, finally)
301 end)
302
303 it("returns false if the rockspec file name is not right", function()
304 test_env.run_in_tmp(function()
305 write_file("invalid_rockspec_name.rockspec", [[
306 package="invalid_rockspec_name"
307 version="1.0-1"
308 source = {
309 url = "http://example.com/foo.tar.gz"
310 }
311 build = {
312
313 }
314 ]])
315 assert.falsy(fetch.load_local_rockspec("invalid_rockspec_name.rockspec"))
316 end, finally)
317 end)
318
319 it("returns false if the version in the rockspec file name doesn't match the version declared in the rockspec", function()
320 test_env.run_in_tmp(function()
321 write_file("inconsistent_versions-1.0-1.rockspec", [[
322 package="inconsistent_versions"
323 version="1.0-2"
324 source = {
325 url = "http://example.com/foo.tar.gz"
326 }
327 build = {
328
329 }
330 ]])
331 assert.falsy(fetch.load_local_rockspec("inconsistent_versions-1.0-1.rockspec"))
332 end, finally)
333 end)
334 end)
335
336 describe("fetch.load_rockspec", function()
337
338 it("returns a table containing the requested rockspec by downloading it into a temporary directory", function()
339 test_env.run_in_tmp(function()
340 path.use_tree(testing_paths.testing_tree)
341 local rockspec = fetch.load_rockspec("http://localhost:8080/file/a_rock-1.0-1.rockspec")
342 assert.same(rockspec.name, "a_rock")
343 assert.same(rockspec.version, "1.0-1")
344 assert.same(rockspec.description, { summary = "An example rockspec" })
345 assert.same(rockspec.source.url, "http://localhost:8080/file/a_rock.lua")
346 rockspec = fetch.load_rockspec(testing_paths.fixtures_dir .. "/a_rock-1.0-1.rockspec")
347 assert.same(rockspec.name, "a_rock")
348 assert.same(rockspec.version, "1.0-1")
349 assert.same(rockspec.description, { summary = "An example rockspec" })
350 assert.same(rockspec.source.url, "http://localhost:8080/file/a_rock.lua")
351 end, finally)
352 end)
353
354 it("returns a table containing the requested rockspec by downloading it into a given directory", function()
355 test_env.run_in_tmp(function()
356 local tmpdir = get_tmp_path()
357 lfs.mkdir(tmpdir)
358
359 path.use_tree(testing_paths.testing_tree)
360 local rockspec = fetch.load_rockspec("http://localhost:8080/file/a_rock-1.0-1.rockspec", tmpdir)
361 assert.same(rockspec.name, "a_rock")
362 assert.same(rockspec.version, "1.0-1")
363 assert.same(rockspec.description, { summary = "An example rockspec" })
364 assert.same(rockspec.source.url, "http://localhost:8080/file/a_rock.lua")
365 assert.truthy(lfs.attributes(tmpdir .. "/a_rock-1.0-1.rockspec"))
366
367 lfs.rmdir(tmpdir)
368 end, finally)
369 end)
370
371 it("returns false if the given download directory doesn't exist", function()
372 assert.falsy(fetch.load_rockspec("http://localhost:8080/file/a_rock-1.0-1.rockspec", "nonexistent"))
373 end)
374
375 it("returns false if the given filename is not a valid rockspec name", function()
376 assert.falsy(fetch.load_rockspec("http://localhost:8080/file/a_rock.lua"))
377 end)
378 end)
379
380 describe("fetch.get_sources", function()
381
382 it("downloads the sources for building a rock and returns the resulting source filename and its parent directory", function()
383 test_env.run_in_tmp(function()
384 local rockspec = assert(fetch.load_rockspec("http://localhost:8080/file/a_rock-1.0-1.rockspec"))
385 local file, dirname = fetch.get_sources(rockspec, false)
386 assert.truthy(are_same_files(dirname .. "/a_rock.lua", file))
387 end, finally)
388 end)
389
390 it("downloads the sources for building a rock into a given directory and returns the resulting source filename and its parent directory", function()
391 test_env.run_in_tmp(function()
392 local tmpdir = get_tmp_path()
393 lfs.mkdir(tmpdir)
394 local rockspec = assert(fetch.load_rockspec("http://localhost:8080/file/a_rock-1.0-1.rockspec"))
395 local file, dirname = fetch.get_sources(rockspec, false, tmpdir)
396 assert.truthy(are_same_files(tmpdir, dirname))
397 assert.truthy(are_same_files(dirname .. "/a_rock.lua", file))
398 lfs.rmdir(tmpdir)
399 end, finally)
400 end)
401
402 it("downloads the sources for building a rock, extracts the downloaded tarball and returns the resulting source filename and its parent directory", function()
403 test_env.run_in_tmp(function()
404 local rockspec = assert(fetch.load_rockspec("http://localhost:8080/file/busted_project-0.1-1.rockspec"))
405 local file, dirname = fetch.get_sources(rockspec, true)
406 assert.truthy(are_same_files(dirname .. "/busted_project-0.1.tar.gz", file))
407 assert.truthy(lfs.attributes(dirname .. "/busted_project"))
408 assert.truthy(lfs.attributes(dirname .. "/busted_project/sum.lua"))
409 assert.truthy(lfs.attributes(dirname .. "/busted_project/spec/sum_spec.lua"))
410 end, finally)
411 end)
412
413 it("returns false and does nothing if the destination directory doesn't exist", function()
414 test_env.run_in_tmp(function()
415 local rockspec = assert(fetch.load_rockspec("http://localhost:8080/file/a_rock-1.0-1.rockspec"))
416 assert.falsy(fetch.get_sources(rockspec, false, "nonexistent"))
417 end, finally)
418 end)
419
420 it("returns false and does nothing if the rockspec source url is invalid", function()
421 test_env.run_in_tmp(function(tmpdir)
422 write_file(tmpdir .. "/invalid_url-1.0-1.rockspec", [[
423 package="invalid_url"
424 version="1.0-1"
425 source = {
426 url = "http://localhost:8080/file/nonexistent"
427 }
428 build = {
429
430 }
431 ]])
432 local rockspec = assert(fetch.load_rockspec(tmpdir .. "/invalid_url-1.0-1.rockspec"))
433 assert.falsy(fetch.get_sources(rockspec, false))
434 end, finally)
435 end)
436
437 it("returns false and does nothing if the downloaded rockspec has an invalid md5 checksum", function()
438 test_env.run_in_tmp(function()
439 write_file("invalid_checksum-1.0-1.rockspec", [[
440 package="invalid_checksum"
441 version="1.0-1"
442 source = {
443 url = "http://localhost:8080/file/a_rock.lua",
444 md5 = "invalid"
445 }
446 build = {
447
448 }
449 ]])
450 local rockspec = assert(fetch.load_rockspec("invalid_checksum-1.0-1.rockspec"))
451 assert.falsy(fetch.get_sources(rockspec, false))
452 end, finally)
453 end)
454 end)
455
456 describe("fetch_sources #unix #git", function()
457 local git_repo = require("spec.util.git_repo")
458
459 local git
460
461 setup(function()
462 git = git_repo.start()
463 end)
464
465 teardown(function()
466 if git then
467 git:stop()
468 end
469 end)
470
471 it("from #git", function()
472 local rockspec, err = rockspecs.from_persisted_table("testrock-dev-1.rockspec", {
473 rockspec_format = "3.0",
474 package = "testrock",
475 version = "dev-1",
476 source = {
477 url = "git://localhost/testrock",
478 },
479 }, nil)
480 assert.falsy(err)
481 local pathname, tmpdir = fetch.fetch_sources(rockspec, false)
482 assert.are.same("testrock", pathname)
483 assert.match("luarocks_testrock%-dev%-1%-", tmpdir)
484 assert.match("^%d%d%d%d%d%d%d%d.%d%d%d%d%d%d.%x+$", tostring(rockspec.source.identifier))
485 end)
486 end)
487
488end)