diff options
| author | William Boman <william@redwill.se> | 2022-02-21 20:56:16 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-21 20:56:16 +0100 |
| commit | 12d97eeabf44c2dd2cf841db218aac449022143d (patch) | |
| tree | 5c8b996a262761ae2433663351861e505dbceb8d /tests | |
| parent | run autogen_metadata.lua (diff) | |
| download | mason-12d97eeabf44c2dd2cf841db218aac449022143d.tar mason-12d97eeabf44c2dd2cf841db218aac449022143d.tar.gz mason-12d97eeabf44c2dd2cf841db218aac449022143d.tar.bz2 mason-12d97eeabf44c2dd2cf841db218aac449022143d.tar.lz mason-12d97eeabf44c2dd2cf841db218aac449022143d.tar.xz mason-12d97eeabf44c2dd2cf841db218aac449022143d.tar.zst mason-12d97eeabf44c2dd2cf841db218aac449022143d.zip | |
feat: add async module (#499)
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/core/async_spec.lua | 50 | ||||
| -rw-r--r-- | tests/core/process_spec.lua | 117 | ||||
| -rw-r--r-- | tests/luassertx/lua/luassertx.lua | 15 | ||||
| -rw-r--r-- | tests/server_spec.lua | 77 | ||||
| -rw-r--r-- | tests/ui_spec.lua | 198 |
5 files changed, 274 insertions, 183 deletions
diff --git a/tests/core/async_spec.lua b/tests/core/async_spec.lua new file mode 100644 index 00000000..42c1fe65 --- /dev/null +++ b/tests/core/async_spec.lua @@ -0,0 +1,50 @@ +local assert = require "luassert" +local spy = require "luassert.spy" +local a = require "nvim-lsp-installer.core.async" +local process = require "nvim-lsp-installer.process" + +local function timestamp() + local seconds, microseconds = vim.loop.gettimeofday() + return (seconds * 1000) + math.floor(microseconds / 1000) +end + +describe("async", function() + it("should run in blocking mode", function() + local start = timestamp() + a.run_blocking(function() + a.sleep(1000) + end) + local stop = timestamp() + local grace_ms = 5 + assert.is_true((stop - start) >= (1000 - grace_ms)) + end) + + it( + "should wrap callback-style async functions", + async_test(function() + local stdio = process.in_memory_sink() + local ok, success = a.promisify(process.spawn)("env", { + args = {}, + env = { "FOO=BAR", "BAR=BAZ" }, + stdio_sink = stdio.sink, + }) + assert.is_true(ok) + assert.is_true(success) + assert.equals("FOO=BAR\nBAR=BAZ\n", table.concat(stdio.buffers.stdout, "")) + end) + ) + + it( + "should cancel coroutine", + async_test(function() + local james_bond = spy.new() + local poutine = a.scope(function() + a.sleep(100) + james_bond() + end)() + poutine() + a.sleep(200) + assert.spy(james_bond).was_not.called() + end) + ) +end) diff --git a/tests/core/process_spec.lua b/tests/core/process_spec.lua index 17ff0822..2db42865 100644 --- a/tests/core/process_spec.lua +++ b/tests/core/process_spec.lua @@ -63,74 +63,83 @@ end) describe("process.spawn", function() -- Unix only - async.tests.it("should spawn command and feed output to sink", function() - local stdio = process.in_memory_sink() - local callback = spy.new() - process.spawn("env", { - args = {}, - env = { - "HELLO=world", - "MY_ENV=var", - }, - stdio_sink = stdio.sink, - }, callback) + it( + "should spawn command and feed output to sink", + async_test(function() + local stdio = process.in_memory_sink() + local callback = spy.new() + process.spawn("env", { + args = {}, + env = { + "HELLO=world", + "MY_ENV=var", + }, + stdio_sink = stdio.sink, + }, callback) - assert.wait_for(function() - assert.spy(callback).was_called(1) - assert.spy(callback).was_called_with(true) - assert.equal(table.concat(stdio.buffers.stdout, ""), "HELLO=world\nMY_ENV=var\n") + assert.wait_for(function() + assert.spy(callback).was_called(1) + assert.spy(callback).was_called_with(true) + assert.equal(table.concat(stdio.buffers.stdout, ""), "HELLO=world\nMY_ENV=var\n") + end) end) - end) + ) end) describe("process.chain", function() -- Unix only - async.tests.it("should chain commands", function() - local stdio = process.in_memory_sink() - local callback = spy.new() + it( + "should chain commands", + async_test(function() + local stdio = process.in_memory_sink() + local callback = spy.new() - local c = process.chain { - env = { - "HELLO=world", - "MY_ENV=var", - }, - stdio_sink = stdio.sink, - } + local c = process.chain { + env = { + "HELLO=world", + "MY_ENV=var", + }, + stdio_sink = stdio.sink, + } - c.run("env", {}) - c.run("bash", { "-c", "echo Hello $HELLO" }) - c.spawn(callback) + c.run("env", {}) + c.run("bash", { "-c", "echo Hello $HELLO" }) + c.spawn(callback) - assert.wait_for(function() - assert.spy(callback).was_called(1) - assert.spy(callback).was_called_with(true) - assert.equal(table.concat(stdio.buffers.stdout, ""), "HELLO=world\nMY_ENV=var\nHello world\n") + assert.wait_for(function() + assert.spy(callback).was_called(1) + assert.spy(callback).was_called_with(true) + assert.equal(table.concat(stdio.buffers.stdout, ""), "HELLO=world\nMY_ENV=var\nHello world\n") + end) end) - end) + ) -- Unix only - async.tests.it("should abort chain commands should one fail", function() - local stdio = process.in_memory_sink() - local callback = spy.new() + it( + "should abort chain commands should one fail", + async_test(function() + local stdio = process.in_memory_sink() + local callback = spy.new() - local c = process.chain { - env = { - "HELLO=world", - "MY_ENV=var", - }, - stdio_sink = stdio.sink, - } + local c = process.chain { + env = { + "HELLO=world", + "MY_ENV=var", + }, + stdio_sink = stdio.sink, + } - c.run("env", {}) - c.run("bash", { "-c", ">&2 echo Uh oh; exit 1" }) - c.run("bash", { "-c", "echo Hello $HELLO" }) - c.spawn(callback) + c.run("env", {}) + c.run("bash", { "-c", ">&2 echo Uh oh; exit 1" }) + c.run("bash", { "-c", "echo Hello $HELLO" }) + c.spawn(callback) - assert.wait_for(function() - assert.spy(callback).was_called(1) - assert.spy(callback).was_called_with(false) - assert.equal(table.concat(stdio.buffers.stdout, ""), "HELLO=world\nMY_ENV=var\n") - assert.equal(table.concat(stdio.buffers.stderr, ""), "Uh oh\n") + assert.wait_for(function() + assert.spy(callback).was_called(1) + assert.spy(callback).was_called_with(false) + assert.equal(table.concat(stdio.buffers.stdout, ""), "HELLO=world\nMY_ENV=var\n") + assert.equal(table.concat(stdio.buffers.stderr, ""), "Uh oh\n") + end) end) - end) + ) end) diff --git a/tests/luassertx/lua/luassertx.lua b/tests/luassertx/lua/luassertx.lua index 33fa9957..0722a6d7 100644 --- a/tests/luassertx/lua/luassertx.lua +++ b/tests/luassertx/lua/luassertx.lua @@ -1,6 +1,17 @@ -local a = require "plenary.async" +local a = require "nvim-lsp-installer.core.async" local assert = require "luassert" +local util = require "luassert.util" + +function async_test(suspend_fn) + return function() + local ok, err = pcall(a.run_blocking, suspend_fn) + if not ok then + error(err, util.errorlevel()) + end + end +end + local function wait_for(_, arguments) ---@type fun() @Function to execute until it does not error. local assertions_fn = arguments[1] @@ -13,7 +24,7 @@ local function wait_for(_, arguments) repeat is_ok, err = pcall(assertions_fn) if not is_ok then - a.util.sleep(math.min(timeout, 100)) + a.sleep(math.min(timeout, 100)) end until is_ok or ((vim.loop.hrtime() - start) / 1e6) > timeout diff --git a/tests/server_spec.lua b/tests/server_spec.lua index 53507d27..5204fb69 100644 --- a/tests/server_spec.lua +++ b/tests/server_spec.lua @@ -1,44 +1,55 @@ +local spy = require "luassert.spy" +local match = require "luassert.match" local lsp_installer = require "nvim-lsp-installer" local server = require "nvim-lsp-installer.server" -local spy = require "luassert.spy" -local a = require "plenary.async" +local a = require "nvim-lsp-installer.core.async" +local std = require "nvim-lsp-installer.installers.std" +local fs = require "nvim-lsp-installer.fs" +local path = require "nvim-lsp-installer.path" +local settings = require "nvim-lsp-installer.settings" describe("server", function() - a.tests.it("calls registered on_ready handlers upon successful installation", function() - local on_ready_handler = spy.new() - local generic_handler = spy.new() + it( + "calls registered on_ready handlers upon successful installation", + async_test(function() + local on_ready_handler = spy.new() + local generic_handler = spy.new() - lsp_installer.on_server_ready(generic_handler) + lsp_installer.on_server_ready(generic_handler) - local srv = ServerGenerator { - name = "on_ready_fixture", - root_dir = server.get_server_root_path "on_ready_fixture", - } - srv:on_ready(on_ready_handler) - srv:install() - assert.wait_for(function() - assert.spy(on_ready_handler).was_called(1) - assert.spy(generic_handler).was_called(1) - assert.spy(generic_handler).was_called_with(srv) + local srv = ServerGenerator { + name = "on_ready_fixture", + root_dir = server.get_server_root_path "on_ready_fixture", + } + srv:on_ready(on_ready_handler) + srv:install() + assert.wait_for(function() + assert.spy(on_ready_handler).was_called(1) + assert.spy(generic_handler).was_called(1) + assert.spy(generic_handler).was_called_with(srv) + end) + assert.is_true(srv:is_installed()) end) - assert.is_true(srv:is_installed()) - end) + ) - a.tests.it("doesn't call on_ready handler when server fails installation", function() - local on_ready_handler = spy.new() - local generic_handler = spy.new() + it( + "doesn't call on_ready handler when server fails installation", + async_test(function() + local on_ready_handler = spy.new() + local generic_handler = spy.new() - lsp_installer.on_server_ready(generic_handler) + lsp_installer.on_server_ready(generic_handler) - local srv = FailingServerGenerator { - name = "on_ready_fixture_failing", - root_dir = server.get_server_root_path "on_ready_fixture_failing", - } - srv:on_ready(on_ready_handler) - srv:install() - a.util.sleep(500) - assert.spy(on_ready_handler).was_not_called() - assert.spy(generic_handler).was_not_called() - assert.is_false(srv:is_installed()) - end) + local srv = FailingServerGenerator { + name = "on_ready_fixture_failing", + root_dir = server.get_server_root_path "on_ready_fixture_failing", + } + srv:on_ready(on_ready_handler) + srv:install() + a.sleep(500) + assert.spy(on_ready_handler).was_not_called() + assert.spy(generic_handler).was_not_called() + assert.is_false(srv:is_installed()) + end) + ) end) diff --git a/tests/ui_spec.lua b/tests/ui_spec.lua index df771a4d..0c075eef 100644 --- a/tests/ui_spec.lua +++ b/tests/ui_spec.lua @@ -2,7 +2,7 @@ local display = require "nvim-lsp-installer.ui.display" local match = require "luassert.match" local spy = require "luassert.spy" local Ui = require "nvim-lsp-installer.ui" -local a = require "plenary.async" +local a = require "nvim-lsp-installer.core.async" describe("ui", function() it("produces a correct tree", function() @@ -136,114 +136,124 @@ describe("ui", function() end) describe("integration test", function() - a.tests.it("calls vim APIs as expected during rendering", function() - local window = display.new_view_only_win "test" + it( + "calls vim APIs as expected during rendering", + async_test(function() + local window = display.new_view_only_win "test" - window.view(function(state) - return Ui.Node { - Ui.Keybind("U", "EFFECT", nil, true), - Ui.Text { - "Line number 1!", - state.text, - }, - Ui.Keybind("R", "R_EFFECT", { state.text }), - Ui.HlTextNode { - { - { "My highlighted text", "MyHighlightGroup" }, + window.view(function(state) + return Ui.Node { + Ui.Keybind("U", "EFFECT", nil, true), + Ui.Text { + "Line number 1!", + state.text, + }, + Ui.Keybind("R", "R_EFFECT", { state.text }), + Ui.HlTextNode { + { + { "My highlighted text", "MyHighlightGroup" }, + }, }, + } + end) + + local mutate_state = window.init { text = "Initial state" } + + window.open { + effects = { + ["EFFECT"] = function() end, + ["R_EFFECT"] = function() end, + }, + highlight_groups = { + "hi def MyHighlight gui=bold", }, } - end) - local mutate_state = window.init { text = "Initial state" } + local clear_namespace = spy.on(vim.api, "nvim_buf_clear_namespace") + local buf_set_option = spy.on(vim.api, "nvim_buf_set_option") + local win_set_option = spy.on(vim.api, "nvim_win_set_option") + local set_lines = spy.on(vim.api, "nvim_buf_set_lines") + local set_extmark = spy.on(vim.api, "nvim_buf_set_extmark") + local add_highlight = spy.on(vim.api, "nvim_buf_add_highlight") + local set_keymap = spy.on(vim.api, "nvim_buf_set_keymap") - window.open { - effects = { - ["EFFECT"] = function() end, - ["R_EFFECT"] = function() end, - }, - highlight_groups = { - "hi def MyHighlight gui=bold", - }, - } + -- Initial window and buffer creation + initial render + a.scheduler() - local clear_namespace = spy.on(vim.api, "nvim_buf_clear_namespace") - local buf_set_option = spy.on(vim.api, "nvim_buf_set_option") - local win_set_option = spy.on(vim.api, "nvim_win_set_option") - local set_lines = spy.on(vim.api, "nvim_buf_set_lines") - local set_extmark = spy.on(vim.api, "nvim_buf_set_extmark") - local add_highlight = spy.on(vim.api, "nvim_buf_add_highlight") - local set_keymap = spy.on(vim.api, "nvim_buf_set_keymap") + assert.spy(win_set_option).was_called(8) + assert.spy(win_set_option).was_called_with(match.is_number(), "number", false) + assert.spy(win_set_option).was_called_with(match.is_number(), "relativenumber", false) + assert.spy(win_set_option).was_called_with(match.is_number(), "wrap", false) + assert.spy(win_set_option).was_called_with(match.is_number(), "spell", false) + assert.spy(win_set_option).was_called_with(match.is_number(), "foldenable", false) + assert.spy(win_set_option).was_called_with(match.is_number(), "signcolumn", "no") + assert.spy(win_set_option).was_called_with(match.is_number(), "colorcolumn", "") + assert.spy(win_set_option).was_called_with(match.is_number(), "cursorline", true) - -- Initial window and buffer creation + initial render - a.util.scheduler() + assert.spy(buf_set_option).was_called(9) + assert.spy(buf_set_option).was_called_with(match.is_number(), "modifiable", false) + assert.spy(buf_set_option).was_called_with(match.is_number(), "swapfile", false) + assert.spy(buf_set_option).was_called_with(match.is_number(), "textwidth", 0) + assert.spy(buf_set_option).was_called_with(match.is_number(), "buftype", "nofile") + assert.spy(buf_set_option).was_called_with(match.is_number(), "bufhidden", "wipe") + assert.spy(buf_set_option).was_called_with(match.is_number(), "buflisted", false) + assert.spy(buf_set_option).was_called_with(match.is_number(), "filetype", "lsp-installer") - assert.spy(win_set_option).was_called(8) - assert.spy(win_set_option).was_called_with(match.is_number(), "number", false) - assert.spy(win_set_option).was_called_with(match.is_number(), "relativenumber", false) - assert.spy(win_set_option).was_called_with(match.is_number(), "wrap", false) - assert.spy(win_set_option).was_called_with(match.is_number(), "spell", false) - assert.spy(win_set_option).was_called_with(match.is_number(), "foldenable", false) - assert.spy(win_set_option).was_called_with(match.is_number(), "signcolumn", "no") - assert.spy(win_set_option).was_called_with(match.is_number(), "colorcolumn", "") - assert.spy(win_set_option).was_called_with(match.is_number(), "cursorline", true) + assert.spy(set_lines).was_called(1) + assert.spy(set_lines).was_called_with( + match.is_number(), + 0, + -1, + false, + { "Line number 1!", "Initial state", "My highlighted text" } + ) - assert.spy(buf_set_option).was_called(9) - assert.spy(buf_set_option).was_called_with(match.is_number(), "modifiable", false) - assert.spy(buf_set_option).was_called_with(match.is_number(), "swapfile", false) - assert.spy(buf_set_option).was_called_with(match.is_number(), "textwidth", 0) - assert.spy(buf_set_option).was_called_with(match.is_number(), "buftype", "nofile") - assert.spy(buf_set_option).was_called_with(match.is_number(), "bufhidden", "wipe") - assert.spy(buf_set_option).was_called_with(match.is_number(), "buflisted", false) - assert.spy(buf_set_option).was_called_with(match.is_number(), "filetype", "lsp-installer") + assert.spy(set_extmark).was_called(0) - assert.spy(set_lines).was_called(1) - assert.spy(set_lines).was_called_with( - match.is_number(), - 0, - -1, - false, - { "Line number 1!", "Initial state", "My highlighted text" } - ) + assert.spy(add_highlight).was_called(1) + assert.spy(add_highlight).was_called_with( + match.is_number(), + match.is_number(), + "MyHighlightGroup", + 2, + 0, + 19 + ) - assert.spy(set_extmark).was_called(0) + assert.spy(set_keymap).was_called(2) + assert.spy(set_keymap).was_called_with( + match.is_number(), + "n", + "U", + match.has_match [[<cmd>lua require%('nvim%-lsp%-installer%.ui%.display'%)%.dispatch_effect%(%d, "55"%)<cr>]], + { nowait = true, silent = true, noremap = true } + ) + assert.spy(set_keymap).was_called_with( + match.is_number(), + "n", + "R", + match.has_match [[<cmd>lua require%('nvim%-lsp%-installer%.ui%.display'%)%.dispatch_effect%(%d, "52"%)<cr>]], + { nowait = true, silent = true, noremap = true } + ) - assert.spy(add_highlight).was_called(1) - assert.spy(add_highlight).was_called_with(match.is_number(), match.is_number(), "MyHighlightGroup", 2, 0, 19) + assert.spy(clear_namespace).was_called(1) + assert.spy(clear_namespace).was_called_with(match.is_number(), match.is_number(), 0, -1) - assert.spy(set_keymap).was_called(2) - assert.spy(set_keymap).was_called_with( - match.is_number(), - "n", - "U", - match.has_match [[<cmd>lua require%('nvim%-lsp%-installer%.ui%.display'%)%.dispatch_effect%(%d, "55"%)<cr>]], - { nowait = true, silent = true, noremap = true } - ) - assert.spy(set_keymap).was_called_with( - match.is_number(), - "n", - "R", - match.has_match [[<cmd>lua require%('nvim%-lsp%-installer%.ui%.display'%)%.dispatch_effect%(%d, "52"%)<cr>]], - { nowait = true, silent = true, noremap = true } - ) + mutate_state(function(state) + state.text = "New state" + end) - assert.spy(clear_namespace).was_called(1) - assert.spy(clear_namespace).was_called_with(match.is_number(), match.is_number(), 0, -1) + assert.spy(set_lines).was_called(1) + a.scheduler() + assert.spy(set_lines).was_called(2) - mutate_state(function(state) - state.text = "New state" + assert.spy(set_lines).was_called_with( + match.is_number(), + 0, + -1, + false, + { "Line number 1!", "New state", "My highlighted text" } + ) end) - - assert.spy(set_lines).was_called(1) - a.util.scheduler() - assert.spy(set_lines).was_called(2) - - assert.spy(set_lines).was_called_with( - match.is_number(), - 0, - -1, - false, - { "Line number 1!", "New state", "My highlighted text" } - ) - end) + ) end) |
