From ac1cb9b178fd5bd7bbd909062034fdb60434092a Mon Sep 17 00:00:00 2001 From: George Roman Date: Wed, 16 May 2018 19:07:04 +0300 Subject: Update fs module tests to use fs.set_permissions --- spec/fs_spec.lua | 316 ++++++++++++++++++++++++++++++++----------------------- 1 file changed, 186 insertions(+), 130 deletions(-) diff --git a/spec/fs_spec.lua b/spec/fs_spec.lua index a74980f3..9681b0e7 100644 --- a/spec/fs_spec.lua +++ b/spec/fs_spec.lua @@ -4,8 +4,58 @@ test_env.unload_luarocks() local fs = require("luarocks.fs") local lfs = require("lfs") local is_win = test_env.TEST_TARGET_OS == "windows" +local posix_ok = pcall(require, "posix") describe("Luarocks fs test #whitebox #w_fs", function() + local get_tmp_path = function() + local path = os.tmpname() + if is_win and not path:find(":") then + path = os.getenv("TEMP") .. path + end + return path + end + + local exists_file = function(path) + local ok, err, code = os.rename(path, path) + if not ok and code == 13 then + return true + end + return ok + end + + local create_file = function(path, content) + local fd = assert(io.open(path, "w")) + if not content then + content = "foo" + end + assert(fd:write(content)) + fd:close() + end + + local make_unreadable = function(path) + if is_win then + fs.execute("icacls " .. fs.Q(path) .. " /deny %USERNAME%:(RD)") + else + fs.execute("chmod -r " .. fs.Q(path)) + end + end + + local make_unwritable = function(path) + if is_win then + fs.execute("icacls " .. fs.Q(path) .. " /deny %USERNAME%:(WD,AD)") + else + fs.execute("chmod -w " .. fs.Q(path)) + end + end + + local make_unexecutable = function(path) + if is_win then + fs.execute("icacls " .. fs.Q(path) .. " /deny %USERNAME%:(X)") + else + fs.execute("chmod -x " .. fs.Q(path)) + end + end + describe("fs.Q", function() it("simple argument", function() assert.are.same(is_win and '"foo"' or "'foo'", fs.Q("foo")) @@ -20,6 +70,72 @@ describe("Luarocks fs test #whitebox #w_fs", function() end) end) + describe("fs.set_permissions", function() + local readfile + local execfile + local tmpdir + + after_each(function() + if readfile then + os.remove(readfile) + readfile = nil + end + if execfile then + os.remove(execfile) + execfile = nil + end + if tmpdir then + lfs.rmdir(tmpdir) + tmpdir = nil + end + end) + + it("returns true and sets the permissions of the argument accordingly", function() + readfile = get_tmp_path() + create_file(readfile) + make_unreadable(readfile) + assert.falsy(io.open(readfile, "r")) + assert.truthy(fs.set_permissions(readfile, "read", "user")) + assert.truthy(io.open(readfile, "r")) + + if is_win then + execfile = get_tmp_path() .. ".exe" + create_file(execfile) + else + execfile = get_tmp_path() .. ".sh" + create_file(execfile, "#!/bin/bash") + end + make_unexecutable(execfile) + local fd = assert(io.popen(execfile .. " 2>&1")) + local result = assert(fd:read("*a")) + assert.truthy(result:match("denied")) + fd:close() + assert.truthy(fs.set_permissions(execfile, "exec", "user")) + fd = assert(io.popen(execfile .. " 2>&1")) + result = assert(fd:read("*a")) + assert.falsy(result:match("denied")) + fd:close() + + tmpdir = get_tmp_path() + os.remove(tmpdir) + lfs.mkdir(tmpdir) + make_unexecutable(tmpdir) + fd = assert(io.popen("cd " .. fs.Q(tmpdir) .. " 2>&1")) + result = assert(fd:read("*a")) + assert.truthy(result:match("denied") or result:match("can't cd")) + fd:close() + assert.truthy(fs.set_permissions(tmpdir, "exec", "user")) + fd = assert(io.popen("cd " .. fs.Q(tmpdir) .. " 2>&1")) + result = assert(fd:read("*a")) + assert.falsy(result:match("denied") or result:match("can't cd")) + fd:close() + end) + + it("returns false and does nothing if the argument is nonexistent", function() + assert.falsy(fs.set_permissions("/nonexistent", "read", "user")) + end) + end) + describe("fs.is_file", function() local tmpfile local tmpdir @@ -36,10 +152,8 @@ describe("Luarocks fs test #whitebox #w_fs", function() end) it("returns true when the argument is a file", function() - tmpfile = os.tmpname() - local fd = assert(io.open(tmpfile, "w")) - assert(fd:write("foo")) - fd:close() + tmpfile = get_tmp_path() + create_file(tmpfile) assert.same(true, fs.is_file(tmpfile)) end) @@ -48,7 +162,7 @@ describe("Luarocks fs test #whitebox #w_fs", function() end) it("returns false when the argument exists but is not a file", function() - tmpdir = os.tmpname() + tmpdir = get_tmp_path() os.remove(tmpdir) lfs.mkdir(tmpdir) assert.same(false, fs.is_file("/nonexistent")) @@ -71,17 +185,15 @@ describe("Luarocks fs test #whitebox #w_fs", function() end) it("returns true when the argument is a directory", function() - tmpdir = os.tmpname() + tmpdir = get_tmp_path() os.remove(tmpdir) lfs.mkdir(tmpdir) assert.truthy(fs.is_dir(tmpdir)) end) it("returns false when the argument is a file", function() - tmpfile = os.tmpname() - local fd = assert(io.open(tmpfile, "w")) - assert(fd:write("foo")) - fd:close() + tmpfile = get_tmp_path() + create_file(tmpfile) assert.falsy(fs.is_dir(tmpfile)) end) @@ -106,15 +218,13 @@ describe("Luarocks fs test #whitebox #w_fs", function() end) it("returns true when the argument is a file", function() - tmpfile = os.tmpname() - local fd = assert(io.open(tmpfile, "w")) - assert(fd:write("foo")) - fd:close() + tmpfile = get_tmp_path() + create_file(tmpfile) assert.truthy(fs.exists(tmpfile)) end) it("returns true when the argument is a directory", function() - tmpdir = os.tmpname() + tmpdir = get_tmp_path() os.remove(tmpdir) lfs.mkdir(tmpdir) assert.truthy(fs.exists(tmpdir)) @@ -147,7 +257,7 @@ describe("Luarocks fs test #whitebox #w_fs", function() it("returns the current working directory", function() local currentdir = lfs.currentdir() assert.same(currentdir, fs.current_dir()) - tmpdir = os.tmpname() + tmpdir = get_tmp_path() os.remove(tmpdir) lfs.mkdir(tmpdir) assert.truthy(fs.change_dir(tmpdir)) @@ -184,7 +294,7 @@ describe("Luarocks fs test #whitebox #w_fs", function() end) it("returns true and changes the current working directory if the argument is a directory", function() - tmpdir = os.tmpname() + tmpdir = get_tmp_path() os.remove(tmpdir) lfs.mkdir(tmpdir) assert.truthy(fs.change_dir(tmpdir)) @@ -196,10 +306,8 @@ describe("Luarocks fs test #whitebox #w_fs", function() end) it("returns false and does nothing when the argument is a file", function() - tmpfile = os.tmpname() - local fd = assert(io.open(tmpfile, "w")) - assert(fd:write("foo")) - fd:close() + tmpfile = get_tmp_path() + create_file(tmpfile) assert.falsy(fs.change_dir(tmpfile)) assert.same(olddir, lfs.currentdir()) end) @@ -229,7 +337,7 @@ describe("Luarocks fs test #whitebox #w_fs", function() end) it("returns true and changes the current directory to root if the current directory is valid", function() - tmpdir = os.tmpname() + tmpdir = get_tmp_path() os.remove(tmpdir) lfs.mkdir(tmpdir) assert.truthy(fs.change_dir(tmpdir)) @@ -241,7 +349,7 @@ describe("Luarocks fs test #whitebox #w_fs", function() end) it("returns false and does nothing if the current directory is not valid #unix", function() - tmpdir = os.tmpname() + tmpdir = get_tmp_path() os.remove(tmpdir) lfs.mkdir(tmpdir) lfs.chdir(tmpdir) @@ -270,7 +378,7 @@ describe("Luarocks fs test #whitebox #w_fs", function() end) it("returns true and changes the current directory to the previous one in the dir stack if the dir stack is not empty", function() - tmpdir = os.tmpname() + tmpdir = get_tmp_path() os.remove(tmpdir) lfs.mkdir(tmpdir) assert.truthy(fs.change_dir(tmpdir)) @@ -300,14 +408,14 @@ describe("Luarocks fs test #whitebox #w_fs", function() end) it("returns true and creates the directory specified by the argument", function() - tmpdir = os.tmpname() + tmpdir = get_tmp_path() os.remove(tmpdir) assert.truthy(fs.make_dir(tmpdir)) assert.same("directory", lfs.attributes(tmpdir, "mode")) end) it("returns true and creates the directory path specified by the argument", function() - tmpdir = os.tmpname() + tmpdir = get_tmp_path() os.remove(tmpdir) intdir = "/internaldir" local dirpath = tmpdir .. intdir @@ -317,7 +425,7 @@ describe("Luarocks fs test #whitebox #w_fs", function() end) it("returns false and does nothing if the argument is not valid (file in the path)", function() - tmpfile = os.tmpname() + tmpfile = get_tmp_path() local fd = assert(io.open(tmpfile, "w")) assert(fd:write("foo")) fd:close() @@ -327,22 +435,12 @@ describe("Luarocks fs test #whitebox #w_fs", function() end) it("returns false and does nothing if the argument already exists", function() - tmpfile = os.tmpname() - local fd = assert(io.open(tmpfile, "w")) - assert(fd:write("foo")) - fd:close() + tmpfile = get_tmp_path() + create_file(tmpfile) assert.falsy(fs.make_dir(tmpfile)) end) end) - local exists_file = function(path) - local ok, err, code = os.rename(path, path) - if not ok and code == 13 then - return true - end - return ok - end - describe("fs.remove_dir_if_empty", function() local tmpfile local tmpdir @@ -359,7 +457,7 @@ describe("Luarocks fs test #whitebox #w_fs", function() end) it("removes the directory specified by the argument if it is empty", function() - tmpdir = os.tmpname() + tmpdir = get_tmp_path() os.remove(tmpdir) lfs.mkdir(tmpdir) fs.remove_dir_if_empty(tmpdir) @@ -367,15 +465,12 @@ describe("Luarocks fs test #whitebox #w_fs", function() end) it("does nothing if the directory specified by the argument is not empty", function() - tmpdir = os.tmpname() + tmpdir = get_tmp_path() os.remove(tmpdir) lfs.mkdir(tmpdir) tmpfile = "/internalfile" local filepath = tmpdir .. tmpfile - lfs.touch(filepath) - local fd = assert(io.open(filepath, "w")) - assert(fd:write("foo")) - fd:close() + create_file(filepath) fs.remove_dir_if_empty(tmpdir) assert.truthy(exists_file(tmpdir)) end) @@ -402,7 +497,7 @@ describe("Luarocks fs test #whitebox #w_fs", function() end) it("removes the directory path specified by the argument if it is empty", function() - tmpdir = os.tmpname() + tmpdir = get_tmp_path() os.remove(tmpdir) lfs.mkdir(tmpdir) fs.remove_dir_tree_if_empty(tmpdir) @@ -410,7 +505,7 @@ describe("Luarocks fs test #whitebox #w_fs", function() end) it("does nothing if the directory path specified by the argument is not empty", function() - tmpdir = os.tmpname() + tmpdir = get_tmp_path() os.remove(tmpdir) lfs.mkdir(tmpdir) intdir = "/internaldir" @@ -418,7 +513,6 @@ describe("Luarocks fs test #whitebox #w_fs", function() lfs.mkdir(dirpath) tmpfile = "/internalfile" local filepath = dirpath .. tmpfile - lfs.touch(filepath) fs.remove_dir_tree_if_empty(tmpdir) assert.truthy(exists_file(dirpath)) assert.truthy(exists_file(tmpdir)) @@ -447,71 +541,58 @@ describe("Luarocks fs test #whitebox #w_fs", function() it("returns true and copies the contents and the permissions of the source file to the destination file", function() srcfile = os.tmpname() - local fd = assert(io.open(srcfile, "w")) - local srccontent = "foo" - assert(fd:write(srccontent)) - fd:close() + create_file(srcfile, srccontent) dstfile = os.tmpname() os.remove(dstfile) - assert.truthy(fs.copy(srcfile, dstfile, nil)) + assert.truthy(fs.copy(srcfile, dstfile)) fd = assert(io.open(dstfile, "r")) local dstcontent = fd:read("*a") - assert.same(srccontent, dstcontent) - if not is_win then + assert.same("foo", dstcontent) + if posix_ok then assert.same(lfs.attributes(srcfile, "permissions"), lfs.attributes(dstfile, "permissions")) end end) it("returns true and copies contents of the source file to the destination file with custom permissions", function() srcfile = os.tmpname() - local fd = assert(io.open(srcfile, "w")) - local srccontent = "foo" - assert(fd:write(srccontent)) - fd:close() + create_file(srcfile, srccontent) dstfile = os.tmpname() os.remove(dstfile) - assert.truthy(fs.copy(srcfile, dstfile, "755")) + assert.truthy(fs.copy(srcfile, dstfile, "exec")) fd = assert(io.open(dstfile, "r")) local dstcontent = fd:read("*a") - assert.same(srccontent, dstcontent) - if not is_win then - assert.same("rwxr-xr-x", lfs.attributes(dstfile, "permissions")) - end + assert.same("foo", dstcontent) end) it("returns false and does nothing if the source file does not exist", function() - srcfile = os.tmpname() + srcfile = get_tmp_path() os.remove(srcfile) - dstfile = os.tmpname() + dstfile = get_tmp_path() os.remove(dstfile) assert.falsy(fs.copy(srcfile, dstfile, nil)) assert.falsy(exists_file(dstfile)) end) - it("returns false and does nothing if the source file doesn't have the proper permissions #unix", function() - srcfile = os.tmpname() - local fd = assert(io.open(srcfile, "w")) - assert(fd:write("foo")) - fd:close() - assert(fs.chmod(srcfile, "333")) - dstfile = os.tmpname() + it("returns false and does nothing if the source file doesn't have the proper permissions", function() + srcfile = get_tmp_path() + create_file(srcfile) + make_unreadable(srcfile) + dstfile = get_tmp_path() os.remove(dstfile) assert.falsy(fs.copy(srcfile, dstfile, nil)) assert.falsy(exists_file(dstfile)) end) - it("returns false and does nothing if the destination file directory doesn't have the proper permissions #unix", function() - srcfile = os.tmpname() - local fd = assert(io.open(srcfile, "w")) - assert(fd:write("foo")) - fd:close() - tmpdir = os.tmpname() + it("returns false and does nothing if the destination file directory doesn't have the proper permissions", function() + srcfile = get_tmp_path() + create_file(srcfile) + tmpdir = get_tmp_path() os.remove(tmpdir) lfs.mkdir(tmpdir) - assert(fs.chmod(tmpdir, "666")) + make_unwritable(tmpdir) dstfile = tmpdir .. "/dstfile" assert.falsy(fs.copy(srcfile, dstfile, nil)) - assert(fs.chmod(tmpdir, "777")) + assert(fs.set_permissions(tmpdir, "exec", "all")) assert.falsy(exists_file(dstfile)) end) end) @@ -552,23 +633,20 @@ describe("Luarocks fs test #whitebox #w_fs", function() end) local create_dir_tree = function() - srcdir = os.tmpname() + srcdir = get_tmp_path() os.remove(srcdir) lfs.mkdir(srcdir) srcintdir = srcdir .. "/internaldir" lfs.mkdir(srcintdir) srcfile = srcintdir .. "/internalfile" - lfs.touch(srcfile) - local fd = assert(io.open(srcfile, "w")) - assert(fd:write("foo")) - fd:close() - dstdir = os.tmpname() + create_file(srcfile) + dstdir = get_tmp_path() os.remove(dstdir) end it("returns true and copies the contents (with their permissions) of the source dir to the destination dir", function() create_dir_tree() - assert.truthy(fs.copy_contents(srcdir, dstdir, nil)) + assert.truthy(fs.copy_contents(srcdir, dstdir)) assert.truthy(exists_file(dstdir)) dstintdir = dstdir .. "/internaldir" assert.truthy(exists_file(dstintdir)) @@ -576,14 +654,14 @@ describe("Luarocks fs test #whitebox #w_fs", function() local fd = assert(io.open(dstfile, "r")) local dstfilecontent = fd:read("*a") assert.same("foo", dstfilecontent) - if not is_win then + if posix_ok then assert.same(lfs.attributes(srcfile, "permissions"), lfs.attributes(dstfile, "permissions")) end end) it("returns true and copies the contents of the source dir to the destination dir with custom permissions", function() create_dir_tree() - assert.truthy(fs.copy_contents(srcdir, dstdir, "755")) + assert.truthy(fs.copy_contents(srcdir, dstdir, "read")) assert.truthy(exists_file(dstdir)) dstintdir = dstdir .. "/internaldir" assert.truthy(exists_file(dstintdir)) @@ -591,36 +669,32 @@ describe("Luarocks fs test #whitebox #w_fs", function() local fd = assert(io.open(dstfile, "r")) local dstfilecontent = fd:read("*a") assert.same("foo", dstfilecontent) - if not is_win then - assert.same("rwxr-xr-x", lfs.attributes(dstfile, "permissions")) - end end) it("returns false and does nothing if the source dir doesn't exist", function() - srcdir = os.tmpname() + srcdir = get_tmp_path() os.remove(srcdir) - dstdir = os.tmpname() + dstdir = get_tmp_path() os.remove(dstdir) - assert.falsy(fs.copy_contents(srcdir, dstdir, nil)) + assert.falsy(fs.copy_contents(srcdir, dstdir)) assert.falsy(exists_file(dstdir)) end) it("returns false if the source argument is a file", function() - srcdir = os.tmpname() - local fd = assert(io.open(srcdir, "w")) - assert(fd:write("foo")) - fd:close() - dstdir = os.tmpname() + srcdir = get_tmp_path() + create_file(srcdir) + dstdir = get_tmp_path() os.remove(dstdir) - assert.falsy(fs.copy_contents(srcdir, dstdir, nil)) + assert.falsy(fs.copy_contents(srcdir, dstdir)) assert.falsy(exists_file(dstdir)) end) - it("returns false and does nothing if the source dir doesn't have the proper permissions #unix", function() + it("returns false and does nothing if the source dir doesn't have the proper permissions", function() create_dir_tree() - assert(fs.chmod(srcdir, "333")) - assert.falsy(fs.copy_contents(srcdir, dstdir, nil)) - assert.falsy(exists_file(dstdir)) + make_unreadable(srcdir) + assert.falsy(fs.copy_contents(srcdir, dstdir)) + assert.falsy(exists_file(dstdir .. "/internaldir")) + assert.falsy(exists_file(dstdir .. "/internalfile")) end) end) @@ -650,26 +724,20 @@ describe("Luarocks fs test #whitebox #w_fs", function() end) local create_dir_tree = function() - tmpdir = os.tmpname() + tmpdir = get_tmp_path() os.remove(tmpdir) lfs.mkdir(tmpdir) tmpintdir = tmpdir .. "/internaldir" lfs.mkdir(tmpintdir) tmpfile1 = tmpdir .. "/internalfile1" - lfs.touch(tmpfile1) - local fd = assert(io.open(tmpfile1, "w")) - assert(fd:write("foo")) - fd:close() - tmpfile2 = tmpintdir .. "/internalfile2" - lfs.touch(tmpfile2) - fd = assert(io.open(tmpfile2, "w")) - assert(fd:write("foo")) - fd:close() + create_file(tmpfile1) + tmpfile2 = tmpdir .. "/internalfile2" + create_file(tmpfile2) end it("deletes the file specified by the argument", function() - tmpfile1 = os.tmpname() - tmpfile2 = os.tmpname() + tmpfile1 = get_tmp_path() + tmpfile2 = get_tmp_path() fs.delete(tmpfile1) fs.delete(tmpfile2) assert.falsy(exists_file(tmpfile1)) @@ -684,17 +752,5 @@ describe("Luarocks fs test #whitebox #w_fs", function() assert.falsy(exists_file(tmpfile1)) assert.falsy(exists_file(tmpdir)) end) - - it("does nothing if the parent directory of the argument doesn't have the proper permissions #unix", function() - create_dir_tree() - assert(fs.chmod(tmpdir, "000")) - fs.delete(tmpfile1) - fs.delete(tmpfile2) - fs.delete(tmpintdir) - assert.truthy(exists_file(tmpfile2)) - assert.truthy(exists_file(tmpintdir)) - assert.truthy(exists_file(tmpfile1)) - assert.truthy(exists_file(tmpdir)) - end) end) end) -- cgit v1.2.3-55-g6feb