From bec4a9cbf72c8e392163f50f7b6bbb18763d9f90 Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Mon, 6 Jun 2022 15:28:14 -0300 Subject: loader.which: new option for searching package.path and cpath Adds a new second argument, `where`, a string which indicates places to search for the module. If `where` contains "l", it will search using the LuaRocks loader; if it contains "p", it will look in the filesystem using package.path and package.cpath. You can use both at the same time. If successful, it will return four values. * If found using the LuaRocks loader, it will return: * filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so"), * rock name * rock version * "l" to indicate the match comes from the loader. * If found scanning package.path and package.cpath, it will return: * filename of the module (eg. "/usr/local/lib/lua/5.1/socket/core.so"), * "path" or "cpath" * nil * "p" to indicate the match comes from scanning package.path and cpath. If unsuccessful, nothing is returned. --- spec/loader_spec.lua | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'spec') diff --git a/spec/loader_spec.lua b/spec/loader_spec.lua index 35179080..c8526117 100644 --- a/spec/loader_spec.lua +++ b/spec/loader_spec.lua @@ -4,10 +4,21 @@ local testing_paths = test_env.testing_paths local write_file = test_env.write_file describe("luarocks.loader", function() + + before_each(function() + test_env.setup_specs() + end) + describe("#unit", function() it("starts", function() assert(run.lua_bool([[-e "require 'luarocks.loader'; print(package.loaded['luarocks.loaded'])"]])) end) + + describe("which", function() + it("finds modules using package.path", function() + assert(run.lua_bool([[-e "loader = require 'luarocks.loader'; local x,y,z,p = loader.which('luarocks.loader', 'p'); assert(p == 'p')"]])) + end) + end) end) describe("#integration", function() @@ -21,13 +32,13 @@ describe("luarocks.loader", function() url = "file://]] .. tmpdir:gsub("\\", "/") .. [[/rock_b_01.lua" } build = { - type = "builtin", + type = "builtin", modules = { rock_b = "rock_b_01.lua" } } ]], finally) - + write_file("rock_b_10.lua", "print('ROCK B 1.0'); return {}", finally) write_file("rock_b-1.0-1.rockspec", [[ package = "rock_b" @@ -36,13 +47,13 @@ describe("luarocks.loader", function() url = "file://]] .. tmpdir:gsub("\\", "/") .. [[/rock_b_10.lua" } build = { - type = "builtin", + type = "builtin", modules = { rock_b = "rock_b_10.lua" } } ]], finally) - + write_file("rock_a.lua", "require('rock_b'); return {}", finally) write_file("rock_a-2.0-1.rockspec", [[ package = "rock_a" @@ -54,7 +65,7 @@ describe("luarocks.loader", function() "rock_b < 1.0", } build = { - type = "builtin", + type = "builtin", modules = { rock_a = "rock_a.lua" } @@ -64,9 +75,9 @@ describe("luarocks.loader", function() print(run.luarocks("make --server=" .. testing_paths.fixtures_dir .. "/a_repo --tree=" .. testing_paths.testing_tree .. " ./rock_b-0.1-1.rockspec")) print(run.luarocks("make --server=" .. testing_paths.fixtures_dir .. "/a_repo --tree=" .. testing_paths.testing_tree .. " ./rock_b-1.0-1.rockspec --keep")) print(run.luarocks("make --server=" .. testing_paths.fixtures_dir .. "/a_repo --tree=" .. testing_paths.testing_tree .. " ./rock_a-2.0-1.rockspec")) - + local output = run.lua([[-e "require 'luarocks.loader'; require('rock_a')"]]) - + assert.matches("ROCK B 0.1", output, 1, true) end) end) -- cgit v1.2.3-55-g6feb