plenary.nvim icon indicating copy to clipboard operation
plenary.nvim copied to clipboard

Perpetually scheduling tests

Open kylechui opened this issue 2 years ago • 6 comments
trafficstars

Hi there, I recently switched over to lazy.nvim for package management (which I suspect is the reason), and now my plenary test suite no longer runs. Here's the output for the testing window: image

And I've been using this command to trigger it (it just runs the tests on the correct folder, as long as you're in the git project):

map("n", "<Leader>t", function()
    local cur_path = vim.fn.expand("%:p:h")
    while cur_path and vim.fn.finddir("tests", cur_path) == "" do
        cur_path = cur_path:match("(.*)/.*")
    end

    if cur_path then
        require("plenary.test_harness").test_directory(cur_path .. "/tests", {
            minimal_init = cur_path .. "/tests/minimal_init.lua",
        })
    end
end, {
    silent = true,
    buffer = true,
})

Any advice would be greatly appreciated (plenary is loaded in when I trigger this keymap; I tried both lazy-loading with telescope as well as forcing it to load on startup, but neither resolved the issue).

It appears that the regular <Plug>PlenaryTestFile still works for individual files though.

kylechui avatar Mar 21 '23 20:03 kylechui

I have the same issue, but have installed plenary via home-manager in nixos. I thought my issue was related to what i saw in #319, so I posted there, but I'm not sure about that anymore.

jghauser avatar Mar 25 '23 11:03 jghauser

As workaround/more clean test environment, you can add tests/minimal.lua with

vim.opt.rtp:append(".")
vim.opt.rtp:append("../plenary.nvim")
vim.cmd.runtime { "plugin/plenary.vim", bang = true }

and use nvim --headless --noplugin -u tests/minimal.lua -c "PlenaryBustedDirectory tests/ {minimal_init = 'tests/minimal.lua'}" with plenary.nvim checked out in $HOME/projects/ and dev = true set for the plugin.

matu3ba avatar Apr 01 '23 00:04 matu3ba

Probably the same problem appears on using

local add_cmd = vim.api.nvim_create_user_command
add_cmd('CheckLua', function()
  local os_env_path = os.getenv 'PATH'
  local tests_run = plenary.job:new({ command = 'nvim', args = { '--headless', '--noplugin', '-u', 'tests/minimal.lua', '-c', [["PlenaryBustedDirectory tests/ {minimal_init = 'tests/minimal.lua'}"]] }, env = { ["PATH"] = os_env_path } })
  tests_run:sync()
  print(vim.inspect.inspect(tests_run:result()))
end, {})
:CheckLua                                                                                                                                   
Error executing Lua callback: .../.local/share/nvim/lazy/plenary.nvim/lua/plenary/job.lua:499: 'nvim --headless --noplugin -u tests/minimal.lua -c "PlenaryBustedDirect
ory tests/ {minimal_init = 'tests/minimal.lua'}"' was unable to complete in 5000 ms                                                         
stack traceback:                                                                                                                            
        [C]: in function 'error'                                                                                                            
        .../.local/share/nvim/lazy/plenary.nvim/lua/plenary/job.lua:499: in function 'wait'                                                 
        .../.local/share/nvim/lazy/plenary.nvim/lua/plenary/job.lua:453: in function 'sync'                                                 
        /home/user/.config/nvim/lua/my_cmds.lua:385: in function </home/user/.config/nvim/lua/my_cmds.lua:376>

From cli it just works with env -i PATH="$PATH" nvim --headless --noplugin -u tests/minimal.lua -c "PlenaryBustedDirectory tests/ {minimal_init = 'tests/minimal.lua'}"

Starting...Scheduling: tests/example_spec.lua

========================================
Testing:        /home/user/projects/buf.nvim/tests/example_spec.lua
Success ||      Test example Test can access vim namespace
Success ||      Test example Test giveme123

Success:        2
Failed :        0
Errors :        0
========================================

matu3ba avatar Apr 02 '23 11:04 matu3ba

I faced freezing tests in my project. There you can find minimal.lua for lazy.nvim.

Missing dependencies caused the problem. I have require out of describe block. I moved the logic into the describe and tests started to fail with stacktrace. I'd recommend you to move require inside the describe (in case you don't have it so).

IlyasYOY avatar Apr 10 '23 21:04 IlyasYOY

A simpler reproduction only with rg:

  local job = Job:new {
    command = "rg",
    args = { "test" }, -- does not work
  }
  job:sync()

Must run this in a subdir, not $HOME. Then it makes no difference with cwd = vim.loop.cwd().

UPDATE: Looks like ripgrep is unable to get the correct cwd and might default to $HOME. Might be the same cause here, that cwd is not set:

  local job = Job:new {
    command = "rg",
    args = { "test", "." }, -- works
  }
  job:sync()

Adding cwd = vim.loop.cwd() does neither fix the issue.

matu3ba avatar May 07 '23 16:05 matu3ba

I faced freezing tests in my project. There you can find minimal.lua for lazy.nvim.

Missing dependencies caused the problem. I have require out of describe block. I moved the logic into the describe and tests started to fail with stacktrace. I'd recommend you to move require inside the describe (in case you don't have it so).

I just checked my own tests and I'm pretty sure all of my require calls are inside describe blocks, but it still seems to hang for me :/

kylechui avatar Jun 06 '23 16:06 kylechui