diff options
| -rw-r--r-- | lua/mason-core/fs.lua | 49 | ||||
| -rw-r--r-- | lua/mason-core/installer/context/InstallContextFs.lua | 8 | ||||
| -rw-r--r-- | tests/mason-core/fs_spec.lua | 24 | ||||
| -rw-r--r-- | tests/mason-core/installer/compiler/link_spec.lua | 16 |
4 files changed, 49 insertions, 48 deletions
diff --git a/lua/mason-core/fs.lua b/lua/mason-core/fs.lua index 597ba3fa..2f620a49 100644 --- a/lua/mason-core/fs.lua +++ b/lua/mason-core/fs.lua @@ -7,32 +7,29 @@ local function make_module(uv) local M = {} ---@param path string - function M.fstat(path) - log.trace("fs: fstat", path) - local fd = uv.fs_open(path, "r", 438) - local fstat = uv.fs_fstat(fd) - uv.fs_close(fd) - return fstat + function M.stat(path) + log.trace("fs: stat", path) + return assert(uv.fs_stat(path)) end ---@param path string function M.file_exists(path) log.trace("fs: file_exists", path) - local ok, fstat = pcall(M.fstat, path) + local ok, stat = pcall(M.stat, path) if not ok then return false end - return fstat.type == "file" + return stat.type == "file" end ---@param path string function M.dir_exists(path) log.trace("fs: dir_exists", path) - local ok, fstat = pcall(M.fstat, path) + local ok, stat = pcall(M.stat, path) if not ok then return false end - return fstat.type == "directory" + return stat.type == "directory" end ---@param path string @@ -188,33 +185,13 @@ local function make_module(uv) ---@param path string: The full path to the directory to read. ---@return ReaddirEntry[] function M.readdir(path) - log.trace("fs: fs_opendir", path) - local dir = assert(vim.loop.fs_opendir(path, nil, 25)) local all_entries = {} - local exhausted = false - - repeat - local entries = uv.fs_readdir(dir) - log.trace("fs: fs_readdir", path, entries) - if entries and #entries > 0 then - for i = 1, #entries do - if entries[i].name and not entries[i].type then - -- See https://github.com/luvit/luv/issues/660 - local full_path = Path.concat { path, entries[i].name } - log.trace("fs: fs_readdir falling back to fs_stat to find type", full_path) - local stat = uv.fs_stat(full_path) - entries[i].type = stat.type - end - all_entries[#all_entries + 1] = entries[i] - end - else - log.trace("fs: fs_readdir exhausted scan", path) - exhausted = true - end - until exhausted - - uv.fs_closedir(dir) - + M.ls(path, function(_, entry, type) + all_entries[#all_entries + 1] = { + name = entry, + type = type, + } + end) return all_entries end diff --git a/lua/mason-core/installer/context/InstallContextFs.lua b/lua/mason-core/installer/context/InstallContextFs.lua index a36d8410..fd875de0 100644 --- a/lua/mason-core/installer/context/InstallContextFs.lua +++ b/lua/mason-core/installer/context/InstallContextFs.lua @@ -88,7 +88,7 @@ function InstallContextFs:chmod_exec(file_path) local GRP_EXEC = 0x8 local ALL_EXEC = 0x1 local EXEC = bit.bor(USR_EXEC, GRP_EXEC, ALL_EXEC) - local fstat = self:fstat(file_path) + local fstat = self:stat(file_path) if bit.band(fstat.mode, EXEC) ~= EXEC then local plus_exec = bit.bor(fstat.mode, EXEC) log.fmt_debug("Setting exec flags on file %s %o -> %o", file_path, fstat.mode, plus_exec) @@ -100,13 +100,13 @@ end ---@param file_path string ---@param mode integer function InstallContextFs:chmod(file_path, mode) - return fs.async.chmod(path.concat { self.cwd:get(), file_path }, mode) + return fs.sync.chmod(path.concat { self.cwd:get(), file_path }, mode) end ---@async ---@param file_path string -function InstallContextFs:fstat(file_path) - return fs.async.fstat(path.concat { self.cwd:get(), file_path }) +function InstallContextFs:stat(file_path) + return fs.sync.stat(path.concat { self.cwd:get(), file_path }) end return InstallContextFs diff --git a/tests/mason-core/fs_spec.lua b/tests/mason-core/fs_spec.lua index 3962b8cd..22a467fe 100644 --- a/tests/mason-core/fs_spec.lua +++ b/tests/mason-core/fs_spec.lua @@ -31,4 +31,28 @@ describe("fs", function() local stat = assert(vim.uv.fs_stat(nested), "fs_stat returned no value") assert.equals("directory", stat.type) end) + + it("should check if file_exists", function() + local temp = vim.fn.tempname() + + assert.is_false(fs.sync.file_exists(temp)) + fs.sync.write_file(temp, "") + assert.is_true(fs.sync.file_exists(temp)) + + local temp_dir = vim.fn.tempname() + fs.sync.mkdir(temp_dir) + assert.is_false(fs.sync.file_exists(temp_dir)) + end) + + it("should check if dir_exists", function() + local temp = vim.fn.tempname() + + assert.is_false(fs.sync.dir_exists(temp)) + fs.sync.mkdir(temp) + assert.is_true(fs.sync.dir_exists(temp)) + + local temp_file = vim.fn.tempname() + fs.sync.write_file(temp_file, "") + assert.is_false(fs.sync.dir_exists(temp_file)) + end) end) diff --git a/tests/mason-core/installer/compiler/link_spec.lua b/tests/mason-core/installer/compiler/link_spec.lua index 62777bc9..6c77bbc6 100644 --- a/tests/mason-core/installer/compiler/link_spec.lua +++ b/tests/mason-core/installer/compiler/link_spec.lua @@ -22,10 +22,10 @@ describe("registry linker", function() local ctx = test_helpers.create_context() stub(ctx.fs, "file_exists") stub(ctx.fs, "chmod") - stub(ctx.fs, "fstat") + stub(ctx.fs, "stat") ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), "exec.sh").returns(true) - ctx.fs.fstat.on_call_with(match.is_ref(ctx.fs), "exec.sh").returns { + ctx.fs.stat.on_call_with(match.is_ref(ctx.fs), "exec.sh").returns { mode = 493, -- 0755 } @@ -59,10 +59,10 @@ describe("registry linker", function() local ctx = test_helpers.create_context() stub(ctx.fs, "file_exists") stub(ctx.fs, "chmod") - stub(ctx.fs, "fstat") + stub(ctx.fs, "stat") ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), "exec.sh").returns(true) - ctx.fs.fstat.on_call_with(match.is_ref(ctx.fs), "exec.sh").returns { + ctx.fs.stat.on_call_with(match.is_ref(ctx.fs), "exec.sh").returns { mode = 420, -- 0644 } @@ -88,10 +88,10 @@ describe("registry linker", function() local ctx = test_helpers.create_context() stub(ctx.fs, "file_exists") stub(ctx.fs, "chmod") - stub(ctx.fs, "fstat") + stub(ctx.fs, "stat") ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), "v1.0.0-exec.sh").returns(true) - ctx.fs.fstat.on_call_with(match.is_ref(ctx.fs), "v1.0.0-exec.sh").returns { + ctx.fs.stat.on_call_with(match.is_ref(ctx.fs), "v1.0.0-exec.sh").returns { mode = 493, -- 0755 } @@ -120,7 +120,7 @@ describe("registry linker", function() local ctx = test_helpers.create_context() stub(ctx.fs, "file_exists") stub(ctx.fs, "chmod") - stub(ctx.fs, "fstat") + stub(ctx.fs, "stat") local matrix = { ["cargo:executable"] = "bin/executable", @@ -135,7 +135,7 @@ describe("registry linker", function() for bin, path in pairs(matrix) do ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), path).returns(true) - ctx.fs.fstat.on_call_with(match.is_ref(ctx.fs), path).returns { + ctx.fs.stat.on_call_with(match.is_ref(ctx.fs), path).returns { mode = 493, -- 0755 } |
