diff options
| author | William Boman <william@redwill.se> | 2023-05-18 17:06:38 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-18 17:06:38 +0200 |
| commit | 227f8a9aaae495f481c768f8346edfceaf6d2951 (patch) | |
| tree | 8b2dad3c6175fac3c02489690304f759f21b3649 | |
| parent | fix(ui): use vim.cmd("") for nvim-0.7.0 compatibility (#1307) (diff) | |
| download | mason-227f8a9aaae495f481c768f8346edfceaf6d2951.tar mason-227f8a9aaae495f481c768f8346edfceaf6d2951.tar.gz mason-227f8a9aaae495f481c768f8346edfceaf6d2951.tar.bz2 mason-227f8a9aaae495f481c768f8346edfceaf6d2951.tar.lz mason-227f8a9aaae495f481c768f8346edfceaf6d2951.tar.xz mason-227f8a9aaae495f481c768f8346edfceaf6d2951.tar.zst mason-227f8a9aaae495f481c768f8346edfceaf6d2951.zip | |
feat(installer): lock package installation (#1290)
| -rw-r--r-- | lua/mason-core/installer/init.lua | 114 | ||||
| -rw-r--r-- | lua/mason-core/path.lua | 7 | ||||
| -rw-r--r-- | tests/mason-core/installer/installer_spec.lua | 44 | ||||
| -rw-r--r-- | tests/mason-core/managers/cargo_spec.lua | 12 | ||||
| -rw-r--r-- | tests/mason-core/managers/composer_spec.lua | 4 | ||||
| -rw-r--r-- | tests/mason-core/managers/dotnet_spec.lua | 4 | ||||
| -rw-r--r-- | tests/mason-core/managers/gem_spec.lua | 4 | ||||
| -rw-r--r-- | tests/mason-core/managers/git_spec.lua | 8 | ||||
| -rw-r--r-- | tests/mason-core/managers/github_spec.lua | 8 | ||||
| -rw-r--r-- | tests/mason-core/managers/go_spec.lua | 4 | ||||
| -rw-r--r-- | tests/mason-core/managers/luarocks_spec.lua | 13 | ||||
| -rw-r--r-- | tests/mason-core/managers/npm_spec.lua | 8 | ||||
| -rw-r--r-- | tests/mason-core/managers/opam_spec.lua | 4 | ||||
| -rw-r--r-- | tests/mason-core/managers/pip3_spec.lua | 15 | ||||
| -rw-r--r-- | tests/mason-core/terminator_spec.lua | 25 | ||||
| -rw-r--r-- | tests/minimal_init.vim | 5 |
16 files changed, 185 insertions, 94 deletions
diff --git a/lua/mason-core/installer/init.lua b/lua/mason-core/installer/init.lua index 059f30bd..994ab847 100644 --- a/lua/mason-core/installer/init.lua +++ b/lua/mason-core/installer/init.lua @@ -16,7 +16,7 @@ local sem = Semaphore.new(settings.current.max_concurrent_installers) local M = {} ---@async -local function create_prefix_dirs() +function M.create_prefix_dirs() return Result.try(function(try) for _, p in ipairs { path.install_prefix(), @@ -53,10 +53,28 @@ function M.context() end ---@async +---@param ctx InstallContext +local function lock_package(ctx) + log.debug("Attempting to lock package", ctx.package) + local lockfile = path.package_lock(ctx.package.name) + if not ctx.opts.force and fs.async.file_exists(lockfile) then + log.error("Lockfile already exists.", ctx.package) + return Result.failure( + ("Lockfile exists, installation is already running in another process (pid: %s). Run with :MasonInstall --force to bypass."):format( + fs.sync.read_file(lockfile) + ) + ) + end + a.scheduler() + fs.async.write_file(lockfile, vim.fn.getpid()) + log.debug("Wrote lockfile", ctx.package) + return Result.success(lockfile) +end + +---@async ---@param context InstallContext function M.prepare_installer(context) return Result.try(function(try) - try(create_prefix_dirs()) local package_build_prefix = path.package_build_prefix(context.package.name) if fs.async.dir_exists(package_build_prefix) then try(Result.pcall(fs.async.rmrf, package_build_prefix)) @@ -165,35 +183,66 @@ function M.execute(handle, opts) log.fmt_info("Executing installer for %s %s", pkg, opts) - return Result.try(function(try) - -- 1. prepare directories and initialize cwd - local installer = try(M.prepare_installer(context)) + return M.create_prefix_dirs() + :and_then(function() + return lock_package(context) + end) + :and_then(function(lockfile) + local release_lock = _.partial(pcall, fs.async.unlink, lockfile) + return Result.try(function(try) + -- 1. prepare directories and initialize cwd + local installer = try(M.prepare_installer(context)) - -- 2. execute installer - try(run_installer(context, installer)) + -- 2. execute installer + try(run_installer(context, installer)) - -- 3. promote temporary installation dir - try(Result.pcall(function() - context:promote_cwd() - end)) + -- 3. promote temporary installation dir + try(Result.pcall(function() + context:promote_cwd() + end)) - -- 4. link package - try(linker.link(context)) + -- 4. link package + try(linker.link(context)) - -- 5. build & write receipt - ---@type InstallReceipt - local receipt = try(build_receipt(context)) - try(Result.pcall(function() - receipt:write(context.cwd:get()) - end)) - end) + -- 5. build & write receipt + ---@type InstallReceipt + local receipt = try(build_receipt(context)) + try(Result.pcall(function() + receipt:write(context.cwd:get()) + end)) + end) + :on_success(function() + release_lock() + if opts.debug then + context.fs:write_file("mason-debug.log", table.concat(tailed_output, "")) + end + end) + :on_failure(function() + release_lock() + if not opts.debug then + -- clean up installation dir + pcall(function() + fs.async.rmrf(context.cwd:get()) + end) + else + context.fs:write_file("mason-debug.log", table.concat(tailed_output, "")) + context.stdio_sink.stdout( + ("[debug] Installation directory retained at %q.\n"):format(context.cwd:get()) + ) + end + + -- unlink linked executables (in the occasion an error occurs after linking) + build_receipt(context):on_success(function(receipt) + linker.unlink(context.package, receipt):on_failure(function(err) + log.error("Failed to unlink failed installation", err) + end) + end) + end) + end) :on_success(function() permit:forget() handle:close() log.fmt_info("Installation succeeded for %s", pkg) - if opts.debug then - context.fs:write_file("mason-debug.log", table.concat(tailed_output, "")) - end end) :on_failure(function(failure) permit:forget() @@ -201,25 +250,6 @@ function M.execute(handle, opts) context.stdio_sink.stderr(tostring(failure)) context.stdio_sink.stderr "\n" - if not opts.debug then - -- clean up installation dir - pcall(function() - fs.async.rmrf(context.cwd:get()) - end) - else - context.fs:write_file("mason-debug.log", table.concat(tailed_output, "")) - context.stdio_sink.stdout( - ("[debug] Installation directory retained at %q.\n"):format(context.cwd:get()) - ) - end - - -- unlink linked executables (in the occasion an error occurs after linking) - build_receipt(context):on_success(function(receipt) - linker.unlink(context.package, receipt):on_failure(function(err) - log.error("Failed to unlink failed installation", err) - end) - end) - if not handle:is_closed() and not handle.is_terminated then handle:close() end diff --git a/lua/mason-core/path.lua b/lua/mason-core/path.lua index 1e0038be..3d7c8668 100644 --- a/lua/mason-core/path.lua +++ b/lua/mason-core/path.lua @@ -55,7 +55,12 @@ end ---@param name string? function M.package_build_prefix(name) - return M.concat { M.install_prefix ".packages", name } + return M.concat { M.install_prefix "staging", name } +end + +---@param name string +function M.package_lock(name) + return M.package_build_prefix(("%s.lock"):format(name)) end function M.registry_prefix() diff --git a/tests/mason-core/installer/installer_spec.lua b/tests/mason-core/installer/installer_spec.lua index 4a45e997..23eeb69e 100644 --- a/tests/mason-core/installer/installer_spec.lua +++ b/tests/mason-core/installer/installer_spec.lua @@ -167,4 +167,48 @@ describe("installer", function() assert.equals("spawn: bash failed with exit code 42 and signal 0. ", tostring(result:err_or_nil())) end) ) + + it( + "should lock package", + async_test(function() + local handle = InstallHandleGenerator "dummy" + local callback = spy.new() + stub(handle.package.spec, "install", function() + a.sleep(3000) + end) + + a.run(function() + return installer.execute(handle, { debug = true }) + end, callback) + + assert.wait_for(function() + assert.is_true(fs.sync.file_exists(path.package_lock "dummy")) + end) + handle:terminate() + assert.wait_for(function() + assert.spy(callback).was_called(1) + end) + assert.is_false(fs.sync.file_exists(path.package_lock "dummy")) + end) + ) + + it( + "should not run installer if package lock exists", + async_test(function() + local handle = InstallHandleGenerator "dummy" + local install = spy.new() + stub(handle.package.spec, "install", install) + + fs.sync.write_file(path.package_lock "dummy", "dummypid") + local result = installer.execute(handle, { debug = true }) + assert.is_true(fs.sync.file_exists(path.package_lock "dummy")) + fs.sync.unlink(path.package_lock "dummy") + + assert.spy(install).was_not_called() + assert.equals( + "Lockfile exists, installation is already running in another process (pid: dummypid). Run with :MasonInstall --force to bypass.", + result:err_or_nil() + ) + end) + ) end) diff --git a/tests/mason-core/managers/cargo_spec.lua b/tests/mason-core/managers/cargo_spec.lua index 9f43d517..92d04588 100644 --- a/tests/mason-core/managers/cargo_spec.lua +++ b/tests/mason-core/managers/cargo_spec.lua @@ -18,7 +18,7 @@ describe("cargo manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle, { version = "42.13.37" }) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, cargo.crate "my-crate") assert.spy(ctx.spawn.cargo).was_called(1) assert.spy(ctx.spawn.cargo).was_called_with { @@ -39,7 +39,7 @@ describe("cargo manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, cargo.crate("my-crate", { git = { url = "https://my-crate.git" } })) assert.spy(ctx.spawn.cargo).was_called(1) assert.spy(ctx.spawn.cargo).was_called_with { @@ -60,7 +60,7 @@ describe("cargo manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, cargo.crate("crate-name", { git = { url = "https://my-crate.git" } })) assert.spy(ctx.spawn.cargo).was_called(1) assert.spy(ctx.spawn.cargo).was_called_with { @@ -81,7 +81,7 @@ describe("cargo manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle, { version = "42.13.37" }) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, cargo.crate("my-crate", { features = "lsp" })) assert.spy(ctx.spawn.cargo).was_called(1) assert.spy(ctx.spawn.cargo).was_called_with { @@ -104,7 +104,7 @@ describe("cargo manager", function() stub(github, "tag") github.tag.returns { tag = "v2.1.1", with_receipt = mockx.just_runs } local ctx = InstallContextGenerator(handle, { version = "42.13.37" }) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context( ctx, cargo.crate("my-crate", { @@ -133,7 +133,7 @@ describe("cargo manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, cargo.crate "main-package") assert.same({ type = "cargo", diff --git a/tests/mason-core/managers/composer_spec.lua b/tests/mason-core/managers/composer_spec.lua index 577becaa..5e5b97ba 100644 --- a/tests/mason-core/managers/composer_spec.lua +++ b/tests/mason-core/managers/composer_spec.lua @@ -14,7 +14,7 @@ describe("composer manager", function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle, { version = "42.13.37" }) ctx.fs.file_exists = spy.new(mockx.returns(false)) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context( ctx, composer.packages { "main-package", "supporting-package", "supporting-package2" } @@ -41,7 +41,7 @@ describe("composer manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle, { version = "42.13.37" }) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context( ctx, composer.packages { "main-package", "supporting-package", "supporting-package2" } diff --git a/tests/mason-core/managers/dotnet_spec.lua b/tests/mason-core/managers/dotnet_spec.lua index 441b6a97..b6e0f8c0 100644 --- a/tests/mason-core/managers/dotnet_spec.lua +++ b/tests/mason-core/managers/dotnet_spec.lua @@ -7,7 +7,7 @@ describe("dotnet manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle, { version = "42.13.37" }) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, dotnet.package "main-package") assert.spy(ctx.spawn.dotnet).was_called(1) assert.spy(ctx.spawn.dotnet).was_called_with { @@ -27,7 +27,7 @@ describe("dotnet manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle, { version = "42.13.37" }) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, dotnet.package "main-package") assert.same({ type = "dotnet", diff --git a/tests/mason-core/managers/gem_spec.lua b/tests/mason-core/managers/gem_spec.lua index 94443c2e..a99bf5e9 100644 --- a/tests/mason-core/managers/gem_spec.lua +++ b/tests/mason-core/managers/gem_spec.lua @@ -15,7 +15,7 @@ describe("gem manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle, { version = "42.13.37" }) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, gem.packages { "main-package", "supporting-package", "supporting-package2" }) assert.spy(ctx.spawn.gem).was_called(1) assert.spy(ctx.spawn.gem).was_called_with(match.tbl_containing { @@ -39,7 +39,7 @@ describe("gem manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle, { version = "42.13.37" }) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, gem.packages { "main-package", "supporting-package", "supporting-package2" }) assert.same({ type = "gem", diff --git a/tests/mason-core/managers/git_spec.lua b/tests/mason-core/managers/git_spec.lua index 9c1b8a72..50d2470f 100644 --- a/tests/mason-core/managers/git_spec.lua +++ b/tests/mason-core/managers/git_spec.lua @@ -14,7 +14,7 @@ describe("git manager", function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle) local err = assert.has_error(function() - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, function() git.clone {} end) @@ -29,7 +29,7 @@ describe("git manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, function() git.clone { "https://github.com/williamboman/mason.nvim.git" } end) @@ -50,7 +50,7 @@ describe("git manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle, { version = "1337" }) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, function() git.clone { "https://github.com/williamboman/mason.nvim.git" } end) @@ -79,7 +79,7 @@ describe("git manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, function() git.clone({ "https://github.com/williamboman/mason.nvim.git" }).with_receipt() end) diff --git a/tests/mason-core/managers/github_spec.lua b/tests/mason-core/managers/github_spec.lua index d0a88faa..852c3ff6 100644 --- a/tests/mason-core/managers/github_spec.lua +++ b/tests/mason-core/managers/github_spec.lua @@ -14,7 +14,7 @@ describe("github release file", function() stub(providers.github, "get_latest_release") local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() local source = installer.exec_in_context(ctx, function() return github.release_file { repo = "williamboman/mason.nvim", @@ -40,7 +40,7 @@ describe("github release file", function() })) local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() local source = installer.exec_in_context(ctx, function() return github.release_file { repo = "williamboman/mason.nvim", @@ -68,7 +68,7 @@ describe("github release version", function() stub(providers.github, "get_latest_release") local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() local source = installer.exec_in_context(ctx, function() return github.release_version { repo = "williamboman/mason.nvim", @@ -88,7 +88,7 @@ describe("github release version", function() providers.github.get_latest_release.returns(Result.success { tag_name = "v42" }) local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() local source = installer.exec_in_context(ctx, function() return github.release_version { repo = "williamboman/mason.nvim", diff --git a/tests/mason-core/managers/go_spec.lua b/tests/mason-core/managers/go_spec.lua index b4b0f892..fd3ca043 100644 --- a/tests/mason-core/managers/go_spec.lua +++ b/tests/mason-core/managers/go_spec.lua @@ -13,7 +13,7 @@ describe("go manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle, { version = "42.13.37" }) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, go.packages { "main-package", "supporting-package", "supporting-package2" }) assert.spy(ctx.spawn.go).was_called(3) assert.spy(ctx.spawn.go).was_called_with { @@ -42,7 +42,7 @@ describe("go manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle, { version = "42.13.37" }) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, go.packages { "main-package", "supporting-package", "supporting-package2" }) assert.same({ type = "go", diff --git a/tests/mason-core/managers/luarocks_spec.lua b/tests/mason-core/managers/luarocks_spec.lua index 67cddbc0..d89f65fa 100644 --- a/tests/mason-core/managers/luarocks_spec.lua +++ b/tests/mason-core/managers/luarocks_spec.lua @@ -1,14 +1,19 @@ +local a = require "mason-core.async" local installer = require "mason-core.installer" local luarocks = require "mason-core.managers.luarocks" local path = require "mason-core.path" describe("luarocks manager", function() + before_each(function() + a.run_blocking(installer.create_prefix_dirs) + end) + it( "should install provided package", async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, luarocks.package "lua-cjson") assert.spy(ctx.spawn.luarocks).was_called(1) assert.spy(ctx.spawn.luarocks).was_called_with { @@ -28,7 +33,7 @@ describe("luarocks manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle, { version = "1.2.3" }) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, luarocks.package "lua-cjson") assert.spy(ctx.spawn.luarocks).was_called(1) assert.spy(ctx.spawn.luarocks).was_called_with { @@ -48,7 +53,7 @@ describe("luarocks manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, luarocks.package("lua-cjson", { dev = true })) assert.spy(ctx.spawn.luarocks).was_called(1) assert.spy(ctx.spawn.luarocks).was_called_with { @@ -68,7 +73,7 @@ describe("luarocks manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, luarocks.package("luaformatter", { server = "https://luarocks.org/dev" })) assert.spy(ctx.spawn.luarocks).was_called(1) assert.spy(ctx.spawn.luarocks).was_called_with { diff --git a/tests/mason-core/managers/npm_spec.lua b/tests/mason-core/managers/npm_spec.lua index 2eb4db26..31500ac7 100644 --- a/tests/mason-core/managers/npm_spec.lua +++ b/tests/mason-core/managers/npm_spec.lua @@ -15,7 +15,7 @@ describe("npm manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle, { version = "42.13.37" }) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, npm.packages { "main-package", "supporting-package", "supporting-package2" }) assert.spy(ctx.spawn.npm).was_called_with(match.tbl_containing { "install", @@ -35,7 +35,7 @@ describe("npm manager", function() local ctx = InstallContextGenerator(handle) ctx.fs.file_exists = mockx.returns(false) ctx.fs.dir_exists = mockx.returns(false) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, function() npm.install { "main-package", "supporting-package", "supporting-package2" } end) @@ -53,7 +53,7 @@ describe("npm manager", function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle, { version = "42.13.37" }) ctx.fs.append_file = spy.new(mockx.just_runs()) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, npm.packages { "main-package", "supporting-package", "supporting-package2" }) assert.spy(ctx.fs.append_file).was_called(1) assert.spy(ctx.fs.append_file).was_called_with(ctx.fs, ".npmrc", "global-style=true") @@ -65,7 +65,7 @@ describe("npm manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle, { version = "42.13.37" }) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, npm.packages { "main-package", "supporting-package", "supporting-package2" }) assert.same({ type = "npm", diff --git a/tests/mason-core/managers/opam_spec.lua b/tests/mason-core/managers/opam_spec.lua index df14023f..297f9d8b 100644 --- a/tests/mason-core/managers/opam_spec.lua +++ b/tests/mason-core/managers/opam_spec.lua @@ -8,7 +8,7 @@ describe("opam manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle, { version = "42.13.37" }) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context( ctx, opam.packages { "main-package", "supporting-package", "supporting-package2" } @@ -33,7 +33,7 @@ describe("opam manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle, { version = "42.13.37" }) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context( ctx, opam.packages { "main-package", "supporting-package", "supporting-package2" } diff --git a/tests/mason-core/managers/pip3_spec.lua b/tests/mason-core/managers/pip3_spec.lua index bff7eadd..cf7aff9b 100644 --- a/tests/mason-core/managers/pip3_spec.lua +++ b/tests/mason-core/managers/pip3_spec.lua @@ -15,6 +15,7 @@ local spawn = require "mason-core.spawn" describe("pip3 manager", function() before_each(function() settings.set(settings._DEFAULT_SETTINGS) + a.run_blocking(installer.create_prefix_dirs) end) it("normalizes pip3 packages", function() @@ -29,7 +30,7 @@ describe("pip3 manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle, { version = "42.13.37" }) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context( ctx, pip3.packages { "main-package", "supporting-package", "supporting-package2" } @@ -69,7 +70,7 @@ describe("pip3 manager", function() ctx.spawn.python = spy.new(mockx.throws()) ctx.spawn[vim.g.python3_host_prog] = spy.new(mockx.throws()) local err = assert.has_error(function() - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, pip3.packages { "package" }) end) vim.g.python3_host_prog = nil @@ -91,7 +92,7 @@ describe("pip3 manager", function() ctx.spawn.python = spy.new(mockx.returns {}) ctx.spawn[vim.g.python3_host_prog] = spy.new(mockx.returns {}) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, pip3.packages { "package" }) vim.g.python3_host_prog = nil assert.spy(ctx.spawn.python3).was_called(0) @@ -109,7 +110,7 @@ describe("pip3 manager", function() ctx.spawn.python = spy.new(mockx.returns {}) ctx.spawn[vim.env.HOME .. "/python3"] = spy.new(mockx.returns {}) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, pip3.packages { "package" }) a.scheduler() vim.g.python3_host_prog = nil @@ -127,7 +128,7 @@ describe("pip3 manager", function() } local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, pip3.packages { "package" }) assert.spy(ctx.spawn.python).was_called(1) assert.spy(ctx.spawn.python).was_called_with { @@ -153,7 +154,7 @@ describe("pip3 manager", function() } local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context(ctx, pip3.packages { "package" }) assert.spy(ctx.spawn.python).was_called(2) assert.spy(ctx.spawn.python).was_called_with { @@ -184,7 +185,7 @@ describe("pip3 manager", function() async_test(function() local handle = InstallHandleGenerator "dummy" local ctx = InstallContextGenerator(handle, { version = "42.13.37" }) - installer.prepare_installer(ctx) + installer.prepare_installer(ctx):get_or_throw() installer.exec_in_context( ctx, pip3.packages { "main-package", "supporting-package", "supporting-package2" } diff --git a/tests/mason-core/terminator_spec.lua b/tests/mason-core/terminator_spec.lua index d8d0f2eb..66b48ba4 100644 --- a/tests/mason-core/terminator_spec.lua +++ b/tests/mason-core/terminator_spec.lua @@ -11,23 +11,25 @@ describe("terminator", function() it( "should terminate all active handles on nvim exit", async_test(function() - -- TODO: Tests on CI fail for some reason - sleeping helps - a.sleep(500) spy.on(InstallHandle, "terminate") local dummy = registry.get_package "dummy" local dummy2 = registry.get_package "dummy2" for _, pkg in ipairs { dummy, dummy2 } do - stub(pkg.spec, "install") - pkg.spec.install.invokes(function() + stub(pkg.spec, "install", function() a.sleep(10000) end) end local dummy_handle = dummy:install() local dummy2_handle = dummy2:install() + + assert.wait_for(function() + assert.spy(dummy.spec.install).was_called() + assert.spy(dummy2.spec.install).was_called() + end) + terminator.terminate(5000) - a.wait(vim.schedule) assert.spy(InstallHandle.terminate).was_called(2) assert.spy(InstallHandle.terminate).was_called_with(match.is_ref(dummy_handle)) assert.spy(InstallHandle.terminate).was_called_with(match.is_ref(dummy2_handle)) @@ -41,22 +43,25 @@ describe("terminator", function() it( "should print warning messages", async_test(function() - -- TODO: Tests on CI fail for some reason - sleeping helps - a.sleep(500) spy.on(vim.api, "nvim_echo") spy.on(vim.api, "nvim_err_writeln") spy.on(InstallHandle, "terminate") local dummy = registry.get_package "dummy" local dummy2 = registry.get_package "dummy2" for _, pkg in ipairs { dummy, dummy2 } do - stub(pkg.spec, "install") - pkg.spec.install.invokes(function() + stub(pkg.spec, "install", function() a.sleep(10000) end) end local dummy_handle = dummy:install() local dummy2_handle = dummy2:install() + + assert.wait_for(function() + assert.spy(dummy.spec.install).was_called() + assert.spy(dummy2.spec.install).was_called() + end) + terminator.terminate(5000) assert.spy(vim.api.nvim_echo).was_called(1) @@ -85,8 +90,6 @@ describe("terminator", function() it( "should send SIGTERM and then SIGKILL after grace period", async_test(function() - -- TODO: Tests on CI fail for some reason - sleeping helps - a.sleep(500) spy.on(InstallHandle, "kill") local dummy = registry.get_package "dummy" stub(dummy.spec, "install") diff --git a/tests/minimal_init.vim b/tests/minimal_init.vim index 0d9fb211..abd07fa3 100644 --- a/tests/minimal_init.vim +++ b/tests/minimal_init.vim @@ -16,8 +16,11 @@ lua require("luassertx") lua require("test_helpers") lua <<EOF +local path = require "mason-core.path" + require("mason").setup { - install_root_dir = vim.env.INSTALL_ROOT_DIR, + log_level = vim.log.levels.DEBUG, + install_root_dir = vim.env.INSTALL_ROOT_DIR or path.concat { vim.loop.cwd(), "tests", "fixtures", "mason"}, registries = { "lua:dummy-registry.index" } |
