Skip to content

Commit 9a6d587

Browse files
authored
feat: create single table for options (#65)
1 parent 1cb733b commit 9a6d587

File tree

5 files changed

+94
-75
lines changed

5 files changed

+94
-75
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, 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: 8 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,7 @@ 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+
vim.g.neotest_vstest = vim.tbl_deep_extend("force", vim.g.neotest_vstest or {}, opts or {})
587588
return create_adapter(opts)
588589
end
589590

lua/neotest-vstest/vstest/cli_wrapper.lua

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,11 @@ 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
10-
local process = nio.process.run({
11-
cmd = "dotnet",
12-
args = { "--info" },
13-
})
8+
function M.get_vstest_path()
9+
local path_to_search = vim.g.neotest_vstest and vim.g.neotest_vstest.sdk_path
1410

15-
logger.debug(
16-
"neotest-vstest: starting process to detect dotnet sdk path " .. tostring(process.pid)
17-
)
11+
if not path_to_search then
12+
local process = vim.system({ "dotnet", "--info" })
1813

1914
local default_sdk_path
2015
if vim.fn.has("win32") then
@@ -23,42 +18,26 @@ local function get_vstest_path()
2318
default_sdk_path = "/usr/local/share/dotnet/sdk/"
2419
end
2520

26-
if not process then
27-
vim.g.neotest_vstest_sdk_path = default_sdk_path
21+
local obj = process:wait()
22+
23+
local out = obj.stdout
24+
local info = dotnet_utils.parse_dotnet_info(out or "")
25+
if info.sdk_path then
26+
path_to_search = info.sdk_path
27+
logger.info(string.format("neotest-vstest: detected sdk path: %s", path_to_search))
28+
else
29+
path_to_search = default_sdk_path
2830
local log_string = string.format(
2931
"neotest-vstest: failed to detect sdk path. falling back to %s",
30-
vim.g.neotest_vstest_sdk_path
32+
path_to_search
3133
)
32-
3334
logger.info(log_string)
3435
nio.scheduler()
3536
vim.notify_once(log_string)
36-
else
37-
local out = process.stdout.read()
38-
local info = dotnet_utils.parse_dotnet_info(out or "")
39-
if info.sdk_path then
40-
vim.g.neotest_vstest_sdk_path = info.sdk_path
41-
logger.info(
42-
string.format("neotest-vstest: detected sdk path: %s", vim.g.neotest_vstest_sdk_path)
43-
)
44-
else
45-
vim.g.neotest_vstest_sdk_path = default_sdk_path
46-
local log_string = string.format(
47-
"neotest-vstest: failed to detect sdk path. falling back to %s",
48-
vim.g.neotest_vstest_sdk_path
49-
)
50-
logger.info(log_string)
51-
nio.scheduler()
52-
vim.notify_once(log_string)
53-
end
54-
process.close()
5537
end
5638
end
5739

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

6443
local function get_script(script_name)
@@ -76,7 +55,8 @@ end
7655
---@return { execute: fun(content: string), stop: fun() }
7756
function M.create_test_runner(project)
7857
local test_discovery_script = get_script("run_tests.fsx")
79-
local testhost_dll = get_vstest_path()
58+
nio.scheduler()
59+
local testhost_dll = M.get_vstest_path()
8060

8161
logger.debug("neotest-vstest: found discovery script: " .. test_discovery_script)
8262
logger.debug("neotest-vstest: found testhost dll: " .. testhost_dll)

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 and 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)