Skip to content

Commit 1ff83ca

Browse files
committed
feat: create single table for options
1 parent 8e8bdaf commit 1ff83ca

File tree

5 files changed

+99
-51
lines changed

5 files changed

+99
-51
lines changed

README.md

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
src="https://github.com/user-attachments/assets/7e297d7a-f06d-44a9-adef-92131185e8ca" />
1515
</p>
1616

17-
1817
# Neotest VSTest
1918

2019
Neotest adapter for dotnet
@@ -64,34 +63,37 @@ require("neotest").setup({
6463
The adapter optionally supports extra settings:
6564

6665
```lua
66+
-- NOTE: This should be set before calling require("neotest-vstest")
67+
vim.g.neotest_vstest = {
68+
-- Path to dotnet sdk path.
69+
-- Used in cases where the sdk path cannot be auto discovered.
70+
sdk_path = "/usr/local/dotnet/sdk/9.0.101/",
71+
-- table is passed directly to DAP when debugging tests.
72+
dap_settings = {
73+
type = "netcoredbg",
74+
},
75+
-- If multiple solutions exists the adapter will ask you to choose one.
76+
-- If you have a different heuristic for choosing a solution you can provide a function here.
77+
solution_selector = function(solutions)
78+
return nil -- return the solution you want to use or nil to let the adapter choose.
79+
end,
80+
-- If multiple .runsettings/testconfig.json files are present in the test project directory
81+
-- you will be given the choice of file to use when setting up the adapter.
82+
-- Or you can provide a function here
83+
-- default nil to select from all files in project directory
84+
settings_selector = function(project_dir)
85+
return nil -- return the .runsettings/testconfig.json file you want to use or let the adapter choose
86+
end,
87+
build_opts = {
88+
-- Arguments that will be added to all `dotnet build` and `dotnet msbuild` commands
89+
additional_args = {}
90+
},
91+
timeout_ms = 30 * 5 * 1000 -- number of milliseconds to wait before timeout while communicating with adapter client
92+
}
93+
6794
require("neotest").setup({
6895
adapters = {
69-
require("neotest-vstest")({
70-
-- Path to dotnet sdk path.
71-
-- Used in cases where the sdk path cannot be auto discovered.
72-
sdk_path = "/usr/local/dotnet/sdk/9.0.101/",
73-
-- table is passed directly to DAP when debugging tests.
74-
dap_settings = {
75-
type = "netcoredbg",
76-
},
77-
-- If multiple solutions exists the adapter will ask you to choose one.
78-
-- If you have a different heuristic for choosing a solution you can provide a function here.
79-
solution_selector = function(solutions)
80-
return nil -- return the solution you want to use or nil to let the adapter choose.
81-
end,
82-
-- If multiple .runsettings/testconfig.json files are present in the test project directory
83-
-- you will be given the choice of file to use when setting up the adapter.
84-
-- Or you can provide a function here
85-
-- default nil to select from all files in project directory
86-
settings_selector = function(project_dir)
87-
return nil -- return the .runsettings/testconfig.json file you want to use or let the adapter choose
88-
end,
89-
build_opts = {
90-
-- Arguments that will be added to all `dotnet build` and `dotnet msbuild` commands
91-
additional_args = {}
92-
},
93-
timeout_ms = 30 * 5 * 1000 -- number of milliseconds to wait before timeout while communicating with adapter client
94-
})
96+
require("neotest-vstest")
9597
}
9698
})
9799
```

lua/neotest-vstest/health.lua

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
local M = {}
2+
M.check = function()
3+
vim.health.start("neotest-vstest healthcheck")
4+
5+
vim.health.info("checking for dependencies...")
6+
7+
local has_nio = pcall(require("nio"))
8+
if not has_nio then
9+
vim.health.error("nio is not installed. Please install nio to use neotest-vstest.")
10+
else
11+
vim.health.ok("nio is installed.")
12+
end
13+
14+
local has_neotest = pcall(require("neotest"))
15+
if not has_neotest then
16+
vim.health.error("neotest is not installed. Please install neotest to use neotest-vstest.")
17+
else
18+
vim.health.ok("neotest is installed.")
19+
end
20+
21+
vim.health.info("Checking neotest-vstest configuration...")
22+
23+
vim.health.info("Checking for vstest.console.dll...")
24+
local cli_wrapper = require("neotest-vstest.vstest.cli_wrapper")
25+
local vstest_path = cli_wrapper.get_vstest_path()
26+
27+
-- make sure setup function parameters are ok
28+
if not vstest_path then
29+
vim.health.error(
30+
"Could not determine location of vstest.console.dll. Please set vim.g.neotest_vstest.sdk_path to the dotnet sdk path."
31+
)
32+
else
33+
vim.health.ok("Found vstest.console.dll at: " .. vstest_path)
34+
end
35+
end
36+
return M

lua/neotest-vstest/init.lua

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
---@field settings_selector? fun(project_dir: string): string|nil function to find the .runsettings/testconfig.json in the project dir
77
---@field timeout_ms? number milliseconds to wait before timing out connection with test runner
88

9+
---@type neotest-vstest.Config
10+
local default_config = {
11+
timeout_ms = 5 * 30 * 1000,
12+
}
13+
14+
vim.g.neotest_vstest = vim.tbl_deep_extend("force", default_config, vim.g.neotest_vstest or {})
15+
916
---@param config? neotest-vstest.Config
1017
---@return neotest.Adapter
1118
local function create_adapter(config)
@@ -36,7 +43,6 @@ local function create_adapter(config)
3643
local nio = require("nio")
3744
local lib = require("neotest.lib")
3845
local logger = require("neotest.logging")
39-
local dotnet_utils = require("neotest-vstest.dotnet_utils")
4046

4147
if solution_dir then
4248
return solution_dir
@@ -107,7 +113,6 @@ local function create_adapter(config)
107113

108114
function DotnetNeotestAdapter.is_test_file(file_path)
109115
local logger = require("neotest.logging")
110-
local dotnet_utils = require("neotest-vstest.dotnet_utils")
111116
local client_discovery = require("neotest-vstest.client")
112117

113118
logger.trace("neotest-vstest: checking if file is test file: " .. file_path)
@@ -145,7 +150,6 @@ local function create_adapter(config)
145150
end
146151

147152
function DotnetNeotestAdapter.filter_dir(name, rel_path, root)
148-
local dotnet_utils = require("neotest-vstest.dotnet_utils")
149153
local logger = require("neotest.logging")
150154
logger.trace("neotest-vstest: filtering dir", name, rel_path, root)
151155

@@ -378,7 +382,6 @@ local function create_adapter(config)
378382
local lib = require("neotest.lib")
379383
local types = require("neotest.types")
380384
local logger = require("neotest.logging")
381-
local dotnet_utils = require("neotest-vstest.dotnet_utils")
382385
local client_discovery = require("neotest-vstest.client")
383386

384387
logger.info(string.format("neotest-vstest: scanning %s for tests...", path))
@@ -581,9 +584,16 @@ local DotnetNeotestAdapter = create_adapter()
581584

582585
---@param opts neotest-vstest.Config
583586
local function apply_user_settings(_, opts)
584-
vim.g.neotest_vstest_sdk_path = opts and opts.sdk_path or nil
585-
vim.g.neotest_vstest_find_settings = opts and opts.settings_selector or nil
586-
vim.g.neotest_vstest_timeout_ms = opts and opts.timeout_ms or 5 * 30 * 1000
587+
if opts then
588+
vim.g.neotest_vstest.sdk_path = opts.sdk_path or vim.g.neotest_vstest.sdk_path
589+
vim.g.neotest_vstest.build_opts = opts.build_opts or vim.g.neotest_vstest.build_opts
590+
vim.g.neotest_vstest.dap_settings = opts.dap_settings or vim.g.neotest_vstest.dap_settings
591+
vim.g.neotest_vstest.solution_selector = opts.solution_selector
592+
or vim.g.neotest_vstest.solution_selector
593+
vim.g.neotest_vstest.find_settings = opts.settings_selector
594+
or vim.g.neotest_vstest.find_settings
595+
vim.g.neotest_vstest.timeout_ms = opts.timeout_ms or vim.g.neotest_vstest.timeout_ms
596+
end
587597
return create_adapter(opts)
588598
end
589599

lua/neotest-vstest/vstest/cli_wrapper.lua

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ local dotnet_utils = require("neotest-vstest.dotnet_utils")
55

66
local M = {}
77

8-
local function get_vstest_path()
9-
if not vim.g.neotest_vstest_sdk_path then
8+
function M.get_vstest_path()
9+
local path_to_search = vim.g.neotest_vstest.sdk_path
10+
11+
if not path_to_search then
1012
local process = nio.process.run({
1113
cmd = "dotnet",
1214
args = { "--info" },
1315
})
1416

1517
logger.debug(
16-
"neotest-vstest: starting process to detect dotnet sdk path " .. tostring(process.pid)
18+
"neotest-vstest: starting process to detect dotnet sdk path "
19+
.. tostring(process and process.pid or 0)
1720
)
1821

1922
local default_sdk_path
@@ -24,10 +27,10 @@ local function get_vstest_path()
2427
end
2528

2629
if not process then
27-
vim.g.neotest_vstest_sdk_path = default_sdk_path
30+
path_to_search = default_sdk_path
2831
local log_string = string.format(
2932
"neotest-vstest: failed to detect sdk path. falling back to %s",
30-
vim.g.neotest_vstest_sdk_path
33+
default_sdk_path
3134
)
3235

3336
logger.info(log_string)
@@ -37,15 +40,15 @@ local function get_vstest_path()
3740
local out = process.stdout.read()
3841
local info = dotnet_utils.parse_dotnet_info(out or "")
3942
if info.sdk_path then
40-
vim.g.neotest_vstest_sdk_path = info.sdk_path
43+
path_to_search = info.sdk_path
4144
logger.info(
42-
string.format("neotest-vstest: detected sdk path: %s", vim.g.neotest_vstest_sdk_path)
45+
string.format("neotest-vstest: detected sdk path: %s", vim.g.neotest_vstest.sdk_path)
4346
)
4447
else
45-
vim.g.neotest_vstest_sdk_path = default_sdk_path
48+
path_to_search = default_sdk_path
4649
local log_string = string.format(
4750
"neotest-vstest: failed to detect sdk path. falling back to %s",
48-
vim.g.neotest_vstest_sdk_path
51+
path_to_search
4952
)
5053
logger.info(log_string)
5154
nio.scheduler()
@@ -55,10 +58,7 @@ local function get_vstest_path()
5558
end
5659
end
5760

58-
return vim.fs.find(
59-
"vstest.console.dll",
60-
{ upward = false, type = "file", path = vim.g.neotest_vstest_sdk_path }
61-
)[1]
61+
return vim.fs.find("vstest.console.dll", { upward = false, type = "file", path = path_to_search })[1]
6262
end
6363

6464
local function get_script(script_name)

lua/neotest-vstest/vstest/init.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ function Client:new(project)
2222
logger.info("neotest-vstest: Creating new (vstest) client for: " .. vim.inspect(project))
2323
local findSettings = function()
2424
local settings = nil
25-
if vim.g.neotest_vstest_find_settings then
26-
settings = vim.g.neotest_vstest_find_settings(project.proj_dir)
25+
if vim.g.neotest_vstest.find_settings then
26+
settings = vim.g.neotest_vstest.find_settings(project.proj_dir)
2727
end
2828
if settings ~= nil then
2929
return settings
@@ -81,7 +81,7 @@ function Client:run_tests(ids)
8181
end
8282

8383
nio.run(function()
84-
cli_wrapper.spin_lock_wait_file(result_file, vim.g.neotest_vstest_timeout_ms)
84+
cli_wrapper.spin_lock_wait_file(result_file, vim.g.neotest_vstest.timeout_ms)
8585
local parsed = {}
8686
local results = lib.files.read_lines(result_file)
8787
for _, line in ipairs(results) do
@@ -130,7 +130,7 @@ function Client:debug_tests(ids)
130130
nio.run(function()
131131
local parsed = {}
132132
local file_exists =
133-
cli_wrapper.spin_lock_wait_file(result_file, vim.g.neotest_vstest_timeout_ms)
133+
cli_wrapper.spin_lock_wait_file(result_file, vim.g.neotest_vstest.timeout_ms)
134134
assert(
135135
file_exists,
136136
"neotest-vstest: (possible timeout, check logs) result file does not exist: " .. result_file

0 commit comments

Comments
 (0)