diff options
author | Hisham Muhammad <hisham@gobolinux.org> | 2018-06-19 09:58:36 -0300 |
---|---|---|
committer | Hisham Muhammad <hisham@gobolinux.org> | 2018-07-01 15:51:09 -0300 |
commit | a55f09fae00cb3c575f926eda4d76e81ff37b2ba (patch) | |
tree | c36d2d475549f76805b3b9bfd08f1429f032e5ac | |
parent | 392f67d65a950fcf08a08d10d21e80f359160262 (diff) | |
download | luarocks-a55f09fae00cb3c575f926eda4d76e81ff37b2ba.tar.gz luarocks-a55f09fae00cb3c575f926eda4d76e81ff37b2ba.tar.bz2 luarocks-a55f09fae00cb3c575f926eda4d76e81ff37b2ba.zip |
fs: make module loading side-effect free, require explicit init()
-rw-r--r-- | spec/fetch_spec.lua | 2 | ||||
-rw-r--r-- | spec/fs_spec.lua | 1 | ||||
-rw-r--r-- | spec/test_spec.lua | 3 | ||||
-rw-r--r-- | spec/tools_spec.lua | 3 | ||||
-rw-r--r-- | src/luarocks/command_line.lua | 2 | ||||
-rw-r--r-- | src/luarocks/core/cfg.lua | 8 | ||||
-rw-r--r-- | src/luarocks/fs.lua | 107 | ||||
-rw-r--r-- | src/luarocks/util.lua | 4 |
8 files changed, 80 insertions, 50 deletions
diff --git a/spec/fetch_spec.lua b/spec/fetch_spec.lua index ce4cfb56..6e00bc22 100644 --- a/spec/fetch_spec.lua +++ b/spec/fetch_spec.lua | |||
@@ -3,6 +3,7 @@ local git_repo = require("spec.util.git_repo") | |||
3 | 3 | ||
4 | test_env.unload_luarocks() | 4 | test_env.unload_luarocks() |
5 | test_env.setup_specs() | 5 | test_env.setup_specs() |
6 | local fs = require("luarocks.fs") | ||
6 | local fetch = require("luarocks.fetch") | 7 | local fetch = require("luarocks.fetch") |
7 | local fs = require("luarocks.fs") | 8 | local fs = require("luarocks.fs") |
8 | local path = require("luarocks.path") | 9 | local path = require("luarocks.path") |
@@ -20,6 +21,7 @@ describe("Luarocks fetch test #unit #mock", function() | |||
20 | local runner | 21 | local runner |
21 | 22 | ||
22 | setup(function() | 23 | setup(function() |
24 | fs.init() | ||
23 | test_env.mock_server_init() | 25 | test_env.mock_server_init() |
24 | 26 | ||
25 | runner = require("luacov.runner") | 27 | runner = require("luacov.runner") |
diff --git a/spec/fs_spec.lua b/spec/fs_spec.lua index 9b2aa01b..b8091f75 100644 --- a/spec/fs_spec.lua +++ b/spec/fs_spec.lua | |||
@@ -55,6 +55,7 @@ describe("Luarocks fs test #unit", function() | |||
55 | local runner | 55 | local runner |
56 | 56 | ||
57 | setup(function() | 57 | setup(function() |
58 | fs.init() | ||
58 | runner = require("luacov.runner") | 59 | runner = require("luacov.runner") |
59 | runner.init(testing_paths.testrun_dir .. "/luacov.config") | 60 | runner.init(testing_paths.testrun_dir .. "/luacov.config") |
60 | runner.tick = true | 61 | runner.tick = true |
diff --git a/spec/test_spec.lua b/spec/test_spec.lua index 0111f89a..7a2399a9 100644 --- a/spec/test_spec.lua +++ b/spec/test_spec.lua | |||
@@ -82,6 +82,7 @@ end) | |||
82 | test_env.unload_luarocks() | 82 | test_env.unload_luarocks() |
83 | 83 | ||
84 | local fs = require("luarocks.fs") | 84 | local fs = require("luarocks.fs") |
85 | local cfg = require("luarocks.core.cfg") | ||
85 | local path = require("luarocks.path") | 86 | local path = require("luarocks.path") |
86 | local test = require("luarocks.test") | 87 | local test = require("luarocks.test") |
87 | local test_busted = require("luarocks.test.busted") | 88 | local test_busted = require("luarocks.test.busted") |
@@ -91,6 +92,8 @@ describe("LuaRocks test #unit", function() | |||
91 | local runner | 92 | local runner |
92 | 93 | ||
93 | setup(function() | 94 | setup(function() |
95 | cfg.init() | ||
96 | fs.init() | ||
94 | runner = require("luacov.runner") | 97 | runner = require("luacov.runner") |
95 | runner.init(testing_paths.testrun_dir .. "/luacov.config") | 98 | runner.init(testing_paths.testrun_dir .. "/luacov.config") |
96 | runner.tick = true | 99 | runner.tick = true |
diff --git a/spec/tools_spec.lua b/spec/tools_spec.lua index 8805d703..10dafb0b 100644 --- a/spec/tools_spec.lua +++ b/spec/tools_spec.lua | |||
@@ -5,7 +5,7 @@ local write_file = test_env.write_file | |||
5 | 5 | ||
6 | test_env.unload_luarocks() | 6 | test_env.unload_luarocks() |
7 | local fs = require("luarocks.fs") | 7 | local fs = require("luarocks.fs") |
8 | local patch = package.loaded["luarocks.tools.patch"] | 8 | local patch = require("luarocks.tools.patch") |
9 | 9 | ||
10 | local lao = | 10 | local lao = |
11 | [[The Nameless is the origin of Heaven and Earth; | 11 | [[The Nameless is the origin of Heaven and Earth; |
@@ -148,6 +148,7 @@ describe("Luarocks patch test #unit", function() | |||
148 | local runner | 148 | local runner |
149 | 149 | ||
150 | setup(function() | 150 | setup(function() |
151 | fs.init() | ||
151 | runner = require("luacov.runner") | 152 | runner = require("luacov.runner") |
152 | runner.init(testing_paths.testrun_dir .. "/luacov.config") | 153 | runner.init(testing_paths.testrun_dir .. "/luacov.config") |
153 | runner.tick = true | 154 | runner.tick = true |
diff --git a/src/luarocks/command_line.lua b/src/luarocks/command_line.lua index 5ba5ae1b..b542fb9c 100644 --- a/src/luarocks/command_line.lua +++ b/src/luarocks/command_line.lua | |||
@@ -93,6 +93,8 @@ function command_line.run_command(...) | |||
93 | die(flags.ERROR.." See --help.") | 93 | die(flags.ERROR.." See --help.") |
94 | end | 94 | end |
95 | 95 | ||
96 | fs.init() | ||
97 | |||
96 | if flags["from"] then flags["server"] = flags["from"] end | 98 | if flags["from"] then flags["server"] = flags["from"] end |
97 | if flags["only-from"] then flags["only-server"] = flags["only-from"] end | 99 | if flags["only-from"] then flags["only-server"] = flags["only-from"] end |
98 | if flags["only-sources-from"] then flags["only-sources"] = flags["only-sources-from"] end | 100 | if flags["only-sources-from"] then flags["only-sources"] = flags["only-sources-from"] end |
diff --git a/src/luarocks/core/cfg.lua b/src/luarocks/core/cfg.lua index 38b10f6e..00efeaf3 100644 --- a/src/luarocks/core/cfg.lua +++ b/src/luarocks/core/cfg.lua | |||
@@ -763,4 +763,12 @@ function cfg.is_platform(query) | |||
763 | end | 763 | end |
764 | end | 764 | end |
765 | 765 | ||
766 | function cfg.each_platform() | ||
767 | local i = 0 | ||
768 | return function() | ||
769 | i = i + 1 | ||
770 | return cfg.platforms[i] | ||
771 | end | ||
772 | end | ||
773 | |||
766 | return cfg | 774 | return cfg |
diff --git a/src/luarocks/fs.lua b/src/luarocks/fs.lua index 7322e552..999572b8 100644 --- a/src/luarocks/fs.lua +++ b/src/luarocks/fs.lua | |||
@@ -16,62 +16,73 @@ local cfg = require("luarocks.core.cfg") | |||
16 | local pack = table.pack or function(...) return { n = select("#", ...), ... } end | 16 | local pack = table.pack or function(...) return { n = select("#", ...), ... } end |
17 | local unpack = table.unpack or unpack | 17 | local unpack = table.unpack or unpack |
18 | 18 | ||
19 | local old_popen, old_exec | 19 | do |
20 | fs.verbose = function() -- patch io.popen and os.execute to display commands in verbose mode | 20 | local old_popen, old_execute |
21 | if old_popen or old_exec then return end | 21 | |
22 | old_popen = io.popen | 22 | -- patch io.popen and os.execute to display commands in verbose mode |
23 | io.popen = function(one, two) | 23 | function fs.verbose() |
24 | if two == nil then | 24 | if old_popen or old_execute then return end |
25 | print("\nio.popen: ", one) | 25 | old_popen = io.popen |
26 | else | 26 | io.popen = function(one, two) |
27 | print("\nio.popen: ", one, "Mode:", two) | 27 | if two == nil then |
28 | end | 28 | print("\nio.popen: ", one) |
29 | return old_popen(one, two) | 29 | else |
30 | end | 30 | print("\nio.popen: ", one, "Mode:", two) |
31 | 31 | end | |
32 | old_exec = os.execute | 32 | return old_popen(one, two) |
33 | os.execute = function(cmd) | 33 | end |
34 | -- redact api keys if present | ||
35 | print("\nos.execute: ", (cmd:gsub("(/api/[^/]+/)([^/]+)/", function(cap, key) return cap.."<redacted>/" end)) ) | ||
36 | local code = pack(old_exec(cmd)) | ||
37 | print("Results: "..tostring(code.n)) | ||
38 | for i = 1,code.n do | ||
39 | print(" "..tostring(i).." ("..type(code[i]).."): "..tostring(code[i])) | ||
40 | end | ||
41 | return unpack(code, 1, code.n) | ||
42 | end | ||
43 | end | ||
44 | if cfg.verbose then fs.verbose() end | ||
45 | 34 | ||
46 | local function load_fns(fs_table) | 35 | old_execute = os.execute |
47 | for name, fn in pairs(fs_table) do | 36 | os.execute = function(cmd) |
48 | if not fs[name] then | 37 | -- redact api keys if present |
49 | fs[name] = fn | 38 | print("\nos.execute: ", (cmd:gsub("(/api/[^/]+/)([^/]+)/", function(cap, key) return cap.."<redacted>/" end)) ) |
39 | local code = pack(old_execute(cmd)) | ||
40 | print("Results: "..tostring(code.n)) | ||
41 | for i = 1,code.n do | ||
42 | print(" "..tostring(i).." ("..type(code[i]).."): "..tostring(code[i])) | ||
43 | end | ||
44 | return unpack(code, 1, code.n) | ||
50 | end | 45 | end |
51 | end | 46 | end |
52 | end | 47 | end |
53 | 48 | ||
54 | -- Load platform-specific functions | 49 | do |
55 | local loaded_platform = nil | 50 | local function load_fns(fs_table) |
56 | for _, platform in ipairs(cfg.platforms) do | 51 | for name, fn in pairs(fs_table) do |
57 | local ok, fs_plat = pcall(require, "luarocks.fs."..platform) | 52 | if not fs[name] then |
58 | if ok and fs_plat then | 53 | fs[name] = fn |
59 | loaded_platform = platform | 54 | end |
60 | load_fns(fs_plat) | 55 | end |
61 | break | ||
62 | end | 56 | end |
63 | end | ||
64 | 57 | ||
65 | -- Load platform-independent pure-Lua functionality | 58 | function fs.init() |
66 | local fs_lua = require("luarocks.fs.lua") | 59 | if fs.current_dir then |
67 | load_fns(fs_lua) | 60 | -- already initialized |
61 | return | ||
62 | end | ||
63 | |||
64 | -- Load platform-specific functions | ||
65 | local loaded_platform = nil | ||
66 | for platform in cfg.each_platform() do | ||
67 | local ok, fs_plat = pcall(require, "luarocks.fs."..platform) | ||
68 | if ok and fs_plat then | ||
69 | loaded_platform = platform | ||
70 | load_fns(fs_plat) | ||
71 | break | ||
72 | end | ||
73 | end | ||
68 | 74 | ||
69 | -- Load platform-specific fallbacks for missing Lua modules | 75 | -- Load platform-independent pure-Lua functionality |
70 | local ok, fs_plat_tools = pcall(require, "luarocks.fs."..loaded_platform..".tools") | 76 | local fs_lua = require("luarocks.fs.lua") |
71 | if ok and fs_plat_tools then | 77 | load_fns(fs_lua) |
72 | load_fns(fs_plat_tools) | ||
73 | load_fns(require("luarocks.fs.tools")) | ||
74 | end | ||
75 | 78 | ||
79 | -- Load platform-specific fallbacks for missing Lua modules | ||
80 | local ok, fs_plat_tools = pcall(require, "luarocks.fs."..loaded_platform..".tools") | ||
81 | if ok and fs_plat_tools then | ||
82 | load_fns(fs_plat_tools) | ||
83 | load_fns(require("luarocks.fs.tools")) | ||
84 | end | ||
85 | end | ||
86 | end | ||
76 | 87 | ||
77 | return fs | 88 | return fs |
diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index ee4a803a..8058a407 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua | |||
@@ -59,7 +59,9 @@ end | |||
59 | -- Functions are executed in the inverse order they were scheduled. | 59 | -- Functions are executed in the inverse order they were scheduled. |
60 | function util.run_scheduled_functions() | 60 | function util.run_scheduled_functions() |
61 | local fs = require("luarocks.fs") | 61 | local fs = require("luarocks.fs") |
62 | fs.change_dir_to_root() | 62 | if fs.change_dir_to_root then |
63 | fs.change_dir_to_root() | ||
64 | end | ||
63 | for i = #scheduled_functions, 1, -1 do | 65 | for i = #scheduled_functions, 1, -1 do |
64 | local item = scheduled_functions[i] | 66 | local item = scheduled_functions[i] |
65 | item.fn(unpack(item.args)) | 67 | item.fn(unpack(item.args)) |