diff options
| author | George Roman <george.roman.99@gmail.com> | 2018-07-07 18:49:41 +0300 |
|---|---|---|
| committer | Hisham Muhammad <hisham@gobolinux.org> | 2018-07-16 20:34:30 -0300 |
| commit | 4b3ee931cd8ecc5937811cd8e9316d79c0c9c00e (patch) | |
| tree | 9b5f36a8f75df296b1bace3413eb4967d9325729 | |
| parent | 1b3b6525a4313404af84fce0fbbc29695e664f73 (diff) | |
| download | luarocks-4b3ee931cd8ecc5937811cd8e9316d79c0c9c00e.tar.gz luarocks-4b3ee931cd8ecc5937811cd8e9316d79c0c9c00e.tar.bz2 luarocks-4b3ee931cd8ecc5937811cd8e9316d79c0c9c00e.zip | |
Add general improvements to the fs module
| -rw-r--r-- | spec/fs_spec.lua | 36 | ||||
| -rw-r--r-- | spec/remove_spec.lua | 34 | ||||
| -rw-r--r-- | src/luarocks/fs/lua.lua | 17 | ||||
| -rw-r--r-- | src/luarocks/fs/tools.lua | 7 | ||||
| -rw-r--r-- | src/luarocks/fs/win32/tools.lua | 11 |
5 files changed, 61 insertions, 44 deletions
diff --git a/spec/fs_spec.lua b/spec/fs_spec.lua index eb8425e2..66453404 100644 --- a/spec/fs_spec.lua +++ b/spec/fs_spec.lua | |||
| @@ -161,11 +161,19 @@ describe("Luarocks fs test #unit", function() | |||
| 161 | it("does nothing if the argument is a file", function() | 161 | it("does nothing if the argument is a file", function() |
| 162 | tmpfile1 = get_tmp_path() | 162 | tmpfile1 = get_tmp_path() |
| 163 | create_file(tmpfile1) | 163 | create_file(tmpfile1) |
| 164 | assert.falsy(pcall(fs.dir_iterator, tmpfile1)) | 164 | local crt = coroutine.create(fs.dir_iterator) |
| 165 | while coroutine.status(crt) ~= "dead" do | ||
| 166 | local ok, val = coroutine.resume(crt, tmpfile1) | ||
| 167 | assert.falsy(ok and res) | ||
| 168 | end | ||
| 165 | end) | 169 | end) |
| 166 | 170 | ||
| 167 | it("does nothing if the argument is invalid", function() | 171 | it("does nothing if the argument is invalid", function() |
| 168 | assert.falsy(pcall(fs.dir_iterator, "/nonexistent")) | 172 | local crt = coroutine.create(fs.dir_iterator) |
| 173 | while coroutine.status(crt) ~= "dead" do | ||
| 174 | local ok, val = coroutine.resume(crt, "/nonexistent") | ||
| 175 | assert.falsy(ok and res) | ||
| 176 | end | ||
| 169 | end) | 177 | end) |
| 170 | end) | 178 | end) |
| 171 | 179 | ||
| @@ -530,11 +538,13 @@ describe("Luarocks fs test #unit", function() | |||
| 530 | tmpdir = get_tmp_path() | 538 | tmpdir = get_tmp_path() |
| 531 | lfs.mkdir(tmpdir) | 539 | lfs.mkdir(tmpdir) |
| 532 | assert.truthy(fs.change_dir(tmpdir)) | 540 | assert.truthy(fs.change_dir(tmpdir)) |
| 533 | local success = fs.change_dir_to_root() | 541 | assert.truthy(fs.change_dir_to_root()) |
| 534 | if not is_win then | 542 | if is_win then |
| 535 | assert.truthy(success) | 543 | local curr_dir = fs.current_dir() |
| 544 | assert.truthy(curr_dir == "C:\\" or curr_dir == "/") | ||
| 545 | else | ||
| 546 | assert.same("/", fs.current_dir()) | ||
| 536 | end | 547 | end |
| 537 | assert.same("/", fs.current_dir()) | ||
| 538 | end) | 548 | end) |
| 539 | 549 | ||
| 540 | it("returns false and does nothing if the current directory is not valid #unix", function() | 550 | it("returns false and does nothing if the current directory is not valid #unix", function() |
| @@ -749,18 +759,14 @@ describe("Luarocks fs test #unit", function() | |||
| 749 | end) | 759 | end) |
| 750 | 760 | ||
| 751 | it("does nothing if the argument is nonexistent", function() | 761 | it("does nothing if the argument is nonexistent", function() |
| 752 | assert.falsy(pcall(fs.list_dir("/nonexistent"))) | 762 | assert.same(fs.list_dir("/nonexistent"), {}) |
| 753 | end) | 763 | end) |
| 754 | 764 | ||
| 755 | it("does nothing if the argument doesn't have the proper permissions", function() | 765 | it("does nothing if the argument doesn't have the proper permissions", function() |
| 756 | tmpdir = get_tmp_path() | 766 | tmpdir = get_tmp_path() |
| 757 | lfs.mkdir(tmpdir) | 767 | lfs.mkdir(tmpdir) |
| 758 | make_unreadable(tmpdir) | 768 | make_unreadable(tmpdir) |
| 759 | if is_win then | 769 | assert.same(fs.list_dir(tmpdir), {}) |
| 760 | assert.same(fs.list_dir(tmpdir), {}) | ||
| 761 | else | ||
| 762 | assert.falsy(pcall(fs.list_dir, tmpdir)) | ||
| 763 | end | ||
| 764 | end) | 770 | end) |
| 765 | end) | 771 | end) |
| 766 | 772 | ||
| @@ -1024,11 +1030,7 @@ describe("Luarocks fs test #unit", function() | |||
| 1024 | tmpdir = get_tmp_path() | 1030 | tmpdir = get_tmp_path() |
| 1025 | lfs.mkdir(tmpdir) | 1031 | lfs.mkdir(tmpdir) |
| 1026 | make_unreadable(tmpdir) | 1032 | make_unreadable(tmpdir) |
| 1027 | if is_win then | 1033 | assert.same(fs.find(tmpdir), {}) |
| 1028 | assert.same(fs.find(tmpdir), {}) | ||
| 1029 | else | ||
| 1030 | assert.falsy(pcall(fs.find, tmpdir)) | ||
| 1031 | end | ||
| 1032 | end) | 1034 | end) |
| 1033 | end) | 1035 | end) |
| 1034 | 1036 | ||
diff --git a/spec/remove_spec.lua b/spec/remove_spec.lua index 55264a75..f43b55c6 100644 --- a/spec/remove_spec.lua +++ b/spec/remove_spec.lua | |||
| @@ -7,9 +7,9 @@ test_env.unload_luarocks() | |||
| 7 | 7 | ||
| 8 | local extra_rocks = { | 8 | local extra_rocks = { |
| 9 | "/abelhas-1.1-1.rockspec", | 9 | "/abelhas-1.1-1.rockspec", |
| 10 | "/lualogging-1.3.0-1.src.rock", | 10 | "/copas-2.0.1-1.src.rock", |
| 11 | "/luasocket-3.0rc1-2.src.rock", | 11 | "/coxpcall-1.16.0-1.src.rock", |
| 12 | "/luasocket-3.0rc1-2.rockspec" | 12 | "/coxpcall-1.16.0-1.rockspec" |
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | describe("LuaRocks remove tests #integration", function() | 15 | describe("LuaRocks remove tests #integration", function() |
| @@ -52,38 +52,38 @@ describe("LuaRocks remove tests #integration", function() | |||
| 52 | 52 | ||
| 53 | describe("LuaRocks remove more complex tests", function() | 53 | describe("LuaRocks remove more complex tests", function() |
| 54 | before_each(function() | 54 | before_each(function() |
| 55 | assert.is.truthy(test_env.need_rock("luasocket")) | 55 | assert.is.truthy(test_env.need_rock("coxpcall")) |
| 56 | end) | 56 | end) |
| 57 | 57 | ||
| 58 | it("LuaRocks remove fail, break dependencies", function() | 58 | it("LuaRocks remove fail, break dependencies", function() |
| 59 | assert.is.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/luasocket")) | 59 | assert.is.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/coxpcall")) |
| 60 | assert.is_true(run.luarocks_bool("build lualogging")) | 60 | assert.is_true(run.luarocks_bool("build copas")) |
| 61 | 61 | ||
| 62 | assert.is_false(run.luarocks_bool("remove luasocket")) | 62 | assert.is_false(run.luarocks_bool("remove coxpcall")) |
| 63 | assert.is.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/luasocket")) | 63 | assert.is.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/coxpcall")) |
| 64 | end) | 64 | end) |
| 65 | 65 | ||
| 66 | it("LuaRocks remove force", function() | 66 | it("LuaRocks remove force", function() |
| 67 | assert.is.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/luasocket")) | 67 | assert.is.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/coxpcall")) |
| 68 | assert.is_true(run.luarocks_bool("build lualogging")) | 68 | assert.is_true(run.luarocks_bool("build copas")) |
| 69 | 69 | ||
| 70 | local output = run.luarocks("remove --force luasocket") | 70 | local output = run.luarocks("remove --force coxpcall") |
| 71 | assert.is.falsy(lfs.attributes(testing_paths.testing_sys_rocks .. "/luasocket")) | 71 | assert.is.falsy(lfs.attributes(testing_paths.testing_sys_rocks .. "/coxpcall")) |
| 72 | assert.is.truthy(output:find("Checking stability of dependencies")) | 72 | assert.is.truthy(output:find("Checking stability of dependencies")) |
| 73 | end) | 73 | end) |
| 74 | 74 | ||
| 75 | it("LuaRocks remove force fast", function() | 75 | it("LuaRocks remove force fast", function() |
| 76 | assert.is.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/luasocket")) | 76 | assert.is.truthy(lfs.attributes(testing_paths.testing_sys_rocks .. "/coxpcall")) |
| 77 | assert.is_true(run.luarocks_bool("build lualogging")) | 77 | assert.is_true(run.luarocks_bool("build copas")) |
| 78 | 78 | ||
| 79 | local output = run.luarocks("remove --force-fast luasocket") | 79 | local output = run.luarocks("remove --force-fast coxpcall") |
| 80 | assert.is.falsy(lfs.attributes(testing_paths.testing_sys_rocks .. "/luasocket")) | 80 | assert.is.falsy(lfs.attributes(testing_paths.testing_sys_rocks .. "/coxpcall")) |
| 81 | assert.is.falsy(output:find("Checking stability of dependencies")) | 81 | assert.is.falsy(output:find("Checking stability of dependencies")) |
| 82 | end) | 82 | end) |
| 83 | end) | 83 | end) |
| 84 | 84 | ||
| 85 | it("#admin remove #ssh", function() | 85 | it("#admin remove #ssh", function() |
| 86 | assert.is_true(run.luarocks_admin_bool("--server=testing remove luasocket-3.0rc1-2.src.rock")) | 86 | assert.is_true(run.luarocks_admin_bool("--server=testing remove coxpcall-1.16.0-1.src.rock")) |
| 87 | end) | 87 | end) |
| 88 | 88 | ||
| 89 | it("#admin remove missing", function() | 89 | it("#admin remove missing", function() |
diff --git a/src/luarocks/fs/lua.lua b/src/luarocks/fs/lua.lua index 373dcbdd..9da3875b 100644 --- a/src/luarocks/fs/lua.lua +++ b/src/luarocks/fs/lua.lua | |||
| @@ -464,9 +464,13 @@ end | |||
| 464 | --- Internal implementation function for fs.dir. | 464 | --- Internal implementation function for fs.dir. |
| 465 | -- Yields a filename on each iteration. | 465 | -- Yields a filename on each iteration. |
| 466 | -- @param at string: directory to list | 466 | -- @param at string: directory to list |
| 467 | -- @return nil | 467 | -- @return nil or (nil and string): an error message on failure |
| 468 | function fs_lua.dir_iterator(at) | 468 | function fs_lua.dir_iterator(at) |
| 469 | for file in lfs.dir(at) do | 469 | local pok, iter, arg = pcall(lfs.dir, at) |
| 470 | if not pok then | ||
| 471 | return nil, iter | ||
| 472 | end | ||
| 473 | for file in iter, arg do | ||
| 470 | if file ~= "." and file ~= ".." then | 474 | if file ~= "." and file ~= ".." then |
| 471 | coroutine.yield(file) | 475 | coroutine.yield(file) |
| 472 | end | 476 | end |
| @@ -479,7 +483,11 @@ end | |||
| 479 | -- @param prefix string: Auxiliary prefix string to form pathname. | 483 | -- @param prefix string: Auxiliary prefix string to form pathname. |
| 480 | -- @param result table: Array of strings where results are collected. | 484 | -- @param result table: Array of strings where results are collected. |
| 481 | local function recursive_find(cwd, prefix, result) | 485 | local function recursive_find(cwd, prefix, result) |
| 482 | for file in lfs.dir(cwd) do | 486 | local pok, iter, arg = pcall(lfs.dir, cwd) |
| 487 | if not pok then | ||
| 488 | return nil | ||
| 489 | end | ||
| 490 | for file in iter, arg do | ||
| 483 | if file ~= "." and file ~= ".." then | 491 | if file ~= "." and file ~= ".." then |
| 484 | local item = prefix .. file | 492 | local item = prefix .. file |
| 485 | table.insert(result, item) | 493 | table.insert(result, item) |
| @@ -502,9 +510,6 @@ function fs_lua.find(at) | |||
| 502 | at = fs.current_dir() | 510 | at = fs.current_dir() |
| 503 | end | 511 | end |
| 504 | at = dir.normalize(at) | 512 | at = dir.normalize(at) |
| 505 | if not fs.is_dir(at) then | ||
| 506 | return {} | ||
| 507 | end | ||
| 508 | local result = {} | 513 | local result = {} |
| 509 | recursive_find(at, "", result) | 514 | recursive_find(at, "", result) |
| 510 | return result | 515 | return result |
diff --git a/src/luarocks/fs/tools.lua b/src/luarocks/fs/tools.lua index 0cfdc9f7..d2dc08ae 100644 --- a/src/luarocks/fs/tools.lua +++ b/src/luarocks/fs/tools.lua | |||
| @@ -82,7 +82,12 @@ end | |||
| 82 | -- Allows leaving a directory (e.g. for deleting it) in | 82 | -- Allows leaving a directory (e.g. for deleting it) in |
| 83 | -- a crossplatform way. | 83 | -- a crossplatform way. |
| 84 | function tools.change_dir_to_root() | 84 | function tools.change_dir_to_root() |
| 85 | local curr_dir = fs.current_dir() | ||
| 86 | if not curr_dir or not fs.is_dir(curr_dir) then | ||
| 87 | return false | ||
| 88 | end | ||
| 85 | table.insert(dir_stack, "/") | 89 | table.insert(dir_stack, "/") |
| 90 | return true | ||
| 86 | end | 91 | end |
| 87 | 92 | ||
| 88 | --- Change working directory to the previous in the directory stack. | 93 | --- Change working directory to the previous in the directory stack. |
| @@ -113,7 +118,7 @@ end | |||
| 113 | -- @param at string: directory to list | 118 | -- @param at string: directory to list |
| 114 | -- @return nil | 119 | -- @return nil |
| 115 | function tools.dir_iterator(at) | 120 | function tools.dir_iterator(at) |
| 116 | local pipe = io.popen(fs.command_at(at, fs.Q(vars.LS))) | 121 | local pipe = io.popen(fs.command_at(at, fs.Q(vars.LS), true)) |
| 117 | for file in pipe:lines() do | 122 | for file in pipe:lines() do |
| 118 | if file ~= "." and file ~= ".." then | 123 | if file ~= "." and file ~= ".." then |
| 119 | coroutine.yield(file) | 124 | coroutine.yield(file) |
diff --git a/src/luarocks/fs/win32/tools.lua b/src/luarocks/fs/win32/tools.lua index c267b316..de3d9031 100644 --- a/src/luarocks/fs/win32/tools.lua +++ b/src/luarocks/fs/win32/tools.lua | |||
| @@ -13,10 +13,15 @@ local vars = setmetatable({}, { __index = function(_,k) return cfg.variables[k] | |||
| 13 | --- Adds prefix to command to make it run from a directory. | 13 | --- Adds prefix to command to make it run from a directory. |
| 14 | -- @param directory string: Path to a directory. | 14 | -- @param directory string: Path to a directory. |
| 15 | -- @param cmd string: A command-line string. | 15 | -- @param cmd string: A command-line string. |
| 16 | -- @param exit_on_error bool: Exits immediately if entering the directory failed. | ||
| 16 | -- @return string: The command-line with prefix. | 17 | -- @return string: The command-line with prefix. |
| 17 | function tools.command_at(directory, cmd) | 18 | function tools.command_at(directory, cmd, exit_on_error) |
| 18 | local drive = directory:match("^([A-Za-z]:)") | 19 | local drive = directory:match("^([A-Za-z]:)") |
| 19 | cmd = "cd " .. fs.Q(directory) .. " & " .. cmd | 20 | local op = " & " |
| 21 | if exit_on_error then | ||
| 22 | op = " && " | ||
| 23 | end | ||
| 24 | local cmd = "cd " .. fs.Q(directory) .. op .. cmd | ||
| 20 | if drive then | 25 | if drive then |
| 21 | cmd = drive .. " & " .. cmd | 26 | cmd = drive .. " & " .. cmd |
| 22 | end | 27 | end |
| @@ -113,7 +118,7 @@ function tools.find(at) | |||
| 113 | return {} | 118 | return {} |
| 114 | end | 119 | end |
| 115 | local result = {} | 120 | local result = {} |
| 116 | local pipe = io.popen(fs.command_at(at, fs.quiet_stderr(fs.Q(vars.FIND)))) | 121 | local pipe = io.popen(fs.command_at(at, fs.quiet_stderr(fs.Q(vars.FIND)), true)) |
| 117 | for file in pipe:lines() do | 122 | for file in pipe:lines() do |
| 118 | -- Windows find is a bit different | 123 | -- Windows find is a bit different |
| 119 | local first_two = file:sub(1,2) | 124 | local first_two = file:sub(1,2) |
