From e5bcda8ffb4668d484c1d08d6b5fbe12f330c727 Mon Sep 17 00:00:00 2001 From: William Boman Date: Wed, 26 Apr 2023 18:23:34 +0200 Subject: fix(context): don't write exec wrapper file if it already exists (#1261) --- lua/mason-core/installer/context.lua | 3 ++ tests/mason-core/installer/context_spec.lua | 44 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/lua/mason-core/installer/context.lua b/lua/mason-core/installer/context.lua index e42393ff..52fbcf16 100644 --- a/lua/mason-core/installer/context.lua +++ b/lua/mason-core/installer/context.lua @@ -342,6 +342,9 @@ local BATCH_TEMPLATE = _.dedent [[ ---@param env table? ---@return string # The created executable filename. function InstallContext:write_shell_exec_wrapper(new_executable_rel_path, command, env) + if self.fs:file_exists(new_executable_rel_path) or self.fs:dir_exists(new_executable_rel_path) then + error(("Cannot write exec wrapper to %q because the file already exists."):format(new_executable_rel_path), 0) + end return platform.when { unix = function() local std = require "mason-core.managers.std" diff --git a/tests/mason-core/installer/context_spec.lua b/tests/mason-core/installer/context_spec.lua index efd632e3..af99089f 100644 --- a/tests/mason-core/installer/context_spec.lua +++ b/tests/mason-core/installer/context_spec.lua @@ -19,6 +19,10 @@ describe("installer", function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle) stub(ctx.fs, "write_file") + stub(ctx.fs, "file_exists") + stub(ctx.fs, "dir_exists") + ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), "my-executable").returns(false) + ctx.fs.dir_exists.on_call_with(match.is_ref(ctx.fs), "my-executable").returns(false) stub(std, "chmod") ctx:write_shell_exec_wrapper("my-executable", "bash -c 'echo $GREETING'", { @@ -44,6 +48,10 @@ exec bash -c 'echo $GREETING' "$@"]] local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle) stub(ctx.fs, "write_file") + stub(ctx.fs, "file_exists") + stub(ctx.fs, "dir_exists") + ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), "my-executable").returns(false) + ctx.fs.dir_exists.on_call_with(match.is_ref(ctx.fs), "my-executable").returns(false) stub(std, "chmod") ctx:write_shell_exec_wrapper("my-executable", "cmd.exe /C echo %GREETING%", { @@ -60,6 +68,23 @@ cmd.exe /C echo %GREETING% %*]] ) end) + it("should not write shell exec wrapper if new executable path already exists", function() + local exec_rel_path = path.concat { "obscure", "path", "to", "server" } + local handle = InstallHandleGenerator "dummy" + local ctx = InstallContextGenerator(handle) + stub(ctx.fs, "file_exists") + stub(ctx.fs, "dir_exists") + ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), exec_rel_path).returns(true) + ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), "my-wrapper-script").returns(true) + ctx.fs.dir_exists.on_call_with(match.is_ref(ctx.fs), "my-wrapper-script").returns(true) + + local err = assert.has_error(function() + ctx:write_shell_exec_wrapper("my-wrapper-script", "contents") + end) + + assert.equals([[Cannot write exec wrapper to "my-wrapper-script" because the file already exists.]], err) + end) + it("should write Node exec wrapper", function() local js_rel_path = path.concat { "some", "obscure", "path", "server.js" } local dummy = registry.get_package "dummy" @@ -79,6 +104,25 @@ cmd.exe /C echo %GREETING% %*]] ) end) + it("should write Ruby exec wrapper", function() + local js_rel_path = path.concat { "some", "obscure", "path", "server.js" } + local dummy = registry.get_package "dummy" + local handle = InstallHandleGenerator "dummy" + local ctx = InstallContextGenerator(handle) + stub(ctx, "write_shell_exec_wrapper") + stub(ctx.fs, "file_exists") + ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), js_rel_path).returns(true) + + ctx:write_ruby_exec_wrapper("my-wrapper-script", js_rel_path) + + assert.spy(ctx.write_shell_exec_wrapper).was_called(1) + assert.spy(ctx.write_shell_exec_wrapper).was_called_with( + match.is_ref(ctx), + "my-wrapper-script", + ("ruby %q"):format(path.concat { dummy:get_install_path(), js_rel_path }) + ) + end) + it("should not write Node exec wrapper if the target script doesn't exist", function() local js_rel_path = path.concat { "some", "obscure", "path", "server.js" } local handle = InstallHandleGenerator "dummy" -- cgit v1.2.3-70-g09d2