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/core | |
| 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/core')
| -rw-r--r-- | tests/core/async_spec.lua | 50 | ||||
| -rw-r--r-- | tests/core/process_spec.lua | 117 |
2 files changed, 113 insertions, 54 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) |
