diff --git a/lua/ruby-lsp/neotest/init.lua b/lua/ruby-lsp/neotest/init.lua index 62cc306..02fdd52 100644 --- a/lua/ruby-lsp/neotest/init.lua +++ b/lua/ruby-lsp/neotest/init.lua @@ -173,11 +173,11 @@ function NeotestAdapter.is_test_file(file_path) return name:match('_test%.rb$') ~= nil or name:match('_spec%.rb$') ~= nil or name:match('^test_.*%.rb$') ~= nil end ----Discover test positions via the ruby-lsp language server. ----Waits for the server to start and finish indexing before making requests. +---Ask Ruby LSP for a file's test items, after it starts and finishes indexing. ---@param file_path string ----@return neotest.Tree|nil -function NeotestAdapter.discover_positions(file_path) +---@return table[]|nil items +---@return string|nil err +local function discover_items(file_path) local file_name = nio.fn.fnamemodify(file_path, ':t') discovery_state.active_count = discovery_state.active_count + 1 @@ -223,9 +223,21 @@ function NeotestAdapter.discover_positions(file_path) discovery_state.warned = false end - if err or not result or type(result) ~= 'table' or #result == 0 then + if err then + return nil, type(err) == 'table' and err.message or tostring(err) + end + return type(result) == 'table' and result or {} +end + +---Discover test positions via the ruby-lsp language server. +---@param file_path string +---@return neotest.Tree|nil +function NeotestAdapter.discover_positions(file_path) + local result = discover_items(file_path) + if not result or #result == 0 then return nil end + local file_name = nio.fn.fnamemodify(file_path, ':t') local last_line = 0 for _, item in ipairs(result) do @@ -256,6 +268,46 @@ function NeotestAdapter.discover_positions(file_path) end) end +---Collect the leaf test items under the given LSP items. +---@param items table[] +---@return table[] +local function collect_leaf_items(items) + local leaves = {} + for _, item in ipairs(items) do + if item.children and #item.children > 0 then + vim.list_extend(leaves, collect_leaf_items(item.children)) + else + table.insert(leaves, utils.wrap_test_item(item)) + end + end + return leaves +end + +---@param path string|nil +---@return string|nil +local function realpath(path) + return path and vim.uv.fs_realpath(path) or path +end + +---Discover a file's tests for a run that started before neotest had them. Picks +---the test at the cursor when the file is in the current window, else them all. +---@param file_path string +---@return table[] items +---@return string|nil reason why there is nothing to run +local function undiscovered_file_items(file_path) + local discovered, err = discover_items(file_path) + if not discovered then + return {}, 'could not discover tests: ' .. err + end + local in_window = realpath(nio.fn.expand('%:p')) == realpath(file_path) + local nearest = in_window and utils.find_nearest_test(discovered, nio.fn.line('.') - 1) + local items = collect_leaf_items(nearest and { nearest } or discovered) + if #items == 0 then + return {}, 'no tests found in ' .. nio.fn.fnamemodify(file_path, ':t') + end + return items +end + ---Build a test command spec from a neotest position. ---Resolves the actual shell command via rubyLsp/resolveTestCommands. ---@param args neotest.RunArgs @@ -273,6 +325,23 @@ function NeotestAdapter.build_spec(args) collect_test_items(tree, items) end + -- neotest runs the file when the nearest test isn't discovered yet. Returning + -- nil there leaves the file marked as running, so discover it now instead. + if #items == 0 and position.type == 'file' then + local reason + items, reason = undiscovered_file_items(position.path) + if reason then + -- A spec that exits at once, so neotest finishes the run and the file + -- gets a skipped result. Raising here would leave neotest's runner + -- tracking the file as running and show a stack trace. + vim.notify('ruby-lsp: ' .. reason, vim.log.levels.WARN) + return { + command = { vim.v.progpath, '--clean', '--headless', '+qa!' }, + context = { pos_id = position.id, skipped = true }, + } + end + end + if #items == 0 then return nil end @@ -413,6 +482,10 @@ function NeotestAdapter.results(spec, result, tree) local pos_id = spec.context.pos_id local framework = spec.context.framework + if spec.context.skipped then + return { [pos_id] = { status = 'skipped' } } + end + if result.code == 0 then -- All tests passed — mark every leaf assign_test_results(tree, {}, framework, result.output, results) @@ -425,12 +498,13 @@ function NeotestAdapter.results(spec, result, tree) end if next(failed) then assign_test_results(tree, failed, framework, result.output, results) - else - -- Could not parse individual failures — fall back to marking parent - results[pos_id] = { status = 'failed', output = result.output } end end + -- Covers runs whose failures couldn't be parsed and files whose tests weren't + -- discovered. neotest only stops showing a position as running once it has a result. + results[pos_id] = results[pos_id] or { status = result.code == 0 and 'passed' or 'failed', output = result.output } + return results end diff --git a/tests/neotest_spec.lua b/tests/neotest_spec.lua index ff5d7ce..2baa50e 100644 --- a/tests/neotest_spec.lua +++ b/tests/neotest_spec.lua @@ -168,9 +168,118 @@ describe('neotest adapter', function() "/^CalculatorTest#(''|test_add|test_average)$/", }, spec.command) end) + + -- neotest falls back to the file when its tests aren't discovered yet. Returning nil + -- there left the file marked as running forever. + describe('for a file whose tests are not discovered yet', function() + local file = '/path/test/foo_test.rb' + local resolved_items, stubs + + local function range(first, last) + return { start = { line = first, character = 0 }, ['end'] = { line = last, character = 3 } } + end + + local function client_with(discovered) + return helpers.mock_client({ + id = 201, + request = function(_, method, params, handler) + if method == 'rubyLsp/discoverTests' then + handler(nil, discovered) + else + resolved_items = params.items + handler(nil, { commands = { 'bin/rails test ' .. file } }) + end + end, + }) + end + + local discovered = { + { + id = 'FooTest', + label = 'FooTest', + range = range(0, 9), + tags = { 'framework:rails' }, + children = { + { id = 'FooTest#test_one', label = 'test_one', range = range(1, 3), tags = { 'framework:rails' } }, + { id = 'FooTest#test_two', label = 'test_two', range = range(5, 7), tags = { 'framework:rails' } }, + }, + }, + } + + before_each(function() + resolved_items = nil + stubs = {} + require('ruby-lsp.utils').on_indexing_complete(201) + end) + + after_each(function() + for _, s in ipairs(stubs) do + s:revert() + end + end) + + local function ids(items) + return vim.tbl_map(function(item) + return item.id + end, items or {}) + end + + it('discovers the file and runs the test at the cursor', function() + helpers.lsp_clients = { client_with(discovered) } + local stub = require('luassert.stub') + stubs = { stub(package.loaded['nio'].fn, 'expand', file), stub(package.loaded['nio'].fn, 'line', 7) } + + local spec = adapter.build_spec({ tree = make_node({ type = 'file', id = file, path = file }) }) + + assert.same({ 'FooTest#test_two' }, ids(resolved_items)) + assert.equals(file, spec.context.pos_id) + end) + + it('runs every test in the file when the cursor is elsewhere', function() + helpers.lsp_clients = { client_with(discovered) } + local stub = require('luassert.stub') + stubs = { stub(package.loaded['nio'].fn, 'expand', '/path/test/other_test.rb') } + + adapter.build_spec({ tree = make_node({ type = 'file', id = file, path = file }) }) + + assert.same({ 'FooTest#test_one', 'FooTest#test_two' }, ids(resolved_items)) + end) + + it('skips the file with a message when it has no tests', function() + helpers.lsp_clients = { client_with({}) } + + local spec = adapter.build_spec({ tree = make_node({ type = 'file', id = file, path = file }) }) + + assert.is_nil(resolved_items) + assert.equals('skipped', adapter.results(spec, { code = 0 }, nil)[file].status) + assert.truthy(helpers.notifications[1].msg:find('no tests found in foo_test.rb', 1, true)) + end) + + it('says why when discovery fails', function() + helpers.lsp_clients = { + helpers.mock_client({ + id = 201, + request = function(_, _, _, handler) + handler({ message = 'boom' }, nil) + end, + }), + } + + adapter.build_spec({ tree = make_node({ type = 'file', id = file, path = file }) }) + + assert.truthy(helpers.notifications[1].msg:find('could not discover tests: boom', 1, true)) + end) + end) end) describe('results', function() + it('reports the run position even when it has no discovered tests', function() + local tree = make_node({ type = 'file', id = '/path/test.rb' }) + local spec = { context = { pos_id = '/path/test.rb' } } + + assert.equals('passed', adapter.results(spec, { code = 0, output = '/dev/null' }, tree)['/path/test.rb'].status) + end) + it('marks all tests as passed when exit code is 0', function() local node1 = make_node({ type = 'test',