Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

async vim.loop calls with coroutines block tests #542

Open
gregorias opened this issue Nov 22, 2023 · 0 comments
Open

async vim.loop calls with coroutines block tests #542

gregorias opened this issue Nov 22, 2023 · 0 comments

Comments

@gregorias
Copy link

Using async vim.loop functions (not the Plenary async ones) block Plenary's test harness.

Reproduction

Have the following files in a directory:

-- test/test_spec.lua
describe("test", function()
	it("runs the test", function()
		local co = coroutine.running()
		assert(co, "not running inside a coroutine")

		local is_done = false
		vim.loop.fs_stat("/bin", function(err, stats)
			is_done = true
			coroutine.resume(co)
		end)
		if not is_done then
			coroutine.yield()
		end
		assert.equals(true, true)
	end)
end)
-- test/minimal_init.lua
local M = {}

function M.root(root)
    local f = debug.getinfo(1, "S").source:sub(2)
    return vim.fn.fnamemodify(f, ":p:h:h") .. "/" .. (root or "")
end

---@param plugin string
function M.load(plugin)
    local name = plugin:match(".*/(.*)")
    local package_root = M.root(".tests/site/pack/deps/start/")
    if not vim.loop.fs_stat(package_root .. name) then
        print("Installing " .. plugin)
        vim.fn.mkdir(package_root, "p")
        vim.fn.system({
            "git",
            "clone",
            "--depth=1",
            "https://github.com/" .. plugin .. ".git",
            package_root .. "/" .. name,
        })
    end
end

function M.setup()
    vim.cmd([[set runtimepath=$VIMRUNTIME]])
    vim.opt.runtimepath:append(M.root())
    vim.opt.packpath = { M.root(".tests/site") }
    M.load("nvim-lua/plenary.nvim")
    vim.env.XDG_CONFIG_HOME = M.root(".tests/config")
    vim.env.XDG_DATA_HOME = M.root(".tests/data")
    vim.env.XDG_STATE_HOME = M.root(".tests/state")
    vim.env.XDG_CACHE_HOME = M.root(".tests/cache")
end

vim.o.swapfile = false
_G.__TEST = true

M.setup()
# Makefile
.PHONY: test

test:
	nvim --headless --noplugin -u test/minimal_init.lua -c "lua require(\"plenary.test_harness\").test_directory_command('test {minimal_init = \"test/minimal_init.lua\"}')"

Run make test. This will get printed:

nvim --headless --noplugin -u test/minimal_init.lua -c "lua require(\"plenary.test_harness\").test_directory_command('test {minimal_init = \"test/minimal_init.lua\"}')"
Starting...Scheduling: test/client_spec.lua

========================================	
Testing: 	/Users/grzesiek/Code/plenary-busted-loop-async-bug/test/client_spec.lua
Success	||	test runs the test	
	
Success: 	1	
Failed : 	0	
Errors : 	0	
========================================	

and the run will hang and never finish.

Expected behavior

make test finishes successfully.

Notes

There's an issue on testing async code, #424. The solution provided there works with code that uses vim.defer_fn or vim.schedule. It doesn't work with vim.loop functions.

Synchronous vim.loop functions work just fine.

@gregorias gregorias changed the title async vim.loop calls wit coroutines block tests async vim.loop calls with coroutines block tests Nov 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant