From 6bc6ede826843c3692971c14c27c3d27714b2126 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Mon, 26 Feb 2024 14:11:37 -0300 Subject: tests(fetch): do not use restserver --- spec/build_spec.lua | 34 ++-- spec/fetch_spec.lua | 540 +++++++++++++++++++++++++------------------------ spec/util/quick.lua | 2 +- spec/util/test_env.lua | 15 ++ 4 files changed, 309 insertions(+), 282 deletions(-) diff --git a/spec/build_spec.lua b/spec/build_spec.lua index 9b34ec54..a2c09119 100644 --- a/spec/build_spec.lua +++ b/spec/build_spec.lua @@ -64,8 +64,8 @@ describe("LuaRocks build #integration", function() test = "test.lua" } } - ]], finally) - write_file("test.lua", "return {}", finally) + ]]) + write_file("test.lua", "return {}") assert.is_true(run.luarocks_bool("build --verbose test-1.0-1.rockspec")) assert.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/test/1.0-1/test-1.0-1.rockspec")) @@ -119,8 +119,8 @@ describe("LuaRocks build #integration", function() test = "test.lua" } } - ]], finally) - write_file("test.lua", "return {}", finally) + ]]) + write_file("test.lua", "return {}") if test_env.TEST_TARGET_OS == "windows" then assert.is_false(run.luarocks_bool("build test-1.0-1.rockspec")) -- Error: This rockspec does not support windows platforms @@ -149,8 +149,8 @@ describe("LuaRocks build #integration", function() test = "test.lua" } } - ]], finally) - write_file("test.lua", "return {}", finally) + ]]) + write_file("test.lua", "return {}") assert.is_true(run.luarocks_bool("build test-1.0-1.rockspec --deps-mode=none")) assert.is.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/test/1.0-1/test-1.0-1.rockspec")) @@ -174,8 +174,8 @@ describe("LuaRocks build #integration", function() test = "test.lua" } } - ]], finally) - write_file("test.lua", "return {}", finally) + ]]) + write_file("test.lua", "return {}") assert.is_true(run.luarocks_bool("build --server=" .. testing_paths.fixtures_dir .. "/a_repo test-1.0-1.rockspec --pin --tree=lua_modules")) assert.is.truthy(lfs.attributes("./lua_modules/lib/luarocks/rocks-" .. test_env.lua_version .. "/test/1.0-1/test-1.0-1.rockspec")) @@ -189,7 +189,7 @@ describe("LuaRocks build #integration", function() ["lua"] = test_env.lua_version .. "-1", } }, lockdata) - end) + end, finally) end) it("supports --pin --only-deps #pinning", function() @@ -209,8 +209,8 @@ describe("LuaRocks build #integration", function() test = "test.lua" } } - ]], finally) - write_file("test.lua", "return {}", finally) + ]]) + write_file("test.lua", "return {}") assert.is_true(run.luarocks_bool("build --server=" .. testing_paths.fixtures_dir .. "/a_repo test-1.0-1.rockspec --pin --only-deps --tree=lua_modules")) assert.is.falsy(lfs.attributes("./lua_modules/lib/luarocks/rocks-" .. test_env.lua_version .. "/test/1.0-1/test-1.0-1.rockspec")) @@ -225,7 +225,7 @@ describe("LuaRocks build #integration", function() ["lua"] = test_env.lua_version .. "-1", } }, lockdata) - end) + end, finally) end) it("lmathx deps partial match", function() @@ -316,8 +316,8 @@ describe("LuaRocks build #integration", function() test = "test.lua" } } - ]], finally) - write_file("test.lua", "return {}", finally) + ]]) + write_file("test.lua", "return {}") assert.is.truthy(run.luarocks_bool("build --server=" .. testing_paths.fixtures_dir .. "/a_repo test-1.0-1.rockspec --only-deps")) assert.is.falsy(lfs.attributes(testing_paths.testing_sys_rocks .. "/test/1.0-1/test-1.0-1.rockspec")) @@ -342,8 +342,8 @@ describe("LuaRocks build #integration", function() test = "test.lua" } } - ]], finally) - write_file("test.lua", "return {}", finally) + ]]) + write_file("test.lua", "return {}") assert.is.truthy(run.luarocks_bool("pack test-1.0-1.rockspec")) assert.is.truthy(lfs.attributes("test-1.0-1.src.rock")) @@ -489,7 +489,7 @@ describe("LuaRocks build #integration", function() build = "build.lua" } } - ]], finally) + ]]) assert.is_false(run.luarocks_bool("build missing_external-0.1-1.rockspec INEXISTENT_INCDIR=\"/invalid/dir\"")) end, finally) end) diff --git a/spec/fetch_spec.lua b/spec/fetch_spec.lua index 046d7b7b..38da379e 100644 --- a/spec/fetch_spec.lua +++ b/spec/fetch_spec.lua @@ -1,19 +1,20 @@ local test_env = require("spec.util.test_env") -local git_repo = require("spec.util.git_repo") test_env.unload_luarocks() test_env.setup_specs() local cfg = require("luarocks.core.cfg") local fetch = require("luarocks.fetch") local fs = require("luarocks.fs") +local dir = require("luarocks.dir") local path = require("luarocks.path") local rockspecs = require("luarocks.rockspecs") local lfs = require("lfs") local get_tmp_path = test_env.get_tmp_path local testing_paths = test_env.testing_paths local write_file = test_env.write_file +local P = test_env.P -describe("luarocks fetch #unit #mock", function() +describe("luarocks fetch #unit", function() local are_same_files = function(file1, file2) return file1 == file2 or lfs.attributes(file1).ino == lfs.attributes(file2).ino end @@ -23,7 +24,40 @@ describe("luarocks fetch #unit #mock", function() setup(function() cfg.init() fs.init() - test_env.mock_server_init() + + -- mock network access + fs.download = function(url, destfile) + local mockfile = P(url:gsub("http://localhost:8080/file", testing_paths.fixtures_dir)) + if not destfile then + destfile = dir.base_name(mockfile) + end + destfile = fs.absolute_name(destfile) + + local fdr = io.open(mockfile, "rb") + if not fdr then + return nil, "mock failed opening for reading" + end + + local fdw = io.open(destfile, "wb") + if not fdr then + return nil, "mock failed opening for writing" + end + + local data = fdr:read("*a") + if not data then + return nil, "mock failed reading" + end + + local ok = fdw:write(data) + if not ok then + return nil, "mock failed writing" + end + + fdr:close() + fdw:close() + + return true, destfile + end runner = require("luacov.runner") runner.init(testing_paths.testrun_dir .. "/luacov.config") @@ -31,32 +65,31 @@ describe("luarocks fetch #unit #mock", function() end) teardown(function() - test_env.mock_server_done() - runner.shutdown() end) + describe("fetch.fetch_url", function() it("fetches the url argument and returns the absolute path of the fetched file", function() - local fetchedfile, err = fetch.fetch_url("http://localhost:8080/file/a_rock.lua") - assert(fetchedfile, err) - assert.truthy(are_same_files(fetchedfile, lfs.currentdir() .. "/a_rock.lua")) - local fd = assert(io.open(fetchedfile, "r")) - local fetchedcontent = assert(fd:read("*a")) - fd:close() - fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r")) - local filecontent = assert(fd:read("*a")) - fd:close() - assert.same(fetchedcontent, filecontent) - os.remove(fetchedfile) + test_env.run_in_tmp(function() + local fetchedfile, err = fetch.fetch_url("http://localhost:8080/file/a_rock.lua") + assert(fetchedfile, err) + assert.truthy(are_same_files(fetchedfile, lfs.currentdir() .. "/a_rock.lua")) + local fd = assert(io.open(fetchedfile, "r")) + local fetchedcontent = assert(fd:read("*a")) + fd:close() + fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r")) + local filecontent = assert(fd:read("*a")) + fd:close() + assert.same(fetchedcontent, filecontent) + end, finally) end) it("returns the absolute path of the filename argument if the url represents a file", function() - test_env.run_in_tmp(function(tmpdir) - write_file("test.lua", "return {}", finally) + test_env.run_in_tmp(function() + write_file("test.lua", "return {}") - fs.change_dir(tmpdir) local file, err = fetch.fetch_url("file://test.lua") assert.truthy(file, err) assert.truthy(are_same_files(file, lfs.currentdir() .. "/test.lua")) @@ -65,9 +98,7 @@ describe("luarocks fetch #unit #mock", function() end) it("fails if local path is invalid and returns a helpful hint for relative paths", function() - test_env.run_in_tmp(function(tmpdir) - write_file("test.lua", "return {}", finally) - + test_env.run_in_tmp(function() local ok, err = fetch.fetch_url("file://boo.lua") assert.falsy(ok) assert.match("note that given path in rockspec is not absolute: file://boo.lua", err) @@ -84,55 +115,46 @@ describe("luarocks fetch #unit #mock", function() end) describe("fetch.fetch_url_at_temp_dir", function() - local tmpfile - local tmpdir - - after_each(function() - if tmpfile then - os.remove(tmpfile) - tmpfile = nil - end - if tmpdir then - lfs.rmdir(tmpdir) - tmpdir = nil - end - end) it("returns the absolute path and the parent directory of the file specified by the url", function() - tmpdir = get_tmp_path() - lfs.mkdir(tmpdir) - tmpfile = tmpdir .. "/tmpfile" - local fd = assert(io.open(tmpfile, "w")) - local pathname, dirname = fetch.fetch_url_at_temp_dir("file://" .. tmpfile, "test") - assert.truthy(are_same_files(tmpfile, pathname)) - assert.truthy(are_same_files(tmpdir, dirname)) + test_env.run_in_tmp(function(tmpdir) + local tmpfile = tmpdir .. "/tmpfile" + assert(io.open(tmpfile, "w")) + local pathname, dirname = fetch.fetch_url_at_temp_dir("file://" .. tmpfile, "test") + assert.truthy(are_same_files(tmpfile, pathname)) + assert.truthy(are_same_files(tmpdir, dirname)) + end, finally) end) it("returns true and fetches the url into a temporary dir", function() - local fetchedfile, tmpdir = fetch.fetch_url_at_temp_dir("http://localhost:8080/file/a_rock.lua", "test") - assert(fetchedfile, tmpdir) - assert.truthy(are_same_files(fetchedfile, tmpdir .. "/a_rock.lua")) - local fd = assert(io.open(fetchedfile, "r")) - local fetchedcontent = assert(fd:read("*a")) - fd:close() - fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r")) - local filecontent = assert(fd:read("*a")) - fd:close() - assert.same(fetchedcontent, filecontent) + test_env.run_in_tmp(function() + local fetchedfile, tmpdir = fetch.fetch_url_at_temp_dir("http://localhost:8080/file/a_rock.lua", "test") + assert(fetchedfile, tmpdir) + assert.truthy(are_same_files(fetchedfile, tmpdir .. "/a_rock.lua")) + local fd = assert(io.open(fetchedfile, "r")) + local fetchedcontent = assert(fd:read("*a")) + fd:close() + fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r")) + local filecontent = assert(fd:read("*a")) + fd:close() + assert.same(fetchedcontent, filecontent) + end, finally) end) it("returns true and fetches the url into a temporary dir with custom filename", function() - local fetchedfile, tmpdir = fetch.fetch_url_at_temp_dir("http://localhost:8080/file/a_rock.lua", "test", "my_a_rock.lua") - assert(fetchedfile, tmpdir) - assert.truthy(are_same_files(fetchedfile, tmpdir .. "/my_a_rock.lua")) - assert.truthy(string.find(tmpdir, "test")) - local fd = assert(io.open(fetchedfile, "r")) - local fetchedcontent = assert(fd:read("*a")) - fd:close() - fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r")) - local filecontent = assert(fd:read("*a")) - fd:close() - assert.same(fetchedcontent, filecontent) + test_env.run_in_tmp(function() + local fetchedfile, tmpdir = fetch.fetch_url_at_temp_dir("http://localhost:8080/file/a_rock.lua", "test", "my_a_rock.lua") + assert(fetchedfile, tmpdir) + assert.truthy(are_same_files(fetchedfile, tmpdir .. "/my_a_rock.lua")) + assert.truthy(string.find(tmpdir, "test")) + local fd = assert(io.open(fetchedfile, "r")) + local fetchedcontent = assert(fd:read("*a")) + fd:close() + fd = assert(io.open(testing_paths.fixtures_dir .. "/a_rock.lua", "r")) + local filecontent = assert(fd:read("*a")) + fd:close() + assert.same(fetchedcontent, filecontent) + end, finally) end) it("returns false and does nothing if the file specified in the url is nonexistent", function() @@ -147,20 +169,24 @@ describe("luarocks fetch #unit #mock", function() describe("fetch.find_base_dir", function() it("extracts the archive given by the file argument and returns the inferred and the actual root directory in the archive", function() - local url = "http://localhost:8080/file/an_upstream_tarball-0.1.tar.gz" - local file, tmpdir = assert(fetch.fetch_url_at_temp_dir(url, "test")) - local inferreddir, founddir = fetch.find_base_dir(file, tmpdir, url) - assert.truthy(are_same_files(inferreddir, founddir)) - assert.truthy(lfs.attributes(tmpdir .. "/" .. founddir)) + test_env.run_in_tmp(function() + local url = "http://localhost:8080/file/an_upstream_tarball-0.1.tar.gz" + local file, tmpdir = assert(fetch.fetch_url_at_temp_dir(url, "test")) + local inferreddir, founddir = fetch.find_base_dir(file, tmpdir, url) + assert.truthy(are_same_files(inferreddir, founddir)) + assert.truthy(lfs.attributes(tmpdir .. "/" .. founddir)) + end, finally) end) 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() - local url = "http://localhost:8080/file/an_upstream_tarball-0.1.tar.gz" - local file, tmpdir = assert(fetch.fetch_url_at_temp_dir(url, "test")) - local inferreddir, founddir = fetch.find_base_dir(file, tmpdir, url, "basedir") - assert.truthy(are_same_files(inferreddir, "basedir")) - assert.truthy(are_same_files(founddir, "an_upstream_tarball-0.1")) - assert.truthy(lfs.attributes(tmpdir .. "/" .. founddir)) + test_env.run_in_tmp(function() + local url = "http://localhost:8080/file/an_upstream_tarball-0.1.tar.gz" + local file, tmpdir = assert(fetch.fetch_url_at_temp_dir(url, "test")) + local inferreddir, founddir = fetch.find_base_dir(file, tmpdir, url, "basedir") + assert.truthy(are_same_files(inferreddir, "basedir")) + assert.truthy(are_same_files(founddir, "an_upstream_tarball-0.1")) + assert.truthy(lfs.attributes(tmpdir .. "/" .. founddir)) + end, finally) end) it("returns false and does nothing if the temporary directory doesn't exist", function() @@ -169,29 +195,25 @@ describe("luarocks fetch #unit #mock", function() end) describe("fetch.fetch_and_unpack_rock", function() - local tmpdir - - after_each(function() - if tmpdir then - lfs.rmdir(tmpdir) - tmpdir = nil - end - end) it("unpacks the rock file from the url and returns its resulting temporary parent directory", function() - tmpdir = fetch.fetch_and_unpack_rock("http://localhost:8080/file/a_rock-1.0-1.src.rock") - assert.truthy(string.find(tmpdir, "a_rock%-1%.0%-1")) - assert.truthy(lfs.attributes(tmpdir .. "/a_rock-1.0-1.rockspec")) - assert.truthy(lfs.attributes(tmpdir .. "/a_rock.lua")) + test_env.run_in_tmp(function() + local tmpdir = fetch.fetch_and_unpack_rock("http://localhost:8080/file/a_rock-1.0-1.src.rock") + assert.truthy(string.find(tmpdir, "a_rock%-1%.0%-1")) + assert.truthy(lfs.attributes(tmpdir .. "/a_rock-1.0-1.rockspec")) + assert.truthy(lfs.attributes(tmpdir .. "/a_rock.lua")) + end, finally) end) it("unpacks the rock file from the url with custom unpacking directory", function() - tmpdir = get_tmp_path() - lfs.mkdir(tmpdir) - local resultingdir = fetch.fetch_and_unpack_rock("http://localhost:8080/file/a_rock-1.0-1.src.rock", tmpdir) - assert.truthy(are_same_files(resultingdir, tmpdir)) - assert.truthy(lfs.attributes(resultingdir .. "/a_rock-1.0-1.rockspec")) - assert.truthy(lfs.attributes(resultingdir .. "/a_rock.lua")) + test_env.run_in_tmp(function() + local tmpdir = get_tmp_path() + lfs.mkdir(tmpdir) + local resultingdir = fetch.fetch_and_unpack_rock("http://localhost:8080/file/a_rock-1.0-1.src.rock", tmpdir) + assert.truthy(are_same_files(resultingdir, tmpdir)) + assert.truthy(lfs.attributes(resultingdir .. "/a_rock-1.0-1.rockspec")) + assert.truthy(lfs.attributes(resultingdir .. "/a_rock.lua")) + end, finally) end) it("does nothing if the url doesn't represent a rock file", function() @@ -209,71 +231,55 @@ describe("luarocks fetch #unit #mock", function() end) describe("fetch.load_local_rockspec", function() - local tmpdir - local olddir - - before_each(function() - tmpdir = get_tmp_path() - olddir = lfs.currentdir() - lfs.mkdir(tmpdir) - lfs.chdir(tmpdir) - fs.change_dir(tmpdir) - end) - - after_each(function() - if olddir then - lfs.chdir(olddir) - if tmpdir then - lfs.rmdir(tmpdir) - end - end - end) - it("returns a table representing the rockspec from the given file skipping some checks if the quick argument is enabled", function() - local rockspec = fetch.load_local_rockspec(testing_paths.fixtures_dir .. "/a_rock-1.0-1.rockspec", true) - assert.same(rockspec.name, "a_rock") - assert.same(rockspec.version, "1.0-1") - assert.same(rockspec.source.url, "http://localhost:8080/file/a_rock.lua") - assert.same(rockspec.description, { summary = "An example rockspec" }) - - write_file("missing_mandatory_field-1.0-1.rockspec", [[ - package="missing_mandatory_field" - version="1.0-1" - source = { - url = "http://example.com/foo.tar.gz" - } - ]], finally) - rockspec = fetch.load_local_rockspec("missing_mandatory_field-1.0-1.rockspec", true) - assert.same(rockspec.name, "missing_mandatory_field") - assert.same(rockspec.version, "1.0-1") - assert.same(rockspec.source.url, "http://example.com/foo.tar.gz") - - write_file("unknown_field-1.0-1.rockspec", [[ - package="unknown_field" - version="1.0-1" - source = { - url = "http://example.com/foo.tar.gz" - } - unknown="foo" - ]], finally) - rockspec = fetch.load_local_rockspec("unknown_field-1.0-1.rockspec", true) - assert.same(rockspec.name, "unknown_field") - assert.same(rockspec.version, "1.0-1") - assert.same(rockspec.source.url, "http://example.com/foo.tar.gz") - - -- The previous calls fail if the detailed checking is done - path.use_tree(testing_paths.testing_tree) - assert.falsy(fetch.load_local_rockspec("missing_mandatory_field-1.0-1.rockspec")) - assert.falsy(fetch.load_local_rockspec("unknown_field-1.0-1.rockspec")) + test_env.run_in_tmp(function() + local rockspec = fetch.load_local_rockspec(testing_paths.fixtures_dir .. "/a_rock-1.0-1.rockspec", true) + assert.same(rockspec.name, "a_rock") + assert.same(rockspec.version, "1.0-1") + assert.same(rockspec.source.url, "http://localhost:8080/file/a_rock.lua") + assert.same(rockspec.description, { summary = "An example rockspec" }) + + write_file("missing_mandatory_field-1.0-1.rockspec", [[ + package="missing_mandatory_field" + version="1.0-1" + source = { + url = "http://example.com/foo.tar.gz" + } + ]]) + rockspec = fetch.load_local_rockspec("missing_mandatory_field-1.0-1.rockspec", true) + assert.same(rockspec.name, "missing_mandatory_field") + assert.same(rockspec.version, "1.0-1") + assert.same(rockspec.source.url, "http://example.com/foo.tar.gz") + + write_file("unknown_field-1.0-1.rockspec", [[ + package="unknown_field" + version="1.0-1" + source = { + url = "http://example.com/foo.tar.gz" + } + unknown="foo" + ]]) + rockspec = fetch.load_local_rockspec("unknown_field-1.0-1.rockspec", true) + assert.same(rockspec.name, "unknown_field") + assert.same(rockspec.version, "1.0-1") + assert.same(rockspec.source.url, "http://example.com/foo.tar.gz") + + -- The previous calls fail if the detailed checking is done + path.use_tree(testing_paths.testing_tree) + assert.falsy(fetch.load_local_rockspec("missing_mandatory_field-1.0-1.rockspec")) + assert.falsy(fetch.load_local_rockspec("unknown_field-1.0-1.rockspec")) + end, finally) end) it("returns a table representing the rockspec from the given file", function() - path.use_tree(testing_paths.testing_tree) - local rockspec = fetch.load_local_rockspec(testing_paths.fixtures_dir .. "/a_rock-1.0-1.rockspec") - assert.same(rockspec.name, "a_rock") - assert.same(rockspec.version, "1.0-1") - assert.same(rockspec.description, { summary = "An example rockspec" }) - assert.same(rockspec.source.url, "http://localhost:8080/file/a_rock.lua") + test_env.run_in_tmp(function() + path.use_tree(testing_paths.testing_tree) + local rockspec = fetch.load_local_rockspec(testing_paths.fixtures_dir .. "/a_rock-1.0-1.rockspec") + assert.same(rockspec.name, "a_rock") + assert.same(rockspec.version, "1.0-1") + assert.same(rockspec.description, { summary = "An example rockspec" }) + assert.same(rockspec.source.url, "http://localhost:8080/file/a_rock.lua") + end, finally) end) it("returns false if the rockspec in invalid", function() @@ -285,70 +291,81 @@ describe("luarocks fetch #unit #mock", function() end) it("returns false if the rockspec doesn't pass the type checking", function() - write_file("type_mismatch_string-1.0-1.rockspec", [[ - package="type_mismatch_version" - version=1.0 - ]], finally) - assert.falsy(fetch.load_local_rockspec("type_mismatch_string-1.0-1.rockspec")) + test_env.run_in_tmp(function() + write_file("type_mismatch_string-1.0-1.rockspec", [[ + package="type_mismatch_version" + version=1.0 + ]]) + assert.falsy(fetch.load_local_rockspec("type_mismatch_string-1.0-1.rockspec")) + end, finally) end) it("returns false if the rockspec file name is not right", function() - write_file("invalid_rockspec_name.rockspec", [[ - package="invalid_rockspec_name" - version="1.0-1" - source = { - url = "http://example.com/foo.tar.gz" - } - build = { - - } - ]], finally) - assert.falsy(fetch.load_local_rockspec("invalid_rockspec_name.rockspec")) + test_env.run_in_tmp(function() + write_file("invalid_rockspec_name.rockspec", [[ + package="invalid_rockspec_name" + version="1.0-1" + source = { + url = "http://example.com/foo.tar.gz" + } + build = { + + } + ]]) + assert.falsy(fetch.load_local_rockspec("invalid_rockspec_name.rockspec")) + end, finally) end) it("returns false if the version in the rockspec file name doesn't match the version declared in the rockspec", function() - write_file("inconsistent_versions-1.0-1.rockspec", [[ - package="inconsistent_versions" - version="1.0-2" - source = { - url = "http://example.com/foo.tar.gz" - } - build = { - - } - ]], finally) - assert.falsy(fetch.load_local_rockspec("inconsistent_versions-1.0-1.rockspec")) + test_env.run_in_tmp(function() + write_file("inconsistent_versions-1.0-1.rockspec", [[ + package="inconsistent_versions" + version="1.0-2" + source = { + url = "http://example.com/foo.tar.gz" + } + build = { + + } + ]]) + assert.falsy(fetch.load_local_rockspec("inconsistent_versions-1.0-1.rockspec")) + end, finally) end) end) describe("fetch.load_rockspec", function() + it("returns a table containing the requested rockspec by downloading it into a temporary directory", function() - path.use_tree(testing_paths.testing_tree) - local rockspec = fetch.load_rockspec("http://localhost:8080/file/a_rock-1.0-1.rockspec") - assert.same(rockspec.name, "a_rock") - assert.same(rockspec.version, "1.0-1") - assert.same(rockspec.description, { summary = "An example rockspec" }) - assert.same(rockspec.source.url, "http://localhost:8080/file/a_rock.lua") - rockspec = fetch.load_rockspec(testing_paths.fixtures_dir .. "/a_rock-1.0-1.rockspec") - assert.same(rockspec.name, "a_rock") - assert.same(rockspec.version, "1.0-1") - assert.same(rockspec.description, { summary = "An example rockspec" }) - assert.same(rockspec.source.url, "http://localhost:8080/file/a_rock.lua") + test_env.run_in_tmp(function() + path.use_tree(testing_paths.testing_tree) + local rockspec = fetch.load_rockspec("http://localhost:8080/file/a_rock-1.0-1.rockspec") + assert.same(rockspec.name, "a_rock") + assert.same(rockspec.version, "1.0-1") + assert.same(rockspec.description, { summary = "An example rockspec" }) + assert.same(rockspec.source.url, "http://localhost:8080/file/a_rock.lua") + rockspec = fetch.load_rockspec(testing_paths.fixtures_dir .. "/a_rock-1.0-1.rockspec") + assert.same(rockspec.name, "a_rock") + assert.same(rockspec.version, "1.0-1") + assert.same(rockspec.description, { summary = "An example rockspec" }) + assert.same(rockspec.source.url, "http://localhost:8080/file/a_rock.lua") + end, finally) end) it("returns a table containing the requested rockspec by downloading it into a given directory", function() - local tmpdir = get_tmp_path() - lfs.mkdir(tmpdir) - - path.use_tree(testing_paths.testing_tree) - local rockspec = fetch.load_rockspec("http://localhost:8080/file/a_rock-1.0-1.rockspec", tmpdir) - assert.same(rockspec.name, "a_rock") - assert.same(rockspec.version, "1.0-1") - assert.same(rockspec.description, { summary = "An example rockspec" }) - assert.same(rockspec.source.url, "http://localhost:8080/file/a_rock.lua") - assert.truthy(lfs.attributes(tmpdir .. "/a_rock-1.0-1.rockspec")) + test_env.run_in_tmp(function() + local tmpdir = get_tmp_path() + lfs.mkdir(tmpdir) + + path.use_tree(testing_paths.testing_tree) + local rockspec = fetch.load_rockspec("http://localhost:8080/file/a_rock-1.0-1.rockspec", tmpdir) + assert.same(rockspec.name, "a_rock") + assert.same(rockspec.version, "1.0-1") + assert.same(rockspec.description, { summary = "An example rockspec" }) + assert.same(rockspec.source.url, "http://localhost:8080/file/a_rock.lua") + assert.truthy(lfs.attributes(tmpdir .. "/a_rock-1.0-1.rockspec")) - lfs.rmdir(tmpdir) + lfs.rmdir(tmpdir) + end, finally) end) it("returns false if the given download directory doesn't exist", function() @@ -361,89 +378,84 @@ describe("luarocks fetch #unit #mock", function() end) describe("fetch.get_sources", function() - local tmpdir - local olddir - - before_each(function() - tmpdir = get_tmp_path() - olddir = lfs.currentdir() - lfs.mkdir(tmpdir) - lfs.chdir(tmpdir) - fs.change_dir(tmpdir) - end) - - after_each(function() - if olddir then - lfs.chdir(olddir) - if tmpdir then - lfs.rmdir(tmpdir) - end - end - end) it("downloads the sources for building a rock and returns the resulting source filename and its parent directory", function() - local rockspec = assert(fetch.load_rockspec("http://localhost:8080/file/a_rock-1.0-1.rockspec")) - local file, dir = fetch.get_sources(rockspec, false) - assert.truthy(are_same_files(dir .. "/a_rock.lua", file)) + test_env.run_in_tmp(function() + local rockspec = assert(fetch.load_rockspec("http://localhost:8080/file/a_rock-1.0-1.rockspec")) + local file, dirname = fetch.get_sources(rockspec, false) + assert.truthy(are_same_files(dirname .. "/a_rock.lua", file)) + end, finally) end) it("downloads the sources for building a rock into a given directory and returns the resulting source filename and its parent directory", function() - local tmpdir = get_tmp_path() - lfs.mkdir(tmpdir) - local rockspec = assert(fetch.load_rockspec("http://localhost:8080/file/a_rock-1.0-1.rockspec")) - local file, dir = fetch.get_sources(rockspec, false, tmpdir) - assert.truthy(are_same_files(tmpdir, dir)) - assert.truthy(are_same_files(dir .. "/a_rock.lua", file)) - lfs.rmdir(tmpdir) + test_env.run_in_tmp(function() + local tmpdir = get_tmp_path() + lfs.mkdir(tmpdir) + local rockspec = assert(fetch.load_rockspec("http://localhost:8080/file/a_rock-1.0-1.rockspec")) + local file, dirname = fetch.get_sources(rockspec, false, tmpdir) + assert.truthy(are_same_files(tmpdir, dirname)) + assert.truthy(are_same_files(dirname .. "/a_rock.lua", file)) + lfs.rmdir(tmpdir) + end, finally) end) it("downloads the sources for building a rock, extracts the downloaded tarball and returns the resulting source filename and its parent directory", function() - local rockspec = assert(fetch.load_rockspec("http://localhost:8080/file/busted_project-0.1-1.rockspec")) - local file, dir = fetch.get_sources(rockspec, true) - assert.truthy(are_same_files(dir .. "/busted_project-0.1.tar.gz", file)) - assert.truthy(lfs.attributes(dir .. "/busted_project")) - assert.truthy(lfs.attributes(dir .. "/busted_project/sum.lua")) - assert.truthy(lfs.attributes(dir .. "/busted_project/spec/sum_spec.lua")) + test_env.run_in_tmp(function() + local rockspec = assert(fetch.load_rockspec("http://localhost:8080/file/busted_project-0.1-1.rockspec")) + local file, dirname = fetch.get_sources(rockspec, true) + assert.truthy(are_same_files(dirname .. "/busted_project-0.1.tar.gz", file)) + assert.truthy(lfs.attributes(dirname .. "/busted_project")) + assert.truthy(lfs.attributes(dirname .. "/busted_project/sum.lua")) + assert.truthy(lfs.attributes(dirname .. "/busted_project/spec/sum_spec.lua")) + end, finally) end) it("returns false and does nothing if the destination directory doesn't exist", function() - local rockspec = assert(fetch.load_rockspec("http://localhost:8080/file/a_rock-1.0-1.rockspec")) - assert.falsy(fetch.get_sources(rockspec, false, "nonexistent")) + test_env.run_in_tmp(function() + local rockspec = assert(fetch.load_rockspec("http://localhost:8080/file/a_rock-1.0-1.rockspec")) + assert.falsy(fetch.get_sources(rockspec, false, "nonexistent")) + end, finally) end) it("returns false and does nothing if the rockspec source url is invalid", function() - write_file("invalid_url-1.0-1.rockspec", [[ - package="invalid_url" - version="1.0-1" - source = { - url = "http://localhost:8080/file/nonexistent" - } - build = { - - } - ]], finally) - local rockspec = assert(fetch.load_rockspec("invalid_url-1.0-1.rockspec")) - assert.falsy(fetch.get_sources(rockspec, false)) + test_env.run_in_tmp(function(tmpdir) + write_file(tmpdir .. "/invalid_url-1.0-1.rockspec", [[ + package="invalid_url" + version="1.0-1" + source = { + url = "http://localhost:8080/file/nonexistent" + } + build = { + + } + ]]) + local rockspec = assert(fetch.load_rockspec(tmpdir .. "/invalid_url-1.0-1.rockspec")) + assert.falsy(fetch.get_sources(rockspec, false)) + end, finally) end) it("returns false and does nothing if the downloaded rockspec has an invalid md5 checksum", function() - write_file("invalid_checksum-1.0-1.rockspec", [[ - package="invalid_checksum" - version="1.0-1" - source = { - url = "http://localhost:8080/file/a_rock.lua", - md5 = "invalid" - } - build = { - - } - ]], finally) - local rockspec = assert(fetch.load_rockspec("invalid_checksum-1.0-1.rockspec")) - assert.falsy(fetch.get_sources(rockspec, false)) + test_env.run_in_tmp(function() + write_file("invalid_checksum-1.0-1.rockspec", [[ + package="invalid_checksum" + version="1.0-1" + source = { + url = "http://localhost:8080/file/a_rock.lua", + md5 = "invalid" + } + build = { + + } + ]]) + local rockspec = assert(fetch.load_rockspec("invalid_checksum-1.0-1.rockspec")) + assert.falsy(fetch.get_sources(rockspec, false)) + end, finally) end) end) describe("fetch_sources #unix #git", function() + local git_repo = require("spec.util.git_repo") + local git setup(function() diff --git a/spec/util/quick.lua b/spec/util/quick.lua index 6b86bfa9..cb4bb4cf 100644 --- a/spec/util/quick.lua +++ b/spec/util/quick.lua @@ -429,7 +429,7 @@ function quick.compile(filename, env) end end end - write([=[ end) ]=]) + write([=[ end, finally) ]=]) write([=[ end ]=]) local program = table.concat(code, "\n") diff --git a/spec/util/test_env.lua b/spec/util/test_env.lua index 41787cb7..f311a370 100644 --- a/spec/util/test_env.lua +++ b/spec/util/test_env.lua @@ -193,10 +193,25 @@ function test_env.run_in_tmp(f, finally) lfs.mkdir(tmpdir) lfs.chdir(tmpdir) + if not finally then + error("run_in_tmp needs a finally argument") + end + + -- for unit tests, so that current dir known by luarocks.fs (when running with non-lfs) + -- is synchronized with actual lfs (system) current dir + local fs = require("luarocks.fs") + if not fs.change_dir then + local cfg = require("luarocks.core.cfg") + cfg.init() + fs.init() + end + fs.change_dir(tmpdir) + if finally then finally(function() lfs.chdir(olddir) lfs.rmdir(tmpdir) + fs.change_dir(olddir) end) end -- cgit v1.2.3-55-g6feb