Skip to content

Commit 1fb58a6

Browse files
authored
feat: add API to open test reports (nvim-java#35)
* refactor: fix after test api changes nvim-java-core#26 * feat: add view report API to show test results * fix(test): add missing java-test dep for testing * fix(test): load dependency plugins before test
1 parent 2c9da81 commit 1fb58a6

File tree

6 files changed

+88
-21
lines changed

6 files changed

+88
-21
lines changed

README.md

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,34 @@ require('java').dap.config_dap()
9999

100100
**Test**
101101

102-
- `run_current_test_class` - Run the test class in the active buffer
102+
- `run_current_class` - Run the test class in the active buffer
103103

104104
```lua
105-
require('java').test.run_current_test_class()
105+
require('java').test.run_current_class()
106106
```
107107

108-
- `debug_current_test_class` - Debug the test class in the active buffer
108+
- `debug_current_class` - Debug the test class in the active buffer
109109

110110
```lua
111-
require('java').test.debug_current_test_class()
111+
require('java').test.debug_current_class()
112+
```
113+
114+
- `run_current_method` - Run the test method on the cursor
115+
116+
```lua
117+
require('java').test.run_current_method()
118+
```
119+
120+
- `debug_current_method` - Debug the test method on the cursor
121+
122+
```lua
123+
require('java').test.debug_current_method()
124+
```
125+
126+
- `view_report` - Open the last test report in a popup window
127+
128+
```lua
129+
require('java').test.view_report()
112130
```
113131

114132
</details>

lua/java.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,13 @@ M.dap.config_dap = dap.config_dap
2626
-- Test APIs --
2727
----------------------------------------------------------------------
2828
M.test = {}
29-
M.test.run_current_test_class = test.run_current_test_class
30-
M.test.debug_current_test_class = test.debug_current_test_class
29+
M.test.run_current_class = test.run_current_class
30+
M.test.debug_current_class = test.debug_current_class
31+
32+
M.test.run_current_method = test.run_current_method
33+
M.test.debug_current_method = test.debug_current_method
34+
35+
M.test.view_report = test.view_report
3136

3237
----------------------------------------------------------------------
3338
-- Manipulate --

lua/java/api/test.lua

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,37 @@
11
local JavaDap = require('java.dap')
22

33
local log = require('java.utils.log')
4-
local get_error_handler = require('java.handlers.error')
54
local jdtls = require('java.utils.jdtls')
5+
local get_error_handler = require('java.handlers.error')
6+
67
local async = require('java-core.utils.async').sync
78

8-
local M = {}
9+
local JUnitReport = require('java-test.reports.junit')
10+
local ResultParserFactory = require('java-test.results.result-parser-factory')
11+
local ReportViewer = require('java-test.ui.floating-report-viewer')
912

10-
---Setup dap config & adapter on jdtls attach event
13+
local M = {
14+
---@type java_test.JUnitTestReport
15+
last_report = nil,
16+
}
1117

12-
function M.run_current_test_class()
18+
---Setup dap config & adapter on jdtls attach event
19+
function M.run_current_class()
1320
log.info('run current test class')
1421

1522
return async(function()
16-
return JavaDap:new(jdtls()):execute_current_test_class({ noDebug = true })
23+
return JavaDap:new(jdtls())
24+
:execute_current_test_class(M.get_report(), { noDebug = true })
1725
end)
1826
.catch(get_error_handler('failed to run the current test class'))
1927
.run()
2028
end
2129

22-
function M.debug_current_test_class()
30+
function M.debug_current_class()
2331
log.info('debug current test class')
2432

2533
return async(function()
26-
JavaDap:new(jdtls()):execute_current_test_class({})
34+
JavaDap:new(jdtls()):execute_current_test_class(M.get_report(), {})
2735
end)
2836
.catch(get_error_handler('failed to debug the current test class'))
2937
.run()
@@ -33,7 +41,8 @@ function M.debug_current_method()
3341
log.info('debug current test method')
3442

3543
return async(function()
36-
return JavaDap:new(jdtls()):execute_current_test_method()
44+
return JavaDap:new(jdtls())
45+
:execute_current_test_method(M.get_report(), {})
3746
end)
3847
.catch(get_error_handler('failed to run the current test method'))
3948
.run()
@@ -44,12 +53,19 @@ function M.run_current_method()
4453

4554
return async(function()
4655
return JavaDap:new(jdtls())
47-
:execute_current_test_method({ noDebug = true })
56+
:execute_current_test_method(M.get_report(), { noDebug = true })
4857
end)
4958
.catch(get_error_handler('failed to run the current test method'))
5059
.run()
5160
end
5261

62+
function M.view_report()
63+
if M.last_report then
64+
M.last_report:show_report()
65+
end
66+
end
67+
68+
---@private
5369
function M.config_dap()
5470
log.info('configuring dap')
5571

@@ -60,4 +76,11 @@ function M.config_dap()
6076
.run()
6177
end
6278

79+
---@private
80+
function M.get_report()
81+
local report = JUnitReport(ResultParserFactory(), ReportViewer())
82+
M.last_report = report
83+
return report
84+
end
85+
6386
return M

lua/java/dap/init.lua

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
local log = require('java.utils.log')
2-
local async = require('java-core.utils.async').sync
32
local buf_util = require('java.utils.buffer')
43
local win_util = require('java.utils.window')
4+
5+
local async = require('java-core.utils.async').sync
56
local notify = require('java-core.utils.notify')
67

78
local JavaCoreDap = require('java-core.dap')
89
local JavaCoreTestApi = require('java-core.api.test')
910
local JavaCoreTestClient = require('java-core.ls.clients.java-test-client')
11+
local JavaCoreDapRunner = require('java-core.dap.runner')
1012

1113
---@class JavaDap
1214
---@field private client LspClient
1315
---@field private dap JavaCoreDap
14-
---@field private test_api JavaCoreTestApi
16+
---@field private test_api java_core.TestApi
1517
---@field private test_client java_core.TestClient
1618
local M = {}
1719

@@ -24,6 +26,7 @@ function M:new(args)
2426

2527
o.test_api = JavaCoreTestApi:new({
2628
client = args.client,
29+
runner = JavaCoreDapRunner:new(),
2730
})
2831

2932
o.test_client = JavaCoreTestClient:new({
@@ -40,14 +43,19 @@ function M:new(args)
4043
end
4144

4245
---Run the current test class
46+
---@param report java_test.JUnitTestReport
4347
---@param config JavaCoreDapLauncherConfigOverridable
44-
function M:execute_current_test_class(config)
48+
function M:execute_current_test_class(report, config)
4549
log.debug('running the current class')
4650

47-
return self.test_api:run_class_by_buffer(buf_util.get_curr_buf(), config)
51+
return self.test_api:run_class_by_buffer(
52+
buf_util.get_curr_buf(),
53+
report,
54+
config
55+
)
4856
end
4957

50-
function M:execute_current_test_method(config)
58+
function M:execute_current_test_method(report, config)
5159
log.debug('running the current method')
5260

5361
local method = self:find_current_test_method()
@@ -57,7 +65,7 @@ function M:execute_current_test_method(config)
5765
return
5866
end
5967

60-
self.test_api:run_test({ method }, config)
68+
self.test_api:run_test({ method }, report, config)
6169
end
6270

6371
function M:find_current_test_method()

tests/prepare-config.lua

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,19 @@ vim.opt.rtp:prepend(lazypath)
1515

1616
local temp_path = './.test_plugins'
1717
local java_core_path = vim.fn.expand('~/Workspace/nvim-java-core')
18+
local java_test_path = vim.fn.expand('~/Workspace/nvim-java-test')
1819

1920
require('lazy').setup({
2021
{
2122
'nvim-lua/plenary.nvim',
2223
lazy = false,
2324
},
25+
{
26+
'nvim-java/nvim-java-test',
27+
---@diagnostic disable-next-line: assign-type-mismatch
28+
dir = vim.fn.isdirectory(java_test_path) == 1 and java_test_path or nil,
29+
lazy = false,
30+
},
2431
{
2532
'nvim-java/nvim-java-core',
2633
---@diagnostic disable-next-line: assign-type-mismatch

tests/test-config.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
local java_core_path = vim.fn.expand('~/Workspace/nvim-java-core')
2+
local java_test_path = vim.fn.expand('~/Workspace/nvim-java-test')
3+
24
local plug_path = './.test_plugins'
35

46
java_core_path = (vim.fn.isdirectory(java_core_path) == 1) and java_core_path
57
or (plug_path .. '/nvim-java-core')
68

9+
java_test_path = (vim.fn.isdirectory(java_test_path) == 1) and java_test_path
10+
or (plug_path .. '/nvim-java-test')
11+
712
vim.opt.rtp:append(plug_path .. '/plenary.nvim')
813
vim.opt.rtp:append(plug_path .. '/nvim-lspconfig')
914
vim.opt.rtp:append(plug_path .. '/mason.nvim')
1015
vim.opt.rtp:append(java_core_path)
16+
vim.opt.rtp:append(java_test_path)

0 commit comments

Comments
 (0)