From 047ec18da56ad8f331e5c6bc7417dc5a9a6e71cc Mon Sep 17 00:00:00 2001 From: William Boman Date: Wed, 11 Oct 2023 16:31:50 +0200 Subject: refactor!: refactor installer internals and add new Package class methods (#1523) This contains the following changes: 1) `Package:install()` now accepts a second, optional, callback argument which is called when installation finishes (successfully or not). 2) Adds a `Package:is_installing()` method. This contains the following breaking changes: 1) `Package:install()` will now error when called while an installation is already ongoing. Use the new `Package:is_installing()` method to check whether an installation is already running. This also refactors large portions of the tests by removing test globals, removing async_test, and adding the `mason-test` Lua module instead. Test helpers via globals are problematic to work with due to not being detected through tools like the Lua language server without additional configuration. This has been replaced with a Lua module `mason-test`. `async_test` has also been removed in favour of explicitly making use of the `mason-core.async` API. These changes stands for a significant portion of the diff. --- tests/mason-core/installer/context_spec.lua | 46 ++++++++++++++--------------- 1 file changed, 22 insertions(+), 24 deletions(-) (limited to 'tests/mason-core/installer/context_spec.lua') diff --git a/tests/mason-core/installer/context_spec.lua b/tests/mason-core/installer/context_spec.lua index 646f7e30..9c1805cb 100644 --- a/tests/mason-core/installer/context_spec.lua +++ b/tests/mason-core/installer/context_spec.lua @@ -3,10 +3,20 @@ local path = require "mason-core.path" local pypi = require "mason-core.installer.managers.pypi" local registry = require "mason-registry" local stub = require "luassert.stub" +local test_helpers = require "mason-test.helpers" describe("installer", function() ---@module "mason-core.platform" local platform + local snapshot + + before_each(function() + snapshot = assert.snapshot() + end) + + after_each(function() + snapshot:revert() + end) before_each(function() package.loaded["mason-core.installer.platform"] = nil @@ -15,8 +25,7 @@ describe("installer", function() end) it("should write shell exec wrapper on Unix", function() - local handle = InstallHandleGenerator "dummy" - local ctx = InstallContextGenerator(handle) + local ctx = test_helpers.create_context() stub(ctx.fs, "write_file") stub(ctx.fs, "file_exists") stub(ctx.fs, "dir_exists") @@ -44,8 +53,7 @@ exec bash -c 'echo $GREETING' "$@"]] platform.is.unix = false platform.is.linux = false platform.is.win = true - local handle = InstallHandleGenerator "dummy" - local ctx = InstallContextGenerator(handle) + local ctx = test_helpers.create_context() stub(ctx.fs, "write_file") stub(ctx.fs, "file_exists") stub(ctx.fs, "dir_exists") @@ -68,8 +76,7 @@ cmd.exe /C echo %GREETING% %*]] 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) + local ctx = test_helpers.create_context() 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) @@ -86,8 +93,7 @@ cmd.exe /C echo %GREETING% %*]] it("should write Node 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) + local ctx = test_helpers.create_context() 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) @@ -105,8 +111,7 @@ cmd.exe /C echo %GREETING% %*]] 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) + local ctx = test_helpers.create_context() 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) @@ -123,8 +128,7 @@ cmd.exe /C echo %GREETING% %*]] 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" - local ctx = InstallContextGenerator(handle) + local ctx = test_helpers.create_context() 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(false) @@ -142,8 +146,7 @@ cmd.exe /C echo %GREETING% %*]] it("should write Python exec wrapper", function() local dummy = registry.get_package "dummy" - local handle = InstallHandleGenerator "dummy" - local ctx = InstallContextGenerator(handle) + local ctx = test_helpers.create_context() stub(ctx.cwd, "get") ctx.cwd.get.returns "/tmp/placeholder" stub(ctx, "write_shell_exec_wrapper") @@ -159,8 +162,7 @@ cmd.exe /C echo %GREETING% %*]] end) it("should not write Python exec wrapper if module cannot be found", function() - local handle = InstallHandleGenerator "dummy" - local ctx = InstallContextGenerator(handle) + local ctx = test_helpers.create_context() stub(ctx.cwd, "get") ctx.cwd.get.returns "/tmp/placeholder" stub(ctx, "write_shell_exec_wrapper") @@ -181,8 +183,7 @@ cmd.exe /C echo %GREETING% %*]] it("should write exec wrapper", function() local dummy = registry.get_package "dummy" local exec_rel_path = path.concat { "obscure", "path", "to", "server" } - local handle = InstallHandleGenerator "dummy" - local ctx = InstallContextGenerator(handle) + local ctx = test_helpers.create_context() stub(ctx, "write_shell_exec_wrapper") stub(ctx.fs, "file_exists") ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), exec_rel_path).returns(true) @@ -201,8 +202,7 @@ cmd.exe /C echo %GREETING% %*]] it("should not write exec wrapper if target executable doesn't exist", function() local exec_rel_path = path.concat { "obscure", "path", "to", "server" } - local handle = InstallHandleGenerator "dummy" - local ctx = InstallContextGenerator(handle) + local ctx = test_helpers.create_context() stub(ctx, "write_shell_exec_wrapper") stub(ctx.fs, "file_exists") ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), exec_rel_path).returns(false) @@ -218,8 +218,7 @@ cmd.exe /C echo %GREETING% %*]] it("should write PHP exec wrapper", function() local php_rel_path = path.concat { "some", "obscure", "path", "cli.php" } local dummy = registry.get_package "dummy" - local handle = InstallHandleGenerator "dummy" - local ctx = InstallContextGenerator(handle) + local ctx = test_helpers.create_context() stub(ctx, "write_shell_exec_wrapper") stub(ctx.fs, "file_exists") ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), php_rel_path).returns(true) @@ -236,8 +235,7 @@ cmd.exe /C echo %GREETING% %*]] it("should not write PHP exec wrapper if the target script doesn't exist", function() local php_rel_path = path.concat { "some", "obscure", "path", "cli.php" } - local handle = InstallHandleGenerator "dummy" - local ctx = InstallContextGenerator(handle) + local ctx = test_helpers.create_context() stub(ctx, "write_shell_exec_wrapper") stub(ctx.fs, "file_exists") ctx.fs.file_exists.on_call_with(match.is_ref(ctx.fs), php_rel_path).returns(false) -- cgit v1.2.3-70-g09d2