From a24cb408296628e160cd60f6083df44acbad5c3b Mon Sep 17 00:00:00 2001 From: rhilliges <32675635+rhilliges@users.noreply.github.com> Date: Mon, 9 Dec 2024 21:39:05 -0700 Subject: [PATCH] @testng; append test result to nvim-dap repl if installed --- lua/neotest-jdtls/testng/reports/testng.lua | 29 ++++++++++++------- .../testng/results/result-parser.lua | 27 ++++++++++++----- 2 files changed, 38 insertions(+), 18 deletions(-) diff --git a/lua/neotest-jdtls/testng/reports/testng.lua b/lua/neotest-jdtls/testng/reports/testng.lua index 4e9484c..d717ab9 100644 --- a/lua/neotest-jdtls/testng/reports/testng.lua +++ b/lua/neotest-jdtls/testng/reports/testng.lua @@ -27,20 +27,27 @@ end function TestNGReport:get_stream_reader(conn) self.conn = conn self.result_parser = self.result_parser_fac:get_parser() - return vim.schedule_wrap(function(err, buffer) - if err then - self.conn:close() - return - end - - if buffer then - self:on_update(buffer) - else - self.conn:close() + self.result_parser = self.result_parser_fac:get_parser() + local buffer_loop = function () + local buffer = "" + return function(err, chunk) + if err then + self.conn:close() + return + end + + if chunk then + buffer = buffer .. chunk + else + self:on_update(buffer) + self.conn:close() + end end - end) + end + return vim.schedule_wrap(buffer_loop()) end + ---Runs on connection update ---@private ---@param text string diff --git a/lua/neotest-jdtls/testng/results/result-parser.lua b/lua/neotest-jdtls/testng/results/result-parser.lua index 52a085a..c349d5d 100644 --- a/lua/neotest-jdtls/testng/results/result-parser.lua +++ b/lua/neotest-jdtls/testng/results/result-parser.lua @@ -32,11 +32,10 @@ local function parse(content) local test = vim.json.decode(line) if test.name ~= 'testStarted' then return test - -- table.insert(tests, test) end - return nil end end + return nil end ---Parse a given text into test details @@ -44,6 +43,7 @@ end function TestParser:parse(text) local parsedResult = parse(text) if parsedResult == nil then + log.info("Could not parse result.", text) return; end local testName = parsedResult["attributes"]["name"] @@ -51,25 +51,38 @@ function TestParser:parse(text) local testPathElements = vim.split(test[1], "%.") local testFile = testPathElements[#testPathElements] local testMethodName = test[2]:sub(1,-3) - local testId = test[1]:gsub("%.","/") .. ".java::" .. testFile .. "::" .. testMethodName - testId = vim.fn.getcwd() .. "/src/test/java/" .. testId + local testId = vim.fn.expand("src/**/" .. test[1]:gsub("%.","/") .. ".java") + testId = vim.fn.getcwd() .. "/" .. testId .. "::" .. testFile .. "::" .. testMethodName + local status = parsedResult["name"] + local repl = require('dap.repl') if status == 'testFailed' then + local msg = parsedResult["attributes"]["message"] or 'test failed' + local trace = parsedResult["attributes"]["trace"] self.test_details.results[testId] = { status = "failed", - short = parsedResult["attributes"]["message"], + short = msg, errors = { { - message = parsedResult["attributes"]["trace"], + message = trace, } }, } + if repl ~= nil then + repl.append(' ' .. testName .. ' failed') + repl.append(msg) + repl.append(trace) + end elseif status == 'testFinished' then self.test_details.results[testId] = { status = "passed", } + if repl ~= nil then + repl.append(' ' .. testName .. ' passed') + end + else + log.warn("Unknown test status: ", parsedResult["name"]) end - log.warn("Unknown test status: ", parsedResult["name"]) end return TestParser